Commit b491c1f3 by arjun.b

task not completed

parents
# Default ignored files
/shelf/
/workspace.xml
<?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
<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>
</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 (MQTT-redis)" 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/MQTT-redis.iml" filepath="$PROJECT_DIR$/.idea/MQTT-redis.iml" />
</modules>
</component>
</project>
\ 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
import uvicorn as uvicorn
from scripts.config.application_config import server_path, port_no
if __name__ == "__main__":
try:
uvicorn.run(server_path, port=int(port_no), reload=True)
except Exception as e:
print(str(e))
[server]
server_path=scripts.services.main:app
port_no=8000
[MQTT]
host=192.168.0.220
port=1883
\ No newline at end of file
import configparser
try:
config = configparser.ConfigParser()
config.read("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")
except configparser.NoOptionError:
print("could not find conf file")
import redis
def redis_connection():
try:
conn = redis.Redis(host="localhost", port=6379, db=0)
return conn
except Exception as e:
print(str(e))
import json
import paho.mqtt.client as mqtt
import redis
from fastapi import FastAPI, UploadFile
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))
# 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
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(hostname, int(port_num))
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)
[
{
"patient_id": "101",
"description": "fever",
"age": 23,
"name": "Raju"
},
{
"patient_id": "102",
"description": "headache",
"age": 32,
"name": "John"
},
{
"patient_id": "103",
"description": "back pain",
"age": 45,
"name": "Jane"
},
{
"patient_id": "104",
"description": "allergies",
"age": 26,
"name": "Emma"
},
{
"patient_id": "105",
"description": "stomach ache",
"age": 18,
"name": "Sam"
}
]
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