Commit 15f50cd9 by ajil.k

added

parent 357bb425
[FastAPI]
link_to_fastapi = scripts.services.main:app
port = 8000
[fastapi]
link_to_fastapi =scripts.services.main:app
port_no =8000
[mqtts]
broker = broker.emqx.io
port = 1883
time_out = 60
\ No newline at end of file
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")
except Exception as e:
print("Exception-", e)
#port_no = config.get("fastapi", "port_no")
#link_to_fastapi = config.get("fastapi", "link_to_fastapi")
config = ConfigParser()
config.read(f"conf/application.conf")
port_no = config.get("FastAPI", "port")
link_to_fastapi = config.get("FastAPI", "link_to_fastapi")
import redis
import paho.mqtt.client as mqtt
# Initialize the Redis cache
redis_cache = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
# MQTT client connection for publishing patient information
mqtt_client = mqtt.Client()
mqtt_client.connect("broker.emqx.io", 1883, 60)
# 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)
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.")
else:
# If the patient is new, assign a doctor using round-robin
assign_doctor(patient_id)
def send_patient_details(patient_id, patient_details):
# Publish the patient information to the doctor's topic
mqtt_client.publish(patient_id, patient_details)
import redis
import paho.mqtt.client as mqtt
"""
# Redis connection
redis_instance = redis.Redis(host='127.0.0.1', port=6379, db=0)
# MQTT connection
client = mqtt.Client()
client.connect("192.168.0.220", 1883, 60)
# Number of doctors
n = 10
def assign_patient(patient_id):
# Check if patient has a prior visit
doctor_id = redis_instance.get(patient_id)
if doctor_id is None:
# Assign patient to next doctor in round-robin fashion
doctor_id = redis_instance.incr("next_doctor_id") % n
else:
# Convert doctor_id from bytes to int
doctor_id = int(doctor_id)
# Store patient-doctor mapping in Redis
redis_instance.set(patient_id, doctor_id)
# Publish patient information to MQTT
patient_info = {"patient_id": patient_id, "doctor_id": doctor_id}
client.publish("patients", patient_info)
# Example usage
assign_patient("patient1")
assign_patient("patient2")
assign_patient("patient1")
# Redis connection
redis_instance = redis.Redis(host='localhost', port=6379, db=0)
# MQTT connection
client = mqtt.Client()
client.connect("192.168.0.220", 1883, 60)
# Global counter to keep track of which doctor to assign next
counter = 0
def on_connect(client, userdata, flags, rc):
print("Connected to Hospital with op no. " + str(rc))
# Subscribing to the patient information topic
client.subscribe("patient_info")
def on_message(client, userdata, msg):
global counter
# Extract patient ID from the message
patient_id = msg.payload.decode()
# Check if patient has a prior visit
doctor = redis_instance.get(patient_id)
# If patient is new, assign to the next doctor in round-robin fashion
if doctor is None:
doctor = counter
counter = (counter + 1) % n
else:
doctor = int(doctor)
# Store the mapping in Redis
print(patient_id, ":", doctor)
redis_instance.set(patient_id, doctor)
# Publish the doctor information to MQTT
print("doctor_assigned", str(doctor))
client.publish("doctor_assigned", str(doctor))
# Number of doctors on duty
n = 10
# Set up MQTT client
client.on_connect = on_connect
client.on_message = on_message
# Start MQTT client
client.loop_forever()
r = redis.Redis(host='localhost', port=6379, db=0)
r.set("key", "value")
value = r.get("key")
print(value)"""
"""import redis
import paho.mqtt.client as mqtt
# Connect to Redis server
r = redis.Redis(host='localhost', port=6379, db=0)
# Set the number of doctors
N = 10
# Keep track of the current doctor in the round-robin fashion
current_doctor = 0
# MQTT client to publish the patient information
client = mqtt.Client()
client.connect("192.168.0.220", 1883, 60)
def assign_doctor(patient_id):
# Check if the patient has a prior visit
doctor_id = r.get(patient_id)
if doctor_id:
# If the patient has a prior visit, assign the same doctor
doctor_id = int(doctor_id)
else:
# If the patient is a new patient, assign the next doctor in the round-robin fashion
global current_doctor
doctor_id = current_doctor
current_doctor = (current_doctor + 1) % N
# Cache the patient-doctor mapping
r.set(patient_id, doctor_id)
return doctor_id
def handle_new_patient(patient_id):
doctor_id = assign_doctor(patient_id)
# Publish the patient information to MQTT topic
client.publish("hospital/patients", f"Patient {patient_id} assigned to Doctor {doctor_id}")"""
# Connect to Redis server
r = redis.Redis(host='localhost', port=6379, db=0)
# Set the number of doctors
N = 10
# Keep track of the current doctor in the round-robin fashion
current_doctor = 0
# MQTT client to subscribe to a topic
client = mqtt.Client()
def on_message(client, userdata, message):
patient_id = int(message.payload.decode())
doctor_id = assign_doctor(patient_id)
print(f"Patient {patient_id} assigned to Doctor {doctor_id}")
def assign_doctor(patient_id):
# Check if the patient has a prior visit
doctor_id = r.get(patient_id)
if doctor_id:
# If the patient has a prior visit, assign the same doctor
doctor_id = int(doctor_id)
else:
# If the patient is a new patient, assign the next doctor in the round-robin fashion
global current_doctor
doctor_id = current_doctor
current_doctor = (current_doctor + 1) % N
# Cache the patient-doctor mapping
r.set(patient_id, doctor_id)
return doctor_id
# Set the callback for received messages
client.on_message = on_message
client.connect("192.168.0.220", 1883, 60)
client.subscribe("hospital/patients")
# Start the MQTT client loop to receive messages
client.loop_forever()
import json
import paho.mqtt.client as mqtt
import redis
from fastapi import FastAPI, UploadFile
app = FastAPI()
# red = redis_connection()
@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))
# Connect to Redis
redis_client = redis.Redis(host='127.0.0.1', port=6379, db=0)
# redis_client.set("current_doctor",0)
# Function to assign patient to doctor using Round Robin strategy
# Function to assign patient to doctor using Round Robin strategy
def assign_patient(patient_id, n_doctors):
current_doctor = redis_client.get("current_doctor")
patient_key = "patient:{}".format(patient_id)
patient_doctor = int(redis_client.get(patient_key)) if redis_client.get(patient_key) else current_doctor
assigned_doctor = int(patient_doctor) + 1 if int(patient_doctor) + 1 <= n_doctors else 1
redis_client.set(patient_key, assigned_doctor)
redis_client.set("current_doctor", assigned_doctor)
return assigned_doctor
# Publish patient information to MQTT
def publish_patient_info(patient_id, assigned_doctor):
# MQTT client setup
mqtt_client = mqtt.Client()
mqtt_client.connect("192.168.0.220", 1883)
patient_info = "Patient {} assigned to doctor {}".format(patient_id, assigned_doctor)
mqtt_client.publish("hospital/patients", patient_info)
# Example usage
n_doctors = 5
patient_id = "12345"
assigned_doctor = assign_patient(patient_id, n_doctors)
publish_patient_info(patient_id, assigned_doctor)
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)
from pydantic import BaseModel
class PatientDetails(BaseModel):
patient_id: str
description: str
age: int
name: str
......@@ -2,6 +2,7 @@ from fastapi import FastAPI, UploadFile
import redis
import paho.mqtt.client as mqtt
from scripts.database.models import PatientDetails
# Create FastAPI instance
app = FastAPI()
......@@ -13,64 +14,7 @@ async def root():
return {"INFO": "It's Working!"}
@app.post("/upload-patient-info/")
async def upload_file_json(file: UploadFile):
data = file.file.read()
print(data.decode())
return {"filename": file.filename}
@app.post("/add-patient/{patient_id}", tags=["add patient"])
async def add_patient(patient_id: str, patient_details: PatientDetails):
"""@app.post("/redis-operation/")
async def redis_operation():
return """
"""
# Redis connection
redis_instance = redis.Redis(host='localhost', port=6379, db=0)
# MQTT connection
client = mqtt.Client()
client.connect("192.168.0.220", 1883, 60)
# Global counter to keep track of which doctor to assign next
counter = 0
def on_connect(client, userdata, flags, rc):
print("Connected to Hospital with op no. " + str(rc))
# Subscribing to the patient information topic
client.subscribe("patient_info")
def on_message(client, userdata, msg):
global counter
# Extract patient ID from the message
patient_id = msg.payload.decode()
# Check if patient has a prior visit
doctor = redis_instance.get(patient_id)
# If patient is new, assign to the next doctor in round-robin fashion
if doctor is None:
doctor = counter
counter = (counter + 1) % n
else:
doctor = int(doctor)
# Store the mapping in Redis
redis_instance.set(patient_id, doctor)
# Publish the doctor information to MQTT
client.publish("doctor_assigned", str(doctor))
# Number of doctors on duty
n = 5
# Set up MQTT client
client.on_connect = on_connect
client.on_message = on_message
# Start MQTT client
client.loop_forever()
"""
\ No newline at end of file
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