annotate light_bridge.py @ 7:05fb0319eb64

cleanup
author drewp@bigasterisk.com
date Sun, 28 Jan 2024 16:03:40 -0800
parents fc8ed0efcd72
children 140633abfa2a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
drewp@bigasterisk.com
parents:
diff changeset
1 """
drewp@bigasterisk.com
parents:
diff changeset
2 replaces a lot of mqtt_to_rdf and rdf_to_mqtt
drewp@bigasterisk.com
parents:
diff changeset
3 """
drewp@bigasterisk.com
parents:
diff changeset
4 import json
drewp@bigasterisk.com
parents:
diff changeset
5 import logging
7
05fb0319eb64 cleanup
drewp@bigasterisk.com
parents: 6
diff changeset
6 import time
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
7 from functools import partial
0
drewp@bigasterisk.com
parents:
diff changeset
8
drewp@bigasterisk.com
parents:
diff changeset
9 from patchablegraph import PatchableGraph
drewp@bigasterisk.com
parents:
diff changeset
10 from patchablegraph.handler import GraphEvents, StaticGraph
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
11 from sse_starlette.sse import EventSourceResponse
0
drewp@bigasterisk.com
parents:
diff changeset
12 from starlette.applications import Starlette
drewp@bigasterisk.com
parents:
diff changeset
13 from starlette.requests import Request
drewp@bigasterisk.com
parents:
diff changeset
14 from starlette.responses import JSONResponse
drewp@bigasterisk.com
parents:
diff changeset
15 from starlette.routing import Route
drewp@bigasterisk.com
parents:
diff changeset
16 from starlette_exporter import PrometheusMiddleware, handle_metrics
2
c6fd04e07db0 refactor light.py
drewp@bigasterisk.com
parents: 1
diff changeset
17
7
05fb0319eb64 cleanup
drewp@bigasterisk.com
parents: 6
diff changeset
18 from light import Lights
0
drewp@bigasterisk.com
parents:
diff changeset
19
drewp@bigasterisk.com
parents:
diff changeset
20 logging.basicConfig(level=logging.INFO)
drewp@bigasterisk.com
parents:
diff changeset
21 log = logging.getLogger()
drewp@bigasterisk.com
parents:
diff changeset
22
drewp@bigasterisk.com
parents:
diff changeset
23
drewp@bigasterisk.com
parents:
diff changeset
24 async def output(lights: Lights, request: Request) -> JSONResponse:
drewp@bigasterisk.com
parents:
diff changeset
25 light = lights.byName(request.query_params['light'])
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 2
diff changeset
26 return JSONResponse(light.to_dict())
0
drewp@bigasterisk.com
parents:
diff changeset
27
drewp@bigasterisk.com
parents:
diff changeset
28
drewp@bigasterisk.com
parents:
diff changeset
29 async def table(lights: Lights, req: Request) -> EventSourceResponse:
drewp@bigasterisk.com
parents:
diff changeset
30
drewp@bigasterisk.com
parents:
diff changeset
31 async def g():
drewp@bigasterisk.com
parents:
diff changeset
32 async for ping in lights.changes():
5
7eeda7f4f9cd spell it to_dict, for compat with DataClassJsonMixin
drewp@bigasterisk.com
parents: 2
diff changeset
33 yield json.dumps({'now': time.time()} | lights.to_dict())
0
drewp@bigasterisk.com
parents:
diff changeset
34
drewp@bigasterisk.com
parents:
diff changeset
35 return EventSourceResponse(g())
drewp@bigasterisk.com
parents:
diff changeset
36
drewp@bigasterisk.com
parents:
diff changeset
37
drewp@bigasterisk.com
parents:
diff changeset
38 def main():
drewp@bigasterisk.com
parents:
diff changeset
39 lights = Lights()
drewp@bigasterisk.com
parents:
diff changeset
40 graph = PatchableGraph()
drewp@bigasterisk.com
parents:
diff changeset
41 app = Starlette(debug=True,
drewp@bigasterisk.com
parents:
diff changeset
42 routes=[
drewp@bigasterisk.com
parents:
diff changeset
43 Route('/api/output', partial(output, lights), methods=['PUT']),
drewp@bigasterisk.com
parents:
diff changeset
44 Route('/api/table', partial(table, lights)),
drewp@bigasterisk.com
parents:
diff changeset
45 Route('/api/graph', StaticGraph(graph)),
drewp@bigasterisk.com
parents:
diff changeset
46 Route('/api/graph/events', GraphEvents(graph)),
drewp@bigasterisk.com
parents:
diff changeset
47 ])
drewp@bigasterisk.com
parents:
diff changeset
48
drewp@bigasterisk.com
parents:
diff changeset
49 app.add_middleware(PrometheusMiddleware, app_name='light-bridge')
drewp@bigasterisk.com
parents:
diff changeset
50 app.add_route("/metrics", handle_metrics)
drewp@bigasterisk.com
parents:
diff changeset
51
drewp@bigasterisk.com
parents:
diff changeset
52 return app
drewp@bigasterisk.com
parents:
diff changeset
53
drewp@bigasterisk.com
parents:
diff changeset
54
drewp@bigasterisk.com
parents:
diff changeset
55 app = main()