Ejemplo n.º 1
0
def find_node_module_info(node_module_id: str) -> dict:
    db = databasehelper.get_database(DB_NAME)
    col = databasehelper.get_collection(db, DB_COLLECTION_NODE_MODULES)
    nodeinfo = list(databasehelper.find(col,
                                        {'_id': ObjectId(node_module_id)}))
    print(nodeinfo)
    return nodeinfo[0]
Ejemplo n.º 2
0
def find_datastore_values(node_id: str, limit: int) -> list:
    colname = DB_COLLECTION_TEMP_DATASTORE + str(node_id)
    db = databasehelper.get_database(DB_NAME)
    col = databasehelper.get_collection(db, colname)
    values = list(
        databasehelper.find(col, {}, {
            'sort': [('time', -1)],
            'limit': limit,
            'projection': {
                '_id': False
            }
        }))
    return values
Ejemplo n.º 3
0
def find_parent_node_module_info_list_w_name(parent_module_name: str) -> list:
    db = databasehelper.get_database(DB_NAME)
    col = databasehelper.get_collection(db, DB_COLLECTION_PARENT_NODE_MODULES)
    nodeinfo = list(
        databasehelper.find(col, {'module_name': parent_module_name}))
    return nodeinfo
Ejemplo n.º 4
0
def find_parent_node_info(node_id: str) -> dict:
    db = databasehelper.get_database(DB_NAME)
    col = databasehelper.get_collection(db, DB_COLLECTION_PARENT_NODES)
    nodeinfo = list(databasehelper.find(col, {'_id': ObjectId(node_id)}))
    return nodeinfo[0]
Ejemplo n.º 5
0
def list_node_modules() -> list:
    db = databasehelper.get_database(DB_NAME)
    col = databasehelper.get_collection(db, DB_COLLECTION_NODE_MODULES)
    listnodes = list(databasehelper.find(col, {}))
    return listnodes
Ejemplo n.º 6
0
    def __connectnodes(self) -> None:
        db = dbhelper.get_database(DB_NAME)
        times = {}
        while self.__isrunning:
            connectionscol = dbhelper.get_collection(db, DB_COLLECTION_RULES)
            connections = list(dbhelper.find(connectionscol, {}))

            for connection in connections:
                if connection['on'] == False:
                    continue

                event = connection['event']
                if 'type' not in event:
                    continue

                eventnodecol = dbhelper.get_collection(
                    db, DB_COLLECTION_TEMP_DATASTORE + str(event['nodeid']))
                eventnodeid = str(event['nodeid'])
                eventnodevalues = {}
                if str(connection['_id']) in times:
                    eventnodevalues = list(
                        dbhelper.find(
                            eventnodecol,
                            {'time': {
                                "$gt": times[str(connection['_id'])]
                            }}, dict(sort=[('time', pymongo.ASCENDING)])))
                else:
                    times[str(connection['_id'])] = time.time()
                    continue

                firstvalue = ''
                secondvalue = ''
                firsttime = 0.0
                secondtime = 0.0
                for eventnodevalue in eventnodevalues:
                    tempvalue = secondvalue
                    temptime = secondtime
                    secondvalue = str(eventnodevalue['value'])
                    secondtime = float(eventnodevalue['time'])
                    firstvalue = tempvalue
                    firsttime = temptime
                    if firstvalue == '' or firsttime == 0.0:
                        continue
                    eventoperator = str(event['operator'])
                    eventvalue = str(event['value'])
                    eventtype = str(event['type'])

                    #logmanager.log(TAG, "Event first time: " + eventnodeid + '  ' + str(eventnodevalue['time']))
                    times[str(connection['_id'])] = firsttime

                    firstrule = eventtype + "('" + firstvalue + "') " + eventoperator + " " + eventtype + "('" + eventvalue + "')"
                    secondrule = eventtype + "('" + secondvalue + "') " + eventoperator + " " + eventtype + "('" + eventvalue + "')"

                    # Rule check
                    if eval(secondrule) and not eval(firstrule):
                        logmanager.log(TAG, "Detected: " + str(connection))
                        action = connection['action']

                        if 'type' not in action:
                            break

                        node_id = action['nodeid']
                        nodemanager.write_node_value(
                            str(node_id),
                            eval(action['type'] + "('" + str(action['value']) +
                                 "')"))
                        break

            time.sleep(self.interval)
Ejemplo n.º 7
0
def list_rules() -> list:
    db = dbhelper.get_database(DB_NAME)
    col = dbhelper.get_collection(db, DB_COLLECTION_RULES)
    listrules = list(dbhelper.find(col, {}))
    return listrules
Ejemplo n.º 8
0
def find_rule(connector_id: str) -> dict:
    db = dbhelper.get_database(DB_NAME)
    col = dbhelper.get_collection(db, DB_COLLECTION_RULES)
    rule = dbhelper.find(col, {'_id': ObjectId(connector_id)})
    return rule[0]
Ejemplo n.º 9
0
def find_device_info(device_id: str) -> dict:
    db = dbhelper.get_database(DB_NAME)
    col = dbhelper.get_collection(db, DB_COLLECTION_DEVICES)
    listdevices = list(dbhelper.find(col, {'_id': ObjectId(device_id)}))
    return listdevices[0]
Ejemplo n.º 10
0
def list_devices() -> list:
    db = dbhelper.get_database(DB_NAME)
    col = dbhelper.get_collection(db, DB_COLLECTION_DEVICES)
    listdevices = list(dbhelper.find(col, {}))
    return listdevices