Skip to content

Commit 65a29fe

Browse files
committed
umqtt.simple: remove debug printing and add debug wrapper for MQTTClient which can optionally be used with examples
1 parent d5a8533 commit 65a29fe

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

umqtt.simple/example_pub.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from umqtt.simple import MQTTClient
2+
#from umqtt_debug import DebugMQTTClient as MQTTClient
23

34
# Test reception e.g. with:
45
# mosquitto_sub -t foo_topic
56

6-
def main(server="localhost"):
7+
def main(server="localhost", topic="foo_topic", msg="hello", qos=0):
78
c = MQTTClient("umqtt_client", server)
89
c.connect()
9-
c.publish(b"foo_topic", b"hello")
10+
c.publish(topic.encode('utf-8'), msg.encode('utf-8'), qos=int(qos))
1011
c.disconnect()
1112

1213
if __name__ == "__main__":
13-
main()
14+
import sys
15+
main(*sys.argv[1:])

umqtt.simple/example_sub.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import time
22
from umqtt.simple import MQTTClient
3+
#from umqtt_debug import DebugMQTTClient as MQTTClient
34

45
# Publish test messages e.g. with:
56
# mosquitto_pub -t foo_topic -m hello
67

78
# Received messages from subscriptions will be delivered to this callback
89
def sub_cb(topic, msg):
910
print((topic, msg))
10-
11-
def main(server="localhost"):
11+
12+
def main(server="localhost", topic="foo_topic", qos=0, blocking=True):
1213
c = MQTTClient("umqtt_client", server)
1314
c.set_callback(sub_cb)
1415
c.connect()
15-
c.subscribe(b"foo_topic")
16+
c.subscribe(topic.encode('utf-8'), int(qos))
1617
while True:
17-
if True:
18+
if blocking:
1819
# Blocking wait for message
1920
c.wait_msg()
2021
else:
@@ -27,4 +28,5 @@ def main(server="localhost"):
2728
c.disconnect()
2829

2930
if __name__ == "__main__":
30-
main()
31+
import sys
32+
main(*sys.argv[1:])

umqtt.simple/umqtt/simple.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import usocket as socket
2-
#from ubinascii import hexlify
32

43
class MQTTException(Exception):
54
pass
@@ -89,9 +88,7 @@ def connect(self, clean_session=True):
8988
msg[7] |= self.lw_retain << 5
9089

9190
plen = self._varlen_encode(sz, premsg, 1)
92-
#print(hex(len(premsg[:plen])), hexlify(premsg[:plen], ":"))
9391
self.sock.write(premsg, plen)
94-
#print(hex(len(msg)), hexlify(msg, ":"))
9592
self.sock.write(msg)
9693
self._send_str(self.client_id)
9794
if self.lw_topic:
@@ -121,7 +118,6 @@ def publish(self, topic, msg, retain=False, qos=0):
121118
if qos > 0:
122119
sz += 2
123120
plen = self._varlen_encode(sz, pkt, 1)
124-
#print(hex(len(pkt[:plen])), hexlify(pkt[:plen], ":"))
125121
self.sock.write(pkt, plen)
126122
self._send_str(topic)
127123
if qos > 0:
@@ -148,15 +144,13 @@ def subscribe(self, topic, qos=0):
148144
sz = 2 + 2 + len(topic) + 1
149145
plen = self._varlen_encode(sz, pkt, 1)
150146
pkt[plen:plen + 2] = self.pid.to_bytes(2, 'big')
151-
#print(hex(len(pkt[:plen + 2])), hexlify(pkt[:plen + 2], ":"))
152147
self.sock.write(pkt, plen + 2)
153148
self._send_str(topic)
154149
self.sock.write(qos.to_bytes(1, "little"))
155150
while 1:
156151
op = self.wait_msg()
157152
if op == 0x90:
158153
resp = self.sock.read(4)
159-
#print(resp)
160154
assert resp[1] == pkt[plen] and resp[2] == pkt[plen + 1]
161155
if resp[3] == 0x80:
162156
raise MQTTException(resp[3])

umqtt.simple/umqtt_debug.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from umqtt.simple import MQTTClient
2+
from ubinascii import hexlify
3+
4+
5+
class debug_socket:
6+
def __init__(self, sock):
7+
self.sock = sock
8+
9+
def read(self, len):
10+
data = self.sock.read(len)
11+
print("RECV:", hexlify(data, ':').decode())
12+
return data
13+
14+
def write(self, data, len=None):
15+
print("SEND:", hexlify(data, ':').decode())
16+
if len is None:
17+
return self.sock.write(data)
18+
else:
19+
return self.sock.write(data, len)
20+
21+
def __getattr__(self, name):
22+
return getattr(self.sock, name)
23+
24+
25+
class DebugMQTTClient(MQTTClient):
26+
def __init__(self, *args, **kw):
27+
super().__init__(*args, **kw)
28+
self._sock = None
29+
30+
@property
31+
def sock(self):
32+
return self._sock
33+
34+
@sock.setter
35+
def sock(self, val):
36+
if val:
37+
val = debug_socket(val)
38+
self._sock = val

0 commit comments

Comments
 (0)