def init_network_records(self): db = DryadDatabase() # Check if SELF record already exists records = db.get_nodes(node_class="SELF") if (records != False) and (len(records) > 0): db.close_session() return False # If it does not, then create a node record for it self_name = os.popen(SYS_CMD_BNAME).read().split(' ')[0].strip() result = db.insert_or_update_node(name=self_name, node_class="SELF", site_name="????", lat=14.37, lon=120.58) if result == False: self.logger.error("Unable to create own node record") db.close_session() return False # Create a node device record for it as well self_address = os.popen(SYS_CMD_ADDR).read().strip() result = db.insert_or_update_device(address=self_address, node_id=self_name, device_type="RPI") if result == False: self.logger.error("Unable to create own device record") db.close_session() return False db.close_session() return True
def classify_node(self, node): db = DryadDatabase() self.logger.info("Discovering node classification...") try: node['type'], node['class'] = ble_utils.discover_node_category(node['addr'], node['id']) except Exception as e: self.logger.error("Failed to discover node classification: {}".format(e)) db.close_session() return False # Update the database node information result = db.insert_or_update_node( name = node['id'], node_class = node['class'] ) if result == False: self.logger.error("Unable to update node record") db.close_session() return False # Update the node device record in the database result = db.insert_or_update_device( address = node['addr'], node_id = node['id'], device_type = node['type'] ) if result == False: self.logger.error("Unable to update node device record") db.close_session() return False db.close_session() return True
def handle_req_update_cache(self, link, content): params = {"name": None, "lat": None, "lon": None, "site_name": None} # remove trailing ";" content = content.strip(';') update_args = content.split(',') if len(update_args) > 0: for arg in update_args: if "=" in arg: param = arg.split("=")[0] val = arg.split("=")[1] if param in params.keys(): if param == "lat" or param == "lon": val = float(val) params[param] = val else: params[param] = val.strip("'").strip('"') db = DryadDatabase() node_matches = db.get_nodes(node_class='SELF') if len(node_matches) <= 0: self.logger.error("Failed to load data for 'SELF'") db.close_session() return link.send_response("RCUPD:FAIL;\r\n") # Extract only the relevant node record node_data = node_matches[0] # Update cache node details in the DB result = db.insert_or_update_node(name=node_data.name, node_class=node_data.node_class, site_name=params['site_name'], lat=params['lat'], lon=params['lon']) db.close_session() if result == False: self.logger.error("Failed to update cache node details") link.send_response("RCUPD:FAIL;\r\n") return False return link.send_response("RCUPD:OK;\r\n")
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 handle_req_update_sensor(self, link, content): params = { "name": None, "site_name": None, "state": None, "lat": None, "lon": None, } # remove trailing ";" if ";" in content: content = content[:-1] update_args = content.split(',') if len(update_args) > 0: for arg in update_args: if "=" in arg: param = arg.split("=")[0] val = arg.split("=")[1] if param in params.keys(): if param == "lat" or param == "lon": val = float(val) params[param] = val else: params[param] = val.strip("'").strip('"') db = DryadDatabase() dt = DataTransformation() result = db.insert_or_update_node(name=params['name'], node_class=CLASS_SENSOR, site_name=params['site_name'], lat=params['lat'], lon=params['lon']) if result == False: self.logger.error("Failed to add node") link.send_response("RSUPD:FAIL;\r\n") db.close_session() return False db.close_session() return link.send_response("RSUPD:OK;\r\n")
def handle_req_setup_sensor(self, link, content): params = { "name": None, "site_name": None, "pf_addr": None, "bl_addr": None, "state": None, "lat": None, "lon": None, "updated": None, } # remove trailing ";" if ";" in content: content = content[:-1] update_args = content.split(',') # TODO WTF is this magickery??? if len(update_args) > 0: for arg in update_args: if "=" in arg: param = arg.split("=")[0] val = arg.split("=")[1] if param in params.keys(): if param == "lat" or param == "lon": val = float(val) params[param] = val else: params[param] = val.strip("'").strip('"') db = DryadDatabase() dt = DataTransformation() bl_addr = dt.conv_mac(params["bl_addr"].upper()) pf_addr = dt.conv_mac(params["pf_addr"].upper()) result = db.insert_or_update_node(name=params['name'], node_class=CLASS_SENSOR, site_name=params['site_name'], lat=params['lat'], lon=params['lon']) if result == False: self.logger.error("Failed to add node") link.send_response("RQRSN:FAIL;\r\n") db.close_session() return False result = db.insert_or_update_device(address=bl_addr, node_id=params['name'], device_type=TYPE_BLUNO) if result == False: self.logger.error("Failed to add node device") link.send_response("RQRSN:FAIL;\r\n") db.close_session() return False result = db.insert_or_update_device(address=pf_addr, node_id=params['name'], device_type=TYPE_PARROT) if result == False: self.logger.error("Failed to add node device") link.send_response("RQRSN:FAIL;\r\n") db.close_session() return False return link.send_response("RQRSN:OK;\r\n")