Mercurial > code > home > repos > front-door-lock
changeset 4:d0fa3638de2a
move to foafAgent
author | drewp@bigasterisk.com |
---|---|
date | Sun, 27 Aug 2023 13:18:36 -0700 |
parents | 89d47e203fc2 |
children | 9eaa993ed373 |
files | front_door_lock.py |
diffstat | 1 files changed, 17 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/front_door_lock.py Sun Aug 27 13:18:06 2023 -0700 +++ b/front_door_lock.py Sun Aug 27 13:18:36 2023 -0700 @@ -32,7 +32,7 @@ from starlette.routing import Route from starlette_exporter import PrometheusMiddleware, handle_metrics -from get_agent import Agent, getAgent +from get_agent import getFoafAgent logging.basicConfig(level=logging.INFO) log = logging.getLogger() @@ -55,7 +55,8 @@ }) -def patchObjectToNone(g: PatchableGraph, ctx, subj, pred): #missing feature for patchObject +# missing feature for patchObject +def patchObjectToNone(g: PatchableGraph, ctx, subj, pred): p = g.getObjectPatch(ctx, subj, pred, URIRef('unused')) g.patch(Patch(delQuads=p.delQuads, addQuads=[])) @@ -74,31 +75,31 @@ def writeHwLockStateToGraph(self, state: URIRef): self.graph.patchObject(ctx, lockUri, ROOM['state'], state) - async def unlock(self, agent: Agent | None, autoLock=True): - if agent is None: + async def unlock(self, foafAgent: URIRef | None, autoLock=True): + if foafAgent is None: raise HTTPException(403) if self.mqtt is None: raise TypeError log.info("mock: await self.mqtt.sendStrikeCommand(True)") await self.mqtt.sendStrikeCommand(True) if autoLock: - asyncio.create_task(self.autoLockTask(agent, sec=6)) + asyncio.create_task(self.autoLockTask(foafAgent, sec=6)) - async def autoLockTask(self, agent: Agent, sec: float): + async def autoLockTask(self, foafAgent: URIRef, sec: float): """running more than one of these should be safe""" end = time.time() + sec while now := time.time(): if now > end: patchObjectToNone(self.graph, ctx, lockUri, ROOM['secondsUntilAutoLock']) - await self.lock(agent) + await self.lock(foafAgent) return await asyncio.sleep(.7) secUntil = round(end - now, 1) self.graph.patchObject(ctx, lockUri, ROOM['secondsUntilAutoLock'], Literal(secUntil)) log.info(f"{secUntil} sec until autolock") - async def lock(self, agent: Agent | None): - if agent is None: + async def lock(self, foafAgent: URIRef | None): + if foafAgent is None: raise HTTPException(403) if self.mqtt is None: raise TypeError @@ -162,15 +163,17 @@ async def simpleCommand(hw: LockHardware, req: Request) -> JSONResponse: command = req.path_params['command'] - agent = await getAgent(req) - log.info(f'{command=} from {agent.asDict() if agent else agent}') + + foafAgent = await getFoafAgent(req) + + log.info(f'{command=} from {foafAgent=}') match command: case 'unlock': - await hw.unlock(agent) + await hw.unlock(foafAgent) case 'lock': - await hw.lock(agent) + await hw.lock(foafAgent) case 'stayUnlocked': - await hw.unlock(agent, autoLock=False) + await hw.unlock(foafAgent, autoLock=False) case _: raise NotImplementedError(command) return JSONResponse({'ok': True})