def message(db_id, coll_id, data_id): # Connect to the database MongoDB try: connection = Connection(hostname, port_number) except: return json.dumps("Unable to connect to the database!") db = connection[db_id] collection = db[coll_id] try: message_collection = collection.find_one({'data_id': data_id}) except: return json.dumps("Unable to read data from the collection!") if message_collection is None: return json.dumps("No data with this ID in the collection!") message_collection['_id'] = str(message_collection['_id']) if request.data == 'protobuf': try: pb_message = protobuf_json.json2pb( raw_data_pb2.RawRFReadingCollection(), message_collection) except: return json.dumps("Unable to read message from the collection!") pb_message_string = pb_message.SerializeToString() return pb_message_string else: return json.dumps(message_collection)
def store_message(db_id, coll_id): try: raw_data_collection = raw_data_pb2.RawRFReadingCollection() raw_data_collection.ParseFromString(request.data) except: return json.dumps('Message is not well formated!') # Connect to the database MongoDB try: connection = Connection(hostname, port_number) except: return json.dumps("Unable to connect to the database!") db_names = connection.database_names() if db_id in db_names: db = connection[db_id] else: return json.dumps("No such database!") coll_names = db.collection_names() if coll_id in coll_names: collection = db[coll_id] else: return json.dumps("No such collection in the database!") try: collection.insert(protobuf_json.pb2json(raw_data_collection)) except: return json.dumps("Unable to store data into the database!") return json.dumps('Data stored!')
def replace_message(db_id, coll_id, data_id): raw_data_collection = raw_data_pb2.RawRFReadingCollection() raw_metadata = raw_metadata_pb2.Metadata() # Connect to the database MongoDB try: connection = Connection(hostname, port_number) except: return json.dumps("Unable to connect to the database!") try: raw_data_collection.ParseFromString(request.data) except: return json.dumps('Message is not well defined!') db_names = connection.database_names() if db_id not in db_names: return json.dumps("Database doesn't exist!") db = connection[db_id] coll_names = db.collection_names() if coll_id not in coll_names: return json.dumps("Collection doesn't exist!") collection = db[coll_id] try: message_collection = collection.find_one({'data_id': data_id}) except: return json.dumps("Unable to read data from the collection!") if message_collection is None: return json.dumps("No data with this ID in the collection!") message_collection['_id'] = str(message_collection['_id']) message_backup = message_collection try: collection.remove({'data_id': data_id}) except: collection.insert(message_backup) return json.dumps("Unable to read data from the database!") try: collection.insert(protobuf_json.pb2json(raw_data_collection)) except: collection.insert(message_backup) return json.dumps("Unable to store data into the collection!") return json.dumps('Message successfully replaced!')
import raw_data_pb2 # The URL where server listens apiURL = 'http://localhost:5000/' # The ID of the database db_id = 'test_db' # The ID of the collection in the database coll_id = 'test_coll' NUMBER_OF_SCANS = 1 if __name__ == '__main__': raw_data_collection = raw_data_pb2.RawRFReadingCollection() raw_data_collection.metadata_id = "1" raw_data_collection.data_id = "1" raw_data_collection.meas_number = NUMBER_OF_SCANS for scans in range(1, NUMBER_OF_SCANS + 1): fpf = wifiFingerprint() fpf.scan(1) fp = fpf.getFingerprint() for key in fp.keys(): raw_data_reading = raw_data_collection.raw_measurement.add() x = datetime.utcnow() raw_data_reading.timestamp_utc = timestamp_utc = int(time.mktime(x.timetuple())) raw_data_reading.receiver_id = 'test' raw_data_reading.receiver_location.coordinate_x = 1 raw_data_reading.receiver_location.coordinate_y = 1
def store_virtual_fingerprints(db_id_original, coll_id_original, db_id_enriched, coll_id_enriched, points, virtual_fingerprints): """Store original and virtual training fingerprint in the same database""" # Connect to the database MongoDB try: connection = Connection(hostname, port_number) except: return json.dumps("Unable to connect to the database!") db_names = connection.database_names() if db_id_original in db_names: db = connection[db_id_original] else: return json.dumps("No such database!") coll_names = db.collection_names() if coll_id_original in coll_names: collection = db[coll_id_original] else: return json.dumps("No such collection in the database!") try: message_collection = collection.find({}) except: return json.dumps("Unable to read data from the collection!") message_collection_list = {} message_collection_list_full = list(message_collection) if db_id_enriched in db_names: db = connection[db_id_enriched] else: return json.dumps("No such database!") coll_names = db.collection_names() if coll_id_enriched in coll_names: collection = db[coll_id_enriched] else: return json.dumps("No such collection in the database!") for i in range(0, len(message_collection_list_full)): try: del message_collection_list_full[i]['_id'] collection.insert(message_collection_list_full[i]) except: return json.dumps("Unable to store data into the database!") data_id_virtual = len(message_collection_list_full) + 1 iteration = 0 for point in points: raw_data_collection = raw_data_pb2.RawRFReadingCollection() raw_data_collection.data_id = str(data_id_virtual) data_id_virtual += 1 raw_data_collection.meas_number = message_collection_list_full[0][ 'meas_number'] for key in virtual_fingerprints[iteration].keys(): for num in range(0, len(virtual_fingerprints[iteration][key])): raw_data_reading = raw_data_collection.raw_measurement.add() x = datetime.utcnow() raw_data_reading.timestamp_utc = timestamp_utc = int( time.mktime(x.timetuple())) raw_data_reading.receiver_location.coordinate_x = point[0] raw_data_reading.receiver_location.coordinate_y = point[1] raw_data_reading.run_nr = num + 1 raw_data_reading.sender_bssid = key raw_data_reading.rssi = float( virtual_fingerprints[iteration][key][num]) iteration += 1 try: collection.insert(protobuf_json.pb2json(raw_data_collection)) except: collection.insert(message_backup) return json.dumps("Unable to store data into the collection!") return json.dumps('Data stored!')