digital_out, settings #1375, dev.5b

This commit is contained in:
MichaelDvP
2023-11-02 19:13:22 +01:00
parent 1097b519ae
commit 8307b0920c
21 changed files with 325 additions and 195 deletions

View File

@@ -22,6 +22,8 @@ namespace emsesp {
using namespace std::placeholders; // for `_1` etc
bool WebCustomization::_start = true;
WebCustomizationService::WebCustomizationService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
: _httpEndpoint(WebCustomization::read,
WebCustomization::update,
@@ -132,10 +134,13 @@ StateUpdateResult WebCustomization::update(JsonObject & root, WebCustomization &
sensor.factor = analogJson["factor"];
sensor.uom = analogJson["uom"];
sensor.type = analogJson["type"];
if (_start && sensor.type == EMSESP::analogsensor_.AnalogType::DIGITAL_OUT && sensor.uom > DeviceValue::DeviceValueUOM::NONE) {
sensor.offset = sensor.uom - 1;
}
customizations.analogCustomizations.push_back(sensor); // add to list
}
}
_start = false;
// load array of entities id's with masks, building up the object class
customizations.entityCustomizations.clear();
if (root["masked_entities"].is<JsonArray>()) {
@@ -203,7 +208,7 @@ void WebCustomizationService::device_entities(AsyncWebServerRequest * request) {
size_t buffer = EMSESP_JSON_SIZE_XXXXLARGE;
auto * response = new MsgpackAsyncJsonResponse(true, buffer);
while (!response->getSize()) {
while (!response) {
delete response;
buffer -= 1024;
response = new MsgpackAsyncJsonResponse(true, buffer);
@@ -259,7 +264,7 @@ void WebCustomizationService::customization_entities(AsyncWebServerRequest * req
// emsesp::EMSESP::logger().info(id.as<const char *>());
}
// add deleted entities from file
EMSESP::webCustomizationService.read([&](WebCustomization & settings) {
read([&](WebCustomization & settings) {
for (EntityCustomization entityCustomization : settings.entityCustomizations) {
if (entityCustomization.device_id == device_id) {
for (std::string entity_id : entityCustomization.entity_ids) {
@@ -288,7 +293,7 @@ void WebCustomizationService::customization_entities(AsyncWebServerRequest * req
emsdevice->getCustomizationEntities(entity_ids);
// Save the list to the customization file
EMSESP::webCustomizationService.update(
update(
[&](WebCustomization & settings) {
// see if we already have a mask list for this device, if so remove it
for (auto it = settings.entityCustomizations.begin(); it != settings.entityCustomizations.end();) {

View File

@@ -73,6 +73,9 @@ class WebCustomization {
std::list<EntityCustomization> entityCustomizations; // for a list of entities that have a special mask set
static void read(WebCustomization & customizations, JsonObject & root);
static StateUpdateResult update(JsonObject & root, WebCustomization & customizations);
private:
static bool _start;
};
class WebCustomizationService : public StatefulService<WebCustomization> {

View File

@@ -93,7 +93,8 @@ void WebDataService::core_data(AsyncWebServerRequest * request) {
}
// add any custom entities
if (EMSESP::webCustomEntityService.count_entities()) {
uint8_t customEntities = EMSESP::webCustomEntityService.count_entities();
if (customEntities) {
JsonObject obj = devices.createNestedObject();
obj["id"] = 99; // the last unique id
obj["tn"] = Helpers::translated_word(FL_(custom_device)); // translated device type name
@@ -103,7 +104,7 @@ void WebDataService::core_data(AsyncWebServerRequest * request) {
obj["d"] = 0; // deviceid
obj["p"] = 0; // productid
obj["v"] = 0; // version
obj["e"] = EMSESP::webCustomEntityService.count_entities(); // number of custom entities
obj["e"] = customEntities; // number of custom entities
}
root["connected"] = EMSESP::bus_status() != 2;
@@ -163,7 +164,8 @@ void WebDataService::sensor_data(AsyncWebServerRequest * request) {
}
}
root["analog_enabled"] = EMSESP::analogsensor_.analog_enabled();
root["analog_enabled"] = EMSESP::analog_enabled();
root["platform"] = EMSESP_PLATFORM;
response->setLength();
request->send(response);
@@ -180,7 +182,7 @@ void WebDataService::device_data(AsyncWebServerRequest * request) {
auto * response = new MsgpackAsyncJsonResponse(false, buffer);
// check size
while (!response->getSize()) {
while (!response) {
delete response;
buffer -= 1024;
response = new MsgpackAsyncJsonResponse(false, buffer);