import os
import json
from pprint import pprint
# MangoDB database
import mongoengine as mdb
# NoSQL serverless database
from unqlite import UnQLite
# Sqlite database
import sqlite3 as lite
import global_vars as gbv
#SENSOR = "Sensor"
#SENSOR_CAT = ["Temperature", "Pressure", "Humidity"]
#ACTUATOR = "Actuator"
#ACTUATOR_CAT = ['Led']
PRIOT_COMPONENT_TYPE = ('Sensor', 'Actuator', 'Transciver', 'HMI', 'MCU')
PRIOT_SENSOR_CATEGORY = ('Temperature', 'Pressure', 'Humidity')
PRIOT_ACTUATOR_CATEGORY = ('LED', 'Relay')
PRIOT_INTERFACE = ('I2C', 'SPI', 'GPIO', 'UART')
class Hardware_Template(mdb.Document):
component_type = mdb.StringField(required=True, max_length=15, choices=PRIOT_COMPONENT_TYPE)
category = mdb.ListField(mdb.StringField(choices=PRIOT_SENSOR_CATEGORY), required=True)
interface = mdb.ListField(mdb.StringField(choices=PRIOT_INTERFACE), required=True)
manufacturer = mdb.StringField(max_length=20)
demo_board_url = mdb.URLField()
class Actuator_Template(mdb.Document):
component_type = mdb.StringField(required=True, max_length=15, choices=PRIOT_COMPONENT_TYPE)
category = mdb.ListField(mdb.StringField(choices=PRIOT_ACTUATOR_CATEGORY), required=True)
unique_id = mdb.StringField(required=True, max_length=15)
interface = mdb.ListField(mdb.StringField(choices=PRIOT_INTERFACE), required=True)
manufacturer = mdb.StringField(max_length=20)
demo_board_url = mdb.URLField()
class Sensor_Template(mdb.Document):
component_type = mdb.StringField(required=True, max_length=15, choices=PRIOT_COMPONENT_TYPE)
category = mdb.ListField(mdb.StringField(choices=PRIOT_SENSOR_CATEGORY), required=True)
unique_id = mdb.StringField(required=True, max_length=15)
interface = mdb.ListField(mdb.StringField(choices=PRIOT_INTERFACE), required=True)
manufacturer = mdb.StringField(max_length=20)
demo_board_url = mdb.URLField()
class Board_Template(mdb.Document):
name = mdb.StringField(required=True, max_length=15)
mcu_family = mdb.StringField(required=True, max_length=15)
mcu_name = mdb.StringField(required=True, max_length=15)
class MCU_Template(mdb.Document):
name = mdb.StringField(required=True, max_length=15)
family = mdb.StringField(required=True, max_length=15)
[docs]class PrIoT_DB_Mangodb():
"""
"""
def __init__(self, database_name='priot_mango_db', host='localhost', port=27017):
"""
"""
self.database_name = database_name
self.host = host
self.port = port
[docs] def create_mango_db(self):
"""
"""
mdb.connect( self.database_name, host=self.host, port=self.port )
[docs] def insert_sensor_mdb(self, json_filenames):
"""
"""
mdb.connect(self.database_name, host=self.host, port=self.port)
json_filenames_list = json_filenames.split()
for j_file in json_filenames_list:
j_obj = json.load(open(j_file))
post = Sensor_Template()
post.component_type = j_obj['component_type']
post.category = j_obj['category']
post.unique_id = j_obj['unique_id']
post.interface = j_obj['interface']
post.manufacturer = j_obj['manufacturer']
post.demo_board_url = j_obj['demo_board_url']
post.save()
[docs] def insert_actuator_mdb(self, json_filenames):
"""
"""
mdb.connect(self.database_name, host=self.host, port=self.port)
json_filenames_list = json_filenames.split()
for j_file in json_filenames_list:
j_obj = json.load(open(j_file))
post = Actuator_Template()
post.component_type = j_obj['component_type']
post.category = j_obj['category']
post.unique_id = j_obj['unique_id']
post.interface = j_obj['interface']
post.manufacturer = j_obj['manufacturer']
post.demo_board_url = j_obj['demo_board_url']
post.save()
[docs] def insert_board_mdb(self, json_filenames):
""" Function for inserting board specific information into PrIoT database
"""
mdb.connect(self.database_name, host=self.host, port=self.port)
json_filenames_list = json_filenames.split()
for j_file in json_filenames_list:
j_obj = json.load(open(j_file))
post = Board_Template()
post.name = j_obj['name']
post.mcu_family = j_obj['mcu_family']
post.mcu_name = j_obj['mcu_name']
post.save()
[docs] def insert_mcu_mdb(self, json_filenames):
"""
"""
mdb.connect(self.database_name, host=self.host, port=self.port)
json_filenames_list = json_filenames.split()
for j_file in json_filenames_list:
j_obj = json.load(open(j_file))
post = MCU_Template()
post.name = j_obj['name']
post.family = j_obj['family']
post.save()
"""
def print_mdb(self, collection_name):
mdb.connect(self.database_name, host=self.host, port=self.port)
if collection_name is "sensor":
for item in collection_name.objects:
print(item.unique_id)
"""
[docs]class Priot_DB_Sqlite:
"""
"""
def __init__(self, dbname='priot_sqlite_db.db'):
self.dbname = dbname
def create_sqlite_db(self):
con = lite.connect(self.dbname)
con.commit()
con.close()
def insert_sensor_sqlite_db(self, json_filename):
con = lite.connect(self.dbname)
con.row_factory = lite.Row
with con:
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Sensor( component_type TEXT(10), \
category TEXT(20), \
unique_id TEXT(15), \
interface TEXT(20), \
manufacturer TEXT(50), \
demo_board_url TEXT(100), \
UNIQUE (unique_id) )")
j_obj = json.load(open(json_filename))
data = ( j_obj['component_type'], " ".join(j_obj['category']), j_obj['unique_id'], " ".join(j_obj['interface']), \
j_obj['manufacturer'], j_obj['demo_board_url'] )
cur.execute( "INSERT INTO Sensor (component_type, category, unique_id, interface, manufacturer, demo_board_url) \
VALUES (?, ?, ?, ?, ?, ?)", data )
con.commit()
con.close()
def insert_actuator_sqlite_db(self, json_filename):
con = lite.connect(self.dbname)
con.row_factory = lite.Row
with con:
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Actuator( component_type TEXT(10), \
category TEXT(20), \
unique_id TEXT(15), \
interface TEXT(20), \
manufacturer TEXT(30), \
demo_board_url TEXT(100), \
UNIQUE (unique_id) )")
j_obj = json.load(open(json_filename))
data = ( j_obj['component_type'], " ".join(j_obj['category']), j_obj['unique_id'], " ".join(j_obj['interface']), \
j_obj['manufacturer'], j_obj['demo_board_url'] )
cur.execute( "INSERT INTO Actuator (component_type, category, unique_id, interface, manufacturer, demo_board_url) \
VALUES (?, ?, ?, ?, ?, ?)", data )
con.commit()
con.close()
def insert_board_sqlite_db(self, json_filename):
con = lite.connect(self.dbname)
con.row_factory = lite.Row
with con:
cur = con.cursor()
cur.execute( "CREATE TABLE IF NOT EXISTS Board( name TEXT(15), \
mcu_family TEXT(15), \
mcu_name TEXT(10) )" )
j_obj = json.load(open(json_filename))
data = ( j_obj['name'], j_obj['mcu_family'], j_obj['mcu_name'])
cur.execute( "INSERT INTO Board (name, mcu_family, mcu_name) VALUES (?, ?, ?)", data )
con.commit()
con.close()
def insert_mcu_sqlite_db(self, json_filename):
con = lite.connect(self.dbname)
con.row_factory = lite.Row
with con:
cur = con.cursor()
cur.execute( "CREATE TABLE IF NOT EXISTS MCU( name TEXT(15), \
family TEXT(15) )" )
j_obj = json.load(open(json_filename))
data = ( j_obj['name'], j_obj['family'])
cur.execute( "INSERT INTO MCU (name, family) VALUES (?, ?)", data )
con.commit()
con.close()
[docs]class Priot_DB_Unqlite:
"""
"""
def __init__(self, dbname='priot_unqlite_db.udb'):
self.dbname = dbname
self.PRIOT_COMPONENT_TYPE = ('Sensor', 'Actuator', 'Transciver', 'HMI', 'MCU')
self.PRIOT_SENSOR_CATEGORY = ('Temperature', 'Pressure')
self.PRIOT_ACTUATOR_CATEGORY = ('LED', 'Push_Button')
self.PRIOT_COMPONENT_INTERFACE = ('I2C', 'SPI', 'GPIO', 'UART')
def create_unqlite_db(self):
db = UnQLite(self.dbname)
sensor = db.collection(self.SENSOR)
actuator = db.collection(self.ACTUATOR)
sensor.create()
actuator.create()
def insert_sensor_unqlite_db(self, json_filename):
db = UnQLite(self.dbname)
sensor = db.collection(self.SENSOR)
sensor.create()
json_obj = json.load(open(json_filename))
if sensor.filter( lambda sensor: sensor["unique_id"] == json_obj["unique_id"] ) is not []:
sensor.store(json_obj)
else:
print("Database for this sensor already exist, use update_sensor_unqlite_db() to update")
return false
def insert_actuator_unqlite_db(self, json_filename):
db = UnQLite(self.dbname)
actuator = db.collection(self.ACTUATOR)
actuator.create()
json_obj = json.load(open(json_filename))
if actuator.filter( lambda actuator: actuator["unique_id"] == json_obj["unique_id"] ) is not []:
actuator.store(json_obj)
return true
else:
print("Database for this actuator already exist, use update_actuator_unqlite_db() to update")
return false
def delete_unqlite_db(self):
db = UnQLite(self.dbname)
sensor = db.collection(self.SENSOR)
actuator = db.collection(self.ACTUATOR)
sensor.create()
actuator.create()
sensor.drop()
actuator.drop()
def print_unqlite_db(self):
db = UnQLite(self.dbname)
sensor = db.collection(self.SENSOR)
actuator = db.collection(self.ACTUATOR)
sensor.create()
actuator.create()
print(sensor.all())
print(actuator.all())
if __name__ == "__main__":
# Test MangoDB
priot_mdb = PrIoT_DB_Mangodb(database_name='priot_mango_db')
#priot_mdb.print_mdb()
#priot_mdb.insert_mcu_mdb(json_filenames='priot_database/mcu/atmega328p.json')
priot_mdb.insert_sensor_mdb(json_filenames='priot_database/sensor/temperature/temperature.json')
#priot_mdb.insert_actuator_mdb(json_filenames='priot_database/actuator/relay/relay.json')
#priot_mdb.insert_board_mdb(json_filenames='priot_database/board/uno.json')
# Test Unqlite database
#priot_uqlitedb = Priot_DB_Unqlite('priot_unqlite_db.udb')
#priot_uqlitedb.create_unqlite_db()
#priot_uqlitedb.insert_sensor_unqlite_db(json_filename='priot_database/sensor/temperature/temperature.json')
#priot_uqlitedb.print_unqlite_db()
#insert_sensor_unqlite_db(json_filename='priot_database/sensor/temperature/temperature.json')
#print_unqlite_db()
# Test sqlite database
#priot_sqlitedb = Priot_DB_Sqlite(dbname='priot_sqlite_db.db')
#priot_sqlitedb.insert_sensor_sqlite_db(json_filename='priot_database/sensor/humidity/humidity.json')
#priot_sqlitedb.insert_actuator_sqlite_db(json_filename='priot_database/actuator/relay/relay.json')
#priot_sqlitedb.insert_board_sqlite_db(json_filename='priot_database/board/uno.json')
#priot_sqlitedb.insert_mcu_sqlite_db(json_filename='priot_database/mcu/atmega328p.json')