Esempio n. 1
0
    def SendCommand(self,
                    oAction: cAction,
                    oSetting: cInterFaceSettings,
                    uRetVar: str,
                    bNoLogOut: bool = False) -> eReturnCode:
        super().SendCommand(oAction=oAction,
                            oSetting=oSetting,
                            uRetVar=uRetVar,
                            bNoLogOut=bNoLogOut)

        iTryCount: int = 0
        eRet: eReturnCode = eReturnCode.Error
        uMsg: str
        byMsg: bytes

        if uRetVar != "":
            oAction.uGlobalDestVar = uRetVar

        oSetting.uRetVar = uRetVar

        uMsg = oAction.uCmd
        uMsg = ReplaceVars(uMsg, self.uObjectName + '/' + oSetting.uConfigName)
        uMsg = ReplaceVars(uMsg)

        #Logger.info ('Interface '+self.uObjectName+': Sending Command: '+sCommand + ' to '+oSetting.sHost+':'+oSetting.sPort)
        while iTryCount < self.iMaxTryCount:
            iTryCount += 1
            oSetting.Connect()
            if oSetting.bIsConnected:
                #time.sleep(2)
                try:
                    self.ShowInfo(uMsg=u'Sending Command: "' + uMsg +
                                  u'" to ' + oSetting.aIniSettings.uHost +
                                  ':' + oSetting.aIniSettings.uPort,
                                  uParConfigName=oSetting.uConfigName)
                    uMsg = uMsg.replace('\\n', '\n')
                    uMsg = uMsg.replace('\\r', '\r')
                    byMsg = ToBytes(uMsg)

                    if oAction.bWaitForResponse:
                        #All response comes to receiver thread, so we should hold the queue until vars are set
                        StartWait(self.iWaitMs)

                    oSetting.oTelnet.write(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()
        self.CloseSettingConnection(oSetting=oSetting, bNoLogOut=bNoLogOut)
        return eRet
Esempio n. 2
0
    def SendCommand(self, oAction, oSetting, uRetVar, bNoLogOut=False):
        cBaseInterFace.SendCommand(self, oAction, oSetting, uRetVar, bNoLogOut)

        iTryCount = 0
        iRet = 1

        if uRetVar != "":
            oAction.uGlobalDestVar = uRetVar

        oSetting.uRetVar = uRetVar

        uMsg = oAction.uCmd
        uMsg = ReplaceVars(uMsg,
                           self.uInterFaceName + '/' + oSetting.uConfigName)
        uMsg = ReplaceVars(uMsg)

        #Logger.info ('Interface '+self.uInterFaceName+': Sending Command: '+sCommand + ' to '+oSetting.sHost+':'+oSetting.sPort)
        while iTryCount < 2:
            iTryCount += 1
            oSetting.Connect()
            if oSetting.bIsConnected:
                #time.sleep(2)
                try:
                    self.ShowInfo(
                        u'Sending Command: "' + uMsg + u'" to ' +
                        oSetting.aInterFaceIniSettings.uHost + ':' +
                        oSetting.aInterFaceIniSettings.uPort,
                        oSetting.uConfigName)
                    uMsg = uMsg.replace('\\n', '\n')
                    uMsg = uMsg.replace('\\r', '\r')
                    #Bypass unicode bug in python 2.7
                    uMsg = str(uMsg)
                    if oAction.bWaitForResponse == True:
                        #All response comes to receiver thread, so we should hold the queue until vars are set
                        StartWait(self.iWaitMs)

                    oSetting.oTelnet.write(uMsg)
                    iRet = 0
                    break
                except Exception as e:
                    self.ShowError(u'can\'t Send Message',
                                   oSetting.uConfigName, e)
                    iRet = 1
                    oSetting.Disconnect()
        if oSetting.bIsConnected:
            if oSetting.aInterFaceIniSettings.iTimeToClose == 0:
                oSetting.Disconnect()
            elif oSetting.aInterFaceIniSettings.iTimeToClose != -1:
                Clock.unschedule(oSetting.FktDisconnect)
                Clock.schedule_once(
                    oSetting.FktDisconnect,
                    oSetting.aInterFaceIniSettings.iTimeToClose)
        return iRet
Esempio n. 3
0
    def SendCommand_Helper(self, oAction, oSetting, uInterFaceName):

        iRet = None
        self.uResponse = u''

        uUrlFull = oAction.uProtocol + oSetting.aInterFaceIniSettings.uHost + ":" + oSetting.aInterFaceIniSettings.uPort + oAction.uCmd
        uUrlFull = ReplaceVars(uUrlFull,
                               uInterFaceName + u'/' + oSetting.uConfigName)
        uUrlFull = ReplaceVars(uUrlFull)

        # oSetting.SetContextVar('Host',oSetting.aInterFaceIniSettings.uHost)
        # oSetting.SetContextVar('Port',oSetting.aInterFaceIniSettings.uPort)
        sAuth = u''

        #self.register_app('http://'+oAction.uHost,oSetting)
        #self.register_app(uUrlFull,oSetting)

        try:
            uData = oAction.uParams
            uData = ReplaceVars(uData,
                                uInterFaceName + u'/' + oSetting.uConfigName)
            uData = ReplaceVars(uData)

            # we do nothing special on soap actions
            if oAction.uType == 'soap':
                pass

            # we do nothing special on plain string actions
            if oAction.uType == 'string':
                pass

            if oAction.uType == 'none':
                return -1

            if oAction.uType == 'encode':
                if PY2:
                    uData = urllib.urlencode(uData)
                else:
                    uData = urllib.parse.urlencode(uData)

            uData = ReplaceVars(uData,
                                uInterFaceName + u'/' + oSetting.uConfigName)
            uData = ReplaceVars(uData)

            SetVar(uVarName='datalenght',
                   oVarValue=ToUnicode(len(uData)),
                   uContext=uInterFaceName + u'/' + oSetting.uConfigName)

            if oAction.uActionName == 'logon' or True:
                # base64 encode the username and password
                uTmp = ReplaceVars(
                    '%s:%s' %
                    ('$cvar(CONTEXT_USER)', '$cvar(CONTEXT_PASSWORD)'),
                    uInterFaceName + u'/' + oSetting.uConfigName)
                if PY2:
                    sAuth = base64.encodestring(uTmp).replace('\n', '')
                else:
                    bTmp = ToBytes(uTmp)
                    #bTmp  = base64.encodestring(bTmp)
                    bTmp = base64.encodebytes(bTmp)
                    uTmp = bTmp.decode('utf-8')
                    sAuth = uTmp.replace('\n', '')
                    #sAuth = base64.encodestring(bTmp).replace('\n', '')
            #construct and send the header
            uHeader = oAction.uHeaders
            uHeader = ReplaceVars(uHeader,
                                  uInterFaceName + u'/' + oSetting.uConfigName)
            uHeader = ReplaceVars(uHeader)
            uHeader = ReplaceVars(uHeader,
                                  uInterFaceName + u'/' + oSetting.uConfigName)

            aHeader = ToDic(uHeader)
            for uKey in aHeader:
                if isinstance(aHeader[uKey], string_types):
                    aHeader[uKey] = UnEscapeUnicode(aHeader[uKey])

            if not sAuth == u'':
                aHeader['Authorization'] = "Basic %s" % sAuth

            self.ShowInfo(
                u'Sending Command [%s]: %s %s to %s' %
                (oAction.uActionName, uData, UnEscapeUnicode(uHeader),
                 uUrlFull), oSetting.uConfigName)

            if not oAction.bWaitForResponse:
                oSetting.bIgnoreTimeOut = True
                fTimeOut = 0.02
            else:
                oSetting.bIgnoreTimeOut = False
                fTimeOut = oSetting.aInterFaceIniSettings.fTimeOut

            self.bStopWait = False
            self.oReq = UrlRequest(uUrlFull,
                                   method=oAction.uRequestType,
                                   req_body=uData,
                                   req_headers=aHeader,
                                   timeout=fTimeOut,
                                   on_error=oSetting.OnError,
                                   on_success=oSetting.OnReceive,
                                   debug=False)
            if oAction.bWaitForResponse:
                self.NewWait(0.05)
                if self.oReq.resp_status is not None:
                    uCmd, uRetVal = self.ParseResult(oAction, self.oReq.result,
                                                     oSetting)
                    iRet = self.oReq.resp_status
                    self.uResponse = self.oReq.result
                else:
                    if self.oReq._error is not None:
                        self.uResponse = self.oReq._error.strerror
                        iRet = self.oReq._error.errno
                if iRet is None:
                    iRet = 200
                if self.uResponse is None:
                    self.uResponse = "unknown"
                return iRet, self.uResponse
            else:
                return 200, ''

        except Exception as e:
            self.ShowError(u'can\'t Send Message #2', oSetting.uConfigName, e)

        return -1, ''