Commit b51fac45 authored by vipul.v's avatar vipul.v

mqtt and mongo

parents
client=mongodb://intern_23:intern%40123@192.168.0.220:2717/?authSource=interns_b2_23&authMechanism=SCRAM-SHA-256
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
mqtt_mongo_connect
\ 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" />
<option value="N803" />
</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.11 (mqttone)" 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_mongo_connect.iml" filepath="$PROJECT_DIR$/.idea/mqtt_mongo_connect.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
import json
import paho.mqtt.client as mqtt
from datetime import datetime
import random
import time
from script.config.app_config import Service
# publish
topic1 = "test/Vipul"
message1 = {"Temperature": random.randint(1, 10), "Humidity": random.randint(1, 100),
"Apparent Temperature": random.randint(1, 100),
"Wind Speed": random.randint(1, 200), "Visibility": random.randint(1, 500)}
message2 = {"Message send at": datetime.utcnow().isoformat()}
message1.update({"Message send at": datetime.utcnow().isoformat()})
rand_num = random.randint(1, 100)
client = mqtt.Client()
client.connect(Service.host, Service.port)
while True:
if client.publish(topic1, json.dumps(message1)):
print(f"The message got published to MQTT {message1}")
time.sleep(5)
client.disconnect()
break
[MQTT_SERVICE]
broker_host = 192.168.0.220
broker_port = 1883
from script.core.engine.subscriber import MqttConnectMongodb
mqtt_data = MqttConnectMongodb()
mqtt_data.mqtt_file()
\ No newline at end of file
import os
from dotenv import load_dotenv
from configparser import SafeConfigParser
config = SafeConfigParser()
config.read('conf/application.conf')
load_dotenv()
class Service:
port = int(config.get("MQTT_SERVICE", "broker_port"))
host = config.get("MQTT_SERVICE", "broker_host")
class Mongo:
mongo_uri: str = os.environ.get("CLIENT")
import json
from datetime import datetime
import paho.mqtt.client as mqtt
from script.db.mongo_connection import mongo_connection
from script.config.app_config import Service
# subscribe
topic1 = "test/#"
class MqttConnectMongodb:
try:
@staticmethod
def mqtt_file():
def on_connect(client, userdata, flags, rc):
print("Connected to MQTT broker with result code " + str(rc))
client.subscribe(topic1)
def on_message(client_id, userdata, msg):
print(
"Received message on topic '" + msg.topic + "' with message '" + msg.payload.decode('utf-8') + "'")
db = mongo_connection['interns_b2_23']
collection = db['Vipul-data']
data = json.loads(msg.payload.decode('utf-8'))
document = {'topic': msg.topic, 'payload': data}
data.update({"Recevied message at": datetime.utcnow().isoformat()})
result = collection.insert_one(document)
print("Saved message to MongoDB with ID " + str(result.inserted_id))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Service().host, Service().port)
client.loop_forever()
except ValueError:
raise ValueError
except Exception as e:
print(e, "Excel Error Detected")
\ No newline at end of file
import pymongo
from script.config.app_config import Mongo
mongo_connection = pymongo.MongoClient(Mongo.mongo_uri)
\ 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