mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-28 09:39:11 +03:00
more to psram, names for sensors, schedule, custom as char[20]
This commit is contained in:
@@ -55,7 +55,7 @@ void WebCustomEntity::read(WebCustomEntity & webEntity, JsonObject root) {
|
||||
ei["type_id"] = entityItem.type_id;
|
||||
ei["offset"] = entityItem.offset;
|
||||
ei["factor"] = entityItem.factor;
|
||||
ei["name"] = entityItem.name;
|
||||
ei["name"] = (const char *)entityItem.name;
|
||||
ei["uom"] = entityItem.value_type == DeviceValueType::BOOL ? 0 : entityItem.uom;
|
||||
ei["value_type"] = entityItem.value_type;
|
||||
ei["writeable"] = entityItem.writeable;
|
||||
@@ -91,12 +91,12 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
|
||||
entityItem.type_id = ei["type_id"];
|
||||
entityItem.offset = ei["offset"];
|
||||
entityItem.factor = ei["factor"];
|
||||
entityItem.name = ei["name"].as<std::string>();
|
||||
entityItem.uom = ei["uom"];
|
||||
entityItem.value_type = ei["value_type"];
|
||||
entityItem.writeable = ei["writeable"];
|
||||
entityItem.hide = ei["hide"] | false;
|
||||
entityItem.data = ei["value"].as<std::string>();
|
||||
strlcpy(entityItem.name, ei["name"].as<const char *>(), sizeof(entityItem.name));
|
||||
if (entityItem.ram == 1) {
|
||||
entityItem.device_id = 0;
|
||||
entityItem.type_id = 0;
|
||||
@@ -130,12 +130,12 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
|
||||
|
||||
webCustomEntity.customEntityItems.push_back(entityItem); // add to list
|
||||
|
||||
if (entityItem.writeable && !entityItem.name.empty()) {
|
||||
if (entityItem.writeable && entityItem.name[0] != '\0') {
|
||||
Command::add(
|
||||
EMSdevice::DeviceType::CUSTOM,
|
||||
webCustomEntity.customEntityItems.back().name.c_str(),
|
||||
webCustomEntity.customEntityItems.back().name,
|
||||
[webCustomEntity](const char * value, const int8_t id) {
|
||||
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str());
|
||||
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name);
|
||||
},
|
||||
FL_(entity_cmd),
|
||||
CommandFlag::ADMIN_ONLY);
|
||||
@@ -143,7 +143,7 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
|
||||
|
||||
if (entityItem.ram && doc[entityItem.name].is<JsonVariantConst>() && doc[entityItem.name] != entityItem.value) {
|
||||
char cmd[COMMAND_MAX_LENGTH];
|
||||
snprintf(cmd, sizeof(cmd), "%s/%s", F_(custom), entityItem.name.c_str());
|
||||
snprintf(cmd, sizeof(cmd), "%s/%s", F_(custom), entityItem.name);
|
||||
EMSESP::webSchedulerService.onChange(cmd);
|
||||
}
|
||||
}
|
||||
@@ -213,7 +213,7 @@ bool WebCustomEntityService::command_setvalue(const char * value, const int8_t i
|
||||
publish();
|
||||
}
|
||||
char cmd[COMMAND_MAX_LENGTH];
|
||||
snprintf(cmd, sizeof(cmd), "%s/%s", F_(custom), entityItem.name.c_str());
|
||||
snprintf(cmd, sizeof(cmd), "%s/%s", F_(custom), entityItem.name);
|
||||
EMSESP::webSchedulerService.onChange(cmd);
|
||||
return true;
|
||||
}
|
||||
@@ -224,19 +224,16 @@ bool WebCustomEntityService::command_setvalue(const char * value, const int8_t i
|
||||
// output of a single value
|
||||
// if add_uom is true it will add the UOM string to the value
|
||||
void WebCustomEntityService::render_value(JsonObject output, CustomEntityItem const & entity, const bool useVal, const bool web, const bool add_uom) {
|
||||
char payload[20];
|
||||
std::string name = useVal ? "value" : entity.name;
|
||||
char payload[20];
|
||||
const char * name = useVal ? "value" : entity.name;
|
||||
|
||||
switch (entity.value_type) {
|
||||
case DeviceValueType::BOOL:
|
||||
if ((uint8_t)entity.value != EMS_VALUE_BOOL_NOTSET) {
|
||||
if (web) {
|
||||
output[name] = Helpers::render_boolean(payload, (uint8_t)entity.value, true);
|
||||
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||
output[name] = (uint8_t)entity.value ? true : false;
|
||||
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||
output[name] = (uint8_t)entity.value ? 1 : 0;
|
||||
} else {
|
||||
output[name] = Helpers::render_boolean(payload, (uint8_t)entity.value);
|
||||
Mqtt::add_value_bool(output, name, (uint8_t)entity.value != 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -248,7 +245,7 @@ void WebCustomEntityService::render_value(JsonObject output, CustomEntityItem co
|
||||
break;
|
||||
case DeviceValueType::UINT8:
|
||||
if ((uint8_t)entity.value != EMS_VALUE_UINT8_NOTSET) {
|
||||
std::string v = Helpers::render_value(payload, entity.factor * (uint8_t)entity.value, 2);
|
||||
std::string v = Helpers::render_value(payload, entity.factor * (int8_t)entity.value, 2);
|
||||
output[name] = add_uom ? serialized(v + ' ' + EMSdevice::uom_to_string(entity.uom)) : serialized(v);
|
||||
}
|
||||
break;
|
||||
@@ -336,8 +333,8 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
|
||||
|
||||
// build the json for specific entity
|
||||
void WebCustomEntityService::get_value_json(JsonObject output, CustomEntityItem const & entity) {
|
||||
output["name"] = entity.name;
|
||||
output["fullname"] = entity.name;
|
||||
output["name"] = (const char *)entity.name;
|
||||
output["fullname"] = (const char *)entity.name;
|
||||
output["storage"] = entity.ram ? "ram" : "ems";
|
||||
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number);
|
||||
output["readable"] = true;
|
||||
@@ -369,9 +366,9 @@ void WebCustomEntityService::publish_single(CustomEntityItem & entity) {
|
||||
|
||||
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||
if (Mqtt::publish_single2cmd()) {
|
||||
snprintf(topic, sizeof(topic), "%s/%s", F_(custom), entity.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "%s/%s", F_(custom), entity.name);
|
||||
} else {
|
||||
snprintf(topic, sizeof(topic), "%s_data/%s", F_(custom), entity.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "%s_data/%s", F_(custom), entity.name);
|
||||
}
|
||||
|
||||
JsonDocument doc;
|
||||
@@ -411,15 +408,15 @@ void WebCustomEntityService::publish(const bool force) {
|
||||
// create HA config
|
||||
if (Mqtt::ha_enabled() && !ha_registered_) {
|
||||
JsonDocument config;
|
||||
config["~"] = Mqtt::base();
|
||||
config["~"] = Mqtt::base();
|
||||
|
||||
char stat_t[50];
|
||||
char stat_t[50];
|
||||
snprintf(stat_t, sizeof(stat_t), "~/%s_data", F_(custom));
|
||||
config["stat_t"] = stat_t;
|
||||
|
||||
char val_obj[50];
|
||||
char val_cond[65];
|
||||
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", entityItem.name.c_str());
|
||||
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", entityItem.name);
|
||||
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
|
||||
// don't bother with value template conditions if using Domoticz which doesn't fully support MQTT Discovery
|
||||
if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
|
||||
@@ -429,31 +426,31 @@ void WebCustomEntityService::publish(const bool force) {
|
||||
}
|
||||
|
||||
char uniq_s[70];
|
||||
snprintf(uniq_s, sizeof(uniq_s), "%s_%s", F_(custom), entityItem.name.c_str());
|
||||
snprintf(uniq_s, sizeof(uniq_s), "%s_%s", F_(custom), entityItem.name);
|
||||
|
||||
config["uniq_id"] = uniq_s;
|
||||
config["name"] = entityItem.name.c_str();
|
||||
config["name"] = (const char *)entityItem.name;
|
||||
|
||||
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||
|
||||
if (entityItem.writeable) {
|
||||
if (entityItem.value_type == DeviceValueType::BOOL) {
|
||||
snprintf(topic, sizeof(topic), "switch/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "switch/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name);
|
||||
} else if (entityItem.value_type == DeviceValueType::STRING) {
|
||||
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name);
|
||||
} else if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT || Mqtt::discovery_type() == Mqtt::discoveryType::DOMOTICZ_LATEST) {
|
||||
snprintf(topic, sizeof(topic), "number/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "number/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name);
|
||||
} else {
|
||||
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name);
|
||||
}
|
||||
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||
snprintf(command_topic, sizeof(command_topic), "~/%s/%s", F_(custom), entityItem.name.c_str());
|
||||
snprintf(command_topic, sizeof(command_topic), "~/%s/%s", F_(custom), entityItem.name);
|
||||
config["cmd_t"] = command_topic;
|
||||
} else {
|
||||
if (entityItem.value_type == DeviceValueType::BOOL) {
|
||||
snprintf(topic, sizeof(topic), "binary_sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "binary_sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name);
|
||||
} else {
|
||||
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(custom), entityItem.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -523,7 +520,7 @@ void WebCustomEntityService::generate_value_web(JsonObject output, const bool is
|
||||
obj = root_obj;
|
||||
}
|
||||
|
||||
obj["id"] = "00" + entity.name;
|
||||
obj["id"] = std::string("00") + entity.name;
|
||||
if (entity.value_type != DeviceValueType::BOOL) {
|
||||
obj["u"] = entity.uom;
|
||||
}
|
||||
@@ -648,15 +645,15 @@ bool WebCustomEntityService::get_value(std::shared_ptr<const Telegram> telegram)
|
||||
if (rest > 0) {
|
||||
memcpy(&entity.raw[offset], message_data, rest);
|
||||
auto data = Helpers::data_to_hex(entity.raw, (uint8_t)length);
|
||||
if (entity.data != data && length == (int)entity.factor) {
|
||||
entity.data = data;
|
||||
if (entity.data != data.c_str() && length == (int)entity.factor) {
|
||||
entity.data = data.c_str();
|
||||
if (Mqtt::publish_single()) {
|
||||
publish_single(entity);
|
||||
} else if (EMSESP::mqtt_.get_publish_onchange(0)) {
|
||||
has_change = true;
|
||||
}
|
||||
char cmd[COMMAND_MAX_LENGTH];
|
||||
snprintf(cmd, sizeof(cmd), "%s/%s", F_(custom), entity.name.c_str());
|
||||
snprintf(cmd, sizeof(cmd), "%s/%s", F_(custom), entity.name);
|
||||
EMSESP::webSchedulerService.onChange(cmd);
|
||||
}
|
||||
}
|
||||
@@ -678,10 +675,10 @@ bool WebCustomEntityService::get_value(std::shared_ptr<const Telegram> telegram)
|
||||
has_change = true;
|
||||
}
|
||||
char cmd[COMMAND_MAX_LENGTH];
|
||||
snprintf(cmd, sizeof(cmd), "%s/%s", F_(custom), entity.name.c_str());
|
||||
snprintf(cmd, sizeof(cmd), "%s/%s", F_(custom), entity.name);
|
||||
EMSESP::webSchedulerService.onChange(cmd);
|
||||
}
|
||||
// EMSESP::logger().debug("custom entity %s received with value %d", entity.name.c_str(), (int)entity.val);
|
||||
// EMSESP::logger().debug("custom entity %s received with value %d", entity.name, (int)entity.val);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -709,7 +706,7 @@ void WebCustomEntityService::load_test_data() {
|
||||
entityItem.type_id = 24;
|
||||
entityItem.offset = 0;
|
||||
entityItem.factor = 1;
|
||||
entityItem.name = "test_custom";
|
||||
strcpy(entityItem.name,"test_custom");
|
||||
entityItem.uom = 1;
|
||||
entityItem.value_type = 1;
|
||||
entityItem.writeable = true;
|
||||
@@ -718,9 +715,9 @@ void WebCustomEntityService::load_test_data() {
|
||||
webCustomEntity.customEntityItems.push_back(entityItem);
|
||||
Command::add(
|
||||
EMSdevice::DeviceType::CUSTOM,
|
||||
webCustomEntity.customEntityItems.back().name.c_str(),
|
||||
webCustomEntity.customEntityItems.back().name,
|
||||
[webCustomEntity](const char * value, const int8_t id) {
|
||||
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str());
|
||||
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name);
|
||||
},
|
||||
FL_(entity_cmd),
|
||||
CommandFlag::ADMIN_ONLY);
|
||||
@@ -732,7 +729,7 @@ void WebCustomEntityService::load_test_data() {
|
||||
entityItem.type_id = 677;
|
||||
entityItem.offset = 3;
|
||||
entityItem.factor = 1;
|
||||
entityItem.name = "test_read_only";
|
||||
strcpy(entityItem.name, "test_read_only");
|
||||
entityItem.uom = 0;
|
||||
entityItem.value_type = 2;
|
||||
entityItem.writeable = false;
|
||||
@@ -746,7 +743,7 @@ void WebCustomEntityService::load_test_data() {
|
||||
entityItem.type_id = 0;
|
||||
entityItem.offset = 0;
|
||||
entityItem.factor = 1;
|
||||
entityItem.name = "test_ram";
|
||||
strcpy(entityItem.name, "test_ram");
|
||||
entityItem.uom = 0;
|
||||
entityItem.value_type = 8;
|
||||
entityItem.writeable = true;
|
||||
@@ -754,9 +751,9 @@ void WebCustomEntityService::load_test_data() {
|
||||
webCustomEntity.customEntityItems.push_back(entityItem);
|
||||
Command::add(
|
||||
EMSdevice::DeviceType::CUSTOM,
|
||||
webCustomEntity.customEntityItems.back().name.c_str(),
|
||||
webCustomEntity.customEntityItems.back().name,
|
||||
[webCustomEntity](const char * value, const int8_t id) {
|
||||
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str());
|
||||
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name);
|
||||
},
|
||||
FL_(entity_cmd),
|
||||
CommandFlag::ADMIN_ONLY);
|
||||
@@ -768,7 +765,7 @@ void WebCustomEntityService::load_test_data() {
|
||||
entityItem.type_id = 0;
|
||||
entityItem.offset = 0;
|
||||
entityItem.factor = 1;
|
||||
entityItem.name = "test_seltemp";
|
||||
strcpy(entityItem.name, "test_seltemp");
|
||||
entityItem.uom = 0;
|
||||
entityItem.value_type = 8;
|
||||
entityItem.writeable = true;
|
||||
@@ -777,9 +774,9 @@ void WebCustomEntityService::load_test_data() {
|
||||
webCustomEntity.customEntityItems.push_back(entityItem);
|
||||
Command::add(
|
||||
EMSdevice::DeviceType::CUSTOM,
|
||||
webCustomEntity.customEntityItems.back().name.c_str(),
|
||||
webCustomEntity.customEntityItems.back().name,
|
||||
[webCustomEntity](const char * value, const int8_t id) {
|
||||
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str());
|
||||
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name);
|
||||
},
|
||||
FL_(entity_cmd),
|
||||
CommandFlag::ADMIN_ONLY);
|
||||
|
||||
@@ -34,11 +34,11 @@ class CustomEntityItem {
|
||||
uint8_t offset;
|
||||
int8_t value_type;
|
||||
uint8_t uom; // DeviceValueUOM
|
||||
std::string name;
|
||||
char name[20];
|
||||
double factor;
|
||||
bool writeable;
|
||||
uint32_t value;
|
||||
std::string data;
|
||||
stringPSRAM data;
|
||||
uint8_t ram;
|
||||
uint8_t * raw;
|
||||
bool hide;
|
||||
|
||||
@@ -54,23 +54,23 @@ void WebCustomization::read(WebCustomization & customizations, JsonObject root)
|
||||
JsonArray sensorsJson = root["ts"].to<JsonArray>();
|
||||
for (const SensorCustomization & sensor : customizations.sensorCustomizations) {
|
||||
JsonObject sensorJson = sensorsJson.add<JsonObject>();
|
||||
sensorJson["id"] = sensor.id; // ID of chip
|
||||
sensorJson["name"] = sensor.name; // n
|
||||
sensorJson["offset"] = sensor.offset; // o
|
||||
sensorJson["is_system"] = sensor.is_system; // s for core_voltage, supply_voltage
|
||||
sensorJson["id"] = (const char *)sensor.id; // ID of chip
|
||||
sensorJson["name"] = (const char *)sensor.name; // n
|
||||
sensorJson["offset"] = sensor.offset; // o
|
||||
sensorJson["is_system"] = sensor.is_system; // s for core_voltage, supply_voltage
|
||||
}
|
||||
|
||||
// Analog Sensor customization
|
||||
JsonArray analogJson = root["as"].to<JsonArray>();
|
||||
for (const AnalogCustomization & sensor : customizations.analogCustomizations) {
|
||||
JsonObject sensorJson = analogJson.add<JsonObject>();
|
||||
sensorJson["gpio"] = sensor.gpio; // g
|
||||
sensorJson["name"] = sensor.name; // n
|
||||
sensorJson["offset"] = sensor.offset; // o
|
||||
sensorJson["factor"] = sensor.factor; // f
|
||||
sensorJson["uom"] = sensor.uom; // u
|
||||
sensorJson["type"] = sensor.type; // t
|
||||
sensorJson["is_system"] = sensor.is_system; // s for core_voltage, supply_voltage
|
||||
sensorJson["gpio"] = sensor.gpio; // g
|
||||
sensorJson["name"] = (const char *)sensor.name; // n
|
||||
sensorJson["offset"] = sensor.offset; // o
|
||||
sensorJson["factor"] = sensor.factor; // f
|
||||
sensorJson["uom"] = sensor.uom; // u
|
||||
sensorJson["type"] = sensor.type; // t
|
||||
sensorJson["is_system"] = sensor.is_system; // s for core_voltage, supply_voltage
|
||||
}
|
||||
|
||||
// Masked entities customization and custom device name (optional)
|
||||
@@ -83,8 +83,8 @@ void WebCustomization::read(WebCustomization & customizations, JsonObject root)
|
||||
|
||||
// entries are in the form <XX><shortname>[optional customname] e.g "08heatingactive|heating is on"
|
||||
JsonArray masked_entityJson = entityJson["entity_ids"].to<JsonArray>();
|
||||
for (const std::string & entity_id : entityCustomization.entity_ids) {
|
||||
masked_entityJson.add(entity_id);
|
||||
for (const auto & entity_id : entityCustomization.entity_ids) {
|
||||
masked_entityJson.add(entity_id.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,16 +98,18 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c
|
||||
auto sensorsJsons = root["ts"].as<JsonArray>();
|
||||
for (const JsonObject sensorJson : sensorsJsons) {
|
||||
// create each of the sensor, overwriting any previous settings
|
||||
auto sensor = SensorCustomization();
|
||||
sensor.id = sensorJson["id"].as<std::string>();
|
||||
sensor.name = sensorJson["name"].as<std::string>();
|
||||
sensor.offset = sensorJson["offset"];
|
||||
if (sensor.id == sensor.name) {
|
||||
sensor.name = ""; // no need to store id as name
|
||||
}
|
||||
auto sensor = SensorCustomization();
|
||||
strlcpy(sensor.id, sensorJson["id"].as<const char *>(), sizeof(sensor.id));
|
||||
strlcpy(sensor.name, sensorJson["name"].as<const char *>(), sizeof(sensor.name));
|
||||
sensor.offset = sensorJson["offset"];
|
||||
sensor.is_system = sensorJson["is_system"] | false;
|
||||
std::replace(sensor.id.begin(), sensor.id.end(), '-', '_'); // change old ids to v3.7 style
|
||||
customizations.sensorCustomizations.push_back(sensor); // add to list
|
||||
// change old ids to v3.7 style
|
||||
for (char * p = sensor.id; *p != '\0'; p++) {
|
||||
if (*p == '-') {
|
||||
*p = '_';
|
||||
}
|
||||
}
|
||||
customizations.sensorCustomizations.push_back(sensor); // add to list
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,12 +123,12 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c
|
||||
if (!EMSESP::system_.add_gpio(analogJson["gpio"].as<uint8_t>(), "Analog Sensor")) {
|
||||
EMSESP::logger().warning("Analog sensor: Invalid GPIO %d for %s. Skipping.",
|
||||
analogJson["gpio"].as<uint8_t>(),
|
||||
analogJson["name"].as<std::string>().c_str());
|
||||
analogJson["name"].as<const char *>());
|
||||
continue;
|
||||
}
|
||||
auto analog = AnalogCustomization();
|
||||
auto analog = AnalogCustomization();
|
||||
strlcpy(analog.name, analogJson["name"].as<const char *>(), sizeof(analog.name));
|
||||
analog.gpio = analogJson["gpio"];
|
||||
analog.name = analogJson["name"].as<std::string>();
|
||||
analog.offset = analogJson["offset"];
|
||||
analog.factor = analogJson["factor"];
|
||||
analog.uom = analogJson["uom"];
|
||||
@@ -153,7 +155,7 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c
|
||||
auto masked_entity_ids = masked_entity["entity_ids"].as<JsonArray>();
|
||||
for (const JsonVariant masked_entity_id : masked_entity_ids) {
|
||||
if (masked_entity_id.is<std::string>()) {
|
||||
emsEntity.entity_ids.push_back(masked_entity_id.as<std::string>()); // add entity list
|
||||
emsEntity.entity_ids.push_back(masked_entity_id.as<std::string>().c_str()); // add entity list
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,9 +314,9 @@ void WebCustomizationService::customization_entities(AsyncWebServerRequest * req
|
||||
read([&](WebCustomization & settings) {
|
||||
for (EntityCustomization entityCustomization : settings.entityCustomizations) {
|
||||
if (entityCustomization.device_id == device_id) {
|
||||
for (const std::string & entity_id : entityCustomization.entity_ids) {
|
||||
for (const auto & entity_id : entityCustomization.entity_ids) {
|
||||
uint8_t mask = Helpers::hextoint(entity_id.substr(0, 2).c_str());
|
||||
std::string name = DeviceValue::get_name(entity_id);
|
||||
std::string name = DeviceValue::get_name(entity_id.c_str());
|
||||
if (mask & 0x80) {
|
||||
bool is_set = false;
|
||||
for (const JsonVariant id : entity_ids_json) {
|
||||
@@ -382,23 +384,23 @@ void WebCustomizationService::load_test_data() {
|
||||
// Temperature sensors
|
||||
webCustomization.sensorCustomizations.clear(); // delete all existing sensors
|
||||
|
||||
auto sensor1 = SensorCustomization();
|
||||
sensor1.id = "01_0203_0405_0607";
|
||||
sensor1.name = "test_tempsensor1";
|
||||
auto sensor1 = SensorCustomization();
|
||||
strcpy(sensor1.id, "01_0203_0405_0607");
|
||||
strcpy(sensor1.name, "test_tempsensor1");
|
||||
sensor1.offset = 0;
|
||||
sensor1.is_system = false;
|
||||
webCustomization.sensorCustomizations.push_back(sensor1);
|
||||
|
||||
auto sensor2 = SensorCustomization();
|
||||
sensor2.id = "0B_0C0D_0E0F_1011";
|
||||
sensor2.name = "test_tempsensor2";
|
||||
auto sensor2 = SensorCustomization();
|
||||
strcpy(sensor2.id, "0B_0C0D_0E0F_1011");
|
||||
strcpy(sensor2.name, "test_tempsensor2");
|
||||
sensor2.offset = 4;
|
||||
sensor2.is_system = false;
|
||||
webCustomization.sensorCustomizations.push_back(sensor2);
|
||||
|
||||
auto sensor3 = SensorCustomization();
|
||||
sensor3.id = "28_1767_7B13_2502";
|
||||
sensor3.name = "gateway_temperature";
|
||||
auto sensor3 = SensorCustomization();
|
||||
strcpy(sensor3.id, "28_1767_7B13_2502");
|
||||
strcpy(sensor3.name, "gateway_temperature");
|
||||
sensor3.offset = 0;
|
||||
sensor3.is_system = true;
|
||||
webCustomization.sensorCustomizations.push_back(sensor3);
|
||||
@@ -406,9 +408,9 @@ void WebCustomizationService::load_test_data() {
|
||||
// Analog sensors
|
||||
// This actually adds the sensors as we use customizations to store them
|
||||
webCustomization.analogCustomizations.clear();
|
||||
auto analog = AnalogCustomization();
|
||||
analog.gpio = 36;
|
||||
analog.name = "test_analogsensor1";
|
||||
auto analog = AnalogCustomization();
|
||||
analog.gpio = 36;
|
||||
strcpy(analog.name, "test_analogsensor1");
|
||||
analog.offset = 0;
|
||||
analog.factor = 0.2;
|
||||
analog.uom = 17;
|
||||
@@ -416,9 +418,9 @@ void WebCustomizationService::load_test_data() {
|
||||
analog.is_system = false;
|
||||
webCustomization.analogCustomizations.push_back(analog);
|
||||
|
||||
analog = AnalogCustomization();
|
||||
analog.gpio = 37;
|
||||
analog.name = "test_analogsensor2";
|
||||
analog = AnalogCustomization();
|
||||
analog.gpio = 37;
|
||||
strcpy(analog.name, "test_analogsensor2");
|
||||
analog.offset = 0;
|
||||
analog.factor = 1;
|
||||
analog.uom = 0;
|
||||
@@ -426,9 +428,9 @@ void WebCustomizationService::load_test_data() {
|
||||
analog.is_system = false;
|
||||
webCustomization.analogCustomizations.push_back(analog);
|
||||
|
||||
analog = AnalogCustomization();
|
||||
analog.gpio = 38;
|
||||
analog.name = "test_analogsensor3";
|
||||
analog = AnalogCustomization();
|
||||
analog.gpio = 38;
|
||||
strcpy(analog.name, "test_analogsensor3");
|
||||
analog.offset = 0;
|
||||
analog.factor = 1;
|
||||
analog.uom = 0;
|
||||
@@ -436,9 +438,9 @@ void WebCustomizationService::load_test_data() {
|
||||
analog.is_system = false;
|
||||
webCustomization.analogCustomizations.push_back(analog);
|
||||
|
||||
analog = AnalogCustomization();
|
||||
analog.gpio = 33;
|
||||
analog.name = "test_analogsensor4";
|
||||
analog = AnalogCustomization();
|
||||
analog.gpio = 33;
|
||||
strcpy(analog.name, "test_analogsensor4");
|
||||
analog.offset = 0;
|
||||
analog.factor = 1;
|
||||
analog.uom = 0;
|
||||
@@ -446,9 +448,9 @@ void WebCustomizationService::load_test_data() {
|
||||
analog.is_system = false;
|
||||
webCustomization.analogCustomizations.push_back(analog);
|
||||
|
||||
analog = AnalogCustomization();
|
||||
analog.gpio = 39;
|
||||
analog.name = "test_analogsensor5"; // core_voltage
|
||||
analog = AnalogCustomization();
|
||||
analog.gpio = 39;
|
||||
strcpy(analog.name, "test_analogsensor5"); // core_voltage
|
||||
analog.offset = 0;
|
||||
analog.factor = 0.003771;
|
||||
analog.uom = 23;
|
||||
|
||||
@@ -35,21 +35,21 @@ namespace emsesp {
|
||||
// Customization for temperature sensor
|
||||
class SensorCustomization {
|
||||
public:
|
||||
std::string id;
|
||||
std::string name;
|
||||
uint16_t offset;
|
||||
bool is_system; // if true, the customization is a system customization
|
||||
char id[18];
|
||||
char name[20];
|
||||
uint16_t offset;
|
||||
bool is_system; // if true, the customization is a system customization
|
||||
};
|
||||
|
||||
class AnalogCustomization {
|
||||
public:
|
||||
uint8_t gpio;
|
||||
std::string name;
|
||||
double offset;
|
||||
double factor;
|
||||
uint8_t uom; // 0 is none
|
||||
int8_t type; // -1 is for deletion
|
||||
bool is_system; // if true, the customization is a system customization
|
||||
uint8_t gpio;
|
||||
char name[20];
|
||||
double offset;
|
||||
double factor;
|
||||
uint8_t uom; // 0 is none
|
||||
int8_t type; // -1 is for deletion
|
||||
bool is_system; // if true, the customization is a system customization
|
||||
|
||||
// used for removing from a list
|
||||
bool operator==(const AnalogCustomization & a) const {
|
||||
|
||||
@@ -321,8 +321,8 @@ void WebDataService::write_temperature_sensor(AsyncWebServerRequest * request, J
|
||||
if (json.is<JsonObject>()) {
|
||||
JsonObject sensor = json;
|
||||
|
||||
std::string id = sensor["id"]; // this is the key
|
||||
std::string name = sensor["name"];
|
||||
const char * id = sensor["id"]; // this is the key
|
||||
const char * name = sensor["name"];
|
||||
|
||||
// calculate offset. We'll convert it to an int and * 10
|
||||
float offset = sensor["offset"];
|
||||
@@ -331,7 +331,7 @@ void WebDataService::write_temperature_sensor(AsyncWebServerRequest * request, J
|
||||
offset10 = offset / 0.18;
|
||||
}
|
||||
|
||||
bool is_system = sensor["is_system"];
|
||||
bool is_system = sensor["is_system"] | false;
|
||||
|
||||
ok = EMSESP::temperaturesensor_.update(id, name, offset10, is_system);
|
||||
}
|
||||
@@ -346,15 +346,15 @@ void WebDataService::write_analog_sensor(AsyncWebServerRequest * request, JsonVa
|
||||
if (json.is<JsonObject>()) {
|
||||
JsonObject analog = json;
|
||||
|
||||
uint8_t gpio = analog["gpio"];
|
||||
std::string name = analog["name"];
|
||||
double factor = analog["factor"];
|
||||
double offset = analog["offset"];
|
||||
uint8_t uom = analog["uom"];
|
||||
int8_t type = analog["type"];
|
||||
bool deleted = analog["deleted"];
|
||||
bool is_system = analog["is_system"];
|
||||
ok = EMSESP::analogsensor_.update(gpio, name, offset, factor, uom, type, deleted, is_system);
|
||||
uint8_t gpio = analog["gpio"];
|
||||
const char * name = analog["name"];
|
||||
double factor = analog["factor"];
|
||||
double offset = analog["offset"];
|
||||
uint8_t uom = analog["uom"];
|
||||
int8_t type = analog["type"];
|
||||
bool deleted = analog["deleted"];
|
||||
bool is_system = analog["is_system"] | false;
|
||||
ok = EMSESP::analogsensor_.update(gpio, name, offset, factor, uom, type, deleted, is_system);
|
||||
}
|
||||
|
||||
AsyncWebServerResponse * response = request->beginResponse(ok ? 200 : 400); // ok or bad request
|
||||
@@ -414,7 +414,7 @@ void WebDataService::dashboard_data(AsyncWebServerRequest * request) {
|
||||
node["id"] = (EMSdevice::DeviceTypeUniqueID::TEMPERATURESENSOR_UID * 100) + count++;
|
||||
|
||||
JsonObject dv = node["dv"].to<JsonObject>();
|
||||
dv["id"] = "00" + sensor.name();
|
||||
dv["id"] = std::string("00") + sensor.name();
|
||||
if (EMSESP::system_.fahrenheit()) {
|
||||
if (Helpers::hasValue(sensor.temperature_c)) {
|
||||
dv["v"] = (float)sensor.temperature_c * 0.18 + 32;
|
||||
@@ -445,7 +445,7 @@ void WebDataService::dashboard_data(AsyncWebServerRequest * request) {
|
||||
node["id"] = (EMSdevice::DeviceTypeUniqueID::ANALOGSENSOR_UID * 100) + count++;
|
||||
|
||||
JsonObject dv = node["dv"].to<JsonObject>();
|
||||
dv["id"] = "00" + sensor.name();
|
||||
dv["id"] = std::string("00") + sensor.name();
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
if (sensor.type() == AnalogSensor::AnalogType::DIGITAL_OUT && (sensor.gpio() == 25 || sensor.gpio() == 26)) {
|
||||
obj["v"] = Helpers::transformNumFloat(sensor.value());
|
||||
@@ -486,12 +486,12 @@ void WebDataService::dashboard_data(AsyncWebServerRequest * request) {
|
||||
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()) {
|
||||
if (scheduleItem.name[0] != '\0') {
|
||||
JsonObject node = nodes.add<JsonObject>();
|
||||
node["id"] = (EMSdevice::DeviceTypeUniqueID::SCHEDULER_UID * 100) + count++;
|
||||
|
||||
JsonObject dv = node["dv"].to<JsonObject>();
|
||||
dv["id"] = "00" + scheduleItem.name;
|
||||
dv["id"] = std::string("00") + scheduleItem.name;
|
||||
dv["c"] = scheduleItem.name;
|
||||
char s[12];
|
||||
dv["v"] = Helpers::render_boolean(s, scheduleItem.active, true);
|
||||
|
||||
@@ -67,7 +67,7 @@ WebLogService::QueuedLogMessage::QueuedLogMessage(unsigned long id, uint64_t upt
|
||||
, uptime_(uptime)
|
||||
, level_(level)
|
||||
, name_(name)
|
||||
, text_(text) {
|
||||
, text_(text.c_str()) {
|
||||
}
|
||||
|
||||
void WebLogService::operator<<(std::shared_ptr<uuid::log::Message> message) {
|
||||
@@ -88,7 +88,6 @@ void WebLogService::operator<<(std::shared_ptr<uuid::log::Message> message) {
|
||||
}
|
||||
|
||||
log_messages_.emplace_back(++log_message_id_, message->uptime_ms, message->level, message->name, message->text.c_str());
|
||||
|
||||
}
|
||||
|
||||
// dumps out the contents of log buffer to shell console
|
||||
@@ -160,8 +159,8 @@ char * WebLogService::messagetime(char * out, const uint64_t t, const size_t buf
|
||||
strlcpy(out, uuid::log::format_timestamp_ms(t, 3).c_str(), bufsize);
|
||||
} else {
|
||||
time_t t1 = offset + (time_t)(t / 1000);
|
||||
char timestr[bufsize];
|
||||
strftime(timestr, bufsize, "%FT%T", localtime(&t1));
|
||||
char timestr[25];
|
||||
strftime(timestr, 25, "%FT%T", localtime(&t1));
|
||||
snprintf(out, bufsize, "%s.%03d", timestr, (uint16_t)(t % 1000));
|
||||
}
|
||||
return out;
|
||||
|
||||
@@ -87,19 +87,19 @@ StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webSchedu
|
||||
si.time = si.flags == SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? "" : schedule["time"].as<std::string>();
|
||||
si.cmd = schedule["cmd"].as<std::string>();
|
||||
si.value = schedule["value"].as<std::string>();
|
||||
si.name = schedule["name"].as<std::string>();
|
||||
strlcpy(si.name, schedule["name"].as<const char *>(), sizeof(si.name));
|
||||
|
||||
// calculated elapsed minutes
|
||||
si.elapsed_min = Helpers::string2minutes(si.time);
|
||||
si.elapsed_min = Helpers::string2minutes(si.time.c_str());
|
||||
si.retry_cnt = 0xFF; // no startup retries
|
||||
|
||||
webScheduler.scheduleItems.push_back(si); // add to list
|
||||
if (!webScheduler.scheduleItems.back().name.empty()) {
|
||||
if (webScheduler.scheduleItems.back().name[0] != '\0') {
|
||||
Command::add(
|
||||
EMSdevice::DeviceType::SCHEDULER,
|
||||
webScheduler.scheduleItems.back().name.c_str(),
|
||||
webScheduler.scheduleItems.back().name,
|
||||
[webScheduler](const char * value, const int8_t id) {
|
||||
return EMSESP::webSchedulerService.command_setvalue(value, id, webScheduler.scheduleItems.back().name.c_str());
|
||||
return EMSESP::webSchedulerService.command_setvalue(value, id, webScheduler.scheduleItems.back().name);
|
||||
},
|
||||
FL_(schedule_cmd),
|
||||
CommandFlag::ADMIN_ONLY);
|
||||
@@ -146,15 +146,8 @@ bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) {
|
||||
if (!strlen(cmd) || !strcmp(cmd, F_(values)) || !strcmp(cmd, F_(info))) {
|
||||
// list all names
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (!scheduleItem.name.empty()) {
|
||||
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||
output[scheduleItem.name] = scheduleItem.active;
|
||||
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||
output[scheduleItem.name] = scheduleItem.active ? 1 : 0;
|
||||
} else {
|
||||
char result[12];
|
||||
output[scheduleItem.name] = Helpers::render_boolean(result, scheduleItem.active);
|
||||
}
|
||||
if (scheduleItem.name[0] != '\0') {
|
||||
Mqtt::add_value_bool(output, scheduleItem.name, scheduleItem.active);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -162,9 +155,9 @@ bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) {
|
||||
|
||||
if (!strcmp(cmd, F_(entities))) {
|
||||
uint8_t i = 0;
|
||||
char name[30];
|
||||
char name[20];
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
strlcpy(name, scheduleItem.name == "" ? Helpers::smallitoa(name, i++) : scheduleItem.name.c_str(), sizeof(name));
|
||||
strlcpy(name, scheduleItem.name[0] == '\0' ? Helpers::smallitoa(name, i++) : scheduleItem.name, sizeof(name));
|
||||
get_value_json(output[name].to<JsonObject>(), scheduleItem);
|
||||
}
|
||||
return true;
|
||||
@@ -183,17 +176,10 @@ bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) {
|
||||
|
||||
// build the json for specific entity
|
||||
void WebSchedulerService::get_value_json(JsonObject output, const ScheduleItem & scheduleItem) {
|
||||
output["name"] = scheduleItem.name;
|
||||
output["fullname"] = scheduleItem.name;
|
||||
output["name"] = (const char *)scheduleItem.name;
|
||||
output["fullname"] = (const char *)scheduleItem.name;
|
||||
output["type"] = "boolean";
|
||||
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||
output["value"] = scheduleItem.active;
|
||||
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||
output["value"] = scheduleItem.active ? 1 : 0;
|
||||
} else {
|
||||
char result[12];
|
||||
output["value"] = Helpers::render_boolean(result, scheduleItem.active);
|
||||
}
|
||||
Mqtt::add_value_bool(output, "value", scheduleItem.active);
|
||||
if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_CONDITION) {
|
||||
output["condition"] = scheduleItem.time;
|
||||
} else if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_ONCHANGE) {
|
||||
@@ -205,7 +191,7 @@ void WebSchedulerService::get_value_json(JsonObject output, const ScheduleItem &
|
||||
}
|
||||
output["command"] = scheduleItem.cmd;
|
||||
output["cmd_data"] = scheduleItem.value;
|
||||
bool hasName = scheduleItem.name != "";
|
||||
bool hasName = scheduleItem.name[0] != '\0';
|
||||
output["readable"] = hasName;
|
||||
output["writeable"] = hasName;
|
||||
output["visible"] = hasName;
|
||||
@@ -221,7 +207,7 @@ void WebSchedulerService::publish_single(const char * name, const bool state) {
|
||||
if (Mqtt::publish_single2cmd()) {
|
||||
snprintf(topic, sizeof(topic), "%s/%s", F_(scheduler), name);
|
||||
} else {
|
||||
snprintf(topic, sizeof(topic), "%s%s/%s", F_(scheduler), "_data", name);
|
||||
snprintf(topic, sizeof(topic), "%s_data/%s", F_(scheduler), name);
|
||||
}
|
||||
|
||||
char payload[12];
|
||||
@@ -244,36 +230,28 @@ void WebSchedulerService::publish(const bool force) {
|
||||
|
||||
if (Mqtt::publish_single() && force) {
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
publish_single(scheduleItem.name.c_str(), scheduleItem.active);
|
||||
publish_single(scheduleItem.name, scheduleItem.active);
|
||||
}
|
||||
}
|
||||
|
||||
JsonDocument doc;
|
||||
bool ha_created = ha_registered_;
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (!scheduleItem.name.empty() && !doc[scheduleItem.name].is<JsonVariantConst>()) {
|
||||
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||
doc[scheduleItem.name] = scheduleItem.active;
|
||||
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||
doc[scheduleItem.name] = scheduleItem.active ? 1 : 0;
|
||||
} else {
|
||||
char result[12];
|
||||
doc[scheduleItem.name] = Helpers::render_boolean(result, scheduleItem.active);
|
||||
}
|
||||
if (scheduleItem.name[0] != '\0' && !doc[scheduleItem.name].is<JsonVariantConst>()) {
|
||||
Mqtt::add_value_bool(doc.as<JsonObject>(), scheduleItem.name, scheduleItem.active);
|
||||
|
||||
// create HA config
|
||||
if (Mqtt::ha_enabled() && !ha_registered_) {
|
||||
|
||||
JsonDocument config;
|
||||
config["~"] = Mqtt::base();
|
||||
config["~"] = Mqtt::base();
|
||||
|
||||
char stat_t[50];
|
||||
char stat_t[50];
|
||||
snprintf(stat_t, sizeof(stat_t), "~/%s_data", F_(scheduler));
|
||||
config["stat_t"] = stat_t;
|
||||
|
||||
char val_obj[50];
|
||||
char val_cond[65];
|
||||
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", scheduleItem.name.c_str());
|
||||
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", scheduleItem.name);
|
||||
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
|
||||
|
||||
if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
|
||||
@@ -283,17 +261,17 @@ void WebSchedulerService::publish(const bool force) {
|
||||
}
|
||||
|
||||
char uniq_s[70];
|
||||
snprintf(uniq_s, sizeof(uniq_s), "%s_%s", F_(scheduler), scheduleItem.name.c_str());
|
||||
snprintf(uniq_s, sizeof(uniq_s), "%s_%s", F_(scheduler), scheduleItem.name);
|
||||
|
||||
config["uniq_id"] = uniq_s;
|
||||
config["name"] = scheduleItem.name.c_str();
|
||||
config["def_ent_id"] = (std::string) "switch." + uniq_s;
|
||||
config["name"] = scheduleItem.name;
|
||||
config["def_ent_id"] = std::string("switch.") + uniq_s;
|
||||
|
||||
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||
|
||||
snprintf(topic, sizeof(topic), "switch/%s/%s_%s/config", Mqtt::basename().c_str(), F_(scheduler), scheduleItem.name.c_str());
|
||||
snprintf(command_topic, sizeof(command_topic), "~/%s/%s", F_(scheduler), scheduleItem.name.c_str());
|
||||
snprintf(topic, sizeof(topic), "switch/%s/%s_%s/config", Mqtt::basename().c_str(), F_(scheduler), scheduleItem.name);
|
||||
snprintf(command_topic, sizeof(command_topic), "~/%s/%s", F_(scheduler), scheduleItem.name);
|
||||
config["cmd_t"] = command_topic;
|
||||
|
||||
Mqtt::add_ha_bool(config.as<JsonObject>());
|
||||
@@ -318,15 +296,13 @@ void WebSchedulerService::publish(const bool force) {
|
||||
uint8_t WebSchedulerService::count_entities(bool cmd_only) {
|
||||
uint8_t count = 0;
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (!scheduleItem.name.empty() || !cmd_only) {
|
||||
if (scheduleItem.name[0] != '\0' || !cmd_only) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// execute scheduled command
|
||||
bool WebSchedulerService::command(const char * name, const std::string & command, const std::string & data) {
|
||||
std::string cmd = Helpers::toLower(command);
|
||||
@@ -428,7 +404,7 @@ bool WebSchedulerService::command(const char * name, const std::string & command
|
||||
bool WebSchedulerService::onChange(const char * cmd) {
|
||||
for (ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_ONCHANGE
|
||||
&& Helpers::toLower(scheduleItem.time).find(Helpers::toLower(cmd)) != std::string::npos) {
|
||||
&& Helpers::toLower(scheduleItem.time.c_str()).find(Helpers::toLower(cmd)) != std::string::npos) {
|
||||
cmd_changed_.push_back(&scheduleItem);
|
||||
return true;
|
||||
}
|
||||
@@ -440,12 +416,12 @@ bool WebSchedulerService::onChange(const char * cmd) {
|
||||
void WebSchedulerService::condition() {
|
||||
for (ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_CONDITION) {
|
||||
auto match = compute(scheduleItem.time);
|
||||
auto match = compute(scheduleItem.time.c_str());
|
||||
#ifdef EMESESP_DEBUG
|
||||
// EMSESP::logger().debug("condition match: %s", match.c_str());
|
||||
#endif
|
||||
if (match.length() == 1 && match[0] == '1' && scheduleItem.retry_cnt == 0xFF) {
|
||||
scheduleItem.retry_cnt = command(scheduleItem.name.c_str(), scheduleItem.cmd, compute(scheduleItem.value)) ? 1 : 0xFF;
|
||||
scheduleItem.retry_cnt = command(scheduleItem.name, scheduleItem.cmd.c_str(), compute(scheduleItem.value.c_str())) ? 1 : 0xFF;
|
||||
} else if (match.length() == 1 && match[0] == '0' && scheduleItem.retry_cnt == 1) {
|
||||
scheduleItem.retry_cnt = 0xFF;
|
||||
} else if (match.length() != 1) { // the match is not boolean
|
||||
@@ -475,13 +451,13 @@ void WebSchedulerService::loop() {
|
||||
// check if we have onChange events
|
||||
while (!cmd_changed_.empty()) {
|
||||
ScheduleItem si = *cmd_changed_.front();
|
||||
command(si.name.c_str(), si.cmd, compute(si.value));
|
||||
command(si.name, si.cmd.c_str(), compute(si.value.c_str()));
|
||||
cmd_changed_.pop_front();
|
||||
}
|
||||
|
||||
for (ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_IMMEDIATE) {
|
||||
command(scheduleItem.name.c_str(), scheduleItem.cmd, compute(scheduleItem.value));
|
||||
command(scheduleItem.name, scheduleItem.cmd.c_str(), compute(scheduleItem.value.c_str()));
|
||||
scheduleItem.active = false;
|
||||
}
|
||||
}
|
||||
@@ -497,7 +473,7 @@ void WebSchedulerService::loop() {
|
||||
if (last_tm_min == -2) {
|
||||
for (ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min == 0) {
|
||||
scheduleItem.retry_cnt = command(scheduleItem.name.c_str(), scheduleItem.cmd, compute(scheduleItem.value)) ? 0xFF : 0;
|
||||
scheduleItem.retry_cnt = command(scheduleItem.name, scheduleItem.cmd.c_str(), compute(scheduleItem.value.c_str())) ? 0xFF : 0;
|
||||
}
|
||||
}
|
||||
last_tm_min = -1; // startup done, now use for RTC
|
||||
@@ -510,12 +486,12 @@ void WebSchedulerService::loop() {
|
||||
// retry startup commands not yet executed
|
||||
if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min == 0
|
||||
&& scheduleItem.retry_cnt < MAX_STARTUP_RETRIES) {
|
||||
scheduleItem.retry_cnt = command(scheduleItem.name.c_str(), scheduleItem.cmd, scheduleItem.value) ? 0xFF : scheduleItem.retry_cnt + 1;
|
||||
scheduleItem.retry_cnt = command(scheduleItem.name, scheduleItem.cmd.c_str(), scheduleItem.value.c_str()) ? 0xFF : scheduleItem.retry_cnt + 1;
|
||||
}
|
||||
// scheduled timer commands
|
||||
if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min > 0
|
||||
&& (uptime_min % scheduleItem.elapsed_min == 0)) {
|
||||
command(scheduleItem.name.c_str(), scheduleItem.cmd, compute(scheduleItem.value));
|
||||
command(scheduleItem.name, scheduleItem.cmd.c_str(), compute(scheduleItem.value.c_str()));
|
||||
}
|
||||
}
|
||||
last_uptime_min = uptime_min;
|
||||
@@ -532,7 +508,7 @@ void WebSchedulerService::loop() {
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
uint8_t dow = scheduleItem.flags & SCHEDULEFLAG_SCHEDULE_TIMER ? 0 : scheduleItem.flags;
|
||||
if (scheduleItem.active && (real_dow & dow) && real_min == scheduleItem.elapsed_min) {
|
||||
command(scheduleItem.name.c_str(), scheduleItem.cmd, compute(scheduleItem.value));
|
||||
command(scheduleItem.name, scheduleItem.cmd.c_str(), compute(scheduleItem.value.c_str()));
|
||||
}
|
||||
}
|
||||
last_tm_min = tm->tm_min;
|
||||
@@ -559,26 +535,26 @@ void WebSchedulerService::load_test_data() {
|
||||
webScheduler.scheduleItems.clear(); // delete all existing schedules
|
||||
|
||||
// test 1
|
||||
auto si = ScheduleItem();
|
||||
si.active = true;
|
||||
si.flags = 1;
|
||||
si.time = "12:00";
|
||||
si.cmd = "system/fetch";
|
||||
si.value = "10";
|
||||
si.name = "test_scheduler";
|
||||
auto si = ScheduleItem();
|
||||
si.active = true;
|
||||
si.flags = 1;
|
||||
si.time = "12:00";
|
||||
si.cmd = "system/fetch";
|
||||
si.value = "10";
|
||||
strcpy(si.name, "test_scheduler");
|
||||
si.elapsed_min = 0;
|
||||
si.retry_cnt = 0xFF; // no startup retries
|
||||
|
||||
webScheduler.scheduleItems.push_back(si);
|
||||
|
||||
// test 2
|
||||
si = ScheduleItem();
|
||||
si.active = false;
|
||||
si.flags = 1;
|
||||
si.time = "13:00";
|
||||
si.cmd = "system/message";
|
||||
si.value = "20";
|
||||
si.name = ""; // to make sure its excluded from Dashboard
|
||||
si = ScheduleItem();
|
||||
si.active = false;
|
||||
si.flags = 1;
|
||||
si.time = "13:00";
|
||||
si.cmd = "system/message";
|
||||
si.value = "20";
|
||||
strcpy(si.name, ""); // to make sure its excluded from Dashboard
|
||||
si.elapsed_min = 0;
|
||||
si.retry_cnt = 0xFF; // no startup retries
|
||||
|
||||
|
||||
@@ -56,20 +56,17 @@ class ScheduleItem {
|
||||
boolean active;
|
||||
uint8_t flags;
|
||||
uint16_t elapsed_min; // total mins from 00:00
|
||||
std::string time; // HH:MM
|
||||
std::string cmd;
|
||||
std::string value;
|
||||
std::string name;
|
||||
stringPSRAM time; // HH:MM
|
||||
stringPSRAM cmd;
|
||||
stringPSRAM value;
|
||||
char name[20];
|
||||
uint8_t retry_cnt;
|
||||
};
|
||||
|
||||
class WebScheduler {
|
||||
public:
|
||||
#ifndef EMSESP_STANDALONE
|
||||
std::list<ScheduleItem, AllocatorPSRAM<ScheduleItem>> scheduleItems;
|
||||
#else
|
||||
std::list<ScheduleItem> scheduleItems;
|
||||
#endif
|
||||
|
||||
static void read(WebScheduler & webScheduler, JsonObject root);
|
||||
static StateUpdateResult update(JsonObject root, WebScheduler & webScheduler);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user