Commit 0554b09e by ajil.k

updated

parent 15f50cd9
[fastapi]
[fastapi_connection]
link_to_fastapi =scripts.services.main:app
port_no =8000
[mqtts]
[MQTT_Connection]
broker = broker.emqx.io
port = 1883
time_out = 60
\ No newline at end of file
time_out = 60
[redis_connection]
host = 127.0.0.1
redis_port = 6379
cache_db = 0
patient_db = 2
\ No newline at end of file
......@@ -3,15 +3,23 @@ from configparser import ConfigParser
try:
config = ConfigParser()
config.read("conf/application.conf")
broker = config.get("MQTT", "broker")
mqtt_port = config.get("MQTT", "port")
time_out = config.get("MQTT", "time_out")
port_no = config.get("fastapi_connection", "port_no")
link_to_fastapi = config.get("fastapi_connection", "link_to_fastapi")
broker = config.get("MQTT_Connection", "broker")
mqtt_port = config.get("MQTT_Connection", "port")
time_out = config.get("MQTT_Connection", "time_out")
host = config.get("redis_connection", "host")
redis_port = config.get("redis_connection", "redis_port")
cache_db = config.get("redis_connection", "cache_db")
patient_db = config.get("redis_connection", "patient_db")
except Exception as e:
print("Exception-", e)
#port_no = config.get("fastapi", "port_no")
#link_to_fastapi = config.get("fastapi", "link_to_fastapi")
import redis
import paho.mqtt.client as mqtt
from scripts.constants.mqtt_connection import mqtt_client_connection
from scripts.constants.redis_connection import establish_redis_connection
# Initialize the Redis cache
redis_cache = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
redis_connection = establish_redis_connection()
# MQTT client connection for publishing patient information
mqtt_client = mqtt.Client()
mqtt_client.connect("broker.emqx.io", 1883, 60)
mqtt_client = mqtt_client_connection()
# Counter to keep track of the next doctor to assign
counter = 0
# No. of doctors
n = int(input("Enter the number of doctors: "))
def assign_doctor(patient_id):
global counter
# Increment the counter and wrap around if it exceeds n
counter = (counter + 1) % n
doctor_topic = "doctor" + str(counter)
# Store the doctor's topic in the Redis cache for the patient
redis_cache.set(patient_id, doctor_topic)
print(patient_id, " assigned to ", doctor_topic)
try:
global counter
# Increment the counter and wrap around if it exceeds n
counter = (counter + 1) % n
doctor_topic = "doctor" + str(counter)
# Store the doctor's topic in the Redis cache for the patient
redis_connection.set(patient_id, doctor_topic)
print(patient_id, " assigned to ", doctor_topic)
except Exception as e:
print("Exception occurred due to \"", e, "\"")
def handle_new_patient(patient_id):
# Check if the patient has visited before
all_patient_id = redis_cache.keys()
ids = [each.decode() for each in all_patient_id]
if patient_id in ids:
doctor_topic = redis_cache.get(patient_id)
print(patient_id, " consulted ", doctor_topic.decode(), " before.")
try:
# Check if the patient has visited before
all_patient_id = redis_connection.keys()
ids = [each.decode() for each in all_patient_id]
if patient_id in ids:
doctor_topic = redis_connection.get(patient_id)
print(patient_id, " consulted ", doctor_topic.decode(), " before.")
else:
# If the patient is new, assign a doctor using round-robin
assign_doctor(patient_id)
else:
# If the patient is new, assign a doctor using round-robin
assign_doctor(patient_id)
except Exception as e:
print("Exception occurred due to \"", e, "\"")
def send_patient_details(patient_id, patient_details):
# Publish the patient information to the doctor's topic
mqtt_client.publish(patient_id, patient_details)
try:
# Publish the patient information to the doctor's topic
mqtt_client.publish(patient_id, patient_details)
except Exception as e:
print("Exception occurred due to \"", e, "\"")
# MQTT client connection for publishing patient information
import paho.mqtt.client as mqtt
from scripts.constants.application_config import broker, mqtt_port, time_out
def mqtt_client_connection():
try:
mqtt_client = mqtt.Client()
mqtt_client.connect(broker, int(mqtt_port), int(time_out))
return mqtt_client
except Exception as e:
print("Exception occurred due to \"", e, "\"")
# Importing redis library
import redis
from scripts.constants.application_config import host, redis_port, cache_db
def establish_redis_connection():
try:
connection = redis.StrictRedis(host=host, port=int(redis_port), db=int(cache_db))
return connection
except Exception as e:
print("Exception occurred due to \"", e, "\"")
import json
from scripts.constants.handle_patients import handle_new_patient, send_patient_details
i = 1
while i:
print("------Select one Option------\n"
"1.Consult Doctor\n"
"2.Exit")
choice = int(input("Enter your choice:"))
if choice == 1:
print("------Enter patient information------")
patient_id = input("Enter patient id: ")
description = input("Enter the description: ")
age = int(input("Enter the age: "))
name = input("Enter the name: ")
patient_details = {
"description": description,
"age": age,
"name": name
}
# Send patient details for Publishing
send_patient_details(patient_id, json.dumps(patient_details))
# Handle patients using Round Robin Method
handle_new_patient(patient_id)
def start_service(app_api):
@app_api.post("/consultation/", tags="Consultation")
def consultation():
i = 1
while i:
print("------Select one Option------\n"
"1.Consult Doctor\n"
"2.Exit")
choice = int(input("Enter your choice:"))
if choice == 1:
print("------Enter patient information------")
patient_id = input("Enter patient id: ")
description = input("Enter the description: ")
age = int(input("Enter the age: "))
name = input("Enter the name: ")
patient_details = {
"description": description,
"age": age,
"name": name
}
# Send patient details for Publishing
send_patient_details(patient_id, json.dumps(patient_details))
# Handle patients using Round Robin Method
handle_new_patient(patient_id)
elif choice == 2:
i = 0
from fastapi import FastAPI, UploadFile
import redis
import paho.mqtt.client as mqtt
from scripts.database.models import PatientDetails
from fastapi import FastAPI
from scripts.core.handlers.patient_information import start_service
# Create FastAPI instance
app = FastAPI()
......@@ -14,7 +11,4 @@ async def root():
return {"INFO": "It's Working!"}
@app.post("/add-patient/{patient_id}", tags=["add patient"])
async def add_patient(patient_id: str, patient_details: PatientDetails):
return
start_service(app)
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