Commit 746834e1 by arjun.b

structure changed

parent 6599eb68
[server]
server_path=scripts.services.main:app
server_path=scripts.services.publisher:app
port_no=8000
[MQTT]
[MQTT_connect]
host=192.168.0.220
port=1883
topic=hospital/patients
[redis_connect]
host=127.0.0.1
port=6379
\ No newline at end of file
......@@ -2,10 +2,13 @@ import configparser
try:
config = configparser.ConfigParser()
config.read("conf/application.conf")
config.read(r'E:\MQTT-redis\conf\application.conf')
server_path = config.get("server", "server_path")
port_no = config.get("server", "port_no")
hostname = config.get("MQTT", "host")
port_num = config.get("MQTT", "port")
hostname = config.get("MQTT_connect", "host")
port_num = config.get("MQTT_connect", "port")
topic_name = config.get("MQTT_connect", "topic")
redis_host = config.get("redis_connect", "host")
redis_port = config.get("redis_connect", "port")
except configparser.NoOptionError:
print("could not find conf file")
import logging
import redis
from scripts.config.application_config import redis_host, redis_port
try:
# Connect to Redis
r = redis.Redis(host=redis_host, port=int(redis_port), db=0)
def redis_connection():
try:
conn = redis.Redis(host="localhost", port=6379, db=0)
return conn
except Exception as e:
except Exception as e:
logging.error(f'cant connect to redis {e}')
print(str(e))
import json
from paho.mqtt import client as mqtt_client
from scripts.config.application_config import hostname, port_num, topic_name
from scripts.config.redis_connection import r
from scripts.core.handlers.get_next_doctor import get_next_doctor
broker = hostname
port = int(port_num)
topic = topic_name
client = mqtt_client.Client()
client.connect(broker, port, 60)
def assign_patient(patient, doctors):
# Get the next doctor for the patient
next_doctor = get_next_doctor(doctors)
# Check if the patient has an assigned doctor
if r.hexists(patient['patient_id'], 'doctor'):
# Get the assigned doctor for the patient
assigned_doctor = r.hget(patient['patient_id'], 'doctor').decode('utf-8')
# Check if the assigned doctor is still on duty
if assigned_doctor in doctors:
# Assign the patient to the same doctor
next_doctor = assigned_doctor
# Assign the patient to the next doctor
patient.update({"doctor": next_doctor})
r.hset(patient['patient_id'], 'doctor', next_doctor)
client.publish(topic, json.dumps(patient))
import logging
from scripts.config.redis_connection import r
def get_next_doctor(doctors):
try:
# Get the current doctor index from Redis
current_doctor = int(r.get('current_doctor') or 0)
# Update the current doctor index in Redis
r.set('current_doctor', (current_doctor + 1) % len(doctors))
# Return the next doctor
return doctors[current_doctor]
except Exception as e:
logging.error(f'cant find next doctor:{e}')
from scripts.core.handlers.assign_patient import assign_patient
class PublishHandler:
@staticmethod
def main_handler():
doctors = ['Dr. Smith', 'Dr. Johnson', 'Dr. Brown', "Dr. Arjun B"]
i = 1
while i:
print("1.Consult a 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)
import redis
from paho.mqtt import client as mqtt_client
import json
broker = '192.168.0.220'
port = 1883
topic = "hospital/patients"
client = mqtt_client.Client()
client.connect(broker, port, 60)
# Connect to Redis
r = redis.Redis(host='127.0.0.1', port=6379, db=0)
def get_next_doctor(doctors):
# Get the current doctor index from Redis
current_doctor = int(r.get('current_doctor') or 0)
# Update the current doctor index in Redis
r.set('current_doctor', (current_doctor + 1) % len(doctors))
# Return the next doctor
return doctors[current_doctor]
def assign_patient(patient, doctors):
# Get the next doctor for the patient
next_doctor = get_next_doctor(doctors)
# Check if the patient has a assigned doctor
if r.hexists(patient['patient_id'], 'doctor'):
# Get the assigned doctor for the patient
assigned_doctor = r.hget(patient['patient_id'], 'doctor').decode('utf-8')
# Check if the assigned doctor is still on duty
if assigned_doctor in doctors:
# Assign the patient to the same doctor
next_doctor = assigned_doctor
# Assign the patient to the next doctor
patient.update({"doctor":next_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)
import logging
from fastapi import FastAPI
from scripts.core.handlers.publisher_handler import PublishHandler
app = FastAPI()
@app.get("/", tags=["root"])
def root():
return {"data": "mqtt-redis publish"}
@app.post("/publish", tags=["assign doctor and publish"])
def publish_data():
try:
PublishHandler.main_handler()
except Exception as e:
logging.error(f'assign a doctor and publishing the details failed: {e}')
return {"data": "data published"}
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