Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
redis_mqtt_task
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
ajil.k
redis_mqtt_task
Commits
0554b09e
Commit
0554b09e
authored
Feb 02, 2023
by
ajil.k
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated
parent
15f50cd9
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
108 additions
and
63 deletions
+108
-63
conf/application.conf
+10
-4
scripts/constants/application_config.py
+13
-5
scripts/constants/handle_patients.py
+32
-23
scripts/constants/mqtt_connection.py
+12
-0
scripts/constants/redis_connection.py
+11
-0
scripts/core/handlers/patient_information.py
+27
-22
scripts/services/main.py
+3
-9
No files found.
conf/application.conf
View file @
0554b09e
[
fastapi
]
[
fastapi
_connection
]
link_to_fastapi
=
scripts
.
services
.
main
:
app
link_to_fastapi
=
scripts
.
services
.
main
:
app
port_no
=
8000
port_no
=
8000
[
mqtts
]
[
MQTT_Connection
]
broker
=
broker
.
emqx
.
io
broker
=
broker
.
emqx
.
io
port
=
1883
port
=
1883
time_out
=
60
time_out
=
60
\ No newline at end of file
[
redis_connection
]
host
=
127
.
0
.
0
.
1
redis_port
=
6379
cache_db
=
0
patient_db
=
2
\ No newline at end of file
scripts/constants/application_config.py
View file @
0554b09e
...
@@ -3,15 +3,23 @@ from configparser import ConfigParser
...
@@ -3,15 +3,23 @@ from configparser import ConfigParser
try
:
try
:
config
=
ConfigParser
()
config
=
ConfigParser
()
config
.
read
(
"conf/application.conf"
)
config
.
read
(
"conf/application.conf"
)
broker
=
config
.
get
(
"MQTT"
,
"broker"
)
mqtt_port
=
config
.
get
(
"MQTT"
,
"port"
)
port_no
=
config
.
get
(
"fastapi_connection"
,
"port_no"
)
time_out
=
config
.
get
(
"MQTT"
,
"time_out"
)
link_to_fastapi
=
config
.
get
(
"fastapi_connection"
,
"link_to_fastapi"
)
broker
=
config
.
get
(
"MQTT_Connection"
,
"broker"
)
mqtt_port
=
config
.
get
(
"MQTT_Connection"
,
"port"
)
time_out
=
config
.
get
(
"MQTT_Connection"
,
"time_out"
)
host
=
config
.
get
(
"redis_connection"
,
"host"
)
redis_port
=
config
.
get
(
"redis_connection"
,
"redis_port"
)
cache_db
=
config
.
get
(
"redis_connection"
,
"cache_db"
)
patient_db
=
config
.
get
(
"redis_connection"
,
"patient_db"
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
"Exception-"
,
e
)
print
(
"Exception-"
,
e
)
#port_no = config.get("fastapi", "port_no")
#link_to_fastapi = config.get("fastapi", "link_to_fastapi")
scripts/constants/handle_patients.py
View file @
0554b09e
import
redis
from
scripts.constants.mqtt_connection
import
mqtt_client_connection
import
paho.mqtt.client
as
mqtt
from
scripts.constants.redis_connection
import
establish_redis_connection
# Initialize the Redis cache
# Initialize the Redis cache
redis_c
ache
=
redis
.
StrictRedis
(
host
=
'127.0.0.1'
,
port
=
6379
,
db
=
0
)
redis_c
onnection
=
establish_redis_connection
(
)
# MQTT client connection for publishing patient information
# MQTT client connection for publishing patient information
mqtt_client
=
mqtt
.
Client
()
mqtt_client
=
mqtt_client_connection
()
mqtt_client
.
connect
(
"broker.emqx.io"
,
1883
,
60
)
# Counter to keep track of the next doctor to assign
# Counter to keep track of the next doctor to assign
counter
=
0
counter
=
0
# No. of doctors
# No. of doctors
n
=
int
(
input
(
"Enter the number of doctors: "
))
n
=
int
(
input
(
"Enter the number of doctors: "
))
def
assign_doctor
(
patient_id
):
def
assign_doctor
(
patient_id
):
global
counter
try
:
# Increment the counter and wrap around if it exceeds n
global
counter
counter
=
(
counter
+
1
)
%
n
# Increment the counter and wrap around if it exceeds n
doctor_topic
=
"doctor"
+
str
(
counter
)
counter
=
(
counter
+
1
)
%
n
# Store the doctor's topic in the Redis cache for the patient
doctor_topic
=
"doctor"
+
str
(
counter
)
redis_cache
.
set
(
patient_id
,
doctor_topic
)
# Store the doctor's topic in the Redis cache for the patient
print
(
patient_id
,
" assigned to "
,
doctor_topic
)
redis_connection
.
set
(
patient_id
,
doctor_topic
)
print
(
patient_id
,
" assigned to "
,
doctor_topic
)
except
Exception
as
e
:
print
(
"Exception occurred due to
\"
"
,
e
,
"
\"
"
)
def
handle_new_patient
(
patient_id
):
def
handle_new_patient
(
patient_id
):
# Check if the patient has visited before
try
:
all_patient_id
=
redis_cache
.
keys
()
# Check if the patient has visited before
ids
=
[
each
.
decode
()
for
each
in
all_patient_id
]
all_patient_id
=
redis_connection
.
keys
()
if
patient_id
in
ids
:
ids
=
[
each
.
decode
()
for
each
in
all_patient_id
]
doctor_topic
=
redis_cache
.
get
(
patient_id
)
if
patient_id
in
ids
:
print
(
patient_id
,
" consulted "
,
doctor_topic
.
decode
(),
" before."
)
doctor_topic
=
redis_connection
.
get
(
patient_id
)
print
(
patient_id
,
" consulted "
,
doctor_topic
.
decode
(),
" before."
)
else
:
else
:
# If the patient is new, assign a doctor using round-robin
# If the patient is new, assign a doctor using round-robin
assign_doctor
(
patient_id
)
assign_doctor
(
patient_id
)
except
Exception
as
e
:
print
(
"Exception occurred due to
\"
"
,
e
,
"
\"
"
)
def
send_patient_details
(
patient_id
,
patient_details
):
def
send_patient_details
(
patient_id
,
patient_details
):
# Publish the patient information to the doctor's topic
try
:
mqtt_client
.
publish
(
patient_id
,
patient_details
)
# Publish the patient information to the doctor's topic
mqtt_client
.
publish
(
patient_id
,
patient_details
)
except
Exception
as
e
:
print
(
"Exception occurred due to
\"
"
,
e
,
"
\"
"
)
scripts/constants/mqtt_connection.py
View file @
0554b09e
# MQTT client connection for publishing patient information
import
paho.mqtt.client
as
mqtt
from
scripts.constants.application_config
import
broker
,
mqtt_port
,
time_out
def
mqtt_client_connection
():
try
:
mqtt_client
=
mqtt
.
Client
()
mqtt_client
.
connect
(
broker
,
int
(
mqtt_port
),
int
(
time_out
))
return
mqtt_client
except
Exception
as
e
:
print
(
"Exception occurred due to
\"
"
,
e
,
"
\"
"
)
scripts/constants/redis_connection.py
0 → 100644
View file @
0554b09e
# Importing redis library
import
redis
from
scripts.constants.application_config
import
host
,
redis_port
,
cache_db
def
establish_redis_connection
():
try
:
connection
=
redis
.
StrictRedis
(
host
=
host
,
port
=
int
(
redis_port
),
db
=
int
(
cache_db
))
return
connection
except
Exception
as
e
:
print
(
"Exception occurred due to
\"
"
,
e
,
"
\"
"
)
scripts/core/handlers/patient_information.py
View file @
0554b09e
import
json
import
json
from
scripts.constants.handle_patients
import
handle_new_patient
,
send_patient_details
from
scripts.constants.handle_patients
import
handle_new_patient
,
send_patient_details
i
=
1
while
i
:
def
start_service
(
app_api
):
print
(
"------Select one Option------
\n
"
@app_api.post
(
"/consultation/"
,
tags
=
"Consultation"
)
"1.Consult Doctor
\n
"
def
consultation
():
"2.Exit"
)
i
=
1
choice
=
int
(
input
(
"Enter your choice:"
))
while
i
:
if
choice
==
1
:
print
(
"------Select one Option------
\n
"
print
(
"------Enter patient information------"
)
"1.Consult Doctor
\n
"
patient_id
=
input
(
"Enter patient id: "
)
"2.Exit"
)
description
=
input
(
"Enter the description: "
)
choice
=
int
(
input
(
"Enter your choice:"
))
age
=
int
(
input
(
"Enter the age: "
))
if
choice
==
1
:
name
=
input
(
"Enter the name: "
)
print
(
"------Enter patient information------"
)
patient_details
=
{
patient_id
=
input
(
"Enter patient id: "
)
"description"
:
description
,
description
=
input
(
"Enter the description: "
)
"age"
:
age
,
age
=
int
(
input
(
"Enter the age: "
))
"name"
:
name
name
=
input
(
"Enter the name: "
)
}
patient_details
=
{
# Send patient details for Publishing
"description"
:
description
,
send_patient_details
(
patient_id
,
json
.
dumps
(
patient_details
))
"age"
:
age
,
# Handle patients using Round Robin Method
"name"
:
name
handle_new_patient
(
patient_id
)
}
# 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
)
elif
choice
==
2
:
i
=
0
scripts/services/main.py
View file @
0554b09e
from
fastapi
import
FastAPI
,
UploadFile
from
fastapi
import
FastAPI
import
redis
from
scripts.core.handlers.patient_information
import
start_service
import
paho.mqtt.client
as
mqtt
from
scripts.database.models
import
PatientDetails
# Create FastAPI instance
# Create FastAPI instance
app
=
FastAPI
()
app
=
FastAPI
()
...
@@ -14,7 +11,4 @@ async def root():
...
@@ -14,7 +11,4 @@ async def root():
return
{
"INFO"
:
"It's Working!"
}
return
{
"INFO"
:
"It's Working!"
}
@app.post
(
"/add-patient/{patient_id}"
,
tags
=
[
"add patient"
])
start_service
(
app
)
async
def
add_patient
(
patient_id
:
str
,
patient_details
:
PatientDetails
):
return
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