コード例 #1
0
    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
コード例 #2
0
    def handle_req_state(self, link, content):
        # Retrive details about the cache node from the database
        db = DryadDatabase()
        node_matches = db.get_nodes(node_class='SELF')
        data = db.get_data()

        if len(node_matches) <= 0:
            db.close_session()
            self.logger.error("Failed to load data for 'SELF'")

            return link.send_response("RSTAT:FAIL\r\n")

        db.close_session()

        # Retrive uptime
        self_uptime = os.popen(SYS_CMD_UPTIME).read().strip()

        node_data = node_matches[0]

        # Format the string to return
        state = "'name':'{}','state':'{}','batt':{},'version':'{}',"
        state += "'lat':{},'lon':{},'sys_time':'{}','uptime':'{}',"
        state += "'next_sleep_time':'{}','next_collect_time':'{}',"
        state += "'size':{}"
        state = state.format(node_data.name, self.task_node.get_state_str(),
                             -99.0, self.version, node_data.lat, node_data.lon,
                             ctime(), self_uptime,
                             ctime(self.task_node.get_idle_out_time()),
                             ctime(self.task_node.get_collect_time()),
                             len(data))

        return link.send_response("RSTAT:{" + state + "};\r\n")
コード例 #3
0
def load_sensor_nodes():
    ddb = DryadDatabase()
    if ddb.connect(CUSTOM_DATABASE_NAME) == False:
        logger.error("Load Sensor Node Info Failed")
        return False

    node_list = ddb.get_nodes('C_TYPE = "UNKNOWN" OR C_TYPE = "SENSOR"')

    ddb.disconnect()
    
    return node_list
コード例 #4
0
    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")
コード例 #5
0
    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
コード例 #6
0
    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")