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
|
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 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()
|
||||||
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__":
|
if __name__ == "__main__":
|
||||||
@ -76,12 +91,17 @@ if __name__ == "__main__":
|
|||||||
args.username = None
|
args.username = None
|
||||||
password = None
|
password = None
|
||||||
|
|
||||||
my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
|
if BottomBar != None:
|
||||||
my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
|
my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
|
||||||
my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
|
my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
|
||||||
my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
|
my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
|
||||||
my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
|
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 = 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)
|
||||||
my_bb.run()
|
if BottomBar != None:
|
||||||
|
my_bb.run()
|
||||||
|
else:
|
||||||
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user