-
Notifications
You must be signed in to change notification settings - Fork 0
/
infoscreen.py
128 lines (93 loc) · 4.13 KB
/
infoscreen.py
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
#!/usr/bin/env python3
import json
import time
import sys
import re
from threading import Lock
import paho.mqtt.client as mqtt
class Infoscreen():
mqtt_host = "autoc4"
mqtt_port = 1883
mqtt_keepalive = 60
def __init__(self, clientid, stdscr):
# self.is_on = { 'tuer' : False }
self.blank = False
self.stdscr = stdscr
self.lock = Lock()
self.registered_windows = list()
self._init_windows()
self._init_mqtt(clientid)
def _init_mqtt(self, clientid):
self.heartbeat_topic = "heartbeat/" + clientid
self.mqttc = mqtt.Client(clientid)
self.mqttc.on_message = self.on_message
self.mqttc.on_connect = self.on_connect
self.mqttc.will_set(self.heartbeat_topic, bytearray(b'\x00'), 2, True)
self.mqttc.connect(self.mqtt_host, self.mqtt_port, self.mqtt_keepalive)
self.mqttc.loop_start()
def _init_windows(self):
raise NotImplementedError('Abstract method')
def on_connect(self, a, b, c, rc):
if rc != 0:
sys.exit(1) # connect failed # TODO
else:
self.mqttc.subscribe([
listener["subscribe"]
for registration in self.registered_windows
for listener in registration["listeners"]
])
self.mqttc.publish(self.heartbeat_topic, bytearray(b'\x01'), 2, retain=True)
def add_window(self, window, listeners):
self.registered_windows.append({
"window":window,
"listeners": [
{
"custom" : listener["custom"] if "custom" in listener else False,
"subscribe" : listener["subscribe"] if "subscribe" in listener else None,
"json" : listener["json"] if "json" in listener else True,
"utf8" : listener["utf8"] if "utf8" in listener else True,
"callback" : listener["callback"]
}
for listener in listeners
]
})
def on_message(self, client, userdata, message):
self.lock.acquire()
try:
for registration in self.registered_windows:
for listener in registration["listeners"]:
if mqtt.topic_matches_sub(listener["subscribe"][0], message.topic):
if listener["custom"]:
listener["callback"](message)
else:
payload = message.payload
if listener["json"]:
try:
payload = json.loads(message.payload.decode("utf-8"))
except Exception as msg:
print("An error occured while parsing JSON for topic \"%s\" : %s" % (message.topic, str(msg)) )
elif listener["utf8"]:
payload = message.payload.decode("utf-8")
listener["callback"](payload)
except Exception as msg:
print(msg)
self.lock.release()
# elif (message.topic == "licht/wohnzimmer/tuer"):
# self.is_on['tuer'] = (message.payload[0] != 0)
def run(self):
while True:
self.lock.acquire()
self.stdscr.clear()
for registration in self.registered_windows:
if registration["window"] is not None:
registration["window"].show()
self.lock.release()
time.sleep(0.1)
# if (not isOn['tuer'] and not blank):
# blank = True
# os.system("setterm --blank force --powersave on")
# #os.system("setterm --powersave powerdown")
# elif ((isOn['tuer']) and blank):
# blank = False
# os.system("setterm --blank poke")
# os.system("setterm --blank 0")