def test_new_node(): serialized = Message.new_node('1234') assert serialized == Message.serialize({ 'type': 'new', 'uid': '1234', 'dst': 'all' }) serialized = Message.new_node('1234', '5678') assert serialized == Message.serialize({ 'type': 'new', 'uid': '1234', 'dst': '5678' })
def handle_node_check(self, data): """Handle alive message received from coap node.""" node_id = data['id'] node = MQTTNode(node_id) node.check_time = time.time() if node not in self.nodes: resources_topic = 'node/{}/resources'.format(node_id) yield from self.mqtt_client.subscribe([(resources_topic, QOS_1)]) logger.debug("Subscribed to topic: {}".format(resources_topic)) node_uid = str(uuid.uuid4()) self.nodes.update( {node: { 'uid': node_uid, 'data': { 'protocol': PROTOCOL } }}) logger.debug("Available nodes: {}".format(self.nodes)) self._on_message_cb(Msg.new_node(node_uid)) self._on_message_cb(Msg.update_node(node_uid, "protocol", PROTOCOL)) discover_topic = 'gateway/{}/discover'.format(node_id) yield from self.mqtt_client.publish(discover_topic, b"resources", qos=QOS_1) logger.debug("Published '{}' to topic: {}".format( "resources", discover_topic)) else: data = self.nodes.pop(node) self.nodes.update({node: data}) logger.debug("Available nodes: {}".format(self.nodes))
def fetch_nodes_cache(self, source): """Send cached nodes information.""" logger.debug("Fetching cached information of registered nodes.") for _, node in self.nodes.items(): self._on_message_cb(Msg.new_node(node['uid'], dst=source)) for endpoint, value in node['data'].items(): yield self._on_message_cb( Msg.update_node(node['uid'], endpoint, value, dst=source))
def add_node(self, node): """Add a new node to the list of nodes and notify the broker.""" node.set_resource_value('protocol', self.PROTOCOL) self.nodes.update({node.uid: node}) self.send_to_broker(Message.new_node(node.uid)) for res, value in node.resources.items(): self.send_to_broker(Message.update_node(node.uid, res, value)) yield self.discover_node(node)
def fetch_nodes_cache(self, source): """Send cached nodes information.""" logger.debug( "Fetching cached information of registered nodes '{}'.".format( self.nodes)) for _, value in self.nodes.items(): self._on_message_cb(Msg.new_node(value['uid'], dst=source)) for resource, data in value['data'].items(): self._on_message_cb( Msg.update_node(value['uid'], resource, data, dst=source))
def fetch_nodes_cache(self, source): """Send cached nodes information.""" logger.debug("Fetching cached information of registered nodes.") for ws, node in self.nodes.items(): self.send_to_broker(Message.new_node(node['uid'], dst=source)) for endpoint, value in node['data'].items(): self.send_to_broker( Message.update_node(node['uid'], endpoint, value, dst=source))
def fetch_nodes_cache(self, client): """Send cached nodes information to a given client. :param client: the ID of the client """ logger.debug( "Fetching cached information of registered nodes '{}'.".format( self.nodes)) for node in self.nodes.values(): self.send_to_broker(Message.new_node(node.uid, dst=client)) for resource, value in node.resources.items(): self.send_to_broker( Message.update_node(node.uid, resource, value, dst=client))
def open(self): """Discover nodes on each opened connection.""" self.set_nodelay(True) logger.debug("New node websocket opened") self.application.nodes.update( {self: { 'uid': str(uuid.uuid4()), 'data': { 'protocol': PROTOCOL } }}) node_uid = self.application.nodes[self]['uid'] self.application.send_to_broker(Message.new_node(node_uid)) yield self.write_message(Message.discover_node()) self.application.send_to_broker( Message.update_node(node_uid, 'protocol', PROTOCOL))
def handle_coap_alive(self, address): """Handle alive message received from coap node.""" node = CoapNode(address) node.check_time = time.time() if node not in self.nodes: node_uid = str(uuid.uuid4()) self.nodes.update({ node: { 'uid': node_uid, 'data': { 'ip': address, 'protocol': 'coap' } } }) self._on_message_cb(Msg.new_node(node_uid)) self._on_message_cb(Msg.update_node(node_uid, "ip", address)) self._on_message_cb(Msg.update_node(node_uid, "protocol", 'coap')) self.discover_node(node, node_uid) else: data = self.nodes.pop(node) self.nodes.update({node: data})
def handle_coap_check(self, address, reset=False): """Handle check message received from coap node.""" node = CoapNode(address) node.check_time = time.time() if node not in self.nodes: # This is a totally new node: create uid, initialized cached node # send 'new' node notification, 'update' notification. node_uid = str(uuid.uuid4()) self.nodes.update({ node: { 'uid': node_uid, 'data': { 'ip': address, 'protocol': PROTOCOL } } }) self._on_message_cb(Msg.new_node(node_uid)) self._on_message_cb(Msg.update_node(node_uid, "ip", address)) self._on_message_cb(Msg.update_node(node_uid, "protocol", PROTOCOL)) self.discover_node(node, node_uid) elif reset: # The data of the node need to be reset without removing it. This # is particularly the case after a reboot of the node or a # firmware update of the node that triggered the reboot. node_uid = self.nodes[node]['uid'] self.nodes[node]['data'] = {} self.nodes[node]['data'].update({ 'ip': address, 'protocol': PROTOCOL }) self._on_message_cb(Msg.reset_node(node_uid)) self.discover_node(node, node_uid) else: # The node simply sent a check message to notify that it's still # online. data = self.nodes.pop(node) self.nodes.update({node: data})