api, daemon, and process updates
This commit is contained in:
1 parent
3d1a90032a
commit
1e91a43407
3 files changed
+29
-10
No files matched your search
@@ -93,13 +93,18 @@ class ConnectionManager:
|
||||
|
||||
ws_manager = ConnectionManager()
|
||||
|
||||
_db: Optional[aiosqlite.Connection] = None
|
||||
|
||||
|
||||
def set_db(connection: aiosqlite.Connection) -> None:
|
||||
global _db
|
||||
_db = connection
|
||||
|
||||
|
||||
async def get_db() -> aiosqlite.Connection:
|
||||
db = await storage.init_db()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
await db.close()
|
||||
if _db is None:
|
||||
raise RuntimeError("Database not initialized — call set_db() before starting the API")
|
||||
yield _db
|
||||
|
||||
|
||||
@app.websocket("/ws")
|
||||
|
||||
+4
-2
@@ -48,7 +48,7 @@ import uvicorn
|
||||
from cultivar_types import STM32Payload
|
||||
from serial_reader import SerialManager, TCPChamberReader
|
||||
from processor import Processor, BaselineBuilder
|
||||
from api import app, ws_manager
|
||||
from api import app, ws_manager, set_db as api_set_db
|
||||
import storage
|
||||
# ── Logging ───────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -236,12 +236,13 @@ async def _handle_payload(
|
||||
for anomaly in processed.anomalies:
|
||||
await ws_manager.broadcast_anomaly(anomaly)
|
||||
|
||||
z1_str = f"Z1={processed.zone_scores.Z1:.3f} " if processed.zone_scores.Z1 is not None else ""
|
||||
log.info(
|
||||
f"[{chamber_id}] Processed payload {payload_id} — "
|
||||
f"T={raw.ambient.temp_c:.1f}°C "
|
||||
f"RH={raw.ambient.humidity_pct:.1f}% "
|
||||
f"{'CO2=' + str(raw.co2.ppm) + 'ppm ' if raw.co2 and raw.co2.valid else ''}"
|
||||
f"Z1={processed.zone_scores.Z1:.3f} " if processed.zone_scores.Z1 else ""
|
||||
f"{z1_str}"
|
||||
f"anomalies={len(processed.anomalies)}"
|
||||
)
|
||||
|
||||
@@ -267,6 +268,7 @@ async def main() -> None:
|
||||
# Init DB
|
||||
db_path = Path(cfg.DB_PATH)
|
||||
db = await storage.init_db(db_path)
|
||||
api_set_db(db)
|
||||
log.info(f"Database: {db_path}")
|
||||
|
||||
# Init chamber state
|
||||
|
||||
+15
-3
@@ -201,7 +201,12 @@ class Processor:
|
||||
return (raw.ambient.temp_c, "bme_raw")
|
||||
|
||||
def _select_rh_source(self, raw: STM32Payload) -> tuple[float, str]:
|
||||
if raw.co2 and raw.co2.valid and raw.health.humidity_ok:
|
||||
# Prefer SCD43 whenever present and valid — the BME690 humidity
|
||||
# compensation is known to produce 0% when the ADC is mis-ranged
|
||||
# (see health.humidity_ok). humidity_ok=False is the signal that
|
||||
# the two sensors disagree, which is precisely the condition where
|
||||
# we want the SCD43 rather than the BME reading.
|
||||
if raw.co2 and raw.co2.valid:
|
||||
return (raw.co2.humidity_pct, "scd43")
|
||||
return (raw.ambient.humidity_pct, "bme_raw")
|
||||
|
||||
@@ -778,13 +783,20 @@ class BaselineBuilder:
|
||||
if len(self._samples) > self._max_samples:
|
||||
self._samples = self._samples[-self._max_samples:]
|
||||
|
||||
@staticmethod
|
||||
def _best_humidity(s: STM32Payload) -> float:
|
||||
"""Return the best available humidity reading, preferring SCD43."""
|
||||
if s.co2 and s.co2.valid:
|
||||
return s.co2.humidity_pct
|
||||
return s.ambient.humidity_pct
|
||||
|
||||
def established(self) -> bool:
|
||||
"""Return True if enough stable samples have been collected."""
|
||||
if len(self._samples) < self.min_samples:
|
||||
return False
|
||||
|
||||
recent = self._samples[-self.min_samples:]
|
||||
humidities = [s.ambient.humidity_pct for s in recent]
|
||||
humidities = [self._best_humidity(s) for s in recent]
|
||||
variance = max(humidities) - min(humidities)
|
||||
|
||||
if variance > self.humidity_variance:
|
||||
@@ -817,7 +829,7 @@ class BaselineBuilder:
|
||||
if vals
|
||||
}
|
||||
|
||||
humidities = [s.ambient.humidity_pct for s in recent]
|
||||
humidities = [self._best_humidity(s) for s in recent]
|
||||
temps = [s.ambient.temp_c for s in recent]
|
||||
co2_vals = [s.co2.ppm for s in recent if s.co2 and s.co2.valid]
|
||||
|
||||
|
||||
Reference in new issue
Block a user