Timeout before sending + fix api
This commit is contained in:
@@ -415,10 +415,10 @@ async def handle_telemetry_request(endpoint: str):
|
||||
# 1. Fetch systemd logs asynchronously
|
||||
logs_list = await get_systemd_logs(lines=200, service_name="smartwave")
|
||||
|
||||
# 2. Unpack temperature and humidity from sensors.temp_hum
|
||||
# 2. Unpack temperature and humidity
|
||||
ambient_temp, ambient_humidity = temp_hum.get_temperature_and_humidity_with_retry()
|
||||
|
||||
# 3. Build the telemetry payload
|
||||
# 3. Build the telemetry payload (returns a JSON string)
|
||||
telemetry_payload = payloads.telemetry_payload(
|
||||
device_id=DEVICE_ID,
|
||||
microwave_states=microwave_states,
|
||||
@@ -431,14 +431,18 @@ async def handle_telemetry_request(endpoint: str):
|
||||
logs=logs_list
|
||||
)
|
||||
|
||||
print(telemetry_payload)
|
||||
print(f"[Telemetry] Sending payload with {len(logs_list)} log entries...")
|
||||
|
||||
# 4. Offload blocking HTTP POST to thread pool
|
||||
# 4. Wait for he microwave turn to send the telemetry data to the cloud endpoint
|
||||
timeout = int(DEVICE_ID.split("_")[-1]) * config.TELEMETRY_SEND_INTERVAL
|
||||
print(f"[Telemetry] Waiting for {timeout}s before sending telemetry to avoid collisions...")
|
||||
await asyncio.sleep(timeout)
|
||||
|
||||
# 5. Offload blocking HTTP POST to thread pool
|
||||
response = await asyncio.to_thread(
|
||||
requests.post,
|
||||
endpoint,
|
||||
json=telemetry_payload,
|
||||
data=telemetry_payload, # <-- Changed from json=telemetry_payload
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=15
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user