Commit 2421b804 by mohammed.shibili

corrections done

parent 852bad86
from script.service.main import run
run()
\ No newline at end of file
if __name__ == "__main__":
run()
\ No newline at end of file
class MqttFunctions:
@staticmethod
def connection_msg(rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
import json
import random
import redis as redis
import redis
from paho.mqtt import client as mqtt_client
from script.config.appconf import mqtt_broker, mqtt_port, redis_host, redis_port, redis_db2
from script.core.handlers.mqtt_functions import MqttFunctions
from script.config.appconf import redis_host, redis_port, redis_db, mqtt_broker, mqtt_port, redis_db2
broker = mqtt_broker
port = mqtt_port
r = redis.Redis(host=redis_host, port=redis_port, db=redis_db)
r2 = redis.Redis(host=redis_host, port=redis_port, db=redis_db2)
topic_from_redis = r.lrange("available_doctors", 0, -1)
topic = [values.decode() for values in topic_from_redis]
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 100)}'
def connect_mqtt() -> mqtt_client:
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
MqttFunctions().connection_msg(rc)
client = mqtt_client.Client(client_id)
client = mqtt_client.Client()
client.on_connect = on_connect
client.connect(broker, port)
client.connect(mqtt_broker, mqtt_port)
return client
......@@ -35,12 +24,16 @@ def subscribe(client: mqtt_client):
value = json.loads(msg.payload.decode())
key = value["patient_id"]
r2.set(key, msg.payload.decode())
for new_topic in topic:
client.subscribe(new_topic)
client.on_message = on_message
# for wild card:
client.subscribe("KL/#")
client.on_message = on_message
def run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
try:
client = connect_mqtt()
subscribe(client)
client.loop_forever()
except KeyboardInterrupt:
client.loop_stop()
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