Exemple #1
0
    def sendDbHistoryInformation(self):
        rate = rospy.Rate(100)  # 100hz

        while not rospy.is_shutdown():
            temp_dbdata = Tmsdb()
            object_information = TmsdbStamped()

            d = datetime.now() - timedelta(days=3, hours=11)
            t1 = 'ISODate(' + d.isoformat() + ')'
            t2 = 'ISODate(' + (
                d + timedelta(days=0, hours=1, minutes=0)).isoformat() + ')'
            print(t1)
            # print(db.history.ensure_index({'id':1,'time':1}))
            # cursor = db.history.find({'id':{'$gte':1006, '$lte':1018},
            # 'time':{'$gte':t1,'$lt':t2}, 'state':1}).sort('time')
            cursor = db.history.find({
                'id': {
                    '$gte': 1006,
                    '$lte': 1018
                },
                'time': {
                    '$gte': t1,
                    '$lt': t2
                },
                'state': 1
            })
            for doc in cursor:
                # if not rospy.is_shutdown():
                #    print(doc)
                del doc['_id']
                temp_dbdata = db_util.document_to_msg(doc, Tmsdb)
                object_information.tmsdb.append(temp_dbdata)
            self.data_pub.publish(object_information)

            rate.sleep()
    def sendDbCurrentInformation(self):
        rate = rospy.Rate(100) # 100hz

        while not rospy.is_shutdown():
            temp_dbdata = Tmsdb()
            current_environment_information = TmsdbStamped()

            cursor = db.now.find({'state':1})
            # print(cursor.count())
            for doc in cursor:
                del doc['_id']
                temp_dbdata = db_util.document_to_msg(doc, Tmsdb)
                current_environment_information.tmsdb.append(temp_dbdata)
            # rospy.loginfo("send db data!")
            self.data_pub.publish(current_environment_information)

            rate.sleep()
Exemple #3
0
            temp_name  = cursor[0]['name'];
            temp_place = cursor[0]['place'];

        # Search only using the place tag
        if mode == MODE_PLACE :
            ret = TmsdbGetDataResponse()
            cursor = db.now.find({'place':req.tmsdb.place})
            if cursor.count() == 0:
                temp_dbdata.note = "Wrong request! Try to check the place tag!";
>>>>>>> 51ecc3540900cfe208d8c2ca1ecaf2184d407ca7
                ret.tmsdb.append(temp_dbdata)
                return ret
            else:
                for doc in cursor:
                    del doc['_id']
                    temp_dbdata = db_util.document_to_msg(doc, Tmsdb)
                    ret.tmsdb.append(temp_dbdata)
                return ret

        if mode == MODE_HIERARCHY:
<<<<<<< HEAD
            loop_end_tag = False
            temp_place = req.tmsdb.id

            while temp_place != 0:
                cursor = db.now.find({'id': temp_place}).sort({'time': -1})
                if cursor.count() == 0:
                    temp_dbdata.note = "Wrong request! Try to check the target ID, place info!"
=======
            loop_end_tag = False;
            temp_place = req.tmsdb.id;
Exemple #4
0
    def dbReaderSrvCallback(self, request, response):
        response_tmsdb = Tmsdb()

        mode = 0
        temp_id = 0
        temp_type = ""
        temp_name = ""
        temp_place = 0
        sid = 100000

        MODE_ALL = 0
        MODE_NAME_IDTABLE = 1
        MODE_NAME = 2
        MODE_NAME_SENSOR = 3
        MODE_ID_IDTABLE = 4
        MODE_ID = 5
        MODE_ID_SENSOR = 6
        MODE_TYPE_IDTABLE = 7
        MODE_TYPE = 8
        MODE_TYPE_SENSOR = 9
        MODE_PLACE_IDTABLE = 10
        MODE_PLACE = 11
        MODE_PLACE_TYPE = 12
        MODE_HIERARCHY = 13
        MODE_TAG_IDTABLE = 14
        MODE_ID_STATE = 15
        MODE_ID_SENSOR_STATE = 16
        MODE_ERROR = 999

        if request.tmsdb.tag != '':
            mode = MODE_TAG_IDTABLE
        elif (request.tmsdb.id == 0) and (request.tmsdb.type == '') and (
                request.tmsdb.sensor == 0) and (request.tmsdb.place
                                                == 0) and (request.tmsdb.name
                                                           == ''):
            mode = MODE_ALL
        elif (request.tmsdb.id == sid) and (request.tmsdb.name != ''):
            mode = MODE_NAME_IDTABLE
        elif (request.tmsdb.id == 0) and (request.tmsdb.name != '') and (
                request.tmsdb.id != sid) and (request.tmsdb.type == '') and (
                    request.tmsdb.sensor == 0) and (request.tmsdb.place == 0):
            mode = MODE_NAME
        elif (request.tmsdb.id == 0) and (request.tmsdb.name != '') and (
                request.tmsdb.id != sid) and (request.tmsdb.type == '') and (
                    request.tmsdb.sensor != 0) and (request.tmsdb.place == 0):
            mode = MODE_NAME_SENSOR
        elif (request.tmsdb.id > sid) and (request.tmsdb.type == ''):
            request.tmsdb.id -= sid
            mode = MODE_ID_IDTABLE
        elif (request.tmsdb.id != 0) and (request.tmsdb.id != sid) and (
                request.tmsdb.type == '') and (request.tmsdb.sensor
                                               == 0) and (request.tmsdb.place
                                                          == 0):
            if (request.tmsdb.state == 1):
                mode = MODE_ID_STATE
            else:
                mode = MODE_ID
        elif (request.tmsdb.id != 0) and (request.tmsdb.id != sid) and (
                request.tmsdb.type
                == '') and (request.tmsdb.sensor != 0) and (request.tmsdb.place
                                                            == 0):
            if (request.tmsdb.state == 1):
                mode = MODE_ID_SENSOR_STATE
            else:
                mode = MODE_ID_SENSOR
        elif (request.tmsdb.id == sid) and (request.tmsdb.type != ''):
            mode = MODE_TYPE_IDTABLE
        elif (request.tmsdb.id == 0) and (request.tmsdb.type != '') and (
                request.tmsdb.sensor == 0) and (request.tmsdb.place == 0):
            mode = MODE_TYPE
        elif (request.tmsdb.id == 0) and (request.tmsdb.type != '') and (
                request.tmsdb.sensor != 0) and (request.tmsdb.place == 0):
            mode = MODE_TYPE_SENSOR
        elif (request.tmsdb.id
              == sid) and (request.tmsdb.type
                           == '') and (request.tmsdb.place != 0):
            mode = MODE_PLACE_IDTABLE
        elif (request.tmsdb.id
              == 0) and (request.tmsdb.type
                         == '') and (request.tmsdb.place != 0):
            mode = MODE_PLACE
        elif (request.tmsdb.id == 0) and (request.tmsdb.type !=
                                          '') and (request.tmsdb.place != 0):
            mode = MODE_PLACE_TYPE
        elif (request.tmsdb.id != 0) and (request.tmsdb.type
                                          == '') and (request.tmsdb.place
                                                      == sid):
            mode = MODE_HIERARCHY
        elif (request.tmsdb.id > 0) and ((request.tmsdb.id < 1000) or
                                         (request.tmsdb.id > 20002)):
            mode = MODE_ERROR
        else:
            mode = MODE_ERROR

        # Wrong request
        if mode == MODE_ERROR:
            response_tmsdb.note = "Wrong request! Try to check the command!"
            response.tmsdb.append(response_tmsdb)
            return response

        #  Search the ID, type, etc infomation in ID table
        if (mode == MODE_NAME) or (mode == MODE_NAME_SENSOR):
            cursor = db.default.find({'name': request.tmsdb.name})
            temp_type = cursor[0]['type']
            temp_id = cursor[0]['id']
            temp_place = cursor[0]['place']

        # Search the type, name, etc infomation in ID table
        if (mode == MODE_ID) or (mode == MODE_ID_SENSOR) or (
                mode == MODE_ID_STATE) or (mode == MODE_ID_SENSOR_STATE) or (
                    mode == MODE_HIERARCHY):
            cursor = db.default.find({'id': request.tmsdb.id})
            temp_type = cursor[0]['type']
            temp_name = cursor[0]['name']
            temp_place = cursor[0]['place']

        # Search only using the place tag
        if mode == MODE_PLACE:
            cursor = db.now.find({'place': request.tmsdb.place})
            if cursor.count() == 0:
                response_tmsdb.note = "Wrong request! Try to check the place tag!"
                response.tmsdb.append(response_tmsdb)
                return response
            else:
                for doc in cursor:
                    del doc['_id']
                    response_tmsdb = db_util.document_to_msg(doc, Tmsdb)
                    response.tmsdb.append(response_tmsdb)
                return response

        if mode == MODE_HIERARCHY:
            loop_end_tag = False
            temp_place = request.tmsdb.id

            while temp_place != 0:
                cursor = db.now.find({'id': temp_place}).sort({'time': -1})
                if cursor.count() == 0:
                    response_tmsdb.note = "Wrong request! Try to check the target ID, place info!"
                    response.tmsdb.append(response_tmsdb)
                    return response
                else:
                    for doc in cursor:
                        del doc['_id']
                        response_tmsdb = db_util.document_to_msg(doc, Tmsdb)
                        response.tmsdb.append(response_tmsdb)

                if loop_end_tag:
                    break

                temp_id = response_tmsdb.place  # ?
                cursor = db.default.find({'id': temp_id})
                temp_type = cursor[0]['type']
                temp_name = cursor[0]['name']
                temp_place = cursor[0]['place']

                if temp_id == temp_place:
                    loop_end_tag = True
                else:
                    loop_end_tag = False

                temp_place = temp_id

            return response

        if mode == MODE_ALL:
            cursor = db.default.find()
        elif mode == MODE_TAG_IDTABLE:
            cursor = db.default.find({'tag': {'$regex': request.tmsdb.tag}})
        elif mode == MODE_NAME_IDTABLE:
            cursor = db.default.find({'name': request.tmsdb.name})
        elif mode == MODE_NAME:
            cursor = db.now.find({'name': request.tmsdb.name})
        elif mode == MODE_NAME_SENSOR:
            cursor = db.now.find({
                'name': request.tmsdb.name,
                'sensor': request.tmsdb.sensor
            })
        elif mode == MODE_ID_IDTABLE:
            cursor = db.default.find({'id': request.tmsdb.id})
        elif mode == MODE_ID:
            cursor = db.now.find({'id': request.tmsdb.id})
        elif mode == MODE_ID_STATE:
            cursor = db.now.find({'id': request.tmsdb.id, 'state': 1})
        elif mode == MODE_ID_SENSOR:
            cursor = db.now.find({
                'id': request.tmsdb.id,
                'sensor': request.tmsdb.sensor
            })
        elif mode == MODE_ID_SENSOR_STATE:
            cursor = db.now.find({
                'id': request.tmsdb.id,
                'sensor': request.tmsdb.sensor,
                'state': 1
            })
        elif mode == MODE_TYPE_IDTABLE:
            cursor = db.default.find({'type': request.tmsdb.type})
        elif mode == MODE_TYPE:
            cursor = db.now.find({'type': request.tmsdb.type})
        elif mode == MODE_TYPE_SENSOR:
            cursor = db.now.find({'sensor': request.tmsdb.sensor})
        elif mode == MODE_PLACE_IDTABLE:
            cursor = db.default.find({'place': request.tmsdb.place})
        elif mode == MODE_PLACE_TYPE:
            cursor = db.now.find({'place': request.tmsdb.place})

        if cursor.count() == 0:
            response_tmsdb.note = "Wrong request! Try to check the command"
            response.tmsdb.append(response_tmsdb)
            return response
        else:
            for doc in cursor:
                del doc['_id']
                response_tmsdb = db_util.document_to_msg(doc, Tmsdb)
                response.tmsdb.append(response_tmsdb)
            return response
Exemple #5
0
    def dbReaderSrvCallback(self, req):
        rospy.loginfo("Received the service call!")
        rospy.loginfo(req)
        temp_dbdata = Tmsdb()
        TmsdbGetData()
        result = False

        mode = 0;
        temp_id = 0;
        temp_type = "";
        temp_name = "";
        temp_place = 0;
        sid = 100000

        MODE_ALL           =  0
        MODE_NAME_IDTABLE  =  1
        MODE_NAME          =  2
        MODE_NAME_SENSOR   =  3
        MODE_ID_IDTABLE    =  4
        MODE_ID            =  5
        MODE_ID_SENSOR     =  6
        MODE_TYPE_IDTABLE  =  7
        MODE_TYPE          =  8
        MODE_TYPE_SENSOR   =  9
        MODE_PLACE_IDTABLE = 10
        MODE_PLACE         = 11
        MODE_PLACE_TYPE    = 12
        MODE_HIERARCHY     = 13
        MODE_TAG_IDTABLE   = 14
        MODE_ERROR         = 999

        if req.tmsdb.tag != '':
            mode = MODE_TAG_IDTABLE
        elif (req.tmsdb.id == 0) and (req.tmsdb.type == '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place == 0) and (req.tmsdb.name == ''):
            mode = MODE_ALL
        elif (req.tmsdb.id == sid) and (req.tmsdb.name != ''):
            mode = MODE_NAME_IDTABLE
        elif (req.tmsdb.id == 0) and (req.tmsdb.name != '') and (req.tmsdb.id != sid) and (req.tmsdb.type == '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place == 0):
            mode = MODE_NAME
        elif (req.tmsdb.id == 0) and (req.tmsdb.name != '') and (req.tmsdb.id != sid) and (req.tmsdb.type == '') and (req.tmsdb.sensor != 0) and (req.tmsdb.place == 0):
            mode = MODE_NAME_SENSOR
        elif (req.tmsdb.id > sid) and (req.tmsdb.type == ''):
            req.tmsdb.id -= sid
            mode = MODE_ID_IDTABLE
        elif (req.tmsdb.id != 0) and (req.tmsdb.id != sid) and (req.tmsdb.type == '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place == 0):
            mode = MODE_ID
        elif (req.tmsdb.id != 0) and (req.tmsdb.id != sid) and (req.tmsdb.type == '') and (req.tmsdb.sensor != 0) and (req.tmsdb.place == 0):
            mode = MODE_ID_SENSOR
        elif (req.tmsdb.id == sid) and (req.tmsdb.type != ''):
            mode = MODE_TYPE_IDTABLE
        elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and (req.tmsdb.sensor == 0) and (req.tmsdb.place ==0):
            mode = MODE_TYPE
        elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and (req.tmsdb.sensor != 0) and (req.tmsdb.place ==0):
            mode = MODE_TYPE_SENSOR
        elif (req.tmsdb.id == sid) and (req.tmsdb.type == '') and (req.tmsdb.place !=0):
            mode = MODE_PLACE_IDTABLE
        elif (req.tmsdb.id == 0) and (req.tmsdb.type == '') and (req.tmsdb.place !=0):
          mode = MODE_PLACE
        elif (req.tmsdb.id == 0) and (req.tmsdb.type != '') and (req.tmsdb.place !=0):
            mode = MODE_PLACE_TYPE
        elif (req.tmsdb.id != 0) and (req.tmsdb.type == '') and (req.tmsdb.place == sid):
            mode = MODE_HIERARCHY
        elif (req.tmsdb.id > 0) and ((req.tmsdb.id < 1000) or (req.tmsdb.id > 20002)):
            mode = MODE_ERROR
        else:
          mode = MODE_ERROR

        if mode == MODE_ERROR:
            temp_dbdata.note = "Wrong request! Try to check the command!";
            ret = TmsdbGetDataResponse()
            ret.tmsdb.append(temp_dbdata)
            return ret

        #  Search the ID, type, etc infomation in ID table
        if (mode == MODE_NAME) or (mode == MODE_NAME_SENSOR):
            cursor = db.default.find({'name':req.tmsdb.name})
            temp_type  = cursor[0]['type'];
            temp_id    = cursor[0]['id'];
            temp_place = cursor[0]['place'];

        # Search the type, name, etc infomation in ID table
        if(mode == MODE_ID) or (mode == MODE_ID_SENSOR) or (mode == MODE_HIERARCHY):
            cursor = db.default.find({'id':req.tmsdb.id})
            temp_type  = cursor[0]['type'];
            temp_name  = cursor[0]['name'];
            temp_place = cursor[0]['place'];

        # Search only using the place tag
        if mode == MODE_PLACE :
            ret = TmsdbGetDataResponse()
            cursor = db.now.find({'place':req.tmsdb.place})
            if cursor.count() == 0:
                temp_dbdata.note = "Wrong request! Try to check the place tag!";
                ret.tmsdb.append(temp_dbdata)
                return ret
            else:
                for doc in cursor:
                    del doc['_id']
                    temp_dbdata = db_util.document_to_msg(doc, Tmsdb)
                    ret.tmsdb.append(temp_dbdata)
                return ret

        if mode == MODE_HIERARCHY:
            loop_end_tag = False;
            temp_place = req.tmsdb.id;

            while temp_place != 0:
                cursor = db.now.find({'id':temp_place}).sort({'time':-1})
                if cursor.count() == 0:
                    temp_dbdata.note = "Wrong request! Try to check the target ID, place info!";
                    ret.tmsdb.append(temp_dbdata)
                    return ret
                else:
                    for doc in cursor:
                        del doc['_id']
                        temp_dbdata = db_util.document_to_msg(doc, Tmsdb)
                        ret.tmsdb.append(temp_dbdata)

                if loop_end_tag:
                    break

                temp_id = temp_dbdata.place;
                cursor = db.default.find({'id':temp_id})
                temp_type  = cursor[0]['type'];
                temp_name  = cursor[0]['name'];
                temp_place = cursor[0]['place'];

                if temp_id == temp_place:
                    loop_end_tag = True;
                else:
                    loop_end_tag = False;

                temp_place = temp_id;

            return ret

        if mode == MODE_ALL:
            cursor = db.default.find()
        elif mode == MODE_TAG_IDTABLE:
            cursor = db.default.find({'tag':{'$regex': req.tmsdb.tag}})
        elif mode == MODE_NAME_IDTABLE:
            cursor = db.default.find({'name':req.tmsdb.name})
        elif mode == MODE_NAME:
            cursor = db.now.find({'name':req.tmsdb.name})
        elif mode == MODE_NAME_SENSOR:
            cursor = db.now.find({'name':req.tmsdb.name, 'sensor':req.tmsdb.sensor})
        elif mode == MODE_ID_IDTABLE:
            cursor = db.default.find({'id':req.tmsdb.id})
        elif mode == MODE_ID:
            cursor = db.now.find({'id':req.tmsdb.id})
        elif mode == MODE_ID_SENSOR:
            cursor = db.now.find({'id':req.tmsdb.id, 'sensor':req.tmsdb.sensor})
        elif mode == MODE_TYPE_IDTABLE:
            cursor = db.default.find({'type':req.tmsdb.type})
        elif mode == MODE_TYPE:
            cursor = db.now.find({'type':req.tmsdb.type})
        elif mode == MODE_TYPE_SENSOR:
            cursor = db.now.find({'sensor':req.tmsdb.sensor})
        elif mode == MODE_PLACE_IDTABLE:
            cursor = db.default.find({'place':req.tmsdb.place})
        elif mode == MODE_PLACE_TYPE:
            cursor = db.now.find({'place':req.tmsdb.place})

        ret = TmsdbGetDataResponse()
        if cursor.count() == 0:
            temp_dbdata.note = "Wrong request! Try to check the command";
            ret.tmsdb.append(temp_dbdata)
            return ret
        else:
            for doc in cursor:
                del doc['_id']
                temp_dbdata = db_util.document_to_msg(doc, Tmsdb)
                ret.tmsdb.append(temp_dbdata)
            return ret