soil.cpp
1 #include "routes.h" 2 #include <manager.h> 3 4 #include <ESPAsyncWebServer.h> 5 #include <AsyncJson.h> 6 #include <ArduinoJson.h> 7 8 namespace { 9 10 void handle_get(AsyncWebServerRequest *request) { 11 SensorInventorySnapshot inventory = {}; 12 sensors::manager::accessInventory(&inventory); 13 14 AsyncJsonResponse *response = new AsyncJsonResponse(); 15 JsonObject root = response->getRoot().to<JsonObject>(); 16 root["ok"] = inventory.soil_probe_count > 0; 17 JsonObject data = root["data"].to<JsonObject>(); 18 data["probe_count"] = inventory.soil_probe_count; 19 20 JsonArray probes = data["probes"].to<JsonArray>(); 21 for (uint8_t index = 0; index < inventory.soil_probe_count; index++) { 22 SoilSensorData sensor_data = {}; 23 bool ok = sensors::manager::accessSoil(index, &sensor_data); 24 25 JsonObject probe = probes.add<JsonObject>(); 26 probe["index"] = index; 27 probe["slave_id"] = sensor_data.slave_id; 28 probe["read_ok"] = ok; 29 if (ok) { 30 probe["temperature_celsius"] = sensor_data.temperature_celsius; 31 probe["moisture_percent"] = sensor_data.moisture_percent; 32 probe["conductivity"] = sensor_data.conductivity; 33 probe["salinity"] = sensor_data.salinity; 34 probe["tds"] = sensor_data.tds; 35 } 36 } 37 38 response->setLength(); 39 request->send(response); 40 } 41 42 } 43 44 void services::http::api::sensors::soil::registerRoutes(AsyncWebServer &server) { 45 server.on("/api/sensors/soil", HTTP_GET, handle_get); 46 }