annotate pi_mqtt.py @ 3:0cc41259fddd

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