Commit 852bad86 by mohammed.shibili

finalCommit

parents
# Default ignored files
/shelf/
/workspace.xml
<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" />
</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.9 (redisConsumerReformatted)" 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/redisConsumerReformatted.iml" filepath="$PROJECT_DIR$/.idea/redisConsumerReformatted.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
from script.service.main import run
run()
\ No newline at end of file
[redis]
redis_host=127.0.0.1
redis_port=6379
redis_db=0
redis_db2=1
[mqtt]
mqtt_broker=192.168.0.220
mqtt_port=1883
mqtt_time=60
import configparser
config = configparser.RawConfigParser()
config.read("conf/appconf.conf")
redis_host = config.get("redis", "redis_host")
redis_port = int(config.get("redis", "redis_port"))
redis_db = int(config.get("redis", "redis_db"))
redis_db2 = int(config.get("redis", "redis_db2"))
mqtt_broker = config.get("mqtt", "mqtt_broker")
mqtt_port = int(config.get("mqtt", "mqtt_port"))
mqtt_time = int(config.get("mqtt", "mqtt_time"))
import json
import random
import redis as redis
from paho.mqtt import client as mqtt_client
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 on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.on_connect = on_connect
client.connect(broker, port)
return client
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
print(f"patient `{msg.payload.decode()}` send to `{msg.topic}` ")
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
def run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
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