mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-09 01:09:51 +03:00
Dashboard implementation
This commit is contained in:
@@ -473,15 +473,25 @@ uint8_t WebCustomEntityService::count_entities() {
|
||||
}
|
||||
|
||||
// send to dashboard, msgpack don't like serialized, use number
|
||||
void WebCustomEntityService::generate_value_web(JsonObject output) {
|
||||
JsonArray data = output["data"].to<JsonArray>();
|
||||
void WebCustomEntityService::generate_value_web(JsonObject output, const bool is_dashboard) {
|
||||
JsonArray nodes = output["nodes"].to<JsonArray>();
|
||||
uint8_t index = 0;
|
||||
|
||||
for (const CustomEntityItem & entity : *customEntityItems_) {
|
||||
bool include = false;
|
||||
JsonObject obj = data.add<JsonObject>(); // create the object, we know there is a value
|
||||
obj["id"] = "00" + entity.name;
|
||||
obj["u"] = entity.uom;
|
||||
bool include = false;
|
||||
JsonObject root_obj = nodes.add<JsonObject>(); // create the object, we know there is a value
|
||||
|
||||
JsonObject obj;
|
||||
if (is_dashboard) {
|
||||
// TODO make #define for 99
|
||||
root_obj["id"] = (99 * 100) + index; // make unique
|
||||
obj = root_obj["dv"].to<JsonObject>();
|
||||
} else {
|
||||
obj = root_obj;
|
||||
}
|
||||
|
||||
obj["id"] = "00" + entity.name;
|
||||
obj["u"] = entity.uom;
|
||||
|
||||
if (entity.writeable) {
|
||||
obj["c"] = entity.name;
|
||||
@@ -549,7 +559,7 @@ void WebCustomEntityService::generate_value_web(JsonObject output) {
|
||||
if (include) {
|
||||
index++;
|
||||
} else {
|
||||
data.remove(index);
|
||||
nodes.remove(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ class WebCustomEntityService : public StatefulService<WebCustomEntity> {
|
||||
void fetch();
|
||||
void render_value(JsonObject output, CustomEntityItem & entity, const bool useVal = false, const bool web = false, const bool add_uom = false);
|
||||
void show_values(JsonObject output);
|
||||
void generate_value_web(JsonObject output);
|
||||
void generate_value_web(JsonObject output, const bool is_dashboard = false);
|
||||
|
||||
uint8_t count_entities();
|
||||
void ha_reset() {
|
||||
|
||||
@@ -43,9 +43,13 @@ WebDataService::WebDataService(AsyncWebServer * server, SecurityManager * securi
|
||||
server->on(EMSESP_SENSOR_DATA_SERVICE_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { sensor_data(request); }, AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
|
||||
server->on(EMSESP_DASHBOARD_DATA_SERVICE_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { dashboard_data(request); }, AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
}
|
||||
|
||||
// this is used in the dashboard and contains all ems device information
|
||||
// this is used in the Devices page and contains all EMS device information
|
||||
// /coreData endpoint
|
||||
void WebDataService::core_data(AsyncWebServerRequest * request) {
|
||||
auto * response = new AsyncJsonResponse(false);
|
||||
@@ -150,6 +154,7 @@ void WebDataService::sensor_data(AsyncWebServerRequest * request) {
|
||||
}
|
||||
|
||||
// The unique_id is the unique record ID from the Web table to identify which device to load
|
||||
// endpoint /rest/deviceData?id=n
|
||||
// Compresses the JSON using MsgPack https://msgpack.org/index.html
|
||||
void WebDataService::device_data(AsyncWebServerRequest * request) {
|
||||
uint8_t id;
|
||||
@@ -346,4 +351,53 @@ void WebDataService::write_analog_sensor(AsyncWebServerRequest * request, JsonVa
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
// this is used in the dashboard and contains all ems device information
|
||||
// /dashboardData endpoint
|
||||
void WebDataService::dashboard_data(AsyncWebServerRequest * request) {
|
||||
auto * response = new AsyncJsonResponse(true, true); // its an Array and also msgpack'd
|
||||
|
||||
#if defined(EMSESP_STANDALONE)
|
||||
JsonDocument doc;
|
||||
JsonArray root = doc.to<JsonArray>();
|
||||
#else
|
||||
JsonArray root = response->getRoot();
|
||||
#endif
|
||||
|
||||
for (const auto & emsdevice : EMSESP::emsdevices) {
|
||||
if (emsdevice->count_entities_fav()) {
|
||||
JsonObject obj = root.add<JsonObject>();
|
||||
obj["id"] = emsdevice->unique_id(); // it's unique id
|
||||
obj["n"] = emsdevice->name(); // custom name
|
||||
obj["t"] = emsdevice->device_type(); // device type number
|
||||
emsdevice->generate_values_web(obj, true); // is_dashboard = true
|
||||
}
|
||||
}
|
||||
|
||||
// add custom entities, if we have any
|
||||
if (EMSESP::webCustomEntityService.count_entities()) {
|
||||
JsonObject obj = root.add<JsonObject>();
|
||||
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
|
||||
EMSESP::webCustomEntityService.generate_value_web(obj, true);
|
||||
}
|
||||
|
||||
// add temperature sensors
|
||||
|
||||
// add analog sensors
|
||||
|
||||
// show scheduler, with name, on/off - and pencil edit
|
||||
|
||||
#if defined(EMSESP_TEST) && defined(EMSESP_STANDALONE)
|
||||
Serial.println();
|
||||
Serial.print("ALL dashboard_data: ");
|
||||
serializeJsonPretty(root, Serial);
|
||||
Serial.println();
|
||||
#endif
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
|
||||
} // namespace emsesp
|
||||
@@ -23,6 +23,7 @@
|
||||
#define EMSESP_CORE_DATA_SERVICE_PATH "/rest/coreData"
|
||||
#define EMSESP_DEVICE_DATA_SERVICE_PATH "/rest/deviceData"
|
||||
#define EMSESP_SENSOR_DATA_SERVICE_PATH "/rest/sensorData"
|
||||
#define EMSESP_DASHBOARD_DATA_SERVICE_PATH "/rest/dashboardData"
|
||||
|
||||
// POST
|
||||
#define EMSESP_WRITE_DEVICE_VALUE_SERVICE_PATH "/rest/writeDeviceValue"
|
||||
@@ -44,6 +45,7 @@ class WebDataService {
|
||||
void core_data(AsyncWebServerRequest * request);
|
||||
void sensor_data(AsyncWebServerRequest * request);
|
||||
void device_data(AsyncWebServerRequest * request);
|
||||
void dashboard_data(AsyncWebServerRequest * request);
|
||||
|
||||
// POST
|
||||
void write_device_value(AsyncWebServerRequest * request, JsonVariant json);
|
||||
|
||||
Reference in New Issue
Block a user