def update_scanned_devices(self, scanned_devices): db = DryadDatabase() for device in scanned_devices: record_exists = False # Check if this node already exists in the database result = db.get_devices(address=device.addr.upper()) if (result != False) or \ ((type(result) == type(list)) and (len(result) > 0)): self.logger.debug(str(result)) self.logger.info("Node already exists: [{}] {}/{}".format( result.device_type, result.node_id, result.address)) continue # Get the name of the device first node_id = device.getValueText(ADTYPE_LOCAL_NAME) if node_id == None: self.logger.error("Could not obtain device name: {}".format( device.addr)) continue node_id = node_id.strip('\x00') # Add a node record in the database result = db.insert_or_update_node(name=node_id, node_class="UNKNOWN", site_name="????", lat=14.37, lon=120.58) if result == False: self.logger.error("Unable to add node record") continue # Add a node device record in the database result = db.insert_or_update_device(address=device.addr.upper(), node_id=node_id, device_type="UNKNOWN") if result == False: self.logger.error("Unable to add node device record") continue db.close_session() return
def cache_reading(self, reading): node_name = self.parent.get_name() node_address = self.parent.get_address() db = DryadDatabase() matched_devices = db.get_devices(address=node_address) node = matched_devices[0] print("Reading: {}".format(reading)) result = db.insert_or_update_device(node.address, node.node_id, node.device_type, reading['pf_batt']) db.close_session() if result == False: self.logger.error("Failed to save power reading") ReadThread.cache_reading(self, reading) return
def reload_network_info(self): db = DryadDatabase() # Get all nodes node_records = db.get_nodes() device_records = db.get_devices() if (node_records == False) or (device_records == False): db.close_session() return # Reload the running node list self.node_list = [] for device in device_records: # Get the matching node in the node records list node_name = device.node_id node_addr = device.address node_type = device.device_type # This will contain an Enum node_class = "UNKNOWN" # This will contain an Enum for node in node_records: if node.name == device.node_id: node_class = node.node_class # Add the node to the list self.node_list.append({ "id": node_name, "addr": node_addr, "type": node_type.name, "class": node_class.name }) self.logger.debug(str(self.node_list)) db.close_session() return True
def handle_req_list_sensors(self, link, content): db = DryadDatabase() node_matches = db.get_nodes(node_class='SENSOR') sensors = "{'sensors':[" if len(node_matches) <= 0: sensors += "]}" db.close_session() return link.send_response("RNLST:" + sensors + ";\r\n") snode_list = [] for node in node_matches: pf_addr = "????" bl_addr = "????" pf_batt = -99.0 bl_batt = -99.0 # Get the matching devices sharing the node's name device_matches = db.get_devices(name=node.name) if device_matches == None: self.logger.warn( "Node does not have any associated devices: {}".format( node.name)) continue if len(device_matches) <= 0: self.logger.warn( "Node does not have any associated devices: {}".format( node.name)) continue # For each matching device, extract the parrot fp address and the # bluno address and then store them in separate variables for device in device_matches: device_type = str(device.device_type.name) if device_type == 'BLUNO': bl_addr = device.address elif device_type == "PARROT": pf_addr = device.address pf_batt = device.power snode = "'name':'{}', 'state':'{}'," snode += "'site_name':'{}','lat':'{}', 'lon':'{}'," snode += "'pf_addr':'{}', 'bl_addr':'{}', 'pf_batt':'{}'," snode += "'bl_batt':'{}', 'pf_comms':'{}', 'bl_comms':'{}'" snode = snode.format(node.name, self.task_node.get_state_str(), node.site_name, node.lat, node.lon, pf_addr, bl_addr, pf_batt, -99.0, ctime(0.0), ctime(0.0)) snode_list.append("{" + snode + "}") # Build the sensor node list string snodes_all = ",".join(snode_list) # Build the final string sensors += snodes_all sensors += "]}" # print(sensors) db.close_session() return link.send_response("RNLST:" + sensors + ";\r\n")