Commit 5ee2d738 by ajil.k

added files

parents
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="dict.mobile" />
<option value="dict.dob" />
<option value="dict.gender" />
<option value="dict.name" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (mqtt_task)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/mqtt_publisher.iml" filepath="$PROJECT_DIR$/.idea/mqtt_publisher.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
# importing libraries
import uvicorn
from scripts.constants.app_configuration import port_no, link_to_fastapi
if __name__ == "__main__":
try:
uvicorn.run(link_to_fastapi, port=int(port_no), reload=True)
except Exception as e:
print("Error-", e)
[FastAPI]
link_to_fastapi = scripts.services.main:app
port = 8000
from configparser import ConfigParser
config = ConfigParser()
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"}
{
"data": [
{
"Timestamp": "2019-12-01 21:00:00",
"kWh": 46.228,
"kVAh": 49.759,
"kW": 183.668,
"kVA": 200.532,
"current": 281.582
},
{
"Timestamp": "2019-12-01 21:15:00",
"kWh": 45.763,
"kVAh": 50.228,
"kW": 183.462,
"kVA": 200.567,
"current": 281.617
},
{
"Timestamp": "2019-12-01 21:30:00",
"kWh": 45.763,
"kVAh": 50.493,
"kW": 184.889,
"kVA": 201.794,
"current": 283.040
},
{
"Timestamp": "2019-12-01 21:45:00",
"kWh": 46.552,
"kVAh": 50.482,
"kW": 184.325,
"kVA": 201.423,
"current": 282.446
},
{
"Timestamp": "2019-12-01 22:00:00",
"kWh": 46.004,
"kVAh": 50.138,
"kW": 182.370,
"kVA": 199.271,
"current": 279.428
},
{
"Timestamp": "2019-12-01 22:15:00",
"kWh": 46.110,
"kVAh": 50.633,
"kW": 184.189,
"kVA": 201.039,
"current": 281.973
},
{
"Timestamp": "2019-12-01 22:30:00",
"kWh": 46.476,
"kVAh": 50.834,
"kW": 186.400,
"kVA": 203.868,
"current": 285.969
},
{
"Timestamp": "2019-12-01 22:45:00",
"kWh": 46.296,
"kVAh": 50.868,
"kW": 185.918,
"kVA": 203.102,
"current": 285.101
},
{
"Timestamp": "2019-12-01 23:00:00",
"kWh": 46.596,
"kVAh": 51.312,
"kW": 185.942,
"kVA": 203.367,
"current": 285.234
},
{
"Timestamp": "2019-12-01 23:15:00",
"kWh": 46.763,
"kVAh": 50.514,
"kW": 185.391,
"kVA": 202.444,
"current": 283.991
},
{
"Timestamp": "2019-12-02 18:30:00",
"kWh": 46.563,
"kVAh": 50.294,
"kW": 184.829,
"kVA": 202.093,
"current": 283.225
},
{
"Timestamp": "2019-12-02 18:45:00",
"kWh": 45.987,
"kVAh": 50.719,
"kW": 184.394,
"kVA": 201.154,
"current": 281.929
},
{
"Timestamp": "2019-12-02 19:00:00",
"kWh": 46.714,
"kVAh": 50.687,
"kW": 185.461,
"kVA": 202.850,
"current": 284.172
},
{
"Timestamp": "2019-12-02 19:15:00",
"kWh": 45.838,
"kVAh": 50.328,
"kW": 184.315,
"kVA": 201.363,
"current": 282.219
},
{
"Timestamp": "2019-12-02 19:30:00",
"kWh": 46.5999,
"kVAh": 50.616,
"kW": 184.446,
"kVA": 201.672,
"current": 282.612
},
{
"Timestamp": "2019-12-02 19:45:00",
"kWh": 46.0763,
"kVAh": 50.989,
"kW": 182.896,
"kVA": 199.877,
"current": 280.149
},
{
"Timestamp": "2019-12-02 20:00:00",
"kWh": 45.950,
"kVAh": 50.037,
"kW": 182.440,
"kVA": 199.392,
"current": 279.737
},
{
"Timestamp": "2019-12-02 20:15:00",
"kWh": 46.498,
"kVAh": 50.415,
"kW": 184.442,
"kVA": 201.895,
"current": 283.397
},
{
"Timestamp": "2019-12-02 20:30:00",
"kWh": 45.891,
"kVAh": 50.277,
"kW": 183.253,
"kVA": 200.425,
"current": 281.132
},
{
"Timestamp": "2019-12-02 20:45:00",
"kWh": 41.182,
"kVAh": 44.879,
"kW": 183.252,
"kVA": 200.477,
"current": 281.261
}
]
}
\ No newline at end of file
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