Commit d9022e8f by arjun.b

MQTT Publisher

parents
# Default ignored files
/shelf/
/workspace.xml
<?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
<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>
</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-Task.iml" filepath="$PROJECT_DIR$/.idea/MQTT-Task.iml" />
</modules>
</component>
</project>
\ 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
import uvicorn
from scripts.config.application_config import server_path, port_no
if __name__ == "__main__":
try:
uvicorn.run(server_path, port=int(port_no), reload=True)
except Exception as e:
print(str(e))
[server]
server_path=scripts.services.main:app
port_no=8000
[path]
file_path=scripts/temp/json_file.json
[MQTT]
host=192.168.0.220
port=1883
\ No newline at end of file
import configparser
try:
config = configparser.ConfigParser()
config.read("conf/application.conf")
# server
server_path = config.get("server", "server_path")
port_no = config.get("server", "port_no")
# file path
file_path = config.get("path", "file_path")
# mqtt host and port
hostname = config.get("MQTT", "host")
port_num = config.get("MQTT", "port")
except configparser.NoOptionError:
print("could not find the conf fle")
import json
import paho.mqtt.client as mqtt
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from scripts.config.application_config import file_path, port_num, hostname
app = FastAPI()
@app.get("/", tags=["root"])
async def root():
return {"data": "MQTT Publisher"}
@app.post("/send-json", tags=["send json"])
async def send_json():
try:
with open(file_path, "r") as s:
json_data = json.load(s)
# connect to mqtt broker
client = mqtt.Client()
client.connect(hostname, int(port_num))
# publish json into MQTT Topic
client.publish("mqtt_json/test", json.dumps(json_data))
client.disconnect()
print("JSON published!!!!")
return JSONResponse(content={"message": "JSON data send to MQTT "})
except Exception as e:
print(str(e))
{
"personal_details":[
{
"first_name": "ARJUN",
"last_name": "B",
"age": "22"
},
{
"first_name": "ALAN",
"last_name": "tom",
"age": "22"
},
{
"first_name": "Sanal",
"last_name": "S",
"age": "26"
},
{
"first_name": "Thomas",
"last_name": "PP",
"age": "32"
}
]
}
\ 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