Browse Source

adaption to paho 2.0 and optional botoombar added

master
Dirk Alders 9 months ago
parent
commit
a9050c751d
2 changed files with 40 additions and 20 deletions
  1. 1
    1
      mqtt
  2. 39
    19
      mqtt_sniffer.py

+ 1
- 1
mqtt

1
-Subproject commit 1adfb0626e7777c6d29be65d4ad4ce2d57541301
1
+Subproject commit 328d3471a748472695a61193becdda76c7eefe69

+ 39
- 19
mqtt_sniffer.py View File

1
-import bottombar as bb
2
 import argparse
1
 import argparse
3
-from console_bottombar import BottomBar
2
+try:
3
+    import bottombar as bb
4
+except ModuleNotFoundError:
5
+    bb = None
6
+try:
7
+    from console_bottombar import BottomBar
8
+except ModuleNotFoundError:
9
+    BottomBar = None
4
 import getpass
10
 import getpass
5
 import json
11
 import json
6
 import mqtt
12
 import mqtt
8
 import re
14
 import re
9
 import time
15
 import time
10
 
16
 
11
-VERSION = "0.1.0"
12
-STARTTIME = time.time()
17
+VERSION = "0.2.0"
13
 logfile = None
18
 logfile = None
14
 
19
 
15
 HELPTEXT = """
20
 HELPTEXT = """
27
 """
32
 """
28
 
33
 
29
 
34
 
30
-def rx_mqtt(mc, userdata, message):
31
-    global my_bb
35
+def msg_print_log(message, topic_regex, log2file):
32
     global logfile
36
     global logfile
37
+
33
     try:
38
     try:
34
-        match = len(re.findall(my_bb.get_entry('msg_re'), message.topic)) > 0
39
+        match = len(re.findall(topic_regex, message.topic)) > 0
35
     except re.error:
40
     except re.error:
36
-        print('No valid regular expression (%s). No filter active.' % my_bb.get_entry('msg_re'))
41
+        print('No valid regular expression (%s). No filter active.' % topic_regex)
37
         match = True
42
         match = True
38
 
43
 
39
-    ts = time.time()-STARTTIME
40
     data = None
44
     data = None
41
     try:
45
     try:
42
         data = json.loads(message.payload)
46
         data = json.loads(message.payload)
48
         data = message.payload
52
         data = message.payload
49
 
53
 
50
     if match:
54
     if match:
51
-        print("%9.04f::%75s::%s" % (ts, message.topic, data))
52
-        if my_bb.get_entry('log2file'):
55
+        print("%25s::%75s::%s" % (time.asctime(), message.topic, data))
56
+        if log2file:
53
             if logfile is None:
57
             if logfile is None:
54
                 logfile = open('mqtt_sniffer.log', 'w')
58
                 logfile = open('mqtt_sniffer.log', 'w')
55
-            logfile.write("%9.04f::%s::%s\n" % (ts, message.topic, data))
59
+            logfile.write("%25s::%s::%s\n" % (time.asctime(), message.topic, data))
56
             logfile.flush()
60
             logfile.flush()
57
-    bb.redraw()
61
+
62
+
63
+
64
+def rx_mqtt(mc, userdata, message):
65
+    global my_bb
66
+    global args
67
+
68
+    if BottomBar is not None and bb is not None:
69
+        msg_print_log(message, my_bb.get_entry('msg_re'), my_bb.get_entry('log2file'))
70
+        bb.redraw()
71
+    else:
72
+        msg_print_log(message, args.topicfilter, args.logtofile)
58
 
73
 
59
 
74
 
60
 if __name__ == "__main__":
75
 if __name__ == "__main__":
76
         args.username = None
91
         args.username = None
77
         password = None
92
         password = None
78
 
93
 
79
-    my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
80
-    my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
81
-    my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
82
-    my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
83
-    my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
94
+    if BottomBar != None:
95
+        my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
96
+        my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
97
+        my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
98
+        my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
99
+        my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
84
 
100
 
85
     mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
101
     mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
86
     mc.add_callback("#", rx_mqtt)
102
     mc.add_callback("#", rx_mqtt)
87
-    my_bb.run()
103
+    if BottomBar != None:
104
+        my_bb.run()
105
+    else:
106
+        while True:
107
+            time.sleep(1)

Loading…
Cancel
Save