Commit 7583b00c by arun.uday

commit2

parent f731d271
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (task7)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (task7subscribe)" project-jdk-type="Python SDK" />
</project> </project>
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/.idea/task7.iml" filepath="$PROJECT_DIR$/.idea/task7.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/task7_mqtt_receiver.iml" filepath="$PROJECT_DIR$/.idea/task7_mqtt_receiver.iml" />
</modules> </modules>
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.9 (task7subscribe)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>
\ No newline at end of file
import uvicorn import uvicorn
from scripts.config.application_config import uvicorn_host, uvicorn_port
# starting the application # starting the application
if __name__ == "__main__": if __name__ == "__main__":
print("Fast API task") print("Fast API task")
uvicorn.run("scripts.services.receiver_app_services_run:app", host="127.0.0.1", port=8001) uvicorn.run("scripts.services.receiver_app_services_run:app", host=uvicorn_host, port=int(uvicorn_port))
...@@ -5,3 +5,13 @@ sub_path = temp/ ...@@ -5,3 +5,13 @@ sub_path = temp/
[file] [file]
file_name_csv = MOCK_DATA.csv file_name_csv = MOCK_DATA.csv
file_name_json = data.json file_name_json = data.json
[mqtt]
topic = topic
mqtt_host = 192.168.0.220
port = 1883
requests = 60
[uvicorn]
uvicorn_host = 127.0.0.1
uvicorn_port = 8001
...@@ -10,6 +10,17 @@ base_path = config.get("path", 'base_path') ...@@ -10,6 +10,17 @@ base_path = config.get("path", 'base_path')
sub_path = config.get("path", "sub_path") sub_path = config.get("path", "sub_path")
# mqtt
topic_name = config.get("mqtt", "topic")
mqtt_host = config.get("mqtt", "mqtt_host")
port = config.get("mqtt", "port")
request_no = config.get("mqtt", "requests")
# uvicorn
uvicorn_host = config.get("uvicorn", "uvicorn_host")
uvicorn_port = config.get("uvicorn", "uvicorn_port")
# 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")
......
import sqlite3
connection_db = sqlite3.connect('user_details.db')
import json
import pandas as pd
from scripts.database.mysqlite_connect import connection_db
from scripts.utilis.operations_db import SqliteOperations
def on_connect(client, m, c, rc):
print("Connected with result code " + str(rc))
client.subscribe("topic")
def on_message(m, c, msg):
payload_decoded = msg.payload.decode('utf-8')
json_data = json.loads(payload_decoded)
data_list = list(json_data.values())
frames = pd.DataFrame(data_list, index=None)
obj_db = SqliteOperations()
connection = obj_db.create_db(connection_db)
connection = obj_db.insert_db(connection, frames)
data_fetched = obj_db.fetch_data(connection)
print(data_fetched)
import sqlite3 import paho.mqtt.client as mqtt
from fastapi import FastAPI
import json import json
import pandas as pd import pandas as pd
import paho.mqtt.client as mqtt from scripts.config.application_config import topic_name, mqtt_host, port, request_no
from fastapi import FastAPI from scripts.core.handlers.display_data import display_fetched_data
from scripts.utilis.operations_db import SqliteOperations
# This is the Subscriber # This is the Subscriber
...@@ -12,43 +14,28 @@ app = FastAPI() ...@@ -12,43 +14,28 @@ app = FastAPI()
@app.get("/") @app.get("/")
def receiver(): def receiver():
def on_connect(client, user, check, rc): def on_connect(client_name, userdata, flags, rc):
print(userdata, " ", flags)
print("Connected with result code " + str(rc)) print("Connected with result code " + str(rc))
client.subscribe("topic") client_name.subscribe(topic_name)
def on_message(user, check, msg): def on_message(client_name, userdata, msg):
try: print(client_name, " ", userdata)
# decoding the msg to string
payload_decoded = msg.payload.decode('utf-8') payload_decoded = msg.payload.decode('utf-8')
json_data = json.loads(payload_decoded) json_data = json.loads(payload_decoded)
# decoded msg to list
data_list = list(json_data.values()) data_list = list(json_data.values())
# list to dataframe
frames = pd.DataFrame(data_list, index=None) frames = pd.DataFrame(data_list, index=None)
conn = sqlite3.connect('test.db') obj_db = SqliteOperations()
conn.execute('''CREATE TABLE IF NOT EXISTS details # inserting the data to table
(ID INT PRIMARY KEY NOT NULL, obj_db.insert_db(frames)
FIRSTNAME TEXT NOT NULL, # fetching the data
LASTNAME TEXT NOT NULL);''') data_fetched = obj_db.fetch_data()
frames.to_sql('details', conn, if_exists='replace') display_fetched_data(data_fetched)
conn.commit()
# Create a cursor
cursor = conn.cursor()
# Execute a SELECT statement to fetch data from the "employees" table
cursor.execute("SELECT * FROM details")
# Fetch all the rows as a list of tuples
rows = cursor.fetchall()
# Iterate through the rows and print the data
for row in rows:
print(row)
except Exception as e:
print(e)
client = mqtt.Client() client = mqtt.Client()
client.connect("192.168.0.220", 1883, 60) client.connect(mqtt_host, int(port), int(request_no))
client.on_connect = on_connect client.on_connect = on_connect
client.on_message = on_message client.on_message = on_message
client.loop_forever() client.loop_forever()
id,first_name,last_name,email,Address
1,Maurita,Helks,mhelks0@parallels.com,PO Box 3203
2,Prisca,Baxstar,pbaxstar1@whitehouse.gov,20th Floor
3,Camellia,Gallop,cgallop2@comsenz.com,5th Floor
4,Kingston,Keightley,kkeightley3@rediff.com,Suite 35
5,Suzie,Tredgold,stredgold4@myspace.com,PO Box 76402
6,Ade,Coonihan,acoonihan5@stanford.edu,Apt 376
7,Candida,Kobiela,ckobiela6@jiathis.com,10th Floor
8,Karee,Marty,kmarty7@auda.org.au,Suite 75
9,Granville,Weathey,gweathey8@bloglovin.com,2nd Floor
10,Letty,Brecher,lbrecher9@posterous.com,3rd Floor
11,Tam,Graal,tgraala@dmoz.org,Apt 525
12,Dusty,Causbey,dcausbeyb@drupal.org,Apt 1003
13,Kath,Chattey,kchatteyc@smugmug.com,Apt 1074
14,Rutter,Wattins,rwattinsd@hhs.gov,Room 1920
15,Simonette,Gledstane,sgledstanee@nytimes.com,20th Floor
16,Cassey,Gerrelt,cgerreltf@scribd.com,12th Floor
17,Paige,O'Henery,poheneryg@nature.com,Apt 1111
18,Tabbie,Trever,ttreverh@ask.com,20th Floor
19,Hoebart,Valder,hvalderi@tripod.com,5th Floor
20,Margie,Stollenbecker,mstollenbeckerj@fema.gov,Apt 531
21,Bobby,Beet,bbeetk@odnoklassniki.ru,20th Floor
22,Obie,Le Blanc,oleblancl@bravesites.com,Room 832
23,Alyosha,Ruffli,arufflim@bandcamp.com,Suite 14
24,Syman,Granger,sgrangern@bbb.org,PO Box 19521
25,Slade,Weller,swellero@livejournal.com,Room 1911
26,Robina,Rate,rratep@ask.com,Apt 917
27,Karon,Liveley,kliveleyq@tinypic.com,3rd Floor
28,Patty,McGeouch,pmcgeouchr@paginegialle.it,11th Floor
29,Osborn,Ferriday,oferridays@slashdot.org,Room 1474
30,Ellsworth,Dundin,edundint@wikispaces.com,PO Box 17827
31,Mitch,Stetson,mstetsonu@linkedin.com,Apt 603
32,Hardy,Swansbury,hswansburyv@google.it,Apt 1802
33,Nikolas,Telling,ntellingw@ocn.ne.jp,Room 148
34,Gladi,Florey,gfloreyx@dropbox.com,Suite 55
35,Adela,Overbury,aoverburyy@tinyurl.com,4th Floor
36,Evie,McFfaden,emcffadenz@scribd.com,Suite 88
37,Baldwin,Reaman,breaman10@booking.com,Suite 71
38,Beckie,Leadley,bleadley11@blogs.com,Apt 1592
39,Bendick,Kubes,bkubes12@latimes.com,Room 769
40,Huntlee,Mazzeo,hmazzeo13@columbia.edu,Apt 1564
41,Vally,Dzenisenka,vdzenisenka14@themeforest.net,PO Box 96195
42,Lethia,Bolden,lbolden15@yandex.ru,Suite 80
43,Odille,Chataignier,ochataignier16@slate.com,PO Box 73001
44,Cy,Wescott,cwescott17@reuters.com,17th Floor
45,Darnell,Jays,djays18@yellowbook.com,Room 1722
46,Mikael,Wickwar,mwickwar19@bloglines.com,Room 1780
47,Marguerite,McGahey,mmcgahey1a@1688.com,Room 243
48,Jessie,Gaskill,jgaskill1b@angelfire.com,Suite 98
49,Gardiner,Bestall,gbestall1c@cloudflare.com,17th Floor
50,Jo-ann,Rickett,jrickett1d@live.com,Room 903
51,Gavra,Moquin,gmoquin1e@quantcast.com,Room 1139
52,Dylan,Slewcock,dslewcock1f@trellian.com,Apt 1256
53,Danyette,Coolson,dcoolson1g@biglobe.ne.jp,Apt 452
54,Roderich,Ashworth,rashworth1h@163.com,Apt 1416
55,Hildagarde,Haucke,hhaucke1i@ox.ac.uk,Suite 7
56,Darlleen,Bearsmore,dbearsmore1j@google.com,Suite 43
57,Wilmette,Bedwell,wbedwell1k@weebly.com,Suite 65
58,Neville,Swapp,nswapp1l@ocn.ne.jp,Apt 1054
59,Norene,Kopacek,nkopacek1m@domainmarket.com,Suite 38
60,Aubert,Streeting,astreeting1n@cocolog-nifty.com,Suite 82
61,Katrine,Easson,keasson1o@bizjournals.com,Room 1306
62,Celle,Elgie,celgie1p@sciencedirect.com,16th Floor
63,Alvan,Curtin,acurtin1q@booking.com,16th Floor
64,Cornela,Roder,croder1r@google.pl,2nd Floor
65,Wanids,Dansie,wdansie1s@fc2.com,18th Floor
66,Kip,Fillon,kfillon1t@imgur.com,Suite 82
67,Holmes,Kidstoun,hkidstoun1u@usnews.com,Apt 17
68,Freddy,Featherstone,ffeatherstone1v@parallels.com,13th Floor
69,Gusella,Jodlkowski,gjodlkowski1w@chronoengine.com,Apt 515
70,Marietta,Circuit,mcircuit1x@nydailynews.com,18th Floor
71,Brander,Hackley,bhackley1y@drupal.org,Suite 91
72,Nikola,Hughes,nhughes1z@elegantthemes.com,Suite 34
73,Krisha,Aberkirder,kaberkirder20@blogs.com,Suite 70
74,Shina,Meriott,smeriott21@rakuten.co.jp,18th Floor
75,Debra,Sproat,dsproat22@wikia.com,Apt 1650
76,Dunc,Wallworke,dwallworke23@devhub.com,PO Box 58145
77,Rivalee,Gonnet,rgonnet24@oracle.com,Suite 97
78,Regine,Keling,rkeling25@edublogs.org,Room 1519
79,Ashla,Fair,afair26@ucla.edu,Apt 1213
80,Arabela,Theakston,atheakston27@nsw.gov.au,Apt 1277
81,Barney,Grimsdale,bgrimsdale28@reddit.com,PO Box 144
82,Lucho,Braunlein,lbraunlein29@cisco.com,PO Box 26380
83,Jania,Eldredge,jeldredge2a@bloglovin.com,Suite 3
84,Hector,Remer,hremer2b@va.gov,Suite 51
85,Hans,Cantrell,hcantrell2c@bbb.org,Apt 1834
86,Jacquelynn,Kuhne,jkuhne2d@unc.edu,PO Box 70594
87,Way,Moogan,wmoogan2e@chronoengine.com,PO Box 51805
88,Janenna,Costa,jcosta2f@symantec.com,Room 1115
89,Hermine,Bridywater,hbridywater2g@weibo.com,13th Floor
90,Claudette,Highman,chighman2h@ftc.gov,Room 1359
91,Reinhold,Bromehed,rbromehed2i@cargocollective.com,9th Floor
92,Katleen,Dohmer,kdohmer2j@naver.com,PO Box 73746
93,Kellen,Hull,khull2k@eepurl.com,Apt 1569
94,Chrotoem,Lorincz,clorincz2l@behance.net,PO Box 8905
95,Worth,Putton,wputton2m@qq.com,PO Box 91027
96,Luce,Lyford,llyford2n@nps.gov,Room 739
97,Van,Warre,vwarre2o@zimbio.com,Room 1771
98,Kikelia,Benardette,kbenardette2p@vkontakte.ru,PO Box 74859
99,Fabio,Grigoli,fgrigoli2q@plala.or.jp,Apt 1785
100,Rhodia,Batch,rbatch2r@alibaba.com,Room 1597
import sqlite3
class SqliteOperations: class SqliteOperations:
@staticmethod def __init__(self):
def create_db(conn): # connecting to the database
self.conn = sqlite3.connect('user_details.db')
def create_db(self):
try: try:
conn.execute('''CREATE TABLE IF NOT EXISTS details # creating the table
self.conn.execute('''CREATE TABLE IF NOT EXISTS details
(ID INT PRIMARY KEY NOT NULL, (ID INT PRIMARY KEY NOT NULL,
FIRSTNAME TEXT NOT NULL, FIRSTNAME TEXT NOT NULL,
LASTNAME TEXT NOT NULL);''') LASTNAME TEXT NOT NULL);''')
return conn self.conn.close()
except Exception as e: except Exception as e:
print(e) print(e)
@staticmethod def insert_db(self, frames):
def insert_db(conn, frames):
try: try:
frames.to_sql('details', conn, if_exists='replace') # inserting data to table
conn.commit() frames.to_sql('details', self.conn, if_exists='replace')
return conn self.conn.commit()
except Exception as e: except Exception as e:
print(e) print(e)
@staticmethod def fetch_data(self):
def fetch_data(conn):
try: try:
# Create a cursor # Create a cursor
cursor = conn.cursor() cursor = self.conn.cursor()
# Execute a SELECT statement to fetch data from the "employees" table # Execute a SELECT statement to fetch data from the "detaisl" table
cursor.execute("SELECT * FROM details") cursor.execute("SELECT * FROM details")
return cursor return cursor
except Exception as e: except Exception as e:
......
File deleted
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