adaption to paho 2.0 and optional botoombar added
This commit is contained in:
parent
95c4f81628
commit
a9050c751d
2
mqtt
2
mqtt
@ -1 +1 @@
|
||||
Subproject commit 1adfb0626e7777c6d29be65d4ad4ce2d57541301
|
||||
Subproject commit 328d3471a748472695a61193becdda76c7eefe69
|
@ -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()
|
||||
|
||||
|
||||
|
||||
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,6 +91,7 @@ if __name__ == "__main__":
|
||||
args.username = None
|
||||
password = None
|
||||
|
||||
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)
|
||||
@ -84,4 +100,8 @@ if __name__ == "__main__":
|
||||
|
||||
mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
|
||||
mc.add_callback("#", rx_mqtt)
|
||||
if BottomBar != None:
|
||||
my_bb.run()
|
||||
else:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user