-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_states.py
171 lines (139 loc) · 5.96 KB
/
update_states.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
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
from spacedirectory.models import directory
from spacedirectory.models.space import Space
import json
import spacedirectory.tools as tools
import time
import sys
from pathlib import Path
import paho.mqtt.client as mqtt
from datetime import datetime
from datetime import timedelta
current_state = {}
def log(message):
print("LOG: " + message)
class SpaceStateUpdater:
def __init__(self, config, spacedirectory):
self.topic = config['topic']
self.state = None
self.next_refresh = datetime.now()
self.refresh_interval = config.get('interval', 300)
self.error_initerval = 900
self.mqttc = None
self.mqttconfig = None
self.mqtt_timeout = config.get('mqtt_timeout', 300)
self.mqtt_connected = False
self.mqtt_reconnect = None
self.mqtt_reconnect_interval = config.get('mqtt_reconnect_interval', 60)
if 'spacedirectory' in config:
self.url = spacedirectory[config['spacedirectory']]
elif 'url' in config:
self.url = config['url']
else:
self.url = None
if 'mqtt' in config:
self.setup_mqtt(config['mqtt'])
def update(self):
if self.mqttc is not None:
self.mqttc.loop(timeout=1.0)
if not self.mqtt_connected and self.mqtt_reconnect and datetime.now() > self.mqtt_reconnect:
try:
log("Attempting reconnect for " + self.mqttconfig['host'])
self.mqttc.reconnect()
except:
log("Unable to reconnect MQTT for " + self.mqttconfig['host'])
self.mqtt_reconnect = datetime.now() + timedelta(seconds=self.mqtt_reconnect_interval)
if self.url is None:
self.state = 'unknown'
return
if datetime.now() > self.next_refresh:
try:
log("Updating " + self.url)
data = tools.get_json_data_from_url(self.url)
space = Space(data = data)
self.state = "open" if space.status.is_open else "closed"
self.next_refresh = datetime.now() + timedelta(seconds=self.refresh_interval)
if 'state' in data and 'mqtt' in data['state']:
self.setup_mqtt(data['state']['mqtt'])
except:
self.state = "unknown"
log("Unable to check spacestate for " + self.topic)
self.next_refresh = self.next_refresh + timedelta(seconds=self.error_initerval)
def on_mqtt_message(self, client, userdata, message):
if 'topic' in self.mqttconfig and message.topic == self.mqttconfig['topic']:
payload = message.payload.decode('UTF-8')
if payload == self.mqttconfig.get('closed', 'closed'):
self.state = "closed"
elif payload == self.mqttconfig.get('open', 'open'):
self.state = "open"
else:
self.state = "unknown"
self.next_refresh = datetime.now() + timedelta(seconds=self.mqtt_timeout)
def on_mqtt_connect(self, client, userdata, flags, rc):
self.mqtt_connected = True
if 'topic' in self.mqttconfig:
self.mqttc.subscribe(self.mqttconfig['topic'])
def on_mqtt_disconnect(self, client, userdata, rc):
self.mqtt_connected = False
self.mqtt_reconnect = datetime.now() + timedelta(seconds=self.mqtt_reconnect_interval)
log("MQTT for " + self.mqttconfig['host'] + " disconnected")
def setup_mqtt(self, config):
if self.mqttc is not None:
return
if self.mqttconfig is not None:
return
if 'host' not in config:
return
log("Configuring MQTT for " + config['host'])
try:
self.mqttconfig = config
self.mqttc = mqtt.Client()
self.mqttc.on_message = self.on_mqtt_message
self.mqttc.on_connect = self.on_mqtt_connect
self.mqttc.on_disconnect = self.on_mqtt_disconnect
self.mqttc.connect(config['host'], config.get('port', 1833))
except:
log("Error connecting MQTT for " + self.topic)
self.mqtt_reconnect = datetime.now() + timedelta(seconds=self.mqtt_reconnect_interval)
def on_message(client, userdata, message):
global current_state
current_state[message.topic] = message.payload.decode('UTF-8')
def on_connect(client, userdata, flags, rc):
global config
client.subscribe(config['prefix'] + "#", qos=2)
if __name__ == "__main__":
global config
log("Loading configfile")
configfile = Path('config.json')
if not configfile.is_file():
log("config.json does not exist")
sys.exit(1)
config = json.loads(open("config.json", "r").read())
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.connect(config['server'])
mqttc.loop_start()
spacedirectory = directory.get_spaces_list()
spaces = []
for s in config['spaces']:
spaces.append(SpaceStateUpdater(s, spacedirectory))
while True:
try:
for s in spaces:
s.update()
topic = config['prefix'] + s.topic
if s.state is not None:
if topic in current_state and current_state[topic] != s.state:
log("changed from " + current_state[topic] + " to " + s.state)
current_state[topic] = s.state
mqttc.publish(topic, payload=s.state, retain=True)
elif topic not in current_state:
current_state[topic] = s.state
mqttc.publish(topic, payload=s.state, retain=True)
elif topic not in current_state:
current_state[topic] = "unknown"
mqttc.publish(topic, payload="unknown", retain=True)
time.sleep(.1)
except KeyboardInterrupt:
log("Error")
sys.exit(2)