AttributeChange (the common base of every ATask and component ABC) now sets self.log = logging.getLogger(type(self).__name__), so components no longer need to hand-type their own name into each message. Wired logging.basicConfig() in server/brewpi.py with a bare "%(name)s: %(message)s" formatter - Tee (see prior commit) still supplies the "<date>T<time>:" prefix, so lines read "<date>T<time>:<component>: <message>" without double-stamping, and third-party loggers (websockets, asyncio) now get the same formatting for free. Converted the print() call sites that were standing in for this in tasks/ and components/ (leaving __main__ demo blocks and explicit debug-dump helpers alone).
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
import asyncio
|
|
from tasks import ATask
|
|
from ws.message import MsgIo
|
|
from utils.value import ChangedFloat
|
|
from components import ATemperatureSensor
|
|
|
|
|
|
class TempSensorTask(ATask):
|
|
def __init__(self, sensor_device: ATemperatureSensor, interval, msg_handler: MsgIo):
|
|
ATask.__init__(self, interval)
|
|
|
|
self.msg_handler = msg_handler
|
|
msg_handler.set_recv_handler(self.recv)
|
|
self.sensor = sensor_device
|
|
# Prime before registering - temperature() sets self.sensor.temp,
|
|
# which would otherwise fire on_temp_changed() (and its
|
|
# asyncio.create_task()) before the event loop is running, since
|
|
# all tasks are built synchronously at module level in brewpi.py.
|
|
# Guarded even though TempSensor_max31865.temperature() already
|
|
# catches its own read/reopen failures - belt-and-suspenders
|
|
# against a still-unanticipated exception crashing server startup
|
|
# entirely (same lesson as HeaterTask/StirrerTask's own outer
|
|
# guards - see tasks/heater.py's on_process()).
|
|
try:
|
|
self.sensor.temperature()
|
|
except Exception as e:
|
|
self.log.error(f"comm error priming sensor: {e}")
|
|
self.sensor.set_on_changed("temp", ChangedFloat(self.on_temp_changed, prec=1).set)
|
|
|
|
def on_temp_changed(self, value):
|
|
asyncio.create_task(self.send({'Temp': value}))
|
|
|
|
async def recv(self, data):
|
|
pass
|
|
|
|
async def send(self, data):
|
|
await self.msg_handler.send(data)
|
|
|
|
async def on_process(self):
|
|
# Guarded for the same reason as the priming call above - nothing
|
|
# restarts a dead ATask, so an exception escaping this loop would
|
|
# permanently stop this sensor from ever being read again.
|
|
while True:
|
|
try:
|
|
self.sensor.temperature()
|
|
except Exception as e:
|
|
self.log.error(f"comm error reading sensor: {e}")
|
|
await asyncio.sleep(self.interval)
|