adaption to paho 2.0 and optional botoombar added

This commit is contained in:
Dirk Alders 2024-03-01 13:12:24 +01:00
parent 95c4f81628
commit a9050c751d
2 changed files with 40 additions and 20 deletions

2
mqtt

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

View File

@ -1,6 +1,12 @@
import bottombar as bb
import argparse import argparse
try:
import bottombar as bb
except ModuleNotFoundError:
bb = None
try:
from console_bottombar import BottomBar from console_bottombar import BottomBar
except ModuleNotFoundError:
BottomBar = None
import getpass import getpass
import json import json
import mqtt import mqtt
@ -8,8 +14,7 @@ import os
import re import re
import time import time
VERSION = "0.1.0" VERSION = "0.2.0"
STARTTIME = time.time()
logfile = None logfile = None
HELPTEXT = """ HELPTEXT = """
@ -27,16 +32,15 @@ F12: Quit the mqtt sniffer
""" """
def rx_mqtt(mc, userdata, message): def msg_print_log(message, topic_regex, log2file):
global my_bb
global logfile global logfile
try: try:
match = len(re.findall(my_bb.get_entry('msg_re'), message.topic)) > 0 match = len(re.findall(topic_regex, message.topic)) > 0
except re.error: except re.error:
print('No valid regular expression (%s). No filter active.' % my_bb.get_entry('msg_re')) print('No valid regular expression (%s). No filter active.' % topic_regex)
match = True match = True
ts = time.time()-STARTTIME
data = None data = None
try: try:
data = json.loads(message.payload) data = json.loads(message.payload)
@ -48,13 +52,24 @@ def rx_mqtt(mc, userdata, message):
data = message.payload data = message.payload
if match: if match:
print("%9.04f::%75s::%s" % (ts, message.topic, data)) print("%25s::%75s::%s" % (time.asctime(), message.topic, data))
if my_bb.get_entry('log2file'): if log2file:
if logfile is None: if logfile is None:
logfile = open('mqtt_sniffer.log', 'w') logfile = open('mqtt_sniffer.log', 'w')
logfile.write("%9.04f::%s::%s\n" % (ts, message.topic, data)) logfile.write("%25s::%s::%s\n" % (time.asctime(), message.topic, data))
logfile.flush() logfile.flush()
def rx_mqtt(mc, userdata, message):
global my_bb
global args
if BottomBar is not None and bb is not None:
msg_print_log(message, my_bb.get_entry('msg_re'), my_bb.get_entry('log2file'))
bb.redraw() bb.redraw()
else:
msg_print_log(message, args.topicfilter, args.logtofile)
if __name__ == "__main__": if __name__ == "__main__":
@ -76,6 +91,7 @@ if __name__ == "__main__":
args.username = None args.username = None
password = None password = None
if BottomBar != None:
my_bb = BottomBar(VERSION, label='MQTT-Sniffer') my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT) my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter) my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
@ -84,4 +100,8 @@ if __name__ == "__main__":
mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password) mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
mc.add_callback("#", rx_mqtt) mc.add_callback("#", rx_mqtt)
if BottomBar != None:
my_bb.run() my_bb.run()
else:
while True:
time.sleep(1)