Browse Source

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.

maziggy 3 months ago
parent
commit
09e6bfd121
1 changed files with 6 additions and 4 deletions
  1. 6 4
      spoolbuddy/daemon/scale_reader.py

+ 6 - 4
spoolbuddy/daemon/scale_reader.py

@@ -11,9 +11,7 @@ MOVING_AVG_SIZE = 5
 
 
 class ScaleReader:
 class ScaleReader:
     def __init__(self, tare_offset: int = 0, calibration_factor: float = 1.0):
     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._tare_offset = tare_offset
         self._calibration_factor = calibration_factor
         self._calibration_factor = calibration_factor
         self._samples: deque[float] = deque(maxlen=MOVING_AVG_SIZE)
         self._samples: deque[float] = deque(maxlen=MOVING_AVG_SIZE)
@@ -22,6 +20,9 @@ class ScaleReader:
         self._last_raw = 0
         self._last_raw = 0
 
 
         try:
         try:
+            from scale_diag import NAU7802
+
+            self._scale = NAU7802()
             self._scale.init()
             self._scale.init()
             self._ok = True
             self._ok = True
             logger.info("Scale initialized (tare=%d, cal=%.6f)", tare_offset, calibration_factor)
             logger.info("Scale initialized (tare=%d, cal=%.6f)", tare_offset, calibration_factor)
@@ -38,7 +39,8 @@ class ScaleReader:
 
 
     def close(self):
     def close(self):
         try:
         try:
-            self._scale.close()
+            if self._scale:
+                self._scale.close()
         except Exception:
         except Exception:
             pass
             pass