Commit 2b9b9fe7 by mohammed.shibili

final commit

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 (redisProducerReformatted)" 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/redisProducerReformatted.iml" filepath="$PROJECT_DIR$/.idea/redisProducerReformatted.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 uvicorn
if __name__ == "__main__":
uvicorn.run("script.service.main:app", port=8000, reload=True)
[redis]
redis_host=127.0.0.1
redis_port=6379
redis_db=hospital
[mqtt]
mqtt_broker=192.168.0.220
mqtt_port=1883
mqtt_time=60
import configparser
config = configparser.RawConfigParser()
config.read("conf/app.conf")
redis_host = config.get("redis", "redis_host")
redis_port = int(config.get("redis", "redis_port"))
redis_db = config.get("redis", "redis_db")
mqtt_broker = config.get("mqtt", "mqtt_broker")
mqtt_port = int(config.get("mqtt", "mqtt_port"))
mqtt_time = int(config.get("mqtt", "mqtt_time"))
from paho.mqtt import client as mqtt
from script.config.appconf import mqtt_broker, mqtt_port, mqtt_time
def mqtt_conn():
try:
mqtt_ = mqtt.Client()
mqtt_.connect(mqtt_broker, mqtt_port, mqtt_time)
print("mqtt connected")
return mqtt_
except Exception as e:
print("mqtt fail",e)
\ No newline at end of file
import redis
from script.config.appconf import redis_host, redis_port
def redis_connection():
# creating redis connection
try:
redis_conn = redis.Redis(host=redis_host, port=redis_port, db=0)
print("redis connection successful")
return redis_conn
except Exception as e:
print("redis connection fail", e)
from pydantic import BaseModel
class patient_details(BaseModel):
patient_id: str
name: str
age: int
description: str
\ No newline at end of file
import json
from fastapi import FastAPI
from script.core.handlers.mqtt_coo import mqtt_conn
from script.core.handlers.redis_connection import redis_connection
from script.database.models import patient_details
app = FastAPI()
# defining variables needed
doctors_list = []
current_doctor = 0
topic = ''
patient_info = {}
# api call for generating doctors list
@app.get("/doctors", tags=['REDIS'])
async def doctor(no_of_doctors: int):
for doctors in range(1, no_of_doctors + 1):
doctors_list.append(f'doctor{doctors}')
# publishing doctors list to redis
connection_redis.lpush("available_doctors", f'doctor{doctors}')
@app.post("/patients", tags=["REDIS"])
async def patients(body: patient_details):
global topic
global current_doctor
global doctors_list
# accepting patient details using pydantic BaseModel
patient_data = patient_details(patient_id=body.patient_id, name=body.name, age=body.age,
description=body.description)
patient_info.update(dict(patient_data))
# collecting keys from redis db
keys = connection_redis.keys()
# decoding keys in redis
decoded_keys = [values.decode() for values in keys]
# checking whether patient is consulted before
for patient_id in decoded_keys:
# if patient is consulted before assign him to previous doctor
if patient_id == patient_info['patient_id']:
topic = connection_redis.get(patient_id).decode()
# else assign new doctor who is free
if not topic:
topic = doctors_list[current_doctor]
print(topic)
# Increment the index for the next doctor
current_doctor = (current_doctor + 1) % len(doctors_list)
# Publish the patient information to the MQTT topic
mqtt_.publish(topic, json.dumps(patient_info))
# Store the patient information in Redis cache
def on_message(client, userdata, msg):
connection_redis.set(patient_info['patient_id'], topic)
mqtt_ = mqtt_conn()
connection_redis = redis_connection()
# Subscribe to all the MQTT topics for the doctors
for topic in doctors_list:
mqtt_.subscribe(topic)
# Start the MQTT loop to receive messages
mqtt_.on_message = on_message
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