Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kl_task7_MQTTsender
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
arun.uday
kl_task7_MQTTsender
Commits
23305bf0
Commit
23305bf0
authored
Feb 02, 2023
by
arun.uday
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit3
parent
59b97928
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
94 additions
and
15 deletions
+94
-15
.idea/sonarlint/issuestore/index.pb
+4
-0
app.py
+3
-2
conf/applications.conf
+25
-0
scripts/config/api_routes_config.py
+11
-0
scripts/config/application_config.py
+19
-1
scripts/core/handlers/dict_to_json.py
+4
-1
scripts/core/handlers/uploaded_file_.py
+4
-1
scripts/services/app_services_run.py
+13
-10
scripts/temp/MOCK_DATA (2).csv
+11
-0
No files found.
.idea/sonarlint/issuestore/index.pb
View file @
23305bf0
No preview for this file type
app.py
View file @
23305bf0
import
uvicorn
import
uvicorn
from
scripts.config.application_config
import
uvicorn_app
,
uvicorn_port
# starting the application
# starting the application
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
print
(
"
Fast API
task"
)
print
(
"
MQTT
task"
)
uvicorn
.
run
(
"scripts.services.app_services_run:app"
,
port
=
8000
)
uvicorn
.
run
(
uvicorn_app
,
port
=
int
(
uvicorn_port
)
)
# pip install python-multipart
# pip install python-multipart
conf/applications.conf
View file @
23305bf0
[
path
]
[
path
]
base_path
=
scripts
/
base_path
=
scripts
/
sub_path
=
temp
/
sub_path
=
temp
/
core_path
=
core
/
handlers_path
=
handlers
/
[
file
]
[
file
]
file_name_csv
=
data
.
csv
file_name_csv
=
data
.
csv
file_name_json
=
data
.
json
file_name_json
=
data
.
json
file_mode
=
wb
[
mqtt
]
topic
=
topic
mqtt_host
=
192
.
168
.
0
.
220
port
=
1883
requests
=
60
[
uvicorn
]
uvicorn_app
=
scripts
.
services
.
app_services_run
:
app
uvicorn_port
=
8000
[
api_routes
]
api_upload
= /
upload
/
api_submit
= /
submit_file
/
api_csv
= /
csv
/
[
dataframes
]
orientation
=
records
[
html
]
html_template
=
upload_file
.
html
\ No newline at end of file
scripts/config/api_routes_config.py
0 → 100644
View file @
23305bf0
import
configparser
config
=
configparser
.
RawConfigParser
()
config
.
read
(
"conf/applications.conf"
)
# path
api_upload
=
config
.
get
(
"api_routes"
,
'api_upload'
)
api_submit
=
config
.
get
(
"api_routes"
,
'api_submit'
)
api_csv
=
config
.
get
(
"api_routes"
,
'api_csv'
)
scripts/config/application_config.py
View file @
23305bf0
...
@@ -8,10 +8,28 @@ config.read("conf/applications.conf")
...
@@ -8,10 +8,28 @@ 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"
)
core_path
=
config
.
get
(
"path"
,
"core_path"
)
handlers_path
=
config
.
get
(
"path"
,
"handlers_path"
)
# file name
# file name
file_name_csv
=
config
.
get
(
"file"
,
"file_name_csv"
)
file_name_csv
=
config
.
get
(
"file"
,
"file_name_csv"
)
file_name_json
=
config
.
get
(
"file"
,
"file_name_json"
)
file_name_json
=
config
.
get
(
"file"
,
"file_name_json"
)
file_mode
=
config
.
get
(
"file"
,
"file_mode"
)
full_path_csv
=
base_path
+
sub_path
+
file_name_csv
full_path_csv
=
base_path
+
sub_path
+
file_name_csv
full_path_json
=
base_path
+
sub_path
+
file_name_json
full_path_json
=
base_path
+
sub_path
+
file_name_json
# mqtt
topic_name
=
config
.
get
(
"mqtt"
,
"topic"
)
mqtt_host
=
config
.
get
(
"mqtt"
,
"mqtt_host"
)
mqtt_port
=
config
.
get
(
"mqtt"
,
"port"
)
request_no
=
config
.
get
(
"mqtt"
,
"requests"
)
# uvicorn
uvicorn_port
=
config
.
get
(
"uvicorn"
,
"uvicorn_port"
)
uvicorn_app
=
config
.
get
(
"uvicorn"
,
"uvicorn_app"
)
# data frame
orientation
=
config
.
get
(
"dataframes"
,
"orientation"
)
# html template
html_template
=
config
.
get
(
"html"
,
"html_template"
)
scripts/core/handlers/dict_to_json.py
View file @
23305bf0
# dictionary to json
# dictionary to json
import
pandas
as
pd
import
pandas
as
pd
from
scripts.config.application_config
import
orientation
# extracting the csv to dictionary
# extracting the csv to dictionary
def
extract_data
(
csv_file
):
def
extract_data
(
csv_file
):
data_csv
=
pd
.
read_csv
(
csv_file
)
data_csv
=
pd
.
read_csv
(
csv_file
)
json_data
=
data_csv
.
to_dict
(
orient
=
"records"
)
json_data
=
data_csv
.
to_dict
(
orient
=
orientation
)
return
json_data
return
json_data
scripts/core/handlers/uploaded_file_.py
View file @
23305bf0
# file upload to csv
# file upload to csv
from
scripts.config.application_config
import
file_mode
def
extract_uploaded_data
(
full_path_csv
,
file_data
):
def
extract_uploaded_data
(
full_path_csv
,
file_data
):
with
open
(
full_path_csv
,
"wb"
)
as
file_object
:
with
open
(
full_path_csv
,
file_mode
)
as
file_object
:
file_object
.
write
(
file_data
.
file
.
read
())
file_object
.
write
(
file_data
.
file
.
read
())
scripts/services/app_services_run.py
View file @
23305bf0
...
@@ -2,7 +2,9 @@ from fastapi import FastAPI, Request, UploadFile, File
...
@@ -2,7 +2,9 @@ from fastapi import FastAPI, Request, UploadFile, File
import
json
import
json
from
fastapi.templating
import
Jinja2Templates
from
fastapi.templating
import
Jinja2Templates
from
fastapi.responses
import
HTMLResponse
from
fastapi.responses
import
HTMLResponse
from
scripts.config.application_config
import
full_path_csv
from
scripts.config.api_routes_config
import
api_upload
,
api_submit
,
api_csv
from
scripts.config
import
application_config
from
scripts.core.handlers.dict_to_json
import
extract_data
from
scripts.core.handlers.dict_to_json
import
extract_data
from
scripts.core.handlers.remove_list_json
import
remove_list
from
scripts.core.handlers.remove_list_json
import
remove_list
from
paho.mqtt.client
import
Client
from
paho.mqtt.client
import
Client
...
@@ -10,20 +12,21 @@ from paho.mqtt.client import Client
...
@@ -10,20 +12,21 @@ from paho.mqtt.client import Client
from
scripts.core.handlers.uploaded_file_
import
extract_uploaded_data
from
scripts.core.handlers.uploaded_file_
import
extract_uploaded_data
app
=
FastAPI
()
app
=
FastAPI
()
templates
=
Jinja2Templates
(
"scripts/core/handlers/"
)
templates
=
Jinja2Templates
(
application_config
.
base_path
+
application_config
.
core_path
+
application_config
.
handlers_path
)
# html form access using api
# html form access using api
@app.get
(
"/upload/"
,
response_class
=
HTMLResponse
)
@app.get
(
api_upload
,
response_class
=
HTMLResponse
)
def
template_render
(
request
:
Request
):
def
template_render
(
request
:
Request
):
return
templates
.
TemplateResponse
(
"upload_file.html"
,
{
"request"
:
request
})
return
templates
.
TemplateResponse
(
application_config
.
html_template
,
{
"request"
:
request
})
# handling the form submit using api
# handling the form submit using api
@app.post
(
"/submit_file/"
)
@app.post
(
api_submit
)
async
def
submit_file_data
(
file_data
:
UploadFile
=
File
(
...
)):
async
def
submit_file_data
(
file_data
:
UploadFile
=
File
(
...
)):
try
:
try
:
extract_uploaded_data
(
full_path_csv
,
file_data
)
extract_uploaded_data
(
application_config
.
full_path_csv
,
file_data
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
print
(
e
)
else
:
else
:
...
@@ -31,16 +34,16 @@ async def submit_file_data(file_data: UploadFile = File(...)):
...
@@ -31,16 +34,16 @@ async def submit_file_data(file_data: UploadFile = File(...)):
# reading csv and loading the data to json finally publishing it to mqtt
# reading csv and loading the data to json finally publishing it to mqtt
@app.get
(
"/csv"
)
@app.get
(
api_csv
)
def
dict_to_json
():
def
dict_to_json
():
try
:
try
:
dict_data
=
extract_data
(
full_path_csv
)
dict_data
=
extract_data
(
application_config
.
full_path_csv
)
list_removed
=
remove_list
(
dict_data
)
list_removed
=
remove_list
(
dict_data
)
json_data
=
json
.
dumps
(
list_removed
)
json_data
=
json
.
dumps
(
list_removed
)
client
=
Client
()
client
=
Client
()
client
.
connect
(
'192.168.0.220'
,
1883
)
client
.
connect
(
application_config
.
mqtt_host
,
int
(
application_config
.
mqtt_port
)
)
client
.
publish
(
'topic'
,
json_data
)
client
.
publish
(
application_config
.
topic_name
,
json_data
)
client
.
disconnect
()
client
.
disconnect
()
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
print
(
e
)
...
...
scripts/temp/MOCK_DATA (2).csv
0 → 100644
View file @
23305bf0
id,first_name,last_name
1,Marna,Pergens
2,Morie,Iddens
3,Ilysa,Thonger
4,Catherine,Dundendale
5,Randene,Brookz
6,Hester,Yeude
7,Philomena,Dyott
8,Sarene,Petren
9,Rosella,Kinavan
10,El,Creamer
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