-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDNT.3.0.1
More file actions
executable file
·839 lines (738 loc) · 31.4 KB
/
Copy pathDNT.3.0.1
File metadata and controls
executable file
·839 lines (738 loc) · 31.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#!/usr/bin/env python3
# DNT
VERSION="3.0.1"
#Display Temperatures from neighborhood remote thermometers
# Modeled after
# https://learn.sparkfun.com/tutorials/python-gui-guide-introduction-to-tkinter/experiment-3-sensor-dashboard
#
# HDTodd, Williston, VT, v1 2023.02; v2 2023.04; v3 2024.08; v3.0.1 2025.02.13
import argparse
import sys
import os
import codecs
import signal
from paho.mqtt import client as mqtt_client
import random
import json
#import time
from time import sleep
import datetime
import threading
import requests
from enum import IntEnum
from queue import Queue
try:
import tkinter as tk
from tkinter import ttk
import tkinter.font as tkFont
except ImportError:
import Tkinter as tk
import ttk
import Tkinter.font as tkFont
AP_DESCRIPTION="""
DNT: Display the temperatures of neighborhood remote thermometer sensors
as observed and streamed as HTTP or published as MQTT by rtl_433.
"""
AP_EPILOG="""
"""
###############################################################################
## *** BEGIN LOCAL MODIFICATIONS ***
# Locations of known local sensors to be monitored
# These will appear at the top of the displayed table, with the location
# "familiar name" rather than device identifier listed
# Dictionary entries are "model/id/channel":"Familiar Name".
# Run this program from a terminal session to see a list of "model/channel/id"
# dictionary keywords for devices in your neighborhood, then add entries with
# those keywords and associated locations into the 'location' dictionary here
location = {
"Acurite-609TXC/96/" : "Deck",
"Omni-Multisensor/9/1" : "Office",
"Acurite-606TX/158/1" : "Sun Room",
"Acurite-Tower/4652/A" : "Neighbor",
}
## *** END LOCAL MODIFICATIONS ***
###############################################################################
# Global variable initialization
# MQTT connection management
# Parameters used to establish the mqtt connection to the rtl_433
# receiver mqtt publisher
host = None
port = None
topic = "rtl_433/+/events"
Hport = 8433
Mport = 1883
username = ""
password = ""
client_id = f'python-mqtt-{random.randint(0, 100)}'
# By default, use Fahrenheit scale for display
useF = True
tScale = "°F"
# Variables and constants used globally
global debug
global exit_event
global msg_q
global source
global my_clk
HTTP = 0
MQTT = 1
win = None
frm_table = None
fullscreen = False
dfont = None
hfont = None
ifont = None
lfont = None
pktcnt = 0
debug = False
CSIBLK = "\033[30m"
CSIRED = "\033[31m"
CSIYLW = "\033[93m"
CSIMGN = "\033[95m"
CSIBLU = "\033[34m"
CSIGRN = "\033[32m"
Warn_Sign = "\U000026A0"
bl_flag = "\U0000203C" # '!!': want to use "\U0001FAAB" {low battery} but not available
st_flag = "\U00002049" # '?!'
Warnings = [" ", st_flag, bl_flag]
Warn = IntEnum('Warn', [ ('OK', 0), ('ALERT', 1), ('ALARM', 2)])
# Set 2-sec threshhold for rejecting duplicate records
dup_thresh = 2.0
##########################################################################################
# Define a class to hold the data for a device and procedures to create, update, access
class Data:
def __init__(self, dRow, eTime, loc, temp, hum, battery, status):
self.last_time = eTime
self.loc = loc
self.temp = temp
self.hum = hum
self.battery = battery
self.status = status
self.wlevel = Warn.ALARM if battery==0 else Warn.OK
self.warn = Warnings[self.wlevel]
self.dRow = dRow
return
# Update information about this device
# Return a flag to indicate if it was a duplicate record for this xmit
# and warning flag if battery has EVER been low or status has EVER changed
# That is, the warning is sticky: once set, must be manually reset
def update(self, eTime, temp, hum, battery, status):
dup = eTime < self.last_time + dup_thresh
self.last_time = eTime
self.temp = temp
self.hum = hum
self.wlevel = max(self.wlevel, Warn.ALARM if battery==0 else Warn.OK,
Warn.ALERT if status != self.status else Warn.OK)
self.warn = Warnings[self.wlevel]
self.battery = battery
self.status = status
return dup
# End class Data definition
##############################################################################
# Convert time string (ts) from ISO format to epoch time
# Or, if ts is in epoch time, convert to timestamp format.
# Return both formats for use in processing and displaying
def CnvTime(ts):
if ts.find("-") > 0:
try:
eTime = datetime.datetime.fromisoformat(ts).timestamp()
timestamp = ts
except ValueError as e:
err={}
print("datetime error in input line converting time string: ", ts)
print("datetime msg:", err.get("error", str(e)))
sys.exit(1)
else:
try:
eTime = float(ts)
timestamp = datetime.datetime.fromtimestamp(eTime)
except ValueError as e:
err = {}
print("Datetime conversion failed on line with datetime string", ts)
print("float() error msg:", err.get("error", str(e)))
sys.exit(1)
return(timestamp, eTime)
# End CnvTime()
###############################################################################
# Command-line processing
# Create the command parser, parse cmd line, and set defaults
def make_parser():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=AP_DESCRIPTION, epilog=AP_EPILOG)
parser.add_argument("-S", "--source", dest="source_str", type=str,
choices=["HTTP", "MQTT"], default="MQTT",
help="Connect to source with HTTP or MQTT? (default: %(default)s)")
parser.add_argument("-H", "--host", dest="host", type=str, default=host,
help="HTTP or MQTT hostname to connect to (default: %(default)s)")
parser.add_argument("-P", "--port", type=int,
help="HTTP or MQTT port # (default: 8433 or 1883")
specifyTemp = parser.add_mutually_exclusive_group()
specifyTemp.add_argument("-C", "--Celsius", dest="degC",
action="store_true", default=False,
help="Display temperatures in Celsius (default is Fahrenheit)")
specifyTemp.add_argument("-F", "--Fahrenheit", dest="degC", action="store_false",
default=True,
help="Display temperatures in Fahrenheit (default)")
parser.add_argument("-T", "--topic", dest="topic", type=str,
default=topic,
help="(If needed) rtl_433 MQTT event topic to subscribe to (default: %(default)s)")
parser.add_argument("-u", "--username", type=str, default=username,
help="(If needed) MQTT username; defaults to blank")
parser.add_argument("-p", "--password", type=str, default=password,
help="(If needed) MQTT password; defaults to blank")
parser.add_argument("-v", "--version", action="version", version=VERSION)
parser.add_argument("-d", "--debug", dest="debug", action="store_true",
help="Print actions and messages during program execution")
parser.add_argument("-w", "--warn", dest="warn", action="store_true",
help="Insert warnings at pkts 20 & 40 to debug; requires -d")
args = parser.parse_args()
return args
###############################################################################
# Set execution parameters from defaults and the cmdline
def set_params():
global source, host, topic, username, password, port, useF, tScale, debug, debugW
args = make_parser()
# Set protocol for network connection
source = HTTP if args.source_str=="HTTP" else MQTT
# We need host name or IP, so if not already provided by
# cmdline, env, or embedded source code, then ask for it
if not args.host:
if source==MQTT and "MQTT_HOST" in os.environ:
args.host = os.environ["MQTT_HOST"]
else:
if source==HTTP and "HTTP_HOST" in os.environ:
args.host = os.environ["HTTP_HOST"]
else:
if host is None:
args.host = input("HTTP/MQTT host name or IP: ")
# Set connection parameters from env if possible
# If source is not specified, default to MQTT for historic compatibility
if not args.port:
if source==MQTT:
if "MQTT_PORT" in os.environ:
args.port = os.environ["MQTT_PORT"]
else:
args.port = Mport
else:
if source==HTTP:
if "HTTP_PORT" in os.environ:
args.port = os.environ["HTTP_PORT"]
else:
args.port = Hport
else:
args.port=Mport
source = MQTT
if source==MQTT and not args.topic:
if "MQTT_TOPIC" in os.environ:
args.topic = os.environ["MQTT_TOPIC"]
else:
if topic is None:
args.topic = input("MQTT topic: ")
# username and password may not be required, so don't prompt
# if not already provided; and port just defaults in parser
if not args.username and 'MQTT_USERNAME' in os.environ:
args.username = os.environ['MQTT_USERNAME']
else:
args.username = ""
if not args.password and 'MQTT_PASSWORD' in os.environ:
args.password = os.environ['MQTT_PASSWORD']
else:
args.password = ""
useF = False if args.degC is not None and args.degC else True
tScale = "°F" if useF else "°C"
host = args.host
port = args.port
topic = args.topic
username = args.username
password = args.password
debug = args.debug
debugW = args.debug and args.warn
if debug:
print("Debug is ON")
print("Parameter settings in execution are ...")
print("\tConnect to data source via ", "HTTP" if source==HTTP else "MQTT")
print("\tHTTP/MQTT host: ", host)
print("\tHTTP/MQTT port: ", port)
print("\tMQTT topic: ", topic)
print("\tMQTT username:", username)
print("\tMQTT password:", password)
print("\tUseF: ", useF)
print("\tDebug warnings", debugW)
return
# End set execution parameters from defaults and the cmdline
# End Command-line processing
##########################################################################################
# Window management functions
# DoubleScrolledFrame class adapted from
# https://gist.github.com/novel-yet-trivial/2841b7b640bba48928200ff979204115
class DoubleScrolledFrame:
"""
A vertically scrolled Frame that can be treated like any other Frame
ie it needs a master and layout and it can be a master.
keyword arguments are passed to the underlying Frame
except the keyword arguments 'width' and 'height', which
are passed to the underlying Canvas
note that a widget layed out in this frame will have Canvas as self.master,
if you subclass this there is no built in way for the children to access it.
You need to provide the controller separately.
"""
def __init__(self, master, **kwargs):
width = kwargs.pop('width', None)
height = kwargs.pop('height', None)
self.outer = tk.Frame(master, **kwargs)
self.vsb = ttk.Scrollbar(self.outer, orient=tk.VERTICAL)
self.vsb.grid(row=0, column=1, sticky='ns')
self.hsb = ttk.Scrollbar(self.outer, orient=tk.HORIZONTAL)
self.hsb.grid(row=1, column=0, sticky='ew')
self.canvas = tk.Canvas(self.outer, highlightthickness=0, width=width, height=height)
self.canvas.grid(row=0, column=0, sticky='nsew')
self.outer.rowconfigure(0, weight=1)
self.outer.columnconfigure(0, weight=1)
self.canvas['yscrollcommand'] = self.vsb.set
self.canvas['xscrollcommand'] = self.hsb.set
# mouse scroll does not seem to work with just "bind"; You have
# to use "bind_all". Therefore to use multiple windows you have
# to bind_all in the current widget
self.canvas.bind("<Enter>", self._bind_mouse)
self.canvas.bind("<Leave>", self._unbind_mouse)
self.vsb['command'] = self.canvas.yview
self.hsb['command'] = self.canvas.xview
self.inner = tk.Frame(self.canvas)
# pack the inner Frame into the Canvas with the topleft corner 4 pixels offset
self.canvas.create_window(4, 4, window=self.inner, anchor='nw')
self.inner.bind("<Configure>", self._on_frame_configure)
self.outer_attr = set(dir(tk.Widget))
def __getattr__(self, item):
if item in self.outer_attr:
# geometry attributes etc (eg pack, destroy, tkraise) are passed on to self.outer
return getattr(self.outer, item)
else:
# all other attributes (_w, children, etc) are passed to self.inner
return getattr(self.inner, item)
def _on_frame_configure(self, event=None):
x1, y1, x2, y2 = self.canvas.bbox("all")
height = self.canvas.winfo_height()
width = self.canvas.winfo_width()
self.canvas.config(scrollregion = (0,0, max(x2, width), max(y2, height)))
def _bind_mouse(self, event=None):
self.canvas.bind_all("<4>", self._on_mousewheel)
self.canvas.bind_all("<5>", self._on_mousewheel)
self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
def _unbind_mouse(self, event=None):
self.canvas.unbind_all("<4>")
self.canvas.unbind_all("<5>")
self.canvas.unbind_all("<MouseWheel>")
def _on_mousewheel(self, event):
"""Linux uses event.num; Windows / Mac uses event.delta"""
func = self.canvas.xview_scroll if event.state & 1 else self.canvas.yview_scroll
if event.num == 4 or event.delta > 0:
func(-1, "units" )
elif event.num == 5 or event.delta < 0:
func(1, "units" )
def __str__(self):
return str(self.outer)
# Toggle fullscreen
def toggle_fullscreen(event=None):
global win
global fullscreen
fullscreen = not fullscreen
if fullscreen:
hfont.configure(size=max(36, int((win.winfo_height() / 18))))
dfont.configure(size=max(24, int((win.winfo_height() / 24))))
ifont.configure(size=max(18, int((win.winfo_height() / 18))))
lfont.configure(size=max(30, int((win.winfo_height() / 24))))
else:
hfont.configure(size=40)
dfont.configure(size=24)
ifont.configure(size=18)
lfont.configure(size=30, weight="bold")
win.attributes('-fullscreen', fullscreen)
return
# Table management and display
# Add a row to the display table, "tbl", at current next-entry index and increment the index
# Format the widgets, and handle row 0 differently as it is the header
# Widget values get filled in by "main" (for header) or "on_message" (for data values)
def add_row(dRow):
global frm_table, tbl
tbl.append( (tk.StringVar(), tk.StringVar(), tk.StringVar() , tk.StringVar()) )
row = tk.Frame(frm_table)
# Color the header row differently
if dRow==0:
(bg, fg, fbg, ffg) = ("cornflower blue", "white", "yellow", "black")
else:
(bg, fg, fbg, ffg) = ("white", "black", "white", "red")
lbl_loc = tk.Label(row, width=30, textvariable=tbl[dRow][0], font=dfont, bg=bg, fg=fg)
lbl_temp = tk.Label(row, width=7, textvariable=tbl[dRow][1], font=dfont, bg=bg, fg=fg)
lbl_hum = tk.Label(row, width=4, textvariable=tbl[dRow][2], font=dfont, bg=bg, fg=fg)
lbl_flag = tk.Label(row, width=1, textvariable=tbl[dRow][3], font=dfont, bg=fbg,fg=ffg)
lbl_loc.grid( row=dRow, column=0, padx=5, pady=5, sticky=tk.W)
lbl_temp.grid(row=dRow, column=1, padx=5, pady=5, sticky=tk.E)
lbl_hum.grid( row=dRow, column=2, padx=5, pady=5, sticky=tk.E)
lbl_flag.grid(row=dRow, column=3, padx=5, pady=5, sticky=tk.E)
row.pack(side="top")
return
# Create the display panels and table section
def create_display():
global win, frm_table, dfont, hfont, ifont, lfont, src, port, my_clk
# Create the main window
win = tk.Tk()
win.title("Neighborhood Temperatures")
my_clk = tk.StringVar()
# Create dynamic font for text
hfont = tkFont.Font(size=-36)
dfont = tkFont.Font(size=-24)
ifont = tkFont.Font(size=-18)
lfont = tkFont.Font(size=-30, weight="bold")
# Build the title section of the display
frm_title = tk.Frame(win, borderwidth=10, relief="groove")
frm_title.pack(side="top", fill="x", expand=False)
lbl_title = tk.Label(frm_title, text="Display Neighborhood Temperatures",
font=hfont, bg="cornflower blue", fg="white")
lbl_title.pack(anchor="center", fill="x")
# Build the information section
proto = "HTTP" if source==HTTP else "MQTT"
frm_info = tk.Frame(win, borderwidth=5, relief="raised")
frm_info.pack(side="top", fill="x", expand=False)
lbl_monitor = tk.Label(frm_info, text="Monitoring %s host %s:%d @" % (proto,host,port), font=ifont)
lbl_clock = tk.Label(frm_info, textvariable=my_clk, font=ifont)
btn_wrst = tk.Button(frm_info, text="WRst", width=4, height=1,
font=ifont, fg='green',command=reset_warn)
btn_toggle = tk.Button(frm_info, text="Togl", width=4, height=1,
font=ifont, fg='blue', command=toggle_fullscreen)
btn_quit = tk.Button(frm_info, text="Quit", width=4, height=1,
font=ifont, fg='red', command=quit_prog)
lbl_monitor.pack(side="left", padx=2, pady=2)
lbl_clock.pack(side="left", padx=2, pady=2)
btn_quit.pack(side="right")
btn_toggle.pack(side="right")
btn_wrst.pack(side="right")
# And create the table for data display
frm_table = DoubleScrolledFrame(win, width=700, borderwidth=5, relief=tk.GROOVE)
frm_table.pack(side="top", fill="both", expand=True, padx=5, pady=5)
# First, the header
add_row(0)
tbl[0][0].set("Location or Thermometer")
tbl[0][1].set("Temp "+tScale)
tbl[0][2].set("%RH")
tbl[0][3].set(Warn_Sign)
# Have <ESC> toggle window, and call resize() every time the window is resized
win.bind('<Escape>', toggle_fullscreen)
return
# End create_display()
# Reset device warnings to OK and rewrite the display table accordingly
def reset_warn():
for dev in devices:
devices[dev].battery = 1
devices[dev].wlevel = Warn.OK
devices[dev].warn = Warnings[Warn.OK]
for row in range(1, len(tbl)):
tbl[row][3].set(Warnings[Warn.OK])
return
# End window management functions
###############################################################################
# process_msg() does the real work of understanding and then presenting
# the data in the received message (from HTTP or MQTT)
# If it's a thermometer reading:
# ignore if it's a duplicate, update display if it isn't
def process_msg():
global source
global exit_event
global msg_q
global pktcnt
global devices, tbl
global my_clk
def write_display_row_for_device(dev):
dRow = devices[dev].dRow
try:
tbl[dRow][0].set(devices[dev].loc)
tbl[dRow][1].set(round(devices[dev].temp,1))
tbl[dRow][2].set(devices[dev].hum)
tbl[dRow][3].set(devices[dev].warn)
except Exception as e:
print("Exception when trying to set display values for row", dRow,
"location", loc)
print("Exception:", str(e))
return
# Find which display-table row a device in the location dictionary
# is supposed to be shown at
def location_index(dev):
if dev not in location:
return None
x = 1 # start at 1 because header is row 0 in display table
for el in location:
if el == dev:
break
else:
x += 1
return x
def place_row(dev):
# If device "dev" is in the location dictionary, swap the rows in the
# display table so this new "known" device is in its location
# at the top of the display table
if dev in location:
# dev is in the location dictionary: find its position
x = location_index(dev)
if debug:
print("Found", dev, "at location dictionary index", x,
"Swapping table rows ", devices[dev].dRow, "and", x)
# Find which device currently uses that display row and swap them
for d in devices:
if devices[d].dRow == x:
# found device at that row on display
# now swap display rows and show the prior device in the new display row
devices[d].dRow, devices[dev].dRow = devices[dev].dRow, devices[d].dRow
dRow = x
# And display the new device at display row of old one
write_display_row_for_device(d)
break
return
while not exit_event.is_set():
msg = msg_q.get()
# If it's a null message, that's our signal to quit
if msg == None:
if source==MQTT:
mqtt.loop_stop()
if debug:
print("Stop processing thread in 'process_msg'")
break
# Try to parse the json payload from MQTT or HTTP
try:
if source==MQTT:
y = json.loads(msg.payload.decode())
else:
y = json.loads(codecs.decode(msg,"utf-8"))
except:
# Nope, can't serialize the packet
print("Unable to load JSON fields from record:\n\t", end="")
if source==MQTT:
print(msg.payload.decode())
else:
print(codecs.decode(msg,"utf-8"))
continue
# Got a real message: process it
# Ignore tire pressure monitoring system temp reports
if "type" in y and y["type"]=="TPMS":
continue
# If not a device record, just return
if "model" not in y:
continue
# Create the device identifier as "model/id/channel"
dev = y["model"] + "/"
if "id" in y:
dev += str(y["id"])
dev += "/"
if "channel" in y:
dev += str(y["channel"])
# if this is a themometer reading, process it; otherwise just return
if "temperature_C" in y.keys():
ltemp = y["temperature_C"]
if useF:
ltemp = ltemp*9.0/5.0+32.0
else:
if "temperature_F" in y.keys():
ltemp = y["temperature_F"]
if not useF:
ltemp = (ltemp-32.0)*5.0/9.0
else:
return # if no temp reading in record, ignore it
# OK, we have a temp reading. Get the variables we need
(isoTime,eTime) = CnvTime(y["time"])
dtime = datetime.datetime.fromisoformat(y["time"]).strftime("%H:%M:%S")
loc = dev if dev not in location else location[dev]
hum = 0 if "humidity" not in y.keys() else int(y["humidity"])
battery = None if "battery_ok" not in y else y["battery_ok"]
status = None if "status" not in y else y["status"]
# if debuging warnings (-d -W), inject warning conditions at pkts 10, 20
if debugW:
if pktcnt==10:
status = 9
print(CSIMGN, "Inserting status-code change for display debugging", CSIBLK)
if pktcnt==20:
battery = 0
print(CSIRED, "Inserting battery-low condition for display debugging", CSIBLK)
# Got all the values: update if not a repeated packet, or add if new
if dev in devices:
# Not a duplicate: update the values in the dictionary
# and retrieve display-table row number
dup = devices[dev].update(eTime, ltemp, hum, battery, status)
dRow = devices[dev].dRow
else:
# Append a new device to the ends of the "device" and display "tbl" lists
dRow = len(tbl)
devices[dev] = Data(dRow, eTime, loc, ltemp, hum, battery, status)
dup = False
if debug:
print("Appending new device", dev, " to end of tbl at row", dRow)
add_row(dRow)
place_row(dev)
if not dup:
pktcnt += 1
if debug:
print("Update display row{:>3d}: {:<30} {:<20} {:<30} {:>6.1f}{:2} {:>3}% snr={:>3.0f}".format(
devices[dev].dRow, devices[dev].loc, y["time"], dev, round(ltemp,1), tScale, hum,
0.0 if "snr" not in y else y['snr']) )
write_display_row_for_device(dev)
my_clk.set(dtime)
# No return: thread exits when exit_event is set or
# when a "None" message is received from the message collector
# End process_msg()
###############################################################################
# MQTT functions and message reception
# Message producer thread for MQTT stream
# Connect to MQTT host
def connect_mqtt() -> mqtt_client:
global debug
def on_connect(mqtt, userdata, flags, rc):
if rc == 0:
if debug:
print("Connected to MQTT host!")
else:
print("Failed attempt to connect to ", mqtt)
print(" with userdata ", userdata)
print("Return code %d\n", rc)
sys.exit(1)
return #from on_connect()
# Work around paho-mqtt v1, v2+ Client instantiation parameter diffs
try:
mqtt = mqtt_client.Client(client_id, clean_session=False)
except:
mqtt = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, client_id, clean_session=False)
mqtt.username_pw_set(username, password)
mqtt.on_connect = on_connect
if debug:
print("connecting to ", host, port)
mqtt.connect(host, port)
mqtt_subscribe(mqtt)
return mqtt
# Subscribe to rtl_433 publication & process records we receive
def mqtt_subscribe(mqtt: mqtt_client):
# When we get an MQTT message, just pass it on to the
# message processor to handle, as it does for HTTP as well
def on_message(mqtt, userdata, msg):
global msg_q
msg_q.put(msg)
return
mqtt.subscribe(topic)
mqtt.on_message = on_message
if debug:
print("subscribed to mqtt feed")
mqtt.loop_start()
return #From mqtt_subscribe(), but 'on_message' is active as mqtt callback
# End MQTT functions and message reception
#######################################################################################
# HTTP functions and message reception
# Message producer thread for http stream
# Listen for streamed http messages until 'exit_event' has been set,
# telling us to exit and close this thread.
def stream_lines():
global debug
url = f'http://{host}:{port}/stream'
headers = {'Accept': 'application/json'}
# We will receive JSON events, one per line terminated with CRLF.
# On Events and Stream endpoints a keep-alive of CRLF will be send every 60 seconds.
response = requests.get(url, headers=headers, timeout=70, stream=True)
print(CSIGRN, f'Connected to {url}', CSIBLK)
for chunk in response.iter_lines():
yield chunk
def http_subscribe():
global debug
global msq_q
global exit_event
global my_clk
# We receive JSON events, one per line terminated with CRLF.
# On Events and Stream endpoints a keep-alive of CRLF will be send every 60 seconds.
while not exit_event.is_set():
try:
for line in stream_lines():
if debug:
print("HTTP received\n\t", line)
line = line.rstrip()
if not line:
# filter out keep-alive empty lines
continue
# We have a message. Let the message processor handle it
msg_q.put(line)
if exit_event.is_set():
break
except requests.ConnectionError:
if exit_event.is_set():
break
my_clk.set("Offline")
print('Connection failed, retrying...')
sleep(5)
# We get here when 'exit_event' has been set externally
# Tell the consumer to quit by sending a 'None' message and return
# to caller, terminating this thread
msg_q.put(None)
if debug:
print('\nHTTP message collector exiting.')
return
# END HTTP functions and message reception
##############################################################################
# CNTL-C and QUIT button handler
def quit_prog():
global exit_event
global msg_q
global mqtt
global win
global source
if debug:
print("Quitting from quit_prog")
exit_event.set()
msg_q.put(None)
if source==HTTP:
print(CSIGRN, "\n\nWait for data collection to complete ...", CSIBLK)
win.destroy()
if source==MQTT:
mqtt.loop_stop()
mqtt.disconnect()
return
###############################################################################
# Main script
if __name__ == "__main__":
global msg
global source
exit_event = threading.Event()
msg_q = Queue(0)
t = datetime.datetime.now()
signal.signal(signal.SIGINT, quit_prog)
set_params()
# Device:Data dictionary
devices = {}
# List of widgets to display
tbl = []
# Create the display
create_display()
if source==MQTT:
mqtt = connect_mqtt()
if debug:
print("Entering the main run loops")
print("Start packet processing")
# Start the consumer and producer threads. Common consumer
# but different methods for producing MQTT & HTTP messages
c = threading.Thread(target = process_msg, args=())
if source==MQTT:
p = threading.Thread(target = mqtt_subscribe, args=(mqtt,))
else:
if source==HTTP:
p = threading.Thread(target = http_subscribe, args=())
else:
print("?No protocol specified for data source. Specifiy with `-S`")
sys.exit(1)
c.start()
p.start()
# And finally, start the tkinter main loop for the display window
# It will be terminated by "quit_prog": CNTL-C or QUIT button
if debug:
print("Start window processing thread")
win.mainloop()
# Exiting threads return control to here
p.join()
if debug:
print("Message collecting thread, 'subscribe, exited but processing thread continues")
c.join()
if debug:
print("Message processing thread, 'process_msg' exited; terminate normally")
print("Quitting ...")
sys.exit(0)
# End main script