Commit 6599eb68 by arjun.b

MQTT publish completed

parent b491c1f3
import json
import paho.mqtt.client as mqtt
import redis import redis
from fastapi import FastAPI, UploadFile from paho.mqtt import client as mqtt_client
import json
from scripts.config.application_config import hostname, port_num
app = FastAPI()
@app.get("/", tags=["Root"])
async def root():
return {"data": "MQTT-redis task"}
# upload patient details
@app.post("/upload_patient_details", tags=["patient"])
async def upload_file(file: UploadFile):
try:
file_content = await file.read()
file_content_str = file_content.decode()
json_data = json.loads(file_content_str)
return {"file_name": json_data}
except Exception as e:
print(str(e))
broker = '192.168.0.220'
port = 1883
topic = "hospital/patients"
client = mqtt_client.Client()
client.connect(broker, port, 60)
# Connect to Redis # Connect to Redis
redis_client = redis.Redis(host='127.0.0.1', port=6379, db=0) r = redis.Redis(host='127.0.0.1', port=6379, db=0)
# redis_client.set("current_doctor",0) def get_next_doctor(doctors):
# Get the current doctor index from Redis
current_doctor = int(r.get('current_doctor') or 0)
# Function to assign patient to doctor using Round Robin strategy
def assign_patient(patient_id, n_doctors): # Update the current doctor index in Redis
current_doctor = redis_client.get("current_doctor") r.set('current_doctor', (current_doctor + 1) % len(doctors))
patient_key = "patient:{}".format(patient_id)
patient_doctor = int(redis_client.get(patient_key)) if redis_client.get(patient_key) else current_doctor # Return the next doctor
assigned_doctor = int(patient_doctor + 1) if int(patient_doctor) + 1 <= n_doctors else 1 return doctors[current_doctor]
redis_client.set(patient_key, assigned_doctor)
redis_client.set("current_doctor", assigned_doctor)
return assigned_doctor def assign_patient(patient, doctors):
# Get the next doctor for the patient
next_doctor = get_next_doctor(doctors)
# Publish patient information to MQTT
def publish_patient_info(patient_id, assigned_doctor): # Check if the patient has a assigned doctor
# MQTT client setup if r.hexists(patient['patient_id'], 'doctor'):
mqtt_client = mqtt.Client() # Get the assigned doctor for the patient
mqtt_client.connect(hostname, int(port_num)) assigned_doctor = r.hget(patient['patient_id'], 'doctor').decode('utf-8')
patient_info = "Patient {} assigned to doctor {}".format(patient_id, assigned_doctor)
mqtt_client.publish("hospital/patients", patient_info) # Check if the assigned doctor is still on duty
if assigned_doctor in doctors:
# Assign the patient to the same doctor
# Example usage next_doctor = assigned_doctor
n_doctors = 5
patient_id = "12345" # Assign the patient to the next doctor
assigned_doctor = assign_patient(patient_id, n_doctors) patient.update({"doctor":next_doctor})
publish_patient_info(patient_id, assigned_doctor) r.hset(patient['patient_id'], 'doctor', next_doctor)
client.publish(topic, json.dumps(patient))
def get_patient_info(patient):
# Check if the patient exists in Redis
if r.exists(patient):
# Get the patient information
doctor = r.hget(patient, 'doctor').decode('utf-8')
return f'{patient} has been assigned to {doctor}'
else:
return f'{patient} has not been assigned to any doctor'
# # Example usage
doctors = ['Dr. Smith', 'Dr. Johnson', 'Dr. Brown', "Dr. Arjun B"]
i = 1
while i:
print("1.Consult Doctor\n""2.Exit")
choice = int(input("Enter your choice:"))
if choice == 1:
patient_id = input("Enter patient id: ")
description = input("Enter the description: ")
age = int(input("Enter the age: "))
name = input("Enter the name: ")
patient_details = {
"patient_id": patient_id,
"description": description,
"age": age,
"name": name
}
assign_patient(patient_details, doctors)
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