def handleStatusResponse(self, response, deviceClient): if (response == None): # no connectivity logger.info("Internet access unavailable") return code = response.status_code content = response.content if (code == HttpStatus.SC_OK): logger.info("Status API completed successfully") xmlNode = XMLUtils.stringToElement(content) configNode = XMLParser.getClientConfigFromStatusResponse(xmlNode) actionsNode = XMLParser.getActionsFromStatusResponse(xmlNode) campaignsNode = XMLParser.getCampaignsFromStatusResponseNode( xmlNode) pubNubNode = XMLParser.getPubNubKeysFromStatusResponse(xmlNode) deviceClient.clientConfig.parseNode(configNode) deviceClient.actionHandler.parseNode(actionsNode) deviceClient.campaignHandler.parseNode(campaignsNode) deviceClient.pubNubHandler.parseNode(pubNubNode) elif (code == HttpStatus.SC_NO_CONTENT): # git Nothing hast changed logger.warn("No Content") elif (code == HttpStatus.SC_NOT_MODIFIED): #Nothing changed and NO STATUS on Config moving on logger.warn("Not Modified") elif (code == HttpStatus.SC_NOT_FOUND): logger.warn( "404 Response from Server (Unable to find device in Database)") elif (code == HttpStatus.SC_FORBIDDEN): logger.warn( "403 Response from Server (Forbidden - Access to this device License has expired) " + content) elif (code == HttpStatus.SC_INTERNAL_SERVER_ERROR): logger.error("Status: %d, content: %s" % (response.status_code, content)) else: logger.warn( "Non 200 Response from server unable to handle response: %d" % code)
def loadKeys(self): logger.info("Loading configuration from:" + ClientURISettings.CAMPAIGNS_FILE) if not (os.path.exists(ClientURISettings.CAMPAIGNS_FILE) and os.path.isfile(ClientURISettings.CAMPAIGNS_FILE)): return try: xmlContent = XMLUtils.readNodeFromFile(ClientURISettings.CAMPAIGNS_FILE) self.parsedXml = XMLParser.getPubNubKeysFromCampaignsConfig(xmlContent) self.parseNode(self.parsedXml) except Exception as err: logger.error(err)
def register(self): rest = RESTClient() if (not self.mac): self.mac = ProcessExecutor.getMACeth0() if (not self.serialNumber): self.serialNumber = ProcessExecutor.getSerialNumber() while not self.isRegistered(): try: logger.debug("Starting REGISTER Request, MAC %s, serial %s" % (self.mac, self.serialNumber)) formparams = {} formparams["mac"] = self.mac formparams["serialNumber"] = self.serialNumber formparams[ "major"] = self.mainParent.clientConfig.software.major formparams[ "minor"] = self.mainParent.clientConfig.software.minor formparams[ "build"] = self.mainParent.clientConfig.software.build formparams["kernel"] = ProcessExecutor.getKernelVersion() formparams["platform"] = "BGAX4" r = rest.PUTRequest(uri=ClientURISettings.getRegistrationUri(), content=formparams) if (r == None): logger.error("No response from server, check connectivity") elif (r.status_code == HttpStatus.SC_FORBIDDEN): logger.error( "Device %s already registered in database, please remedy" % self.mac) elif (r.status_code == HttpStatus.SC_OK): xmlNode = XMLUtils.stringToElement(r.content) token = XMLParser.getTokenFromRegisterResponse(xmlNode) self.mainParent.clientConfig.setToken(token) self.mainParent.clientConfig.authentication.setToken(token) self.mainParent.clientConfig.saveConfiguration() logger.info("Successful registration, token %s received" % token) return else: logger.error("Status: %d, content: %s" % (r.status_code, r.content)) except Exception as err: logger.error(err) # If registration failed sleep 60 seconds before retrying logger.debug("Registration failed, sleeping %d s" % TimeConstants.ONE_MINUTE_SECONDS) time.sleep(TimeConstants.ONE_MINUTE_SECONDS)
def handleStatusResponse(self, response, deviceClient): if ( response == None ): # no connectivity logger.info("Internet access unavailable") return code = response.status_code content = response.content if ( code == HttpStatus.SC_OK ): logger.info("Status API completed successfully") xmlNode = XMLUtils.stringToElement(content) configNode = XMLParser.getClientConfigFromStatusResponse(xmlNode) actionsNode = XMLParser.getActionsFromStatusResponse(xmlNode) campaignsNode = XMLParser.getCampaignsFromStatusResponseNode(xmlNode) pubNubNode = XMLParser.getPubNubKeysFromStatusResponse(xmlNode) deviceClient.clientConfig.parseNode(configNode) deviceClient.actionHandler.parseNode(actionsNode) deviceClient.campaignHandler.parseNode(campaignsNode) deviceClient.pubNubHandler.parseNode(pubNubNode) elif ( code == HttpStatus.SC_NO_CONTENT ): # git Nothing hast changed logger.warn("No Content") elif ( code == HttpStatus.SC_NOT_MODIFIED ): #Nothing changed and NO STATUS on Config moving on logger.warn("Not Modified") elif ( code == HttpStatus.SC_NOT_FOUND ): logger.warn("404 Response from Server (Unable to find device in Database)") elif ( code == HttpStatus.SC_FORBIDDEN ): logger.warn("403 Response from Server (Forbidden - Access to this device License has expired) " + content) elif ( code == HttpStatus.SC_INTERNAL_SERVER_ERROR ): logger.error("Status: %d, content: %s" % (response.status_code, content)) else: logger.warn("Non 200 Response from server unable to handle response: %d" % code)
def loadKeys(self): logger.info("Loading configuration from:" + ClientURISettings.CAMPAIGNS_FILE) if not (os.path.exists(ClientURISettings.CAMPAIGNS_FILE) and os.path.isfile(ClientURISettings.CAMPAIGNS_FILE)): return try: xmlContent = XMLUtils.readNodeFromFile( ClientURISettings.CAMPAIGNS_FILE) self.parsedXml = XMLParser.getPubNubKeysFromCampaignsConfig( xmlContent) self.parseNode(self.parsedXml) except Exception as err: logger.error(err)
def register(self): rest = RESTClient() if ( not self.mac ): self.mac = ProcessExecutor.getMACeth0() if ( not self.serialNumber ): self.serialNumber = ProcessExecutor.getSerialNumber() while not self.isRegistered(): try: logger.debug("Starting REGISTER Request, MAC %s, serial %s" % (self.mac, self.serialNumber)) formparams = {} formparams["mac"] = self.mac formparams["serialNumber"] = self.serialNumber formparams["major"] = self.mainParent.clientConfig.software.major formparams["minor"] = self.mainParent.clientConfig.software.minor formparams["build"] = self.mainParent.clientConfig.software.build formparams["kernel"] = ProcessExecutor.getKernelVersion() formparams["platform"] = "BGAX4" r = rest.PUTRequest(uri=ClientURISettings.getRegistrationUri(), content=formparams) if ( r == None ): logger.error("No response from server, check connectivity") elif ( r.status_code == HttpStatus.SC_FORBIDDEN ): logger.error("Device %s already registered in database, please remedy" % self.mac) elif ( r.status_code == HttpStatus.SC_OK ): xmlNode = XMLUtils.stringToElement(r.content) token = XMLParser.getTokenFromRegisterResponse(xmlNode) self.mainParent.clientConfig.setToken(token) self.mainParent.clientConfig.authentication.setToken(token) self.mainParent.clientConfig.saveConfiguration() logger.info("Successful registration, token %s received" % token) return else: logger.error("Status: %d, content: %s" % (r.status_code, r.content)) except Exception as err: logger.error(err) # If registration failed sleep 60 seconds before retrying logger.debug("Registration failed, sleeping %d s" % TimeConstants.ONE_MINUTE_SECONDS) time.sleep(TimeConstants.ONE_MINUTE_SECONDS)