Commit 357bb425 by ajil.k

added

parents
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="dict.mobile" />
<option value="dict.dob" />
<option value="dict.gender" />
<option value="dict.name" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (redis_mqtt_task)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/redis_mqtt_task.iml" filepath="$PROJECT_DIR$/.idea/redis_mqtt_task.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
# importing libraries
import uvicorn
from scripts.constants.application_config import port_no, link_to_fastapi
if __name__ == "__main__":
try:
uvicorn.run(link_to_fastapi, port=int(port_no), reload=True)
except Exception as e:
print("Error-", e)
[FastAPI]
link_to_fastapi = scripts.services.main:app
port = 8000
from configparser import ConfigParser
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
"""
# 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 fastapi import FastAPI, UploadFile
import redis
import paho.mqtt.client as mqtt
# Create FastAPI instance
app = FastAPI()
# Setting Root
@app.get("/")
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("/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
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