Commit ef07dc7b by arun.uday

commit4

parent c4ca5413
...@@ -8,6 +8,7 @@ config.read("conf/applications.conf") ...@@ -8,6 +8,7 @@ config.read("conf/applications.conf")
# path # path
base_path = config.get("path", 'base_path') base_path = config.get("path", 'base_path')
sub_path = config.get("path", "sub_path") sub_path = config.get("path", "sub_path")
full_path = base_path + sub_path
# mqtt # mqtt
......
import sqlite3 import sqlite3
from scripts.config.application_config import db_name, db_table from scripts.config.application_config import db_name, db_table, full_path
from scripts.utilis.db_queries import DbQueries from scripts.utilis.db_queries import DbQueries
class SqliteOperations: class SqliteOperations:
def __init__(self): def __init__(self):
# connecting to the database # connecting to the database
self.conn = sqlite3.connect(db_name) self.conn = sqlite3.connect(full_path + db_name)
def create_db(self): def create_db(self):
try: try:
......
...@@ -14,28 +14,32 @@ app = FastAPI() ...@@ -14,28 +14,32 @@ app = FastAPI()
@app.get("/") @app.get("/")
def receiver(): def receiver():
def on_connect(client_name, userdata, flags, rc): try:
print(userdata, " ", flags) def on_connect(client_name, userdata, flags, rc):
print("Connected with result code " + str(rc)) print(userdata, " ", flags)
client_name.subscribe(topic_name) print("Connected with result code " + str(rc))
client_name.subscribe(topic_name)
def on_message(client_name, userdata, msg):
print(client_name, " ", userdata)
# decoding the msg to string
payload_decoded = msg.payload.decode(utf_encode)
json_data = json.loads(payload_decoded)
# decoded msg to list
data_list = list(json_data.values())
# list to dataframe
frames = pd.DataFrame(data_list, index=None)
obj_db = SqliteOperations()
# inserting the data to table
obj_db.insert_db(frames)
# fetching the data
data_fetched = obj_db.fetch_data()
display_fetched_data(data_fetched)
client = mqtt.Client()
client.connect(mqtt_host, int(port), int(request_no))
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
except Exception as e:
print(e)
def on_message(client_name, userdata, msg):
print(client_name, " ", userdata)
# decoding the msg to string
payload_decoded = msg.payload.decode(utf_encode)
json_data = json.loads(payload_decoded)
# decoded msg to list
data_list = list(json_data.values())
# list to dataframe
frames = pd.DataFrame(data_list, index=None)
obj_db = SqliteOperations()
# inserting the data to table
obj_db.insert_db(frames)
# fetching the data
data_fetched = obj_db.fetch_data()
display_fetched_data(data_fetched)
client = mqtt.Client()
client.connect(mqtt_host, int(port), int(request_no))
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment