Commit 0554b09e by ajil.k

updated

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