Commit 21ca313a by mohammed.shibili

corrections done

parent b2ebebca
import configparser
config = configparser.RawConfigParser()
config.read("conf/app.conf")
get_end = config.get("end_point", "get")
post_end = config.get("end_point", "post")
\ No newline at end of file
get_end = config.get("end_points", "get")
post_end = config.get("end_points", "post")
\ No newline at end of file
......@@ -10,4 +10,4 @@ def mqtt_conn():
print("mqtt connected")
return mqtt_
except Exception as e:
print("mqtt fail",e)
\ No newline at end of file
print("mqtt fail", e)
import json
from script.core.handlers.mqtt_coo import mqtt_conn
from script.core.handlers.redis_connection import redis_connection
class Consultancy:
doctors_list = []
topic = ''
mqtt_ = mqtt_conn()
connection_redis = redis_connection()
connection_redis.set("current_doctor", 0)
@staticmethod
def doctor_details(no_of_doctors):
Consultancy().doctors_list = []
if Consultancy().connection_redis.exists("available_doctors"):
Consultancy().connection_redis.delete("available_doctors")
for doctors in range(1, no_of_doctors + 1):
Consultancy().doctors_list.append(f'KL/doctor{doctors}')
# publishing doctors list to redis
Consultancy().connection_redis.rpush("available_doctors", *Consultancy().doctors_list)
return Consultancy().doctors_list
@staticmethod
def patient_details(patient_info):
# collecting keys from redis db
current_doctor = int(Consultancy().connection_redis.get("current_doctor"))
keys = Consultancy().connection_redis.keys()
# decoding keys in redis
decoded_keys = [values.decode() for values in keys]
topic = ''
# print(Consultancy().doctors_list)
# print(Consultancy.self.current_doctor)
# 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 = Consultancy().connection_redis.get(patient_id).decode()
# else assign new doctor who is free
if not topic:
topic = Consultancy().doctors_list[current_doctor]
# Increment the index for the next doctor
print(len(Consultancy().doctors_list))
Consultancy().connection_redis.set("current_doctor",
(current_doctor + 1) % len(Consultancy().doctors_list))
# Publish the patient information to the MQTT topic
Consultancy().mqtt_.publish(topic, json.dumps(patient_info))
# Store the patient information in Redis cache
Consultancy().connection_redis.set(patient_info['patient_id'], topic)
return patient_info
import json
from fastapi import FastAPI
from script.config.end_points import get_end, post_end
from script.core.handlers.mqtt_coo import mqtt_conn
from script.core.handlers.redis_connection import redis_connection
from script.core.handlers.patient_info import Consultancy
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(get_end, 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}')
print(Consultancy().doctor_details(no_of_doctors))
return "doctors_list generated"
# api call for accepting patient details
@app.post(post_end, 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
patient_info = dict(patient_details(patient_id=body.patient_id, name=body.name, age=body.age,
description=body.description))
# patient_info.update(dict(patient_data))
print(Consultancy().patient_details(patient_info))
return "patient is send for consulting"
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