Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
redisConsumer
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
mohammed.shibili
redisConsumer
Commits
2421b804
Commit
2421b804
authored
Feb 03, 2023
by
mohammed.shibili
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
corrections done
parent
852bad86
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
27 deletions
+35
-27
.idea/sonarlint/issuestore/index.pb
+8
-0
app.py
+3
-3
script/core/handlers/mqtt_functions.py
+7
-0
script/service/main.py
+17
-24
No files found.
.idea/sonarlint/issuestore/index.pb
View file @
2421b804
No preview for this file type
app.py
View file @
2421b804
from
script.service.main
import
run
from
script.service.main
import
run
if
__name__
==
"__main__"
:
run
()
run
()
\ No newline at end of file
\ No newline at end of file
script/core/handlers/mqtt_functions.py
0 → 100644
View file @
2421b804
class
MqttFunctions
:
@staticmethod
def
connection_msg
(
rc
):
if
rc
==
0
:
print
(
"Connected to MQTT Broker!"
)
else
:
print
(
"Failed to connect, return code
%
d
\n
"
,
rc
)
script/service/main.py
View file @
2421b804
import
json
import
json
import
random
import
redis
as
redis
import
redis
from
paho.mqtt
import
client
as
mqtt_client
from
paho.mqtt
import
client
as
mqtt_client
from
script.config.appconf
import
mqtt_broker
,
mqtt_port
,
redis_host
,
redis_port
,
redis_db2
from
script.core.handlers.mqtt_functions
import
MqttFunctions
from
script.config.appconf
import
redis_host
,
redis_port
,
redis_db
,
mqtt_broker
,
mqtt_port
,
redis_db2
broker
=
mqtt_broker
port
=
mqtt_port
r
=
redis
.
Redis
(
host
=
redis_host
,
port
=
redis_port
,
db
=
redis_db
)
r2
=
redis
.
Redis
(
host
=
redis_host
,
port
=
redis_port
,
db
=
redis_db2
)
r2
=
redis
.
Redis
(
host
=
redis_host
,
port
=
redis_port
,
db
=
redis_db2
)
topic_from_redis
=
r
.
lrange
(
"available_doctors"
,
0
,
-
1
)
topic
=
[
values
.
decode
()
for
values
in
topic_from_redis
]
# generate client ID with pub prefix randomly
client_id
=
f
'python-mqtt-{random.randint(0, 100)}'
def
connect_mqtt
()
->
mqtt_client
:
def
connect_mqtt
():
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
if
rc
==
0
:
MqttFunctions
()
.
connection_msg
(
rc
)
print
(
"Connected to MQTT Broker!"
)
else
:
print
(
"Failed to connect, return code
%
d
\n
"
,
rc
)
client
=
mqtt_client
.
Client
(
client_id
)
client
=
mqtt_client
.
Client
()
client
.
on_connect
=
on_connect
client
.
on_connect
=
on_connect
client
.
connect
(
broker
,
port
)
client
.
connect
(
mqtt_broker
,
mqtt_
port
)
return
client
return
client
...
@@ -35,12 +24,16 @@ def subscribe(client: mqtt_client):
...
@@ -35,12 +24,16 @@ def subscribe(client: mqtt_client):
value
=
json
.
loads
(
msg
.
payload
.
decode
())
value
=
json
.
loads
(
msg
.
payload
.
decode
())
key
=
value
[
"patient_id"
]
key
=
value
[
"patient_id"
]
r2
.
set
(
key
,
msg
.
payload
.
decode
())
r2
.
set
(
key
,
msg
.
payload
.
decode
())
for
new_topic
in
topic
:
client
.
subscribe
(
new_topic
)
# for wild card:
client
.
on_message
=
on_message
client
.
subscribe
(
"KL/#"
)
client
.
on_message
=
on_message
def
run
():
def
run
():
client
=
connect_mqtt
()
try
:
subscribe
(
client
)
client
=
connect_mqtt
()
client
.
loop_forever
()
subscribe
(
client
)
client
.
loop_forever
()
except
KeyboardInterrupt
:
client
.
loop_stop
()
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