def send(self, Msg: str, TargetList: list, ScreenTimeout: int = 0, Refresh: bool = True, Secret: bool = False) -> int: if not all(isinstance(T, TargetUnit) for T in TargetList): raise ValueError('Item of TargetList must be TargetUnit') if ScreenTimeout == 0: CurrentScreenTimeout = Config.ScreenTimeOut else: CurrentScreenTimeout = ScreenTimeout self._ReceiveDataQueue = [] BreakDetectAfterSend = False BreakIndex = -1 isSecret = Secret while True: if Refresh and not Msg.endswith(Command.Refresh): Msg = Msg + Command.Refresh try: Msg = Msg.encode('big5-uao', 'ignore') except AttributeError: pass except Exception as e: traceback.print_tb(e.__traceback__) print(e) Msg = Msg.encode('big5', 'ignore') if isSecret: Log.showValue(Log.Level.DEBUG, [i18n.SendMsg], i18n.HideSensitiveInfor) else: Log.showValue(Log.Level.DEBUG, [i18n.SendMsg], Msg) if self._ConnectMode == ConnectMode.Telnet: self._Core.read_very_eager() self._Core.write(Msg) else: asyncio.get_event_loop().run_until_complete( self._Core.send(Msg)) if BreakDetectAfterSend: return BreakIndex Msg = '' CycleTime = 0 CycleWait = 0 ReceiveData = [] ReceiveDataBuffer = bytes() StartTime = time.time() MidTime = time.time() while MidTime - StartTime < CurrentScreenTimeout: if self._ConnectMode == ConnectMode.Telnet: try: ReceiveDataTemp = self._Core.read_very_eager() except EOFError: return -1 else: try: asyncio.get_event_loop().run_until_complete( WebsocketReceiver(self._Core, CurrentScreenTimeout)) ReceiveDataTemp = _WSRecvData except websockets.exceptions.ConnectionClosed: return -1 except asyncio.TimeoutError: return -1 ReceiveDataBuffer += ReceiveDataTemp ReceiveDataTemp = ReceiveDataBuffer.decode('big5-uao', errors='ignore') Screen = self._cleanScreen(ReceiveDataTemp) FindTarget = False for Target in TargetList: Condition = Target.isMatch(Screen) if Condition: if len(Screen) > 0: Screens.show(Screen) self._ReceiveDataQueue.append(Screen) Target.raiseException() FindTarget = True Log.showValue(Target.getLogLevel(), [i18n.PTT, i18n.Msg], Target.getDisplayMsg()) EndTime = time.time() Log.showValue(Log.Level.DEBUG, [ i18n.SpendTime, ], round(EndTime - StartTime, 2)) if Target.isBreak(): return TargetList.index(Target) Msg = Target.getResponse(Screen) AddRefresh = False if Target.isRefresh(): AddRefresh = True elif Refresh: AddRefresh = True if AddRefresh: if not Msg.endswith(Command.Refresh): Msg = Msg + Command.Refresh isSecret = Target.isSecret() if Target.isBreakAfterSend(): BreakIndex = TargetList.index(Target) BreakDetectAfterSend = True break if FindTarget: break if len(Screen) > 0: Screens.show(Screen) self._ReceiveDataQueue.append(Screen) MidTime = time.time() if not FindTarget: raise NoMatchTargetError(self._ReceiveDataQueue) raise NoMatchTargetError(self._ReceiveDataQueue)
def send(self, Msg: str, TargetList: list, ScreenTimeout: int = 0, Refresh: bool = True, Secret: bool = False) -> int: if not all(isinstance(T, TargetUnit) for T in TargetList): raise ValueError('Item of TargetList must be TargetUnit') if self._UseTooManyResources not in TargetList: TargetList.append(self._UseTooManyResources) if ScreenTimeout == 0: CurrentScreenTimeout = self.Config.ScreenTimeOut else: CurrentScreenTimeout = ScreenTimeout BreakDetectAfterSend = False BreakIndex = -1 isSecret = Secret UseTooManyRes = False while True: if Refresh and not Msg.endswith(Command.Refresh): Msg = Msg + Command.Refresh try: Msg = Msg.encode('big5-uao', 'replace') except AttributeError: pass except Exception as e: traceback.print_tb(e.__traceback__) print(e) Msg = Msg.encode('big5', 'replace') if isSecret: Log.showValue(self.Config, Log.Level.DEBUG, [i18n.SendMsg], i18n.HideSensitiveInfor) else: Log.showValue(self.Config, Log.Level.DEBUG, [i18n.SendMsg], Msg) try: asyncio.get_event_loop().run_until_complete( self._Core.send(Msg)) except websockets.exceptions.ConnectionClosedError: raise Exceptions.ConnectionClosed() except RuntimeError: raise Exceptions.ConnectionClosed() except websockets.exceptions.ConnectionClosedOK: raise Exceptions.ConnectionClosed() if BreakDetectAfterSend: return BreakIndex Msg = '' ReceiveDataBuffer = bytes() # print(f'0 {UseTooManyRes}') StartTime = time.time() MidTime = time.time() while MidTime - StartTime < CurrentScreenTimeout: try: asyncio.get_event_loop().run_until_complete( WebsocketReceiver(self._Core, CurrentScreenTimeout)) ReceiveDataTemp = _WSRecvData except websockets.exceptions.ConnectionClosed: # print(f'0.1 {UseTooManyRes}') if UseTooManyRes: # print(f'0.2 {UseTooManyRes}') raise Exceptions.UseTooManyResources() # print(f'0.3 {UseTooManyRes}') raise Exceptions.ConnectionClosed() except websockets.exceptions.ConnectionClosedOK: raise Exceptions.ConnectionClosed() except asyncio.TimeoutError: return -1 ReceiveDataBuffer += ReceiveDataTemp ReceiveDataTemp = ReceiveDataBuffer.decode('big5-uao', errors='replace') Screen = self._cleanScreen(ReceiveDataTemp) FindTarget = False for Target in TargetList: Condition = Target.isMatch(Screen) if Condition: if Target._Handler is not None: Target._Handler() if len(Screen) > 0: Screens.show(self.Config, Screen) self._RDQ.add(Screen) # self._ReceiveDataQueue.append(Screen) if Target == self._UseTooManyResources: # print('!!!!!!!!!!!!!!!') UseTooManyRes = True # print(f'1 {UseTooManyRes}') break Target.raiseException() FindTarget = True Log.showValue(self.Config, Target.getLogLevel(), [i18n.PTT, i18n.Msg], Target.getDisplayMsg()) EndTime = time.time() Log.showValue(self.Config, Log.Level.DEBUG, [ i18n.SpendTime, ], round(EndTime - StartTime, 2)) if Target.isBreak(): return TargetList.index(Target) Msg = Target.getResponse(Screen) AddRefresh = False if Target.isRefresh(): AddRefresh = True elif Refresh: AddRefresh = True if AddRefresh: if not Msg.endswith(Command.Refresh): Msg = Msg + Command.Refresh isSecret = Target.isSecret() if Target.isBreakAfterSend(): BreakIndex = TargetList.index(Target) BreakDetectAfterSend = True break # print(f'2 {UseTooManyRes}') if UseTooManyRes: # print(f'3 {UseTooManyRes}') continue # print(f'4 {UseTooManyRes}') if FindTarget: break if len(Screen) > 0: Screens.show(self.Config, Screen) self._RDQ.add(Screen) # self._ReceiveDataQueue.append(Screen) MidTime = time.time() if not FindTarget: raise Exceptions.NoMatchTargetError(self._RDQ) raise Exceptions.NoMatchTargetError(self._RDQ)