Mercurial > code > home > repos > front-door-lock
changeset 1:3b82ee3b9d79
cleanup
author | drewp@bigasterisk.com |
---|---|
date | Sun, 27 Aug 2023 11:20:30 -0700 |
parents | 4365c72c59f6 |
children | 76cec592435c |
files | front_door_lock.py pyproject.toml |
diffstat | 2 files changed, 12 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/front_door_lock.py Sun Aug 27 11:12:20 2023 -0700 +++ b/front_door_lock.py Sun Aug 27 11:20:30 2023 -0700 @@ -18,26 +18,25 @@ """ import asyncio -from functools import partial import logging import time from dataclasses import dataclass +from functools import partial from typing import Optional, cast import aiomqtt -import background_loop from patchablegraph import PatchableGraph from patchablegraph.handler import GraphEvents, StaticGraph +from rdfdb.patch import Patch from rdflib import Literal, Namespace, URIRef from starlette.applications import Starlette +from starlette.exceptions import HTTPException from starlette.requests import Request from starlette.responses import JSONResponse from starlette.routing import Route -from starlette.exceptions import HTTPException from starlette_exporter import PrometheusMiddleware, handle_metrics from get_agent import Agent, getAgent -from rdfdb.patch import Patch logging.basicConfig(level=logging.INFO) log = logging.getLogger() @@ -60,7 +59,6 @@ }) - def patchObjectToNone(g: PatchableGraph, ctx, subj, pred): #missing feature for patchObject p = g.getObjectPatch(ctx, subj, pred, URIRef('unused')) g.patch(Patch(delQuads=p.delQuads, addQuads=[])) @@ -101,7 +99,7 @@ await asyncio.sleep(.7) secUntil = round(end - now, 1) self.graph.patchObject(ctx, lockUri, ROOM['secondsUntilAutoLock'], Literal(secUntil)) - log.info(f"{end-now} sec until autolock") + log.info(f"{secUntil} sec until autolock") async def lock(self, agent: Agent | None): if agent is None: @@ -118,24 +116,24 @@ topicRoot: str = 'frontdoorlock' def startup(self): - asyncio.create_task(self.go()) + asyncio.create_task(self._go()) - async def go(self): + async def _go(self): self.client = aiomqtt.Client("mosquitto-frontdoor", 10210, client_id="lock-service-%s" % time.time(), keepalive=6) while True: try: async with self.client: - await self.handleMessages() + await self._handleMessages() except aiomqtt.MqttError: log.error('mqtt reconnecting', exc_info=True) await asyncio.sleep(5) - async def handleMessages(self): + async def _handleMessages(self): async with self.client.messages() as messages: await self.client.subscribe(self.topicRoot + '/#') async for message in messages: try: - self.onMessage(message) + self._onMessage(message) except Exception: log.error(f'onMessage {message=}', exc_info=True) await asyncio.sleep(1) @@ -143,13 +141,13 @@ async def sendStrikeCommand(self, value: bool): await self.client.publish(self.topicRoot + '/switch/strike/command', 'ON' if value else 'OFF', qos=0, retain=False) - def stateFromMqtt(self, payload: str) -> URIRef: + def _stateFromMqtt(self, payload: str) -> URIRef: return { 'OFF': ROOM['locked'], 'ON': ROOM['unlocked'], }[payload] - def onMessage(self, message: aiomqtt.Message): + def _onMessage(self, message: aiomqtt.Message): subtopic = str(message.topic).partition(self.topicRoot + '/')[2] payload = cast(bytes, message.payload).decode('utf-8') match subtopic: @@ -157,7 +155,7 @@ log.info(f'command message: {subtopic} {payload=}') case 'switch/strike/state': log.info(f'hw reports strike state = {payload}') - self.hw.writeHwLockStateToGraph(self.stateFromMqtt(payload)) + self.hw.writeHwLockStateToGraph(self._stateFromMqtt(payload)) case 'status': self.hw.setOnline(payload == 'online') case 'debug':
--- a/pyproject.toml Sun Aug 27 11:12:20 2023 -0700 +++ b/pyproject.toml Sun Aug 27 11:20:30 2023 -0700 @@ -10,11 +10,9 @@ "starlette-exporter>=0.13.0", "starlette>=0.20.4", "uvicorn[standard]>=0.18.2", - "background-loop>=1.3.0", "patchablegraph>=1.5.0", "rdfdb==0.24.0", "aiomqtt>=1.1.0", - "yapf>=0.40.1", "jwskate>=0.9.0", ] requires-python = ">=3.11"