annotate pi_mqtt.py @ 4:759f0ba7d345

crash (and get restarted) better
author drewp@bigasterisk.com
date Sat, 23 Mar 2024 16:15:34 -0700
parents 0cc41259fddd
children 492eef562a88
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
1 import logging
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
2 import time
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
3 from dataclasses import dataclass
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
4
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
5 import background_loop
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
6 import pigpio
4
759f0ba7d345 crash (and get restarted) better
drewp@bigasterisk.com
parents: 3
diff changeset
7 import uvicorn
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
8 from prometheus_client import Gauge
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
9 from starlette.applications import Starlette
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
10 from starlette.requests import Request
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
11 from starlette.responses import PlainTextResponse
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
12 from starlette.routing import Route
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
13 from starlette_exporter import PrometheusMiddleware, handle_metrics
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
14
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
15 SENSOR_MOTION = Gauge('sensor_motion', 'motion detected', ['loc'])
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
16 OUTPUT_LIGHT = Gauge('output_light', 'light level', ['loc'])
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
17 logging.basicConfig(level=logging.INFO)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
18 log = logging.getLogger()
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
19
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
20 lastSuccessfulPoll = time.time()
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
21
4
759f0ba7d345 crash (and get restarted) better
drewp@bigasterisk.com
parents: 3
diff changeset
22
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
23 def health(request: Request) -> PlainTextResponse:
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
24 if time.time() - lastSuccessfulPoll > 10:
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
25 return PlainTextResponse('err', status_code=500)
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
26 return PlainTextResponse('ok', status_code=200)
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
27
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
28
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
29 @dataclass
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
30 class Poller:
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
31 garage: pigpio.pi
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
32 last_motion = 0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
33
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
34 def poll(self, first_run=False):
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
35 global lastSuccessfulPoll
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
36 now = time.time()
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
37 mo = self.garage.read(4)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
38 SENSOR_MOTION.labels(loc='ga').set(mo)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
39 if mo:
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
40 self.last_motion = now
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
41
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
42 light_on = self.last_motion > now - 20 * 60
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
43 OUTPUT_LIGHT.labels(loc='ga').set(light_on)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
44 self.garage.write(15, not light_on)
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
45 lastSuccessfulPoll = time.time()
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
46
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
47
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
48 def main():
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
49 garage = pigpio.pi(host='garage5', port=8888)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
50
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
51 poller = Poller(garage)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
52 garage.set_mode(4, pigpio.INPUT)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
53 garage.set_mode(15, pigpio.OUTPUT)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
54 background_loop.loop_forever(poller.poll, 1)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
55
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
56 app = Starlette(debug=True, routes=[
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
57 Route('/health', health),
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
58 ])
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
59
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
60 app.add_middleware(PrometheusMiddleware, app_name='pi_mqtt')
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
61 app.add_route("/metrics", handle_metrics)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
62 return app
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
63
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
64
4
759f0ba7d345 crash (and get restarted) better
drewp@bigasterisk.com
parents: 3
diff changeset
65 uvicorn.run(main, host="0.0.0.0", port=8001, log_level=logging.INFO, factory=True)