9
9
parser .add_argument ('-s' ,'--server' , help = 'MQTT Server' , default = "127.0.0.1" )
10
10
parser .add_argument ('-p' ,'--port' , help = 'MQTT Port' , default = 1883 )
11
11
parser .add_argument ('-t' ,'--topic' , help = 'MQTT Topic' , default = "/gdoor/bus_rx" )
12
- parser .add_argument ('-u' ,'--unique' , help = 'Show only unique' , default = True , type = bool )
12
+ parser .add_argument ('-u' ,'--unique' , help = 'Show only unique' , default = "true" , type = str , choices = [ "false" , "true" ] )
13
13
args = parser .parse_args ()
14
14
15
15
table = Table (show_header = True , header_style = "bold magenta" )
@@ -30,7 +30,7 @@ def prependRow(row):
30
30
table .rows .append (Row (style = None , end_section = False ))
31
31
32
32
datas = []
33
- def check_uinque (data ):
33
+ def check_unique (data ):
34
34
if data in datas :
35
35
return False
36
36
@@ -43,11 +43,16 @@ def on_connect(client, userdata, flags, reason_code, properties=None):
43
43
client .subscribe (topic = args .topic )
44
44
45
45
def on_message (client , userdata , message , properties = None ):
46
- data = json .loads (message .payload )
47
- elem = gdoor .GDOOR (data ["busdata" ])
48
-
49
- if args .unique == True and check_uinque (data ["busdata" ]):
50
- prependRow ([data ["event_id" ], data ["action" ], data ["source" ], data ["destination" ], data ["parameters" ], data ["busdata" ][0 :2 ], data ["busdata" ][2 :4 ], data ["type" ], data ["busdata" ]])
46
+ try :
47
+ data = json .loads (message .payload )
48
+
49
+ if args .unique == "true" :
50
+ if check_unique (data ["busdata" ]):
51
+ prependRow ([data ["event_id" ], data ["action" ], data ["source" ], data ["destination" ], data ["parameters" ], data ["busdata" ][0 :2 ], data ["busdata" ][2 :4 ], data ["type" ], data ["busdata" ]])
52
+ else :
53
+ prependRow ([data ["event_id" ], data ["action" ], data ["source" ], data ["destination" ], data ["parameters" ], data ["busdata" ][0 :2 ], data ["busdata" ][2 :4 ], data ["type" ], data ["busdata" ]])
54
+ except json .JSONDecodeError :
55
+ pass
51
56
52
57
client = mqtt .Client (mqtt .CallbackAPIVersion .VERSION2 , client_id = "mqttlisten.py" , clean_session = True )
53
58
client .on_connect = on_connect
0 commit comments