Przeglądaj źródła

default value handling added and pep8

master
Dirk Alders 1 rok temu
rodzic
commit
7c00cf30f0
4 zmienionych plików z 48 dodań i 7 usunięć
  1. 17
    0
      .vscode/launch.json
  2. 15
    0
      .vscode/settings.json
  3. 1
    1
      console_bottombar
  4. 15
    6
      mqtt_sniffer.py

+ 17
- 0
.vscode/launch.json Wyświetl plik

@@ -0,0 +1,17 @@
1
+{
2
+    // Verwendet IntelliSense zum Ermitteln möglicher Attribute.
3
+    // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
4
+    // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
5
+    "version": "0.2.0",
6
+    "configurations": [
7
+        {
8
+            "name": "Python: Main File execution",
9
+            "type": "python",
10
+            "request": "launch",
11
+            "program": "${workspaceFolder}/mqtt_sniffer.py",
12
+            "console": "externalTerminal",
13
+            "args": ["-n", "-u", "smarthome", "-t", "es.*t", "-l"],
14
+            "justMyCode": true
15
+        }
16
+    ]
17
+}

+ 15
- 0
.vscode/settings.json Wyświetl plik

@@ -0,0 +1,15 @@
1
+{
2
+  "python.defaultInterpreterPath": "./venv/bin/python",
3
+  "autopep8.args": ["--max-line-length=150"],
4
+  "python.formatting.provider": "none",
5
+  "[python]": {
6
+    "editor.defaultFormatter": "ms-python.python",
7
+    "editor.formatOnSave": true
8
+  },
9
+  "editor.formatOnSave": true,
10
+  "editor.fontSize": 14,
11
+  "emmet.includeLanguages": { "django-html": "html" },
12
+  "python.testing.pytestArgs": ["-v", "--cov", "--cov-report=xml", "__test__"],
13
+  "python.testing.unittestEnabled": false,
14
+  "python.testing.pytestEnabled": true,
15
+}

+ 1
- 1
console_bottombar

@@ -1 +1 @@
1
-Subproject commit fa26893496c744f416a1ee0193824075b3709330
1
+Subproject commit c9d7f78f6d0feeeb5e14e4ca6a8ca22ce1c81165

+ 15
- 6
mqtt_sniffer.py Wyświetl plik

@@ -9,17 +9,22 @@ import time
9 9
 
10 10
 VERSION = "0.1.0"
11 11
 STARTTIME = time.time()
12
-# TODO: Implement default values for bottombar_entries
13 12
 
14 13
 HELPTEXT = """
15 14
 F1: Get this help message
16 15
 F2: Set a filter (regular expression) for the topic of a message
17 16
     Examples:
18
-    * "/gfw/.*/videv" Get everything with "/gfw/" before "/videv" 
19
-F9: Start / Stop logging to mqtt_sniffer.log
17
+    * "/gfw/.*/main_light/" to get everything with "/gfw/" before "/main_light/"
18
+    * "^zigbee.*(?>!logging)$" to get everything starting with "zigbee" and not ending with "logging"
19
+    * "^(?!shellies).*/dirk/.*temperature$" to get everything not starting with "shellies" followed by "/dirk/" and ends with "temperature"
20
+F9: Start / Stop logging to mqtt-sniffer.log
20 21
 F12: Quit the mqtt sniffer
22
+
23
+'c': Clear screen
24
+'q': Quit
21 25
 """
22 26
 
27
+
23 28
 def rx_mqtt(mc, userdata, message):
24 29
     global my_bb
25 30
     global logfile
@@ -46,27 +51,31 @@ def rx_mqtt(mc, userdata, message):
46 51
             logfile.write("%9.04f::%s::%s\n" % (ts, message.topic, data))
47 52
             logfile.flush()
48 53
 
54
+
49 55
 if __name__ == "__main__":
50 56
     parser = argparse.ArgumentParser(description='This is a mqtt sniffer.')
51 57
     parser.add_argument('-f', dest='hostname', default='localhost', help='Hostname of the mqtt server')
52 58
     parser.add_argument('-p', dest='port', default=1883, help='Port of the mqtt server')
53 59
     parser.add_argument('-n', dest='no_credentials', action='store_true', help='Avoid asking for credentials')
54 60
     parser.add_argument('-u', dest='username', default=None, help='Set username for mqtt server')
61
+    parser.add_argument('-t', dest='topicfilter', default="", help='Set topic filter')
62
+    parser.add_argument('-l', dest='logtofile', action='store_true', help='Enable logging to file')
55 63
 
56 64
     args = parser.parse_args()
57 65
 
58 66
     if not args.no_credentials:
59 67
         if args.username == None:
60 68
             args.username = input("Username: ")
61
-        password = getpass.getpass(prompt='Password: ', stream=None) 
69
+        password = getpass.getpass(prompt='Password: ', stream=None)
62 70
     else:
71
+        args.username = None
63 72
         password = None
64 73
 
65 74
     my_bb = BottomBar(VERSION, label='MQTT-Sniffer')
66 75
     my_bb.add_entry('help', 1, my_bb.FUNC_INFO, label='[F1] Help', infotext=HELPTEXT)
67
-    my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter')
76
+    my_bb.add_entry('msg_re', 2, my_bb.FUNC_TEXT, label='[F2] Filter', default=args.topicfilter)
68 77
     my_bb.add_entry('quit', 12, my_bb.FUNC_QUIT, "Quit", label='[F12]', right=True)
69
-    my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', right=True)
78
+    my_bb.add_entry('log2file', 9, my_bb.FUNC_BOOL, label='[F9] Log2File', default=args.logtofile, right=True)
70 79
     with open('mqtt_sniffer.log', 'w') as logfile:
71 80
         mc = mqtt.mqtt_client("mqtt_sniffer", args.hostname, port=args.port, username=args.username, password=password)
72 81
         mc.add_callback("#", rx_mqtt)

Ładowanie…
Anuluj
Zapisz