Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mqtt_v2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rakesh.pv
mqtt_v2
Commits
8523d69c
Commit
8523d69c
authored
Feb 06, 2023
by
ajil.k
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added files
parent
5ee2d738
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
24 additions
and
56 deletions
+24
-56
app.py
+2
-2
conf/application.conf
+0
-2
scripts/constants/app_configuration.py
+0
-1
scripts/core/handlers/mqtt_data.db
+0
-0
scripts/core/handlers/mqtt_listener.py
+0
-37
scripts/core/handlers/mqtt_publisher.py
+4
-14
scripts/services/api_call.py
+18
-0
No files found.
app.py
View file @
8523d69c
# importing libraries
# importing libraries
import
uvicorn
import
uvicorn
from
scripts.constants.app_configuration
import
port_no
,
link_to_fastapi
from
scripts.constants.app_configuration
import
port_no
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
try
:
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
:
except
Exception
as
e
:
print
(
"Error-"
,
e
)
print
(
"Error-"
,
e
)
conf/application.conf
View file @
8523d69c
[
FastAPI
]
[
FastAPI
]
link_to_fastapi
=
scripts
.
services
.
main
:
app
port
=
8000
port
=
8000
scripts/constants/app_configuration.py
View file @
8523d69c
...
@@ -6,4 +6,3 @@ config.read(f"conf/application.conf")
...
@@ -6,4 +6,3 @@ config.read(f"conf/application.conf")
port_no
=
config
.
get
(
"FastAPI"
,
"port"
)
port_no
=
config
.
get
(
"FastAPI"
,
"port"
)
link_to_fastapi
=
config
.
get
(
"FastAPI"
,
"link_to_fastapi"
)
scripts/core/handlers/mqtt_data.db
deleted
100644 → 0
View file @
5ee2d738
File deleted
scripts/core/handlers/mqtt_listener.py
deleted
100644 → 0
View file @
5ee2d738
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
()
scripts/
services/main
.py
→
scripts/
core/handlers/mqtt_publisher
.py
View file @
8523d69c
# Importing libraries
import
json
import
json
import
paho.mqtt.client
as
mqtt
import
paho.mqtt.client
as
mqtt
from
fastapi
import
FastAPI
# Create object of FastAPI
app
=
FastAPI
()
def
send_json
():
# Setting Root
try
:
@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
:
with
open
(
"temp/jsonFile.json"
,
'r'
)
as
filename
:
# Load json file
# Load json file
data
=
json
.
load
(
filename
)
data
=
json
.
load
(
filename
)
...
@@ -27,4 +16,5 @@ async def send_json():
...
@@ -27,4 +16,5 @@ async def send_json():
# Publish Data
# Publish Data
client
.
publish
(
"mqtt_test/json"
,
json
.
dumps
(
data
))
client
.
publish
(
"mqtt_test/json"
,
json
.
dumps
(
data
))
print
(
"Data Published"
)
print
(
"Data Published"
)
return
{
"STATUS"
:
"SUCCESS"
}
except
Exception
as
e
:
print
(
"Exception occurred due to: "
,
e
)
scripts/services/api_call.py
0 → 100644
View file @
8523d69c
# 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"
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment