This repository was archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.py
executable file
·428 lines (361 loc) · 12.8 KB
/
cover.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
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
#!/usr/bin/env python3
""" Cover controller script. """
from gpiozero import LED, Button
from systemd.journal import JournalHandler
from typing import List
from typing import Optional
from typing import Tuple
import argparse
import functools
import logging
import logging.handlers
import paho.mqtt.client as mqtt
import posixpath
import queue
import ssl
import sys
import time
logger = logging.getLogger(__name__)
PAYLOAD_UP: str = "OPEN"
PAYLOAD_DOWN: str = "CLOSE"
PAYLOAD_STOP: str = "STOP"
STATE_CLOSED: str = "closed"
STATE_CLOSING: str = "closing"
STATE_ERROR: str = "error"
STATE_INITIAL: str = "initial"
STATE_OPEN: str = "open"
STATE_OPENING: str = "opening"
STATE_PARTIAL: str = "partial"
class Cover:
"""
Represents a single cover.
One cover is a shade that has a motor with an H-bridge, with two GPIOs
outputs connected to the H-bridge and two GPIO inputs connected to switches
that indicate the limits.
Each motor attached to the cover has three states:
* Opening
* Closing
* Stopped
From these motor states and the switches each cover derives several more
states:
* Open
* Closed
* Opening
* if top switch is set -> Open
* if state is Opening for more than 70s -> Error
* if both switches are set -> Error
* Unknown
* if top switch is set -> Open
* if bottom switch is set -> Closed
* if both switches are set -> Error
* Error
"""
class _LoggerAdapter(logging.LoggerAdapter):
"""Prepends the context onto logging messages."""
def process(self, msg: str, kwargs: dict) -> Tuple[str, dict]:
return f"[{self.extra['base_topic']}] {msg}", kwargs
def __init__(
self,
*,
up_motor_pin: int,
down_motor_pin: int,
up_limit_pin: int,
down_limit_pin: int,
up_time_limit: float,
down_time_limit: float,
base_topic: str,
client: mqtt.Client,
):
self.logger = self._LoggerAdapter(
logger=logging.getLogger(__name__),
extra={"base_topic": base_topic},
)
self.up_motor = LED(up_motor_pin)
self.down_motor = LED(down_motor_pin)
self.lower_limit = Button(down_limit_pin)
self.upper_limit = Button(up_limit_pin)
self.down_time_limit = down_time_limit
self.up_time_limit = up_time_limit
self.set_topic = posixpath.join(base_topic, "set")
self.position_topic = posixpath.join(base_topic, "position")
self.client = client
self.queue = queue.Queue()
self._state_start = time.monotonic()
self._state: str = STATE_INITIAL
self._postion: Optional[float] = None
self.motor_stop()
def get_state(self) -> str:
"""Get the current cover state."""
return self._state
def state_elapsed(self) -> float:
"""Get the duration in seconds of the current state."""
return time.monotonic() - self._state_start
def set_state(self, state: str):
"""Set the current cover state."""
elapsed = self.state_elapsed()
self.logger.debug(
f"Changing state from {self._state} to {state} "
f"after {elapsed:.3f}s"
)
if self._state == STATE_OPENING:
self.up_motor.off()
self.down_motor.on()
time.sleep(0.12)
self.down_motor.off()
elif self._state == STATE_CLOSING:
self.down_motor.off()
self.up_motor.on()
time.sleep(0.12)
self.up_motor.off()
if (
state not in {STATE_OPEN, STATE_CLOSED}
and self._postion is not None
):
if self._state == STATE_OPENING:
self._postion += elapsed * (100 / self.up_time_limit)
self._postion = min(self._postion, 99)
elif self._state == STATE_CLOSING:
self._postion -= elapsed * (100 / self.down_time_limit)
self._postion = max(self._postion, 1)
self.publish_position()
self._state = state
self._state_start = time.monotonic()
def publish_position(self):
if self._postion is not None:
p = str(int(self._postion))
self.logger.debug(f"publishing {p}")
self.client.publish(self.position_topic, p)
def get_payload(self) -> Optional[str]:
"""Gets a payload from the queue if avaliable."""
try:
payload = self.queue.get_nowait()
except queue.Empty:
return None
else:
self.logger.debug(f"new payload: {payload}")
return payload
def state_closing(self):
if self.lower_limit.is_pressed:
self.set_state(STATE_CLOSED)
return
self.up_motor.off()
self.down_motor.on()
elapsed = self.state_elapsed()
if elapsed > self.down_time_limit:
self.logger.info(
f"{self.get_state()} timeout "
f"{elapsed} > {self.down_time_limit}"
)
self.set_state(STATE_CLOSED)
return
payload = self.get_payload()
if payload == PAYLOAD_UP:
self.set_state(STATE_OPENING)
elif payload == PAYLOAD_STOP:
self.set_state(STATE_PARTIAL)
elif payload is not None:
self.logger.info(f"discarding payload: {payload}")
def state_opening(self):
if self.upper_limit.is_pressed:
self.set_state(STATE_OPEN)
return
self.down_motor.off()
self.up_motor.on()
elapsed = self.state_elapsed()
if elapsed > self.up_time_limit:
self.logger.info(
f"{self.get_state()} timeout "
f"{elapsed} > {self.up_time_limit}"
)
self.set_state(STATE_OPEN)
return
payload = self.get_payload()
if payload == PAYLOAD_DOWN:
self.set_state(STATE_CLOSING)
elif payload == PAYLOAD_STOP:
self.set_state(STATE_PARTIAL)
elif payload is not None:
self.logger.info(f"discarding payload: {payload}")
def state_closed(self):
self.motor_stop()
if self._postion != 0.0:
self._postion = 0.0
self.publish_position()
if self.upper_limit.is_pressed:
self.set_state(STATE_ERROR)
return
payload = self.get_payload()
if payload == PAYLOAD_UP:
self.set_state(STATE_OPENING)
elif payload is not None:
self.logger.info(f"discarding payload: {payload}")
def state_open(self):
self.motor_stop()
if self._postion != 100.0:
self._postion = 100.0
self.publish_position()
if self.lower_limit.is_pressed:
self.set_state(STATE_ERROR)
return
payload = self.get_payload()
if payload == PAYLOAD_DOWN:
self.set_state(STATE_CLOSING)
elif payload is not None:
self.logger.info(f"discarding payload: {payload}")
def state_error(self):
self.motor_stop()
self.logger.critical("Entered error state, halting execution...")
sys.exit(1)
def state_initial(self):
"""Initial state of the cover."""
self.logger.debug("determining position from initial state")
self.motor_stop()
if self.upper_limit.is_pressed and self.lower_limit.is_pressed:
self.set_state(STATE_ERROR)
elif self.upper_limit.is_pressed:
self.set_state(STATE_OPEN)
elif self.lower_limit.is_pressed:
self.set_state(STATE_CLOSED)
else:
self.set_state(STATE_PARTIAL)
def state_partial(self):
self.motor_stop()
if self.upper_limit.is_pressed and self.lower_limit.is_pressed:
self.set_state(STATE_ERROR)
elif self.upper_limit.is_pressed:
self.set_state(STATE_OPEN)
elif self.lower_limit.is_pressed:
self.set_state(STATE_CLOSED)
payload = self.get_payload()
if payload == PAYLOAD_UP:
self.set_state(STATE_OPENING)
elif payload == PAYLOAD_DOWN:
self.set_state(STATE_CLOSING)
elif payload is not None:
self.logger.info(f"discarding payload: {payload}")
def handle(self):
"""Function to poll to handle events."""
state = self.get_state()
if state == STATE_INITIAL:
self.state_initial()
elif state == STATE_PARTIAL:
self.state_partial()
elif state == STATE_CLOSED:
self.state_closed()
elif state == STATE_OPEN:
self.state_open()
elif state == STATE_OPENING:
self.state_opening()
elif state == STATE_CLOSING:
self.state_closing()
elif state == STATE_ERROR:
self.state_error()
else:
self.logger.error(f"Unknown state: {state}")
self.set_state(STATE_ERROR)
self.state_error()
def motor_stop(self):
self.up_motor.off()
self.down_motor.off()
def on_message(client, userdata, msg, covers: List[Cover]):
topic = msg.topic
payload = msg.payload.decode("utf-8", errors="backslashreplace")
logger.debug(f"new message on '{topic}' with payload '{payload}'")
if payload in {PAYLOAD_UP, PAYLOAD_DOWN, PAYLOAD_STOP}:
for cover in covers:
if cover.set_topic == topic:
cover.queue.put_nowait(payload)
break
else:
logger.warning(f"unknown topic '{topic}' with payload '{payload}'")
else:
logger.warning(
f"unknown message on '{topic}' with payload '{payload}'"
)
def on_connect(client, userdata, flags, rc, covers: List[Cover]):
logger.info(f"connected to MQTT server with code {rc}")
for cover in covers:
logger.info(f"subscribing to {cover.set_topic}")
client.subscribe(cover.set_topic)
def start_daemon(hostname: str):
"""Starts the daemonic process."""
handler = JournalHandler(SYSLOG_IDENTIFIER="cover")
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("[{name}] {message}", style="{")
handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
root_logger.addHandler(handler)
logger.debug("logging initialized")
covers = []
try:
client = mqtt.Client(
# TODO: add for version 2
# callback_api_version=mqtt.CallbackAPIVersion.VERSION1
)
covers = [
Cover(
up_motor_pin=5,
down_motor_pin=12,
up_limit_pin=2,
down_limit_pin=3,
base_topic="/home/sunroom/right_window",
up_time_limit=69.0,
down_time_limit=70.0,
client=client,
),
Cover(
up_motor_pin=6,
down_motor_pin=13,
up_limit_pin=4,
down_limit_pin=17,
base_topic="/home/sunroom/middle_window",
up_time_limit=68.0,
down_time_limit=70.0,
client=client,
),
Cover(
up_motor_pin=19,
down_motor_pin=16,
up_limit_pin=27,
down_limit_pin=22,
base_topic="/home/sunroom/left_window",
up_time_limit=78.0,
down_time_limit=80.0,
client=client,
),
]
client.on_connect = functools.partial(on_connect, covers=covers)
client.on_message = functools.partial(on_message, covers=covers)
ssl_context = ssl.SSLContext()
ssl_context.minimum_version = ssl.TLSVersion.TLSv1_3
ssl_context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
client.tls_set_context(context=ssl_context)
client.connect(hostname, 8883)
client.loop_start()
except Exception:
logger.exception("failed to init")
raise
try:
logger.info("Entering handler loop")
while True:
for cover in covers:
cover.handle()
time.sleep(0.02)
except Exception:
logger.exception("unhandled exception")
finally:
for cover in covers:
try:
cover.motor_stop()
except BaseException: # noqa: B036
logger.exception(f"failed to stop motor for cover={cover}")
def main():
parser = argparse.ArgumentParser(description="cover daemon")
parser.add_argument(
"hostname", type=str, help="MQTT server hostname or IPv4"
)
args = parser.parse_args()
start_daemon(args.hostname)
if __name__ == "__main__":
main()