mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-18 03:54:17 +02:00
moved from scale_diag import NAU7802 and NAU7802() inside the try/except block in ScaleReader.__init__. Previously, NAU7802() opened /dev/i2c-0 in its
constructor before the try/except, so the FileNotFoundError was unhandled and crashed the entire daemon via asyncio.gather(). Now when the scale hardware isn't available (no /dev/i2c-0), it logs "Scale init failed" and sets self._ok = False, so scale_poll_loop will skip polling with "Scale not available, skipping scale polling" — same graceful degradation the NFC reader already has.
This commit is contained in:
@@ -11,9 +11,7 @@ MOVING_AVG_SIZE = 5
|
||||
|
||||
class ScaleReader:
|
||||
def __init__(self, tare_offset: int = 0, calibration_factor: float = 1.0):
|
||||
from scale_diag import NAU7802
|
||||
|
||||
self._scale = NAU7802()
|
||||
self._scale = None
|
||||
self._tare_offset = tare_offset
|
||||
self._calibration_factor = calibration_factor
|
||||
self._samples: deque[float] = deque(maxlen=MOVING_AVG_SIZE)
|
||||
@@ -22,6 +20,9 @@ class ScaleReader:
|
||||
self._last_raw = 0
|
||||
|
||||
try:
|
||||
from scale_diag import NAU7802
|
||||
|
||||
self._scale = NAU7802()
|
||||
self._scale.init()
|
||||
self._ok = True
|
||||
logger.info("Scale initialized (tare=%d, cal=%.6f)", tare_offset, calibration_factor)
|
||||
@@ -38,7 +39,8 @@ class ScaleReader:
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
self._scale.close()
|
||||
if self._scale:
|
||||
self._scale.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user