Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CyanLight
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
CyanLight
Commits
5bf80130
Commit
5bf80130
authored
3 years ago
by
fxk8y
Browse files
Options
Downloads
Patches
Plain Diff
Removing requests; taking Subscriptions from SpiderMQTT
parent
409dcde0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
SiliconTorch/DecoratorTest.py
+0
-1
0 additions, 1 deletion
SiliconTorch/DecoratorTest.py
SiliconTorch/MQTT.py
+66
-33
66 additions, 33 deletions
SiliconTorch/MQTT.py
with
66 additions
and
34 deletions
SiliconTorch/DecoratorTest.py
+
0
−
1
View file @
5bf80130
class
MyDec
:
def
__init__
(
*
args
,
**
kwargs
):
...
...
This diff is collapsed.
Click to expand it.
SiliconTorch/MQTT.py
+
66
−
33
View file @
5bf80130
...
...
@@ -249,36 +249,73 @@ class Executor:
pass
# TODO: logging!!!
class
SpiderMQTT
:
# TODO: Move into SpiderMQTT class!
class
Subscription
:
class
Request
:
class
ACTIVE
:
pass
class
PENDING
:
pass
class
CLEANUP
:
pass
def
__init__
(
self
,
spider
,
requestTopic
,
responseTopic
,
payload
,
callback
,
pub_qos
):
self
.
spider
=
spider
self
.
pub_qos
=
pub_qos
self
.
payload
=
payload
self
.
callback
=
callback
self
.
requestTopic
=
Topic
(
requestTopic
)
self
.
responseTopic
=
Topic
(
responseTopic
)
def
__init__
(
self
,
spider
,
topic
,
callback
=
None
,
subscribeCallback
=
None
):
self
.
spider
=
spider
self
.
topic
=
Topic
(
topic
)
self
.
state
=
self
.
PENDING
self
.
callbacks
=
set
()
self
.
subscribeCallbacks
=
set
()
self
.
__msgCallback
=
self
.
onMessage
self
.
__sub
Callback
=
self
.
onSubscribe
if
callback
is
not
None
:
self
.
add
Callback
(
callback
)
if
s
elf
.
requestTopic
.
containsWildcard
()
:
raise
ValueError
(
'
requestTopic mustn
\'
t contain any wildcards
'
)
if
s
ubscribeCallback
is
not
None
:
self
.
addSubscribeCallback
(
subscribeCallback
)
if
self
.
spider
.
isConnected
():
self
.
spider
.
addCallback
(
self
.
__msgCallback
,
self
.
__subCallback
)
else
:
pass
# TODO: figure out what to do...
self
.
__sub
()
def
__sub
(
self
):
if
self
.
state
is
self
.
PENDING
and
self
.
spider
.
isConnected
():
self
.
sub_mid
=
self
.
spider
.
mqtt
.
subscribe
(
self
.
topic
,
self
.
spider
.
sub_qos
)[
1
]
def
__unsub
(
self
):
pass
# TODO: implement!
def
onSubscribe
(
self
,
mid
):
print
(
'
onSubscribe[mid={}]: self.mid={} matching={}
'
.
format
(
mid
,
self
.
sub_mid
,
self
.
sub_mid
==
mid
))
if
hasattr
(
self
,
'
sub_mid
'
)
and
self
.
sub_mid
==
mid
:
self
.
state
=
self
.
ACTIVE
del
self
.
sub_mid
Executor
(
self
.
subscribeCallbacks
)
def
onMessage
(
self
,
msg
):
if
self
.
topic
.
compare
(
msg
.
topic
):
Executor
(
self
.
callbacks
,
msg
)
def
onConnect
(
self
):
self
.
__sub
()
def
on
Subscribe
(
self
):
self
.
s
pider
.
publish
(
self
.
requestTopic
,
self
.
payload
,
qos
=
pub_qos
,
retain
=
False
)
def
on
Disconnect
(
self
):
self
.
s
tate
=
self
.
PENDING
def
onMessage
(
self
,
msg
):
self
.
spider
.
removeCallback
(
self
.
__msgCallback
,
self
.
__subCallback
)
self
.
spider
.
requests
.
remove
(
self
)
Executor
(
self
.
callback
,
msg
)
def
addCallback
(
self
,
callback
):
self
.
callbacks
.
add
(
callback
)
def
removeCallback
(
self
,
callback
):
if
callback
in
self
.
callbacks
:
self
.
callbacks
.
remove
(
callback
)
if
self
.
callbacks
==
set
():
self
.
state
=
self
.
CLEANUP
self
.
__unsub
()
def
addSubscribeCallbacks
(
self
,
callback
):
self
.
subscribeCallbacks
.
add
(
callback
)
if
self
.
state
is
self
.
ACTIVE
:
Executor
(
callback
)
def
removeSubscribeCallbacks
(
self
,
callback
):
if
callback
in
self
.
subscribeCallbacks
:
self
.
subscribeCallbacks
.
remove
(
callback
)
class
SpiderMQTT
:
def
__init__
(
self
,
broker
:
str
,
port
:
int
=
1883
,
user
:
str
=
None
,
password
:
str
=
None
,
sub_qos
:
int
=
0
,
will_topic
:
str
=
None
,
will_payload
=
None
,
will_qos
:
int
=
0
,
will_retain
:
bool
=
False
,
backgroundTask
:
bool
=
True
):
...
...
@@ -286,7 +323,7 @@ class SpiderMQTT:
self
.
sub_qos
=
sub_qos
self
.
connected
=
False
self
.
requests
=
set
()
#
self.requests = set()
self
.
subscriptions
=
{}
self
.
pendingMessages
=
[]
...
...
@@ -306,11 +343,11 @@ class SpiderMQTT:
self
.
mqtt
.
will_set
(
will_topic
,
will_payload
,
will_qos
,
will_retain
)
# 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
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
self
.
mqtt
.
on_unsubscribe
=
self
.
__onUnSubscribe
self
.
mqtt
.
connect
(
broker
,
port
)
...
...
@@ -352,10 +389,6 @@ class SpiderMQTT:
self
.
pendingMessages
+=
[
msg
]
def
request
(
self
,
requestTopic
,
responseTopic
,
payload
,
callback
,
pub_qos
=
0
):
self
.
requests
.
add
(
self
.
Request
(
self
,
requestTopic
,
responseTopic
,
payload
,
callback
,
pub_qos
))
# TODO: further actions needed????
def
__onMessage
(
self
,
_cl
,
_ud
,
msg
):
message
=
Message
(
msg
.
topic
,
msg
.
payload
)
...
...
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