import asyncio DEBUG=False try: from winrt.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as MediaManager except: print("DEBUG; winrt disabled") DEBUG = True import win32com.client import time import pythoncom from resources import LineState, CLMgrMessage import serial from serial import SerialException portName = 'COM3' def sendSerial(byte): try: ser = serial.Serial(port=portName, baudrate=115200) ser.write(byte) except SerialException: print('port already open') class PhoneLineEventHandler(): phone_mgr = None lines = [] line_selected = None connected = False def OnDispOnLineMgrNotification(self, msg, param, returns=""): if not self.phone_mgr: self.phone_mgr = win32com.client.Dispatch("CLMgr.ClientLineMgr") if not self.phone_mgr: "Swyx not connected!" else: print("Swyx connected!") if self.phone_mgr: if msg == CLMgrMessage.CLMgrLineStateChangedMessage: line = self.phone_mgr.DispGetLine(param) print("Line: {} {}".format( param, LineState.s[line.DispState]) ) if line.DispState != LineState.Inactive: if line.DispState == LineState.Ringing: sendSerial(b'y') else: sendSerial(b'r') asyncio.run(try_pause()) for linenum in range(self.phone_mgr.DispNumberOfLines): line = self.phone_mgr.DispGetLine(linenum) if line.DispState != LineState.Inactive: return True asyncio.run(try_play()) sendSerial(b'o') return True # async def get_media_info(): # sessions = await MediaManager.request_async() # # This source_app_user_model_id check and if statement is optional # # Use it if you want to only get a certain player/program's media # # (e.g. only chrome.exe's media not any other program's). # # To get the ID, use a breakpoint() to run sessions.get_current_session() # # while the media you want to get is playing. # # Then set TARGET_ID to the string this call returns. # current_session = sessions.get_current_session() # if current_session: # there needs to be a media session running # #if current_session.source_app_user_model_id == TARGET_ID: # info = await current_session.try_get_media_properties_async() # # song_attr[0] != '_' ignores system attributes # info_dict = {song_attr: info.__getattribute__(song_attr) for song_attr in dir(info) if song_attr[0] != '_'} # # converts winrt vector to list # info_dict['genres'] = list(info_dict['genres']) # return info_dict # # It could be possible to select a program from a list of current # # available ones. I just haven't implemented this here for my use case. # # See references for more information. # raise Exception('TARGET_PROGRAM is not the current media session') async def try_play(): print("try_play") if not DEBUG: sessions = await MediaManager.request_async() current_session = sessions.get_current_session() if current_session: await current_session.try_play_async() async def try_pause(): print("try_pause") if not DEBUG: sessions = await MediaManager.request_async() current_session = sessions.get_current_session() if current_session: await current_session.try_pause_async() if __name__ == '__main__': win32com.client.WithEvents("CLMgr.ClientLineMgr", PhoneLineEventHandler) while True: pythoncom.PumpWaitingMessages() time.sleep(0.1) # Don't use up all our CPU checking constantly