Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
MQTT-Redis
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
arjun.b
MQTT-Redis
Commits
6599eb68
Commit
6599eb68
authored
Feb 01, 2023
by
arjun.b
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MQTT publish completed
parent
b491c1f3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
57 deletions
+81
-57
.idea/sonarlint/issuestore/index.pb
+12
-0
scripts/core/handlers/patient.py
+0
-0
scripts/services/main.py
+69
-57
No files found.
.idea/sonarlint/issuestore/index.pb
View file @
6599eb68
No preview for this file type
scripts/core/handlers/patient.py
0 → 100644
View file @
6599eb68
scripts/services/main.py
View file @
6599eb68
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
))
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
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
)
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment