Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SpiderMQTT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fxk8y
SpiderMQTT
Commits
05f55d73
Commit
05f55d73
authored
3 years ago
by
fxk8y
Browse files
Options
Downloads
Patches
Plain Diff
Implement more client logic
parent
f5a8ec87
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
SpiderMQTT.py
+40
-0
40 additions, 0 deletions
SpiderMQTT.py
with
40 additions
and
0 deletions
SpiderMQTT.py
+
40
−
0
View file @
05f55d73
...
...
@@ -307,7 +307,9 @@ class SpiderMQTT:
def
__init__
(
self
,
broker
:
str
,
port
:
int
=
1883
,
user
:
str
=
None
,
password
:
str
=
None
,
will_topic
:
str
=
None
,
will_payload
=
None
,
will_qos
:
int
=
0
,
will_retain
:
bool
=
False
):
self
.
connected
=
False
self
.
subscriptions
=
{}
self
.
pendingMessages
=
[]
self
.
mqtt
=
mqtt
.
Client
()
self
.
mqtt
.
reconnect_delay_set
(
1
,
1
)
...
...
@@ -320,6 +322,7 @@ class SpiderMQTT:
self
.
mqtt
.
on_log
=
self
.
onLog
self
.
mqtt
.
on_message
=
self
.
onMessage
self
.
mqtt
.
on_publish
=
self
.
onPublish
self
.
mqtt
.
on_connect
=
self
.
onConnect
self
.
mqtt
.
on_subscribe
=
self
.
onSubscribe
self
.
mqtt
.
on_disconnect
=
self
.
onDisconnect
...
...
@@ -327,6 +330,24 @@ class SpiderMQTT:
self
.
mqtt
.
connect
(
broker
,
port
)
self
.
mqtt
.
loop_forever
(
retry_first_connection
=
True
)
def
isConnected
(
self
):
return
self
.
connected
def
publish
(
self
,
topic
,
payload
=
None
,
qos
=
0
,
retain
=
False
):
if
type
(
payload
)
is
str
:
pl
=
message
.
encode
(
'
utf-8
'
)
else
:
pl
=
message
if
self
.
isConnecteed
():
self
.
mqtt
.
publish
(
topic
,
pl
,
qos
,
retain
)
else
:
msg
=
Message
(
topic
,
pl
)
msg
.
qos
=
qos
msg
.
retain
=
retain
self
.
pendingMessages
+=
[
msg
]
def
onMessage
(
self
,
_cl
,
_ud
,
msg
):
message
=
Message
(
msg
.
topic
,
msg
.
payload
)
...
...
@@ -334,10 +355,17 @@ class SpiderMQTT:
sub
.
onMessage
(
message
)
def
onConnect
(
self
,
*
ignored
):
self
.
connected
=
False
for
sub
in
self
.
subscriptions
.
values
():
sub
.
onConnect
()
for
msg
in
self
.
pendingMessages
.
values
():
msg
.
mid
=
self
.
mqtt
.
publish
(
msg
.
topic
,
msg
.
payload
,
msg
.
qos
,
msg
.
retain
).
mid
def
onDisconnect
(
self
,
*
ignored
):
self
.
connected
=
False
for
sub
in
self
.
subscriptions
.
values
():
sub
.
onDisconnect
()
...
...
@@ -345,9 +373,21 @@ class SpiderMQTT:
for
sub
in
self
.
subscriptions
.
values
():
sub
.
onSubscribe
(
mid
)
def
onPublish
(
self
,
_cl
,
_ud
,
mid
):
for
msg
in
self
.
pendingMessages
:
if
msg
.
mid
==
mid
:
self
.
pendingMessages
.
remove
(
msg
)
def
onLog
():
pass
def
removeCallback
(
self
,
callback
):
for
sub
in
self
.
subscriptions
:
sub
.
removeCallback
()
if
sub
.
callbacks
==
{}:
self
.
mqtt
.
unsubscribe
(
sub
.
topic
)
del
self
.
subscriptions
[
sub
.
topic
]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment