annotate front_door_lock.py @ 1:3b82ee3b9d79

cleanup
author drewp@bigasterisk.com
date Sun, 27 Aug 2023 11:20:30 -0700
parents 4365c72c59f6
children 89d47e203fc2
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 When a client requests
drewp@bigasterisk.com
parents:
diff changeset
3 PUT /output
drewp@bigasterisk.com
parents:
diff changeset
4 body room:unlocked
drewp@bigasterisk.com
parents:
diff changeset
5 head x-foaf-agent: <uri>
drewp@bigasterisk.com
parents:
diff changeset
6
drewp@bigasterisk.com
parents:
diff changeset
7 Then send
drewp@bigasterisk.com
parents:
diff changeset
8 frontdoorlock/switch/strike/command 'ON'
drewp@bigasterisk.com
parents:
diff changeset
9
drewp@bigasterisk.com
parents:
diff changeset
10 Then after a time send
drewp@bigasterisk.com
parents:
diff changeset
11 frontdoorlock/switch/strike/command 'OFF'
drewp@bigasterisk.com
parents:
diff changeset
12
drewp@bigasterisk.com
parents:
diff changeset
13 Also report on
drewp@bigasterisk.com
parents:
diff changeset
14 frontdoorlock/status 'online'
drewp@bigasterisk.com
parents:
diff changeset
15 --
drewp@bigasterisk.com
parents:
diff changeset
16
drewp@bigasterisk.com
parents:
diff changeset
17 Plus, for reliability, a simpler web control ui.
drewp@bigasterisk.com
parents:
diff changeset
18 """
drewp@bigasterisk.com
parents:
diff changeset
19
drewp@bigasterisk.com
parents:
diff changeset
20 import asyncio
drewp@bigasterisk.com
parents:
diff changeset
21 import logging
drewp@bigasterisk.com
parents:
diff changeset
22 import time
drewp@bigasterisk.com
parents:
diff changeset
23 from dataclasses import dataclass
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
24 from functools import partial
0
drewp@bigasterisk.com
parents:
diff changeset
25 from typing import Optional, cast
drewp@bigasterisk.com
parents:
diff changeset
26
drewp@bigasterisk.com
parents:
diff changeset
27 import aiomqtt
drewp@bigasterisk.com
parents:
diff changeset
28 from patchablegraph import PatchableGraph
drewp@bigasterisk.com
parents:
diff changeset
29 from patchablegraph.handler import GraphEvents, StaticGraph
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
30 from rdfdb.patch import Patch
0
drewp@bigasterisk.com
parents:
diff changeset
31 from rdflib import Literal, Namespace, URIRef
drewp@bigasterisk.com
parents:
diff changeset
32 from starlette.applications import Starlette
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
33 from starlette.exceptions import HTTPException
0
drewp@bigasterisk.com
parents:
diff changeset
34 from starlette.requests import Request
drewp@bigasterisk.com
parents:
diff changeset
35 from starlette.responses import JSONResponse
drewp@bigasterisk.com
parents:
diff changeset
36 from starlette.routing import Route
drewp@bigasterisk.com
parents:
diff changeset
37 from starlette_exporter import PrometheusMiddleware, handle_metrics
drewp@bigasterisk.com
parents:
diff changeset
38
drewp@bigasterisk.com
parents:
diff changeset
39 from get_agent import Agent, getAgent
drewp@bigasterisk.com
parents:
diff changeset
40
drewp@bigasterisk.com
parents:
diff changeset
41 logging.basicConfig(level=logging.INFO)
drewp@bigasterisk.com
parents:
diff changeset
42 log = logging.getLogger()
drewp@bigasterisk.com
parents:
diff changeset
43
drewp@bigasterisk.com
parents:
diff changeset
44 ROOM = Namespace('http://projects.bigasterisk.com/room/')
drewp@bigasterisk.com
parents:
diff changeset
45 ctx = ROOM['frontDoorLockGraph']
drewp@bigasterisk.com
parents:
diff changeset
46 lockUri = ROOM['frontDoorLock']
drewp@bigasterisk.com
parents:
diff changeset
47
drewp@bigasterisk.com
parents:
diff changeset
48
drewp@bigasterisk.com
parents:
diff changeset
49 def output(graph: PatchableGraph, request: Request) -> JSONResponse:
drewp@bigasterisk.com
parents:
diff changeset
50 return JSONResponse({"demo": "hello"})
drewp@bigasterisk.com
parents:
diff changeset
51
drewp@bigasterisk.com
parents:
diff changeset
52
drewp@bigasterisk.com
parents:
diff changeset
53 def status(graph: PatchableGraph, request: Request) -> JSONResponse:
drewp@bigasterisk.com
parents:
diff changeset
54 with graph.currentState() as current:
drewp@bigasterisk.com
parents:
diff changeset
55 sneakGraph = current.graph # current doesn't expose __contains__
drewp@bigasterisk.com
parents:
diff changeset
56 return JSONResponse({
drewp@bigasterisk.com
parents:
diff changeset
57 "locked": (lockUri, ROOM['state'], ROOM['locked'], ctx) in sneakGraph,
drewp@bigasterisk.com
parents:
diff changeset
58 "unlocked": (lockUri, ROOM['state'], ROOM['unlocked'], ctx) in sneakGraph,
drewp@bigasterisk.com
parents:
diff changeset
59 })
drewp@bigasterisk.com
parents:
diff changeset
60
drewp@bigasterisk.com
parents:
diff changeset
61
drewp@bigasterisk.com
parents:
diff changeset
62 def patchObjectToNone(g: PatchableGraph, ctx, subj, pred): #missing feature for patchObject
drewp@bigasterisk.com
parents:
diff changeset
63 p = g.getObjectPatch(ctx, subj, pred, URIRef('unused'))
drewp@bigasterisk.com
parents:
diff changeset
64 g.patch(Patch(delQuads=p.delQuads, addQuads=[]))
drewp@bigasterisk.com
parents:
diff changeset
65
drewp@bigasterisk.com
parents:
diff changeset
66
drewp@bigasterisk.com
parents:
diff changeset
67 @dataclass
drewp@bigasterisk.com
parents:
diff changeset
68 class LockHardware:
drewp@bigasterisk.com
parents:
diff changeset
69 graph: PatchableGraph
drewp@bigasterisk.com
parents:
diff changeset
70 mqtt: Optional['MqttConnection'] = None
drewp@bigasterisk.com
parents:
diff changeset
71
drewp@bigasterisk.com
parents:
diff changeset
72 def __post_init__(self):
drewp@bigasterisk.com
parents:
diff changeset
73 self.writeHwLockStateToGraph(ROOM['unknown'])
drewp@bigasterisk.com
parents:
diff changeset
74
drewp@bigasterisk.com
parents:
diff changeset
75 def setOnline(self, yes: bool):
drewp@bigasterisk.com
parents:
diff changeset
76 self.graph.patchObject(ctx, lockUri, ROOM['hardwareConnected'], Literal(yes))
drewp@bigasterisk.com
parents:
diff changeset
77
drewp@bigasterisk.com
parents:
diff changeset
78 def writeHwLockStateToGraph(self, state: URIRef):
drewp@bigasterisk.com
parents:
diff changeset
79 self.graph.patchObject(ctx, lockUri, ROOM['state'], state)
drewp@bigasterisk.com
parents:
diff changeset
80
drewp@bigasterisk.com
parents:
diff changeset
81 async def unlock(self, agent: Agent | None, autoLock=True):
drewp@bigasterisk.com
parents:
diff changeset
82 if agent is None:
drewp@bigasterisk.com
parents:
diff changeset
83 raise HTTPException(403)
drewp@bigasterisk.com
parents:
diff changeset
84 if self.mqtt is None:
drewp@bigasterisk.com
parents:
diff changeset
85 raise TypeError
drewp@bigasterisk.com
parents:
diff changeset
86 log.info("mock: await self.mqtt.sendStrikeCommand(True)")
drewp@bigasterisk.com
parents:
diff changeset
87 await self.mqtt.sendStrikeCommand(True)
drewp@bigasterisk.com
parents:
diff changeset
88 if autoLock:
drewp@bigasterisk.com
parents:
diff changeset
89 asyncio.create_task(self.autoLockTask(agent, sec=6))
drewp@bigasterisk.com
parents:
diff changeset
90
drewp@bigasterisk.com
parents:
diff changeset
91 async def autoLockTask(self, agent: Agent, sec: float):
drewp@bigasterisk.com
parents:
diff changeset
92 """running more than one of these should be safe"""
drewp@bigasterisk.com
parents:
diff changeset
93 end = time.time() + sec
drewp@bigasterisk.com
parents:
diff changeset
94 while now := time.time():
drewp@bigasterisk.com
parents:
diff changeset
95 if now > end:
drewp@bigasterisk.com
parents:
diff changeset
96 patchObjectToNone(self.graph, ctx, lockUri, ROOM['secondsUntilAutoLock'])
drewp@bigasterisk.com
parents:
diff changeset
97 await self.lock(agent)
drewp@bigasterisk.com
parents:
diff changeset
98 return
drewp@bigasterisk.com
parents:
diff changeset
99 await asyncio.sleep(.7)
drewp@bigasterisk.com
parents:
diff changeset
100 secUntil = round(end - now, 1)
drewp@bigasterisk.com
parents:
diff changeset
101 self.graph.patchObject(ctx, lockUri, ROOM['secondsUntilAutoLock'], Literal(secUntil))
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
102 log.info(f"{secUntil} sec until autolock")
0
drewp@bigasterisk.com
parents:
diff changeset
103
drewp@bigasterisk.com
parents:
diff changeset
104 async def lock(self, agent: Agent | None):
drewp@bigasterisk.com
parents:
diff changeset
105 if agent is None:
drewp@bigasterisk.com
parents:
diff changeset
106 raise HTTPException(403)
drewp@bigasterisk.com
parents:
diff changeset
107 if self.mqtt is None:
drewp@bigasterisk.com
parents:
diff changeset
108 raise TypeError
drewp@bigasterisk.com
parents:
diff changeset
109 await self.mqtt.sendStrikeCommand(False)
drewp@bigasterisk.com
parents:
diff changeset
110
drewp@bigasterisk.com
parents:
diff changeset
111
drewp@bigasterisk.com
parents:
diff changeset
112 @dataclass
drewp@bigasterisk.com
parents:
diff changeset
113 class MqttConnection:
drewp@bigasterisk.com
parents:
diff changeset
114
drewp@bigasterisk.com
parents:
diff changeset
115 hw: LockHardware
drewp@bigasterisk.com
parents:
diff changeset
116 topicRoot: str = 'frontdoorlock'
drewp@bigasterisk.com
parents:
diff changeset
117
drewp@bigasterisk.com
parents:
diff changeset
118 def startup(self):
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
119 asyncio.create_task(self._go())
0
drewp@bigasterisk.com
parents:
diff changeset
120
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
121 async def _go(self):
0
drewp@bigasterisk.com
parents:
diff changeset
122 self.client = aiomqtt.Client("mosquitto-frontdoor", 10210, client_id="lock-service-%s" % time.time(), keepalive=6)
drewp@bigasterisk.com
parents:
diff changeset
123 while True:
drewp@bigasterisk.com
parents:
diff changeset
124 try:
drewp@bigasterisk.com
parents:
diff changeset
125 async with self.client:
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
126 await self._handleMessages()
0
drewp@bigasterisk.com
parents:
diff changeset
127 except aiomqtt.MqttError:
drewp@bigasterisk.com
parents:
diff changeset
128 log.error('mqtt reconnecting', exc_info=True)
drewp@bigasterisk.com
parents:
diff changeset
129 await asyncio.sleep(5)
drewp@bigasterisk.com
parents:
diff changeset
130
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
131 async def _handleMessages(self):
0
drewp@bigasterisk.com
parents:
diff changeset
132 async with self.client.messages() as messages:
drewp@bigasterisk.com
parents:
diff changeset
133 await self.client.subscribe(self.topicRoot + '/#')
drewp@bigasterisk.com
parents:
diff changeset
134 async for message in messages:
drewp@bigasterisk.com
parents:
diff changeset
135 try:
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
136 self._onMessage(message)
0
drewp@bigasterisk.com
parents:
diff changeset
137 except Exception:
drewp@bigasterisk.com
parents:
diff changeset
138 log.error(f'onMessage {message=}', exc_info=True)
drewp@bigasterisk.com
parents:
diff changeset
139 await asyncio.sleep(1)
drewp@bigasterisk.com
parents:
diff changeset
140
drewp@bigasterisk.com
parents:
diff changeset
141 async def sendStrikeCommand(self, value: bool):
drewp@bigasterisk.com
parents:
diff changeset
142 await self.client.publish(self.topicRoot + '/switch/strike/command', 'ON' if value else 'OFF', qos=0, retain=False)
drewp@bigasterisk.com
parents:
diff changeset
143
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
144 def _stateFromMqtt(self, payload: str) -> URIRef:
0
drewp@bigasterisk.com
parents:
diff changeset
145 return {
drewp@bigasterisk.com
parents:
diff changeset
146 'OFF': ROOM['locked'],
drewp@bigasterisk.com
parents:
diff changeset
147 'ON': ROOM['unlocked'],
drewp@bigasterisk.com
parents:
diff changeset
148 }[payload]
drewp@bigasterisk.com
parents:
diff changeset
149
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
150 def _onMessage(self, message: aiomqtt.Message):
0
drewp@bigasterisk.com
parents:
diff changeset
151 subtopic = str(message.topic).partition(self.topicRoot + '/')[2]
drewp@bigasterisk.com
parents:
diff changeset
152 payload = cast(bytes, message.payload).decode('utf-8')
drewp@bigasterisk.com
parents:
diff changeset
153 match subtopic:
drewp@bigasterisk.com
parents:
diff changeset
154 case 'switch/strike/command':
drewp@bigasterisk.com
parents:
diff changeset
155 log.info(f'command message: {subtopic} {payload=}')
drewp@bigasterisk.com
parents:
diff changeset
156 case 'switch/strike/state':
drewp@bigasterisk.com
parents:
diff changeset
157 log.info(f'hw reports strike state = {payload}')
1
3b82ee3b9d79 cleanup
drewp@bigasterisk.com
parents: 0
diff changeset
158 self.hw.writeHwLockStateToGraph(self._stateFromMqtt(payload))
0
drewp@bigasterisk.com
parents:
diff changeset
159 case 'status':
drewp@bigasterisk.com
parents:
diff changeset
160 self.hw.setOnline(payload == 'online')
drewp@bigasterisk.com
parents:
diff changeset
161 case 'debug':
drewp@bigasterisk.com
parents:
diff changeset
162 log.info(f'hw debug: {payload}') # note: may include ansi colors
drewp@bigasterisk.com
parents:
diff changeset
163 case _:
drewp@bigasterisk.com
parents:
diff changeset
164 raise NotImplementedError(subtopic)
drewp@bigasterisk.com
parents:
diff changeset
165
drewp@bigasterisk.com
parents:
diff changeset
166
drewp@bigasterisk.com
parents:
diff changeset
167 async def simpleCommand(hw: LockHardware, req: Request) -> JSONResponse:
drewp@bigasterisk.com
parents:
diff changeset
168 command = req.path_params['command']
drewp@bigasterisk.com
parents:
diff changeset
169 agent = await getAgent(req)
drewp@bigasterisk.com
parents:
diff changeset
170 log.info(f'{command=} from {agent.asDict() if agent else agent}')
drewp@bigasterisk.com
parents:
diff changeset
171 match command:
drewp@bigasterisk.com
parents:
diff changeset
172 case 'unlock':
drewp@bigasterisk.com
parents:
diff changeset
173 await hw.unlock(agent)
drewp@bigasterisk.com
parents:
diff changeset
174 case 'lock':
drewp@bigasterisk.com
parents:
diff changeset
175 await hw.lock(agent)
drewp@bigasterisk.com
parents:
diff changeset
176 case 'stayUnlocked':
drewp@bigasterisk.com
parents:
diff changeset
177 await hw.unlock(agent, autoLock=False)
drewp@bigasterisk.com
parents:
diff changeset
178 case _:
drewp@bigasterisk.com
parents:
diff changeset
179 raise NotImplementedError(command)
drewp@bigasterisk.com
parents:
diff changeset
180 return JSONResponse({'ok': True})
drewp@bigasterisk.com
parents:
diff changeset
181
drewp@bigasterisk.com
parents:
diff changeset
182
drewp@bigasterisk.com
parents:
diff changeset
183 def main():
drewp@bigasterisk.com
parents:
diff changeset
184 graph = PatchableGraph()
drewp@bigasterisk.com
parents:
diff changeset
185 hw = LockHardware(graph)
drewp@bigasterisk.com
parents:
diff changeset
186 mqtt = MqttConnection(hw)
drewp@bigasterisk.com
parents:
diff changeset
187 hw.mqtt = mqtt
drewp@bigasterisk.com
parents:
diff changeset
188 app = Starlette(debug=True,
drewp@bigasterisk.com
parents:
diff changeset
189 on_startup=[mqtt.startup],
drewp@bigasterisk.com
parents:
diff changeset
190 routes=[
drewp@bigasterisk.com
parents:
diff changeset
191 Route('/api/status', partial(status, graph)),
drewp@bigasterisk.com
parents:
diff changeset
192 Route('/api/output', partial(output, graph)),
drewp@bigasterisk.com
parents:
diff changeset
193 Route('/api/graph', StaticGraph(graph)),
drewp@bigasterisk.com
parents:
diff changeset
194 Route('/api/graph/events', GraphEvents(graph)),
drewp@bigasterisk.com
parents:
diff changeset
195 Route('/api/simple/{command:str}', partial(simpleCommand, hw), methods=['PUT']),
drewp@bigasterisk.com
parents:
diff changeset
196 ])
drewp@bigasterisk.com
parents:
diff changeset
197
drewp@bigasterisk.com
parents:
diff changeset
198 app.add_middleware(PrometheusMiddleware, app_name='front_door_lock')
drewp@bigasterisk.com
parents:
diff changeset
199 app.add_route("/metrics", handle_metrics)
drewp@bigasterisk.com
parents:
diff changeset
200
drewp@bigasterisk.com
parents:
diff changeset
201 return app
drewp@bigasterisk.com
parents:
diff changeset
202
drewp@bigasterisk.com
parents:
diff changeset
203
drewp@bigasterisk.com
parents:
diff changeset
204 app = main()