From d373309feab3df584181627ddb95ef82c5031b08 Mon Sep 17 00:00:00 2001 From: proddy Date: Thu, 10 Oct 2024 21:27:00 +0100 Subject: [PATCH] Implements back end for Dashboard - Feature: Dashboard showing all data (favourites, sensors, custom) #1958 --- src/web/WebDataService.cpp | 75 +++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/src/web/WebDataService.cpp b/src/web/WebDataService.cpp index f8d8e7979..1aa88ac36 100644 --- a/src/web/WebDataService.cpp +++ b/src/web/WebDataService.cpp @@ -376,22 +376,85 @@ void WebDataService::dashboard_data(AsyncWebServerRequest * request) { // add custom entities, if we have any if (EMSESP::webCustomEntityService.count_entities()) { JsonObject obj = root.add(); - obj["id"] = 99; // it's unique id - obj["n"] = Helpers::translated_word(FL_(custom_device_name)); // custom name - obj["t"] = EMSdevice::DeviceType::CUSTOM; // device type number + obj["id"] = EMSdevice::DeviceTypeUniqueID::CUSTOM_UID; // it's unique id + obj["t"] = EMSdevice::DeviceType::CUSTOM; // device type number EMSESP::webCustomEntityService.generate_value_web(obj, true); } // add temperature sensors + if (EMSESP::temperaturesensor_.have_sensors()) { + JsonObject obj = root.add(); + obj["id"] = EMSdevice::DeviceTypeUniqueID::TEMPERATURESENSOR_UID; // it's unique id + obj["t"] = EMSdevice::DeviceType::TEMPERATURESENSOR; // device type number + JsonArray nodes = obj["nodes"].to(); + uint8_t count = 0; + for (const auto & sensor : EMSESP::temperaturesensor_.sensors()) { + JsonObject node = nodes.add(); + node["id"] = (EMSdevice::DeviceTypeUniqueID::TEMPERATURESENSOR_UID * 100) + count++; + + JsonObject dv = node["dv"].to(); + dv["id"] = sensor.name(); + if (EMSESP::system_.fahrenheit()) { + if (Helpers::hasValue(sensor.temperature_c)) { + dv["v"] = (float)sensor.temperature_c * 0.18 + 32; + } + dv["u"] = DeviceValueUOM::FAHRENHEIT; + } else { + if (Helpers::hasValue(sensor.temperature_c)) { + dv["v"] = (float)sensor.temperature_c / 10; + } + dv["u"] = DeviceValueUOM::DEGREES; + } + } + } // add analog sensors + if (EMSESP::analog_enabled() && EMSESP::analogsensor_.have_sensors()) { + JsonObject obj = root.add(); + obj["id"] = EMSdevice::DeviceTypeUniqueID::ANALOGSENSOR_UID; // it's unique id + obj["t"] = EMSdevice::DeviceType::ANALOGSENSOR; // device type number + JsonArray nodes = obj["nodes"].to(); + uint8_t count = 0; + for (const auto & sensor : EMSESP::analogsensor_.sensors()) { + if (sensor.type() != AnalogSensor::AnalogType::NOTUSED) { + JsonObject node = nodes.add(); + node["id"] = (EMSdevice::DeviceTypeUniqueID::ANALOGSENSOR_UID * 100) + count++; - // show scheduler, with name, on/off - and pencil edit + JsonObject dv = node["dv"].to(); + dv["id"] = sensor.name(); + dv["v"] = Helpers::transformNumFloat(sensor.value(), 0); // is optional and is a float + dv["u"] = sensor.uom(); + } + } + } + + // show scheduler, with name, on/off + if (EMSESP::webSchedulerService.count_entities()) { + JsonObject obj = root.add(); + obj["id"] = EMSdevice::DeviceTypeUniqueID::SCHEDULER_UID; // it's unique id + obj["t"] = EMSdevice::DeviceType::SCHEDULER; // device type number + JsonArray nodes = obj["nodes"].to(); + uint8_t count = 0; + + EMSESP::webSchedulerService.read([&](const WebScheduler & webScheduler) { + for (const ScheduleItem & scheduleItem : webScheduler.scheduleItems) { + // only add if we have a name - we don't need a u (UOM) for this + if (!scheduleItem.name.empty()) { + JsonObject node = nodes.add(); + node["id"] = (EMSdevice::DeviceTypeUniqueID::SCHEDULER_UID * 100) + count++; + + JsonObject dv = node["dv"].to(); + dv["id"] = scheduleItem.name; + dv["v"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.active : false; // value is active + } + } + }); + } #if defined(EMSESP_TEST) && defined(EMSESP_STANDALONE) Serial.println(); - Serial.print("ALL dashboard_data: "); - serializeJsonPretty(root, Serial); + Serial.print("All dashboard_data: "); + serializeJson(root, Serial); Serial.println(); #endif