def node_register(self, message): """ Module registration authority handling. Can be overriden by user""" try: # create an index pointer for each module name namel = {v["name"]: k for k, v in self._modules.iteritems()} if not self._modules.keys(): mod_id = 1 # If the name exists give module the same id elif message.device_name in namel: mod_id = namel[message.device_name] # TODO make it clean old keys based on last time else: mod_id = max(self._modules.keys()) + 1 log.debug("Handing out new module id %d" % mod_id) self._modules[mod_id] = {"name": message.device_name, "last": message.time} # prepare the payload for the acknowlegement pl = self.messenger.new_service(msg="%d" % mod_id) response = self.messenger.ack_msg(pl) # Repond to node with the module id return(response) except Exception as e: log.error("Exception %s" % e) pl = self.messenger.new_service(name="%s" % e) response = self.messenger.ack_msg(pl) # Repond to node with exception return(response)
def network_register(self, message): try: metadata = message.metadata loc = message.location # create an index pointer for each module name namel = {v["name"]: k for k, v in self._clients.iteritems()} if not self._clients.keys(): client_id = 1 # If the name exists give module the same id elif metadata.device_name in namel: client_id = namel[metadata.device_name] # TODO make it clean old keys based on last time else: client_id = max(self._clients.keys()) + 1 if loc: location = self.messenger._location_tpl(lat=loc.lat, long=loc.long, street=loc.street, building=loc.building, floor=loc.floor, room=loc.room, city=loc.city, country=loc.country, postcode=loc.postcode) self._clients[client_id] = {"name": metadata.device_name, "app_id": metadata.app_id, "network_id": metadata.network_id, "last": metadata.time, "location": location} # prepare the payload for the acknowlegement response = self.messenger.ack_msg(cmd="register", params=["%s" % client_id]) # Repond to node with the module id return(response) except Exception as e: log.error("Exception %s" % e) # prepare the payload for the acknowlegement response = self.messenger.nack_msg(cmd="register", params=["%s" % e]) # Repond to node with exception return(response)
def network_register(self): try: reg_msg = self.ul_messenger.register_msg() # Attempt to register and get the response reginfo = self.upload(reg_msg) # If server accepted registration continue if reginfo.metadata.message_type == self.ul_messenger.ACK: node_id = int([n for n in reginfo.control.params][0]) self.messenger.set_id(node_id) log.info("Registration Accepted, new id %d" % node_id) return else: raise Exception() except Exception as e: log.error("Failed to register to network %s" % e) sys.exit(1) print(reg_msg)
def node_register(self): """ User accessible method for handling registration message """ try: # Prepare and send the registration message init_msg = self.messenger.register_msg() self._send(msg=init_msg) # Accept the new module_id from server reginfo = self._recv() # If server accepted registration continue if reginfo.msg_type == NodeMessenger.ACK: node_id = [n for n in reginfo.payload][0].msg node_id = int(node_id) self.messenger.set_id(node_id) log.info("Registration Accepted, new id %d" % node_id) return else: raise Exception() except Exception as e: log.error("Failed to register module %s" % e) sys.exit(1)
def error(self, *args): """ Print a debug message and notify the user with the LED.""" CLogger.error(args[-1])