Commit f00c62a5 authored by arun.uday's avatar arun.uday

moved the mqtt connection of sender and receiver to mqtt_connections.py in utils

parent 8ff7a770
from paho.mqtt.client import Client
from scripts.core.handlers.receiver_handlers.connection_mqtt_receiver import connect_mqtt
from scripts.core.handlers.receiver_handlers.message_mqtt_receive import message_mqtt
from scripts.logging.loggers import logger
from scripts.utils.mqtt_connections import receiver_util_mqtt
def receive_mqtt(mqtt_host, port, request_no):
def receive_mqtt():
try:
# client object creation
client = Client(userdata="Receiver")
# connecting to broker
client.connect(mqtt_host, int(port), int(request_no))
client = receiver_util_mqtt()
client.on_connect = connect_mqtt
# listening to the topic
......
import json
import time
from paho.mqtt.client import Client
from scripts.config import applications_config
from scripts.database.models.models_sites import SitesEncoder
from scripts.logging.loggers import logger
from scripts.utils.mqtt_connections import sender_util_mqtt
def send_data_mqtt(dict_pollution_data):
......@@ -13,11 +12,7 @@ def send_data_mqtt(dict_pollution_data):
# dumps the list to json
json_data = json.dumps(dict_pollution_data, cls=SitesEncoder)
# create the paho client
client = Client()
# connect to the mqtt client
client.connect(applications_config.mqtt_host, int(applications_config.mqtt_port))
client = sender_util_mqtt()
# publish the topic
client.publish(applications_config.topic_name, json_data)
......
......@@ -23,6 +23,6 @@ def sender_get_data(pollution: Quality):
@receiver_app.post(api_path_config.route_receiver_index + api_path_config.route_get)
def receiver_get_data():
try:
receive_mqtt(applications_config.mqtt_host, applications_config.mqtt_port, applications_config.request_no)
receive_mqtt()
except Exception as e:
logger.error("Some exception in receiver api: ", e)
......@@ -9,3 +9,4 @@
2023-02-20 16:37:28 - INFO - Data entered
2023-02-20 16:38:25 - INFO - Data entered
2023-02-20 16:38:25 - INFO - Data entered
2023-02-20 17:00:51 - INFO - Data entered
# create the paho client
from paho.mqtt.client import Client
from scripts.config import applications_config
from scripts.logging.loggers import logger
try:
client = Client()
def sender_util_mqtt():
# connect to the mqtt client for sender
client.connect(applications_config.mqtt_host, int(applications_config.mqtt_port))
return client
def receiver_util_mqtt():
# connect to the mqtt client for receiver
client.connect(applications_config.mqtt_host, int(applications_config.mqtt_port),
int(applications_config.request_no))
return client
except Exception as e:
logger.error("Some exception occurred while connecting to the mqtt: ", e)
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