def ReadAction(self, oAction: cAction) -> None: """ Adds and defaults some common attributes to the action :param cAction oAction: Reads an action from the action pars """ oAction.uType = oAction.dActionPars.get(u'type', u'send') oAction.uCmd = oAction.dActionPars.get(u'cmd', u'No cmd action defined') oAction.uLocalDestVar = oAction.dActionPars.get( u'ldestvar', u'RESULT_' + oAction.uActionName) oAction.uGlobalDestVar = oAction.dActionPars.get( u'gdestvar', u'RESULT_' + oAction.uActionName) oAction.uGetVar = oAction.dActionPars.get(u'getvar', u'') oAction.bWaitForResponse = ToBool( oAction.dActionPars.get(u'waitforresponse', u'0')) oAction.uParseResultOption = oAction.dActionPars.get( u'parseoption', self.aIniSettings.uParseResultOption) oAction.uParseResultTokenizeString = oAction.dActionPars.get( u'parsetoken', self.aIniSettings.uParseResultTokenizeString) oAction.uParseResultFlags = oAction.dActionPars.get( u'parseflags', self.aIniSettings.uParseResultFlags) oAction.uResultEndString = oAction.dActionPars.get( u'parseendstring', self.aIniSettings.uResultEndString) oAction.dActionPars['interface'] = self.oInterFace.uObjectName oAction.dActionPars['configname'] = self.uConfigName if oAction.dActionPars.get('varcontext', '') == "codeset": oAction.dActionPars["varcontext"] = self.uContext
def SendCommand(self, oAction: cAction, oSetting: cInterFaceSettings, uRetVar: str, bNoLogOut: bool = False) -> eReturnCode: super().SendCommand(oAction=oAction, oSetting=oSetting, uRetVar=uRetVar, bNoLogOut=bNoLogOut) if uRetVar != "": oAction.uGlobalDestVar = uRetVar iTryCount = 0 eRet: eReturnCode = eReturnCode.Error #Logger.info ('Interface '+self.sInterFaceName+': Sending Command: '+sCommand + ' to '+oSetting.sHost+':'+oSetting.sPort) while iTryCount < self.iMaxTryCount: iTryCount += 1 oSetting.Connect() # we need to verify if we are really connected, as the connection might have died # and .sendall will not return on error in this case #bIsConnected=oSetting.IsConnected() if oSetting.bIsConnected: uMsg = oAction.uCmd try: uMsg = ReplaceVars( uMsg, self.uObjectName + '/' + oSetting.uConfigName) uMsg = ReplaceVars(uMsg) oAction.uGetVar = ReplaceVars( oAction.uGetVar, self.uObjectName + '/' + oSetting.uConfigName) oAction.uGetVar = ReplaceVars(oAction.uGetVar) oSetting.uMsg = uMsg oSetting.uRetVar = uRetVar oSetting.uRetVar = uRetVar self.ShowInfo(uMsg=u'Sending Command: ' + uMsg + ' to ' + oSetting.aIniSettings.uHost + ':' + oSetting.aIniSettings.uPort, uParConfigName=oSetting.uConfigName) #All response comes to receiver thread, so we should hold the queue until vars are set if oSetting.oAction.bWaitForResponse: StartWait(self.iWaitMs) oSetting.oWebSocket.send(uMsg) fSleep(fSeconds=0.01) eRet = eReturnCode.Success break except Exception as e: self.ShowError(uMsg=u'Can\'t send message', uParConfigName=oSetting.uConfigName, oException=e) eRet = eReturnCode.Error oSetting.Disconnect() oSetting.bOnError = True if not uRetVar == u'': SetVar(uVarName=uRetVar, oVarValue=u"Error") else: if not uRetVar == u'': SetVar(uVarName=uRetVar, oVarValue=u"Error") self.CloseSettingConnection(oSetting=oSetting, bNoLogOut=bNoLogOut) return eRet
def SendCommand(self,oAction:cAction,oSetting:cInterFaceSettings,uRetVar:str,bNoLogOut:bool=False) -> eReturnCode: super().SendCommand(oAction=oAction,oSetting=oSetting,uRetVar=uRetVar,bNoLogOut=bNoLogOut) uTst:str uFormat:str uKey:str iTryCount:int = 0 eRet:eReturnCode = eReturnCode.Error uMsg:str = oAction.uCmd if uRetVar!="": oAction.uGlobalDestVar=uRetVar uTst=uMsg[:3] if uTst==u'MVL' and uMsg!=u'MVLUP' and uMsg!=u'MVLDOWN' and not uMsg.endswith("QSTN"): Var_Int2Hex(uVarName = self.uObjectName+'/'+oSetting.uConfigName+'volumetoset') if (uTst==u'CTL' or uTst==u'SWL') and not uMsg.endswith("QSTN"): uFormat='{:+03X}' uKey = oSetting.aIniSettings.uHost + oSetting.aIniSettings.uPort if uKey in self.dDeviceSettings: if uTst == u'CTL': uFormat=self.dDeviceSettings[uKey].uVar_CTLFormat else: uFormat = self.dDeviceSettings[uKey].uVar_SWLFormat Var_Int2Hex(uVarName = self.uObjectName + '/' + oSetting.uConfigName + 'volumetoset', uFormat = uFormat) uMsg = ReplaceVars(uMsg,self.uObjectName+'/'+oSetting.uConfigName) uMsg = ReplaceVars(uMsg) oSetting.uMsg = uMsg oSetting.uRetVar = uRetVar if uTst == u"NRI": uKey = oSetting.aIniSettings.uHost + oSetting.aIniSettings.uPort if uKey in self.dDeviceSettings and False: self.dDeviceSettings[uKey].WriteVars(uVarPrefix=uRetVar, oAction=oAction) return eReturnCode.Success else: self.dDeviceSettings[uKey] = self.cDeviceSettings(self, oSetting) # we write the defaults to var, in case we can't connect to the receiver self.dDeviceSettings[uKey].WriteVars(uVarPrefix=uRetVar, oAction=oAction) #Logger.info (u'Interface '+self.uObjectName+': Sending Command: '+sCommand + ' to '+oSetting.sHost+':'+oSetting.sPort) while iTryCount<self.iMaxTryCount: iTryCount+=1 oSetting.Connect() if oSetting.bIsConnected: try: oAction.uGetVar = ReplaceVars(oAction.uGetVar,self.uObjectName+'/'+oSetting.uConfigName) oAction.uGetVar = ReplaceVars(oAction.uGetVar) self.ShowInfo (uMsg=u'Sending Command: '+uMsg + ' to '+oSetting.aIniSettings.uHost+':'+oSetting.aIniSettings.uPort,uParConfigName=oSetting.uConfigName) byMsg:bytes = oSetting.CreateEISPHeader(uMsg) if oAction.bWaitForResponse: #All response comes to receiver thread, so we should hold the queue until vars are set if uTst!='NRI': StartWait(self.iWaitMs) else: StartWait(2000) oSetting.oSocket.sendall(byMsg) eRet = eReturnCode.Success break except Exception as e: self.ShowError(uMsg = u'Can\'t Send Message',uParConfigName=oSetting.uConfigName,oException=e) eRet = eReturnCode.Error oSetting.Disconnect() if not uRetVar==u'': SetVar(uVarName = uRetVar, oVarValue = u"Error") else: if iTryCount==self.iMaxTryCount: self.ShowWarning(uMsg=u'Nothing done,not connected! ->[%s]' % oAction.uActionName, uParConfigName=oSetting.uConfigName) if uRetVar: SetVar(uVarName = uRetVar, oVarValue = u"?") self.CloseSettingConnection(oSetting=oSetting,bNoLogOut=bNoLogOut) return eRet