annotate pi_mqtt.py @ 7:f0d549ec5e59 default tip

deployment and hostname
author drewp@bigasterisk.com
date Mon, 23 Sep 2024 01:24:48 -0700
parents 492eef562a88
children
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:
5
492eef562a88 more log detail
drewp@bigasterisk.com
parents: 4
diff changeset
25 log.info('failing health check')
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
26 return PlainTextResponse('err', status_code=500)
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
27 return PlainTextResponse('ok', status_code=200)
0
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
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
30 @dataclass
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
31 class Poller:
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
32 garage: pigpio.pi
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
33 last_motion = 0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
34
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
35 def poll(self, first_run=False):
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
36 global lastSuccessfulPoll
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
37 now = time.time()
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
38 mo = self.garage.read(4)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
39 SENSOR_MOTION.labels(loc='ga').set(mo)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
40 if mo:
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
41 self.last_motion = now
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
42
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
43 light_on = self.last_motion > now - 20 * 60
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
44 OUTPUT_LIGHT.labels(loc='ga').set(light_on)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
45 self.garage.write(15, not light_on)
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
46 lastSuccessfulPoll = time.time()
0
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
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
49 def main():
7
f0d549ec5e59 deployment and hostname
drewp@bigasterisk.com
parents: 5
diff changeset
50 host = 'ga-iot5'
5
492eef562a88 more log detail
drewp@bigasterisk.com
parents: 4
diff changeset
51 log.info(f'connecting to {host=} - if this hangs, garage5 wg maight be missing ip addr')
492eef562a88 more log detail
drewp@bigasterisk.com
parents: 4
diff changeset
52 garage = pigpio.pi(host=host, port=8888)
492eef562a88 more log detail
drewp@bigasterisk.com
parents: 4
diff changeset
53 log.info('connected')
0
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
54
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
55 poller = Poller(garage)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
56 garage.set_mode(4, pigpio.INPUT)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
57 garage.set_mode(15, pigpio.OUTPUT)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
58 background_loop.loop_forever(poller.poll, 1)
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 = Starlette(debug=True, routes=[
3
0cc41259fddd healthcheck
drewp@bigasterisk.com
parents: 0
diff changeset
61 Route('/health', health),
0
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
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
64 app.add_middleware(PrometheusMiddleware, app_name='pi_mqtt')
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
65 app.add_route("/metrics", handle_metrics)
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
66 return app
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
67
3989f073ed9e start. hardcoded motion light in garage
drewp@bigasterisk.com
parents:
diff changeset
68
5
492eef562a88 more log detail
drewp@bigasterisk.com
parents: 4
diff changeset
69 uvicorn.run(main, host="0.0.0.0", port=8001, log_level=logging.DEBUG, factory=True)