diff --git a/simulator.py b/simulator.py index 22ea0143..15039e46 100644 --- a/simulator.py +++ b/simulator.py @@ -748,32 +748,37 @@ def publish_openremote(sid: str, sensor: dict, values: dict) -> bool: return _or_put(asset_id, payload) def publish_influx(sid: str, sensor: dict, values: dict) -> bool: - """Write sensor data to InfluxDB.""" + """Write sensor data to InfluxDB (async, non-blocking).""" if not _influx_write_api: return False - try: - stype = sensor["type"] - lat = sensor.get("lat", BASE_LAT) - lon = sensor.get("lon", BASE_LON) - - points = [] - for field, value in values.items(): - if isinstance(value, (int, float)): - p = influxdb_client.Point(stype)\ - .tag("sensor_id", sid)\ - .tag("location", sensor.get("name", sid))\ - .field(field, float(value))\ - .field("lat", float(lat))\ - .field("lon", float(lon)) - points.append(p) - - if points: - _influx_write_api.write(bucket=INFLUX_BUCKET, record=points) - print(f" πŸ“ˆ InfluxDB: {len(points)} points written") - return True - except Exception as e: - print(f" ⚠️ InfluxDB β†’ {e}") - return False + + def _write_async(): + try: + stype = sensor["type"] + lat = sensor.get("lat", BASE_LAT) + lon = sensor.get("lon", BASE_LON) + + points = [] + for field, value in values.items(): + if isinstance(value, (int, float)): + p = influxdb_client.Point(stype)\ + .tag("sensor_id", sid)\ + .tag("location", sensor.get("name", sid))\ + .field(field, float(value))\ + .field("lat", float(lat))\ + .field("lon", float(lon)) + points.append(p) + + if points: + _influx_write_api.write(bucket=INFLUX_BUCKET, record=points) + print(f" πŸ“ˆ InfluxDB: {len(points)} points written") + except Exception as e: + print(f" ⚠️ InfluxDB β†’ {e}") + + # ExΓ©cution asynchrone (non-bloquante) + t = threading.Thread(target=_write_async, daemon=True) + t.start() + return True def main(): print("╔══════════════════════════════════════════════════╗")