def __handle_devices_response( self, command, parameters, response ): logger.debug( "HC2Driver.handle_devices_response: {0}".format( parameters ) ) try: nodes = json.loads( response ) except: logger.error( "HC2Driver.handle_devices_response: response is not formated as json: {0}".format( response) ) return if isinstance(nodes, dict): """ response contains single device information """ nodes = [nodes] all_nodes = False else: """ response contains all devices information """ all_nodes = True new_nodes = set() for node_info in nodes: node_id = str(node_info['id']) if all_nodes: new_nodes.add(node_id) if node_id in self.__devices: logger.debug('Node {0}: Updated'.format(node_id)) self.nodes[node_id].update_node_info(node_info) else: logger.debug('Node {0}: Added'.format(node_id)) self.lock_nodes() self.__devices.add( node_id ) self.nodes[node_id] = HC2Device(self.network_id, node_id) self.nodes[node_id].update_node_info(node_info) self.release_nodes() notification = Notification( Notification.Type_NodeAdded ) notification.node_id = node_id notification.network_id = self.network_id self.queue_notification( notification ) #self.nodes[node_id].set_query_stage( Node.QueryStage_NodeValues, True ) self.send_query_stage_complete(node_id, Node.QueryStage_NodeInfo ) if all_nodes: missing_nodes = self.__devices - new_nodes for node_id in missing_nodes: logger.debug('Node {0}: Removed'.format(node_id)) self.lock_nodes() del self.nodes[node_id] self.release_nodes() notification = Notification( Notification.Type_NodeRemoved ) notification.node_id = node_id notification.network_id = self.network_id self.queue_notification( notification ) return
def add_value(self, value_obj): logger.debug("Node:add_value: ValueID:%s" % value_obj.value_id) self.__values.insert(0, value_obj) notification = Notification( Notification.Type_ValueAdded ) notification.node_id = self._node_id notification.network_id = self._network_id notification.value_id = value_obj.value_id self.driver.queue_notification( notification )
def update(self, value): self._value = str(value) notification = Notification( Notification.Type_ValueChanged ) notification.node_id = self.__node_id notification.network_id = self.__network_id notification.value_id = self.__id from has.manager.manager import Manager driver = Manager.get_driver( self.__network_id ) driver.queue_notification( notification )
def advance_queries(self): self.add_QSC = False logger.debug("Node {0}: queryPending={1} queryStage={2}".format(self._node_id, self._query_pending, self.query_stage ) ) while not self._query_pending: if self.query_stage == Node.QueryStage_None: logger.debug("Node {0}: Query Stage None".format(self._node_id) ) self.__query_stage = Node.QueryStage_NodeInfo #logger.debug("advance_queries: Node: {0} Move to stage NodeInfo".format(self._node_id)) self.query_retries = 0 elif self.query_stage == Node.QueryStage_NodeInfo: logger.debug("Node {0}: Query Stage NodeInfo".format(self._node_id) ) if not self._node_info_received: self.query_node_info() #logger.info("advance_queries: Node: {0} Query command sent %s".format(self._node_id)) self._query_pending = True self.add_QSC = True else: #logger.debug("advance_queries: Node: {0} Move to stage NodeValues".format(self._node_id)) self.__query_stage = Node.QueryStage_NodeValues self.query_retries = 0 elif self.query_stage == Node.QueryStage_NodeValues: assert self._node_info_received == True, "Advanced to query node values but not info not received" logger.debug("Node {0}: Query Stage NodeValues".format(self._node_id) ) if not self._values_info_received: logger.debug("advance_queries: Node: {0} Update values info command send".format(self._node_id) ) self.update_values_info() self._query_pending = True self.add_QSC = True else: #logger.debug("advance_queries: Node: {0} Move to stage Complete".format(self._node_id)) self.update_values_info() self.__query_stage = Node.QueryStage_Complete self.query_retries = 0 elif self.query_stage == Node.QueryStage_Complete: logger.info("Node {0}: All Queries Complete".format(self._node_id) ) notification = Notification( Notification.Type_NodeQueriesComplete ) notification.node_id = self._node_id notification.network_id = self._network_id self.driver.queue_notification( notification ) self.driver.check_completed_node_queries() return if self.add_QSC: #logger.debug("advance_queries: Sending query stage complete: {0}".format(self._node_id)) self.driver.send_query_stage_complete( self._node_id, self.query_stage ) return
def update_node_info(self, node_info): logger.debug("Node:update_node_info: Node {0}:".format( self._node_id ) ) assert node_info is not None, "Update node info with 'None' value" changes = set() if self._node_info is not None: set_current, set_past = set(node_info.keys()), set(self._node_info.keys()) intersect = set_current.intersection(set_past) changes = set(o for o in intersect if self._node_info[o] != node_info[o]) self._node_info = node_info self._node_info_received = True self._values_info_received = True if changes: notification = Notification( Notification.Type_NodeChanged ) notification.node_id = self._node_id notification.network_id = self._network_id self.driver.queue_notification( notification ) self.set_query_stage( Node.QueryStage_NodeInfo )