This commit is contained in:
MichaelDvP
2023-03-06 16:15:28 +01:00
32 changed files with 1471 additions and 933 deletions

View File

@@ -149,7 +149,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
if (data.is<const char *>()) {
const char * d = data.as<const char *>();
if (strlen(d)) {
char * device_end = strchr(d, '/');
char * device_end = (char *)strchr(d, '/');
if (device_end != nullptr) {
char device_s[15] = {'\0'};
const char * device_p = device_s;

View File

@@ -1050,8 +1050,7 @@ void EMSdevice::generate_values_web_customization(JsonArray & output) {
void EMSdevice::set_climate_minmax(uint8_t tag, int16_t min, uint16_t max) {
for (auto & dv : devicevalues_) {
// TODO check if pointer reference is safe. strcpy better?
if (dv.tag == tag && dv.short_name == FL_(haclimate[0])) {
if (dv.tag == tag && (strcmp(dv.short_name, FL_(haclimate[0])) == 0)) {
if (dv.min != min || dv.max != max) {
dv.min = min;
dv.max = max;

View File

@@ -50,6 +50,7 @@ MAKE_TRANSLATION(sensors_device, "Sensors", "Sensoren", "Sensoren", "Sensorer",
MAKE_TRANSLATION(unknown_device, "Unknown", "Unbekannt", "Onbekend", "Okänt", "Nieznane urządzenie", "Ukjent", "Inconnu")
// commands
// TODO translate
MAKE_TRANSLATION(info_cmd, "lists all values", "Liste aller Werte", "", "", "", "", "", "Tüm değerleri listele")
MAKE_TRANSLATION(commands_cmd, "lists all commands", "Liste aller Kommandos", "", "", "", "", "", "Tüm komutları listele")
MAKE_TRANSLATION(entities_cmd, "lists all entities", "Liste aller Entitäten", "", "", "", "", "", "Tüm varlıkları listele")

View File

@@ -1071,7 +1071,7 @@ bool System::check_upgrade(bool factory_settings) {
// if we're coming from 3.4.4 or 3.5.0b14 then we need to apply new settings
if (missing_version) {
LOG_DEBUG("Setting MQTT ID Entity to v3.4 format");
LOG_DEBUG("Setting MQTT Entity ID format to v3.4 format");
EMSESP::esp8266React.getMqttSettingsService()->update(
[&](MqttSettings & mqttSettings) {
mqttSettings.entity_format = 0; // use old Entity ID format from v3.4

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.6.0-dev.2"
#define EMSESP_APP_VERSION "3.6.0-dev.3"

View File

@@ -34,27 +34,30 @@ void WebSchedulerService::begin() {
}
// this creates the scheduler file, saving it to the FS
// and also calls when the Scheduler web page is refreshed
void WebScheduler::read(WebScheduler & webScheduler, JsonObject & root) {
JsonArray schedule = root.createNestedArray("schedule");
uint8_t counter = 0;
char s[3];
for (const ScheduleItem & scheduleItem : webScheduler.scheduleItems) {
JsonObject si = schedule.createNestedObject();
si["id"] = scheduleItem.id; // name, is unqiue
si["active"] = scheduleItem.active;
si["flags"] = scheduleItem.flags;
si["time"] = scheduleItem.time;
si["cmd"] = scheduleItem.cmd;
si["value"] = scheduleItem.value;
si["description"] = scheduleItem.description;
JsonObject si = schedule.createNestedObject();
si["id"] = Helpers::smallitoa(s, ++counter); // id is only used to render the table and must be unique
si["active"] = scheduleItem.active;
si["flags"] = scheduleItem.flags;
si["time"] = scheduleItem.time;
si["cmd"] = scheduleItem.cmd;
si["value"] = scheduleItem.value;
si["name"] = scheduleItem.name;
}
}
// call on initialization and also when the page is saved via web UI
// call on initialization and also when the Scheduile web page is updated
// this loads the data into the internal class
StateUpdateResult WebScheduler::update(JsonObject & root, WebScheduler & webScheduler) {
#ifdef EMSESP_STANDALONE
// invoke some fake data for testing
const char * json = "{[{\"id\":\"test1\",\"active\":true,\"flags\":31,\"time\": \"07:30\",\"cmd\": \"hc1/mode\",\"value\": \"day\",\"description\": \"turn "
"on central heating\"}]}";
const char * json =
"{[{\"id\":\"01\",\"active\":true,\"flags\":31,\"time\": \"07:30\",\"cmd\": \"hc1/mode\",\"value\": \"day\",\"name\": \"turn on central heating\"}]}";
StaticJsonDocument<500> doc;
deserializeJson(doc, json);
root = doc.as<JsonObject>();
@@ -65,7 +68,7 @@ StateUpdateResult WebScheduler::update(JsonObject & root, WebScheduler & webSche
#endif
for (ScheduleItem & scheduleItem : webScheduler.scheduleItems) {
Command::erase_command(EMSdevice::DeviceType::SCHEDULER, scheduleItem.description.c_str());
Command::erase_command(EMSdevice::DeviceType::SCHEDULER, scheduleItem.name.c_str());
}
webScheduler.scheduleItems.clear();
@@ -73,26 +76,25 @@ StateUpdateResult WebScheduler::update(JsonObject & root, WebScheduler & webSche
for (const JsonObject schedule : root["schedule"].as<JsonArray>()) {
// create each schedule item, overwriting any previous settings
// ignore the id (as this is only used in the web for table rendering)
auto si = ScheduleItem();
si.id = schedule["id"].as<std::string>();
si.active = schedule["active"];
si.flags = schedule["flags"];
si.time = schedule["time"].as<std::string>();
si.cmd = schedule["cmd"].as<std::string>();
si.value = schedule["value"].as<std::string>();
si.description = schedule["description"].as<std::string>();
auto si = ScheduleItem();
si.active = schedule["active"];
si.flags = schedule["flags"];
si.time = 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>();
// calculated elapsed minutes
si.elapsed_min = Helpers::string2minutes(si.time);
si.retry_cnt = 0xFF; // no starup retries
si.retry_cnt = 0xFF; // no startup retries
webScheduler.scheduleItems.push_back(si); // add to list
if (!webScheduler.scheduleItems.back().description.empty()) {
if (!webScheduler.scheduleItems.back().name.empty()) {
Command::add(
EMSdevice::DeviceType::SCHEDULER,
webScheduler.scheduleItems.back().description.c_str(),
webScheduler.scheduleItems.back().name.c_str(),
[webScheduler](const char * value, const int8_t id) {
return EMSESP::webSchedulerService.command_setvalue(value, webScheduler.scheduleItems.back().description);
return EMSESP::webSchedulerService.command_setvalue(value, webScheduler.scheduleItems.back().name);
},
FL_(schedule_cmd),
CommandFlag::ADMIN_ONLY);
@@ -111,7 +113,7 @@ bool WebSchedulerService::command_setvalue(const char * value, const std::string
}
EMSESP::webSchedulerService.read([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems; });
for (ScheduleItem & scheduleItem : *scheduleItems) {
if (scheduleItem.description == name) {
if (scheduleItem.name == name) {
if (scheduleItem.active == v) {
return true;
}
@@ -148,20 +150,20 @@ void WebSchedulerService::publish(const bool force) {
}
if (Mqtt::publish_single() && force) {
for (const ScheduleItem & scheduleItem : *scheduleItems) {
publish_single(scheduleItem.description.c_str(), scheduleItem.active);
publish_single(scheduleItem.name.c_str(), scheduleItem.active);
}
}
DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE);
for (const ScheduleItem & scheduleItem : *scheduleItems) {
if (!scheduleItem.description.empty() && !doc.containsKey(scheduleItem.description)) {
if (!scheduleItem.name.empty() && !doc.containsKey(scheduleItem.name)) {
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
doc[scheduleItem.description] = scheduleItem.active;
doc[scheduleItem.name] = scheduleItem.active;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
doc[scheduleItem.description] = scheduleItem.active ? 1 : 0;
doc[scheduleItem.name] = scheduleItem.active ? 1 : 0;
} else {
char result[12];
doc[scheduleItem.description] = Helpers::render_boolean(result, scheduleItem.active);
doc[scheduleItem.name] = Helpers::render_boolean(result, scheduleItem.active);
}
// create HA config
@@ -173,21 +175,21 @@ void WebSchedulerService::publish(const bool force) {
char val_obj[50];
char val_cond[65];
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", scheduleItem.description.c_str());
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", scheduleItem.name.c_str());
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + "}}";
char uniq_s[70];
snprintf(uniq_s, sizeof(uniq_s), "scheduler_%s", scheduleItem.description.c_str());
snprintf(uniq_s, sizeof(uniq_s), "scheduler_%s", scheduleItem.name.c_str());
config["obj_id"] = uniq_s;
config["uniq_id"] = uniq_s; // same as object_id
config["name"] = scheduleItem.description.c_str();
config["name"] = scheduleItem.name.c_str();
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
snprintf(topic, sizeof(topic), "switch/%s/scheduler_%s/config", Mqtt::basename().c_str(), scheduleItem.description.c_str());
snprintf(command_topic, sizeof(command_topic), "%s/scheduler/%s", Mqtt::basename().c_str(), scheduleItem.description.c_str());
snprintf(topic, sizeof(topic), "switch/%s/scheduler_%s/config", Mqtt::basename().c_str(), scheduleItem.name.c_str());
snprintf(command_topic, sizeof(command_topic), "%s/scheduler/%s", Mqtt::basename().c_str(), scheduleItem.name.c_str());
config["cmd_t"] = command_topic;
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
config["pl_on"] = true;
@@ -220,7 +222,7 @@ bool WebSchedulerService::has_commands() {
return false;
}
for (const ScheduleItem & scheduleItem : *scheduleItems) {
if (!scheduleItem.description.empty()) {
if (!scheduleItem.name.empty()) {
return true;
}
}
@@ -267,7 +269,7 @@ bool WebSchedulerService::command(const char * cmd, const char * data) {
}
// process any scheduled jobs
// checks on the minute
// checks on the minute and at startup
void WebSchedulerService::loop() {
// initialize static value on startup
static int8_t last_tm_min = -1; // invalid value also used for startup commands

View File

@@ -23,20 +23,20 @@
#define EMSESP_SCHEDULER_SERVICE_PATH "/rest/schedule" // GET and POST
#define SCHEDULEFLAG_SCHEDULE_TIMER 0x80 // 7th bit for Timer
#define MAX_STARTUP_RETRIES 3 // retry the statup commands x times
#define MAX_STARTUP_RETRIES 3 // retry the start-up commands x times
namespace emsesp {
class ScheduleItem {
public:
std::string id; // unqiue id, is the name
std::string id; // unqiue id
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 description;
std::string name;
uint8_t retry_cnt;
};