Commit 8523d69c by ajil.k

added files

parent 5ee2d738
# importing libraries
import uvicorn
from scripts.constants.app_configuration import port_no, link_to_fastapi
from scripts.constants.app_configuration import port_no
if __name__ == "__main__":
try:
uvicorn.run(link_to_fastapi, port=int(port_no), reload=True)
uvicorn.run("scripts.services.api_call:app", port=int(port_no), reload=True)
except Exception as e:
print("Error-", e)
[FastAPI]
link_to_fastapi = scripts.services.main:app
port = 8000
......@@ -6,4 +6,3 @@ config.read(f"conf/application.conf")
port_no = config.get("FastAPI", "port")
link_to_fastapi = config.get("FastAPI", "link_to_fastapi")
import json
import paho.mqtt.client as mqtt
import sqlite3
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("mqtt_test/json")
def on_message(client, userdata, msg):
data = json.loads(msg.payload.decode())
# Connect to the database
connection = sqlite3.connect('mqtt_data.db')
cursor = connection.cursor()
print("Database Connected")
# Create a table to store the data
cursor.execute("CREATE TABLE IF NOT EXISTS tbl_mqtt_data (Timestamp VARCHAR(50), kWh DOUBLE, kVAh DOUBLE, "
"kW DOUBLE, kVA DOUBLE, current DOUBLE)")
columns = ', '.join(data["data"][0].keys())
placeholders = ', '.join(['?'] * len(data["data"][0]))
for ele in range(len(data["data"])):
# Insert the data into the table
query = f'INSERT INTO tbl_mqtt_data({columns}) VALUES({placeholders})'
cursor.execute(query, tuple(data["data"][ele].values()))
connection.commit()
print("Completed data inserting on DB")
connection.close()
broker_address = "192.168.0.220"
broker_port_no = 1883
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_address, broker_port_no, 60)
client.loop_forever()
# Importing libraries
import json
import paho.mqtt.client as mqtt
from fastapi import FastAPI
# Create object of FastAPI
app = FastAPI()
# Setting Root
@app.get("/")
async def root():
return {"INFO": "It's Working!"}
@app.post("/send_json")
async def send_json():
with open("temp/jsonFile.json", 'r') as filename:
# Load json file
data = json.load(filename)
# Create MQTT Client
client = mqtt.Client()
# Create Client connection
broker_address = "192.168.0.220"
port_no = 1883
client.connect(broker_address, port_no)
# Publish Data
client.publish("mqtt_test/json", json.dumps(data))
print("Data Published")
return {"STATUS": "SUCCESS"}
def send_json():
try:
with open("temp/jsonFile.json", 'r') as filename:
# Load json file
data = json.load(filename)
# Create MQTT Client
client = mqtt.Client()
# Create Client connection
broker_address = "192.168.0.220"
port_no = 1883
client.connect(broker_address, port_no)
# Publish Data
client.publish("mqtt_test/json", json.dumps(data))
print("Data Published")
except Exception as e:
print("Exception occurred due to: ", e)
# Importing libraries
from fastapi import FastAPI
from scripts.core.handlers.mqtt_publisher import send_json
# Create object of FastAPI
app = FastAPI()
# Setting Root
@app.get("/")
async def root():
return {"INFO": "It's Working!"}
@app.post("/send_json", tags=["send_json_file"])
async def send_file():
send_json()
return {"STATUS": "SUCCESS"}
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