From a9050c751d8f903bc4c206a35fea4ff6ad401c6d Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Fri, 1 Mar 2024 13:12:24 +0100 Subject: [PATCH] adaption to paho 2.0 and optional botoombar added --- mqtt | 2 +- mqtt_sniffer.py | 58 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/mqtt b/mqtt index 1adfb06..328d347 160000 --- a/mqtt +++ b/mqtt @@ -1 +1 @@ -Subproject commit 1adfb0626e7777c6d29be65d4ad4ce2d57541301 +Subproject commit 328d3471a748472695a61193becdda76c7eefe69 diff --git a/mqtt_sniffer.py b/mqtt_sniffer.py index ec3115f..7690b57 100644 --- a/mqtt_sniffer.py +++ b/mqtt_sniffer.py @@ -1,6 +1,12 @@ -import bottombar as bb import argparse -from console_bottombar import BottomBar +try: + import bottombar as bb +except ModuleNotFoundError: + bb = None +try: + from console_bottombar import BottomBar +except ModuleNotFoundError: + BottomBar = None import getpass import json import mqtt @@ -8,8 +14,7 @@ import os import re import time -VERSION = "0.1.0" -STARTTIME = time.time() +VERSION = "0.2.0" logfile = None HELPTEXT = """ @@ -27,16 +32,15 @@ F12: Quit the mqtt sniffer """ -def rx_mqtt(mc, userdata, message): - global my_bb +def msg_print_log(message, topic_regex, log2file): global logfile + 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: - 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 - ts = time.time()-STARTTIME data = None try: data = json.loads(message.payload) @@ -48,13 +52,24 @@ def rx_mqtt(mc, userdata, message): data = message.payload if match: - print("%9.04f::%75s::%s" % (ts, message.topic, data)) - if my_bb.get_entry('log2file'): + print("%25s::%75s::%s" % (time.asctime(), message.topic, data)) + if log2file: if logfile is None: 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() - bb.redraw() + + + +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() + else: + msg_print_log(message, args.topicfilter, args.logtofile) if __name__ == "__main__": @@ -76,12 +91,17 @@ if __name__ == "__main__": args.username = None password = None - 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('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter) - my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True) - my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True) + if BottomBar != None: + 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('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter) + my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True) + my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True) mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password) mc.add_callback("#", rx_mqtt) - my_bb.run() + if BottomBar != None: + my_bb.run() + else: + while True: + time.sleep(1)