mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-09 09:19:51 +03:00
Merge branch 'dev' of https://github.com/proddy/EMS-ESP32 into dev
This commit is contained in:
@@ -648,78 +648,39 @@ void AnalogSensor::publish_values(const bool force) {
|
||||
// called from emsesp.cpp for commands
|
||||
// searches sensor by name
|
||||
bool AnalogSensor::get_value_info(JsonObject output, const char * cmd, const int8_t id) {
|
||||
// check of it a 'commands' command
|
||||
if (Helpers::toLower(cmd) == F_(commands)) {
|
||||
return Command::list(EMSdevice::DeviceType::ANALOGSENSOR, output);
|
||||
}
|
||||
|
||||
if (sensors_.empty()) {
|
||||
return true; // no sensors, return true
|
||||
}
|
||||
|
||||
uint8_t show_all = 0;
|
||||
if (Helpers::hasValue(cmd)) {
|
||||
show_all = (strncmp(cmd, F_(info), 4) == 0) ? 1 : (strncmp(cmd, F_(values), 6) == 0) ? 2 : 0;
|
||||
if (!strcmp(cmd, F_(info)) || !strcmp(cmd, F_(values))) {
|
||||
for (const auto & sensor : sensors_) {
|
||||
output[sensor.name()] = sensor.value();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// see if we're showing all sensors
|
||||
if (show_all) {
|
||||
if (!strcmp(cmd, F_(entities))) {
|
||||
for (const auto & sensor : sensors_) {
|
||||
if (show_all == 1) {
|
||||
// info
|
||||
JsonObject dataSensor = output[sensor.name()].to<JsonObject>();
|
||||
addSensorJson(dataSensor, sensor);
|
||||
} else {
|
||||
// values, shortname version. Also used in 'system allvalues'
|
||||
output[sensor.name()] = sensor.value();
|
||||
}
|
||||
get_value_json(output[sensor.name()].to<JsonObject>(), sensor);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// this is for a specific sensor
|
||||
// make a copy of the string command for parsing, and lowercase it
|
||||
char sensor_name[COMMAND_MAX_LENGTH] = {'\0'};
|
||||
char * attribute_s = nullptr;
|
||||
strlcpy(sensor_name, Helpers::toLower(cmd).c_str(), sizeof(sensor_name));
|
||||
|
||||
// check specific attribute to fetch instead of the complete record
|
||||
char * breakp = strchr(sensor_name, '/');
|
||||
if (breakp) {
|
||||
*breakp = '\0';
|
||||
attribute_s = breakp + 1;
|
||||
}
|
||||
const char * attribute_s = Command::get_attribute(cmd);
|
||||
|
||||
for (const auto & sensor : sensors_) {
|
||||
if (sensor_name == Helpers::toLower(sensor.name()) || Helpers::atoint(sensor_name) == sensor.gpio()) {
|
||||
// add the details
|
||||
addSensorJson(output, sensor);
|
||||
|
||||
/*
|
||||
// if someone wants gpio numbers
|
||||
char gpio_str[9];
|
||||
snprintf(gpio_str, sizeof(gpio_str), "gpio_%02d", sensor.gpio());
|
||||
output[gpio_str] = sensor.value();
|
||||
*/
|
||||
|
||||
// if we're filtering on an attribute, go find it
|
||||
if (attribute_s) {
|
||||
if (output.containsKey(attribute_s)) {
|
||||
std::string data = output[attribute_s].as<std::string>();
|
||||
output.clear();
|
||||
output["api_data"] = data; // always as a string
|
||||
return true;
|
||||
}
|
||||
return EMSESP::return_not_found(output, attribute_s, sensor_name); // not found
|
||||
}
|
||||
return true; // found a match, exit
|
||||
// match custom name or sensor GPIO
|
||||
if (cmd == Helpers::toLower(sensor.name()) || Helpers::atoint(cmd) == sensor.gpio()) {
|
||||
get_value_json(output, sensor);
|
||||
return Command::set_attirbute(output, cmd, attribute_s);
|
||||
}
|
||||
}
|
||||
|
||||
return false; // not found
|
||||
}
|
||||
|
||||
void AnalogSensor::addSensorJson(JsonObject output, const Sensor & sensor) {
|
||||
void AnalogSensor::get_value_json(JsonObject output, const Sensor & sensor) {
|
||||
output["gpio"] = sensor.gpio();
|
||||
output["type"] = F_(number);
|
||||
output["analog"] = FL_(list_sensortype)[sensor.type()];
|
||||
|
||||
@@ -171,6 +171,7 @@ class AnalogSensor {
|
||||
bool command_setvalue(const char * value, const int8_t gpio);
|
||||
void measure();
|
||||
void addSensorJson(JsonObject output, const Sensor & sensor);
|
||||
void get_value_json(JsonObject output, const Sensor & sensor);
|
||||
|
||||
std::vector<Sensor> sensors_; // our list of sensors
|
||||
|
||||
|
||||
@@ -103,9 +103,9 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
|
||||
}
|
||||
if (command_p == nullptr) {
|
||||
// handle dead endpoints like api/system or api/boiler
|
||||
// default to 'info' for SYSTEM, the other devices to 'values' for shortname version
|
||||
// default to 'value' for all devices
|
||||
if (num_paths < (id_n > 0 ? 4 : 3)) {
|
||||
command_p = device_type == EMSdevice::DeviceType::SYSTEM ? F_(info) : F_(values);
|
||||
command_p = F_(values);
|
||||
} else {
|
||||
return json_message(CommandRet::NOT_FOUND, "missing or bad command", output);
|
||||
}
|
||||
@@ -284,6 +284,29 @@ const char * Command::parse_command_string(const char * command, int8_t & id) {
|
||||
return command;
|
||||
}
|
||||
|
||||
// check if command contains an attribute
|
||||
const char * Command::get_attribute(const char * cmd) {
|
||||
char * breakp = strchr(cmd, '/');
|
||||
if (breakp) {
|
||||
*breakp = '\0';
|
||||
return breakp + 1;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool Command::set_attirbute(JsonObject output, const char * cmd, const char * attribute) {
|
||||
if (attribute == nullptr) {
|
||||
return true;
|
||||
}
|
||||
if (output.containsKey(attribute)) {
|
||||
std::string data = output[attribute].as<std::string>();
|
||||
output.clear();
|
||||
output["api_data"] = data; // always as a string
|
||||
return true;
|
||||
}
|
||||
return EMSESP::return_not_found(output, attribute, cmd); // not found
|
||||
}
|
||||
|
||||
// calls a command directly
|
||||
uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id) {
|
||||
// create a temporary buffer
|
||||
@@ -297,10 +320,12 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
||||
// calls a command. Takes a json object for output.
|
||||
// id may be used to represent a heating circuit for example
|
||||
// returns 0 if the command errored, 1 (TRUE) if ok, 2 if not found, 3 if error or 4 if not allowed
|
||||
uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * value, const bool is_admin, const int8_t id, JsonObject output) {
|
||||
if (cmd == nullptr) {
|
||||
uint8_t Command::call(const uint8_t device_type, const char * command, const char * value, const bool is_admin, const int8_t id, JsonObject output) {
|
||||
if (command == nullptr) {
|
||||
return CommandRet::NOT_FOUND;
|
||||
}
|
||||
char cmd[COMMAND_MAX_LENGTH];
|
||||
strcpy(cmd, Helpers::toLower(command).c_str());
|
||||
|
||||
auto dname = EMSdevice::device_type_2_device_name(device_type); // device name, not translated
|
||||
|
||||
@@ -309,6 +334,9 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
||||
// or a special command like 'info', 'values', 'commands', 'entities' etc
|
||||
bool single_command = (!value || !strlen(value));
|
||||
if (single_command) {
|
||||
if (!strcmp(cmd, F_(commands))) {
|
||||
return Command::list(device_type, output);
|
||||
}
|
||||
if (EMSESP::get_device_value_info(output, cmd, id, device_type)) { // entity = cmd
|
||||
LOG_DEBUG("Fetched device entity/attributes for %s/%s", dname, cmd);
|
||||
return CommandRet::OK;
|
||||
@@ -319,17 +347,14 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
||||
|
||||
// determine flags based on id (which is the tag)
|
||||
uint8_t flag = CommandFlag::CMD_FLAG_DEFAULT;
|
||||
// info and values works with all tags, keep it default
|
||||
if (std::string(cmd) != F_(values) && std::string(cmd) != F_(info)) {
|
||||
if (id >= DeviceValueTAG::TAG_HC1 && id <= DeviceValueTAG::TAG_HC8) {
|
||||
flag = CommandFlag::CMD_FLAG_HC;
|
||||
} else if (id >= DeviceValueTAG::TAG_DHW1 && id <= DeviceValueTAG::TAG_DHW10) {
|
||||
flag = CommandFlag::CMD_FLAG_DHW;
|
||||
} else if (id >= DeviceValueTAG::TAG_HS1 && id <= DeviceValueTAG::TAG_HS16) {
|
||||
flag = CommandFlag::CMD_FLAG_HS;
|
||||
} else if (id >= DeviceValueTAG::TAG_AHS1 && id <= DeviceValueTAG::TAG_AHS1) {
|
||||
flag = CommandFlag::CMD_FLAG_AHS;
|
||||
}
|
||||
if (id >= DeviceValueTAG::TAG_HC1 && id <= DeviceValueTAG::TAG_HC8) {
|
||||
flag = CommandFlag::CMD_FLAG_HC;
|
||||
} else if (id >= DeviceValueTAG::TAG_DHW1 && id <= DeviceValueTAG::TAG_DHW10) {
|
||||
flag = CommandFlag::CMD_FLAG_DHW;
|
||||
} else if (id >= DeviceValueTAG::TAG_HS1 && id <= DeviceValueTAG::TAG_HS16) {
|
||||
flag = CommandFlag::CMD_FLAG_HS;
|
||||
} else if (id >= DeviceValueTAG::TAG_AHS1 && id <= DeviceValueTAG::TAG_AHS1) {
|
||||
flag = CommandFlag::CMD_FLAG_AHS;
|
||||
}
|
||||
|
||||
// see if there is a command registered and it's valid
|
||||
@@ -500,15 +525,11 @@ std::string Command::tagged_cmd(const std::string & cmd, const uint8_t flag) {
|
||||
|
||||
// list all commands for a specific device, output as json
|
||||
bool Command::list(const uint8_t device_type, JsonObject output) {
|
||||
// check of it a 'commands' command
|
||||
if (device_type == EMSdevice::DeviceType::TEMPERATURESENSOR || device_type == EMSdevice::DeviceType::ANALOGSENSOR) {
|
||||
output[F_(info)] = Helpers::translated_word(FL_(info_cmd));
|
||||
output[F_(commands)] = Helpers::translated_word(FL_(commands_cmd));
|
||||
output[F_(values)] = Helpers::translated_word(FL_(values_cmd));
|
||||
} else if (cmdfunctions_.empty()) {
|
||||
output["message"] = "no commands available";
|
||||
return false;
|
||||
}
|
||||
// common commands
|
||||
output[F_(info)] = Helpers::translated_word(FL_(info_cmd));
|
||||
output[F_(values)] = Helpers::translated_word(FL_(values_cmd));
|
||||
output[F_(commands)] = Helpers::translated_word(FL_(commands_cmd));
|
||||
output[F_(entities)] = Helpers::translated_word(FL_(entities_cmd));
|
||||
|
||||
// create a list of commands we have registered, and sort them
|
||||
std::list<std::string> sorted_cmds;
|
||||
|
||||
@@ -135,6 +135,8 @@ class Command {
|
||||
static uint8_t process(const char * path, const bool is_admin, const JsonObject input, JsonObject output);
|
||||
|
||||
static const char * parse_command_string(const char * command, int8_t & id);
|
||||
static const char * get_attribute(const char * cmd);
|
||||
static bool set_attirbute(JsonObject output, const char * cmd, const char * attirbute);
|
||||
|
||||
static const char * return_code_string(const uint8_t return_code);
|
||||
|
||||
|
||||
@@ -1607,7 +1607,11 @@ void Boiler::process_UBAEnergySupplied(std::shared_ptr<const Telegram> telegram)
|
||||
//XR1A050001 A05 Pump Heat circuit (1.0 ) 1 >> 1 & 0x01 ?
|
||||
//XR1A040001 A04 Pump Cold circuit (1.0 ) 1 & 0x1 ?
|
||||
void Boiler::process_HpPower(std::shared_ptr<const Telegram> telegram) {
|
||||
has_bitupdate(telegram, VC0valve_, 0, 7);
|
||||
has_bitupdate(telegram, hp3wayValve_, 0, 6);
|
||||
// has_bitupdate(telegram, heating_, 0, 0); // heating on? https://github.com/emsesp/EMS-ESP32/discussions/1898
|
||||
has_bitupdate(telegram, hpSwitchValve_, 0, 4);
|
||||
|
||||
has_bitupdate(telegram, hpCompOn_, 3, 4);
|
||||
has_bitupdate(telegram, hpEA0_, 3, 6);
|
||||
has_update(telegram, hpCircSpd_, 4);
|
||||
@@ -1953,10 +1957,10 @@ void Boiler::process_HpSettings2(std::shared_ptr<const Telegram> telegram) {
|
||||
// Boiler(0x08) -B-> All(0x00), ?(0x049D), data: 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
void Boiler::process_HpSettings3(std::shared_ptr<const Telegram> telegram) {
|
||||
has_update(telegram, heatCable_, 2);
|
||||
has_update(telegram, VC0valve_, 3);
|
||||
// has_update(telegram, VC0valve_, 3); // read in 48D
|
||||
has_update(telegram, primePump_, 4);
|
||||
has_update(telegram, primePumpMod_, 5);
|
||||
has_update(telegram, hp3wayValve_, 6);
|
||||
// has_update(telegram, hp3wayValve_, 6); // read in 48D
|
||||
has_update(telegram, elHeatStep1_, 7);
|
||||
has_update(telegram, elHeatStep2_, 8);
|
||||
has_update(telegram, elHeatStep3_, 9);
|
||||
|
||||
@@ -1106,7 +1106,8 @@ void Thermostat::process_RC300Set(std::shared_ptr<const Telegram> telegram) {
|
||||
has_enumupdate(telegram, hc->reducemode, 5, 1); // 1-outdoor temp threshold, 2-room temp threshold, 3-reduced mode
|
||||
has_update(telegram, hc->reducetemp, 9);
|
||||
has_update(telegram, hc->noreducetemp, 12);
|
||||
has_update(telegram, hc->remoteseltemp, 17); // see https://github.com/emsesp/EMS-ESP32/issues/590
|
||||
has_enumupdate(telegram, hc->switchProgMode, 13, 1); // 1-level, 2-absolute
|
||||
has_update(telegram, hc->remoteseltemp, 17); // see https://github.com/emsesp/EMS-ESP32/issues/590
|
||||
has_update(telegram, hc->boost, 23);
|
||||
has_update(telegram, hc->boosttime, 24);
|
||||
has_update(telegram, hc->cooling, 28);
|
||||
@@ -2280,6 +2281,19 @@ bool Thermostat::set_cooloffdelay(const char * value, const int8_t id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Thermostat::set_switchProgMode(const char * value, const int8_t id) {
|
||||
auto hc = heating_circuit(id);
|
||||
if (hc == nullptr) {
|
||||
return false;
|
||||
}
|
||||
uint8_t set;
|
||||
if (!Helpers::value2enum(value, set, FL_(enum_switchProgMode))) {
|
||||
return false;
|
||||
}
|
||||
write_command(set_typeids[hc->hc()], 13, set + 1, set_typeids[hc->hc()]);
|
||||
return true;
|
||||
}
|
||||
|
||||
// sets the thermostat ww circulation working mode, where mode is a string
|
||||
bool Thermostat::set_wwcircmode(const char * value, const int8_t id) {
|
||||
uint8_t dhw = id2dhw(id);
|
||||
@@ -4511,6 +4525,8 @@ void Thermostat::register_device_values_hc(std::shared_ptr<Thermostat::HeatingCi
|
||||
register_device_value(tag, &hc->coolstart, DeviceValueType::UINT8, FL_(coolstart), DeviceValueUOM::DEGREES, MAKE_CF_CB(set_coolstart), 20, 35);
|
||||
register_device_value(tag, &hc->coolondelay, DeviceValueType::UINT8, FL_(coolondelay), DeviceValueUOM::HOURS, MAKE_CF_CB(set_coolondelay), 1, 48);
|
||||
register_device_value(tag, &hc->cooloffdelay, DeviceValueType::UINT8, FL_(cooloffdelay), DeviceValueUOM::HOURS, MAKE_CF_CB(set_cooloffdelay), 1, 48);
|
||||
register_device_value(
|
||||
tag, &hc->switchProgMode, DeviceValueType::ENUM, FL_(enum_switchProgMode), FL_(control), DeviceValueUOM::NONE, MAKE_CF_CB(set_switchProgMode));
|
||||
|
||||
break;
|
||||
case EMSdevice::EMS_DEVICE_FLAG_CRF:
|
||||
|
||||
@@ -86,6 +86,7 @@ class Thermostat : public EMSdevice {
|
||||
uint8_t climate;
|
||||
uint8_t switchonoptimization;
|
||||
uint8_t statusbyte; // from RC300monitor
|
||||
uint8_t switchProgMode;
|
||||
// RC 10
|
||||
uint8_t reducehours; // night reduce duration
|
||||
uint16_t reduceminutes; // remaining minutes to night->day
|
||||
@@ -655,6 +656,7 @@ class Thermostat : public EMSdevice {
|
||||
bool set_cooling(const char * value, const int8_t id);
|
||||
bool set_coolondelay(const char * value, const int8_t id);
|
||||
bool set_cooloffdelay(const char * value, const int8_t id);
|
||||
bool set_switchProgMode(const char * value, const int8_t id);
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -389,6 +389,7 @@ bool EMSdevice::has_cmd(const char * cmd, const int8_t id) const {
|
||||
|
||||
// list of registered device entries
|
||||
// called from the command 'entities'
|
||||
/*
|
||||
void EMSdevice::list_device_entries(JsonObject output) const {
|
||||
for (const auto & dv : devicevalues_) {
|
||||
auto fullname = dv.get_fullname();
|
||||
@@ -414,6 +415,7 @@ void EMSdevice::list_device_entries(JsonObject output) const {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// list all the telegram type IDs for this device
|
||||
void EMSdevice::show_telegram_handlers(uuid::console::Shell & shell) const {
|
||||
@@ -1435,187 +1437,182 @@ void EMSdevice::dump_telegram_info(std::vector<TelegramFunctionDump> & telegram_
|
||||
// cmd is the endpoint or name of the device entity
|
||||
// returns false if failed, otherwise true
|
||||
bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t tag) {
|
||||
JsonObject json = output;
|
||||
|
||||
// make a copy of the string command for parsing
|
||||
char command_s[30];
|
||||
strlcpy(command_s, cmd, sizeof(command_s));
|
||||
char * attribute_s = nullptr;
|
||||
|
||||
// check specific attribute to fetch instead of the complete record
|
||||
char * breakp = strchr(command_s, '/');
|
||||
if (breakp) {
|
||||
*breakp = '\0';
|
||||
attribute_s = breakp + 1;
|
||||
if (!strcmp(cmd, F_(info))) {
|
||||
return export_values(device_type(), output, tag, EMSdevice::OUTPUT_TARGET::API_VERBOSE);
|
||||
}
|
||||
if (!strcmp(cmd, F_(values))) {
|
||||
return export_values(device_type(), output, tag, EMSdevice::OUTPUT_TARGET::API_SHORTNAMES);
|
||||
}
|
||||
if (!strcmp(cmd, F_(entities))) {
|
||||
char name[30];
|
||||
for (auto & dv : devicevalues_) {
|
||||
if (tag < 0 || tag == dv.tag) {
|
||||
strlcpy(name, tag < 0 ? tag_to_mqtt(dv.tag) : "", sizeof(name));
|
||||
strlcat(name, tag < 0 && dv.tag > 0 ? "." : "", sizeof(name));
|
||||
strlcat(name, dv.short_name, sizeof(name));
|
||||
get_value_json(output[name].to<JsonObject>(), dv);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// search device value with this tag
|
||||
const char * attribute_s = Command::get_attribute(cmd);
|
||||
for (auto & dv : devicevalues_) {
|
||||
if (Helpers::toLower(command_s) == Helpers::toLower(dv.short_name) && (tag <= 0 || tag == dv.tag)) {
|
||||
uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (dv.uom == DeviceValueUOM::DEGREES) ? 2 : (dv.uom == DeviceValueUOM::DEGREES_R) ? 1 : 0;
|
||||
|
||||
const char * type = "type";
|
||||
const char * value = "value";
|
||||
|
||||
json["name"] = dv.short_name;
|
||||
|
||||
auto fullname = dv.get_fullname();
|
||||
if (!fullname.empty()) {
|
||||
json["fullname"] = dv.has_tag() ? fullname + " " + tag_to_string(dv.tag) : fullname; // suffix tag
|
||||
|
||||
// TAG https://github.com/emsesp/EMS-ESP32/issues/1338
|
||||
json["fullname"] = dv.has_tag() ? std::string(tag_to_string(dv.tag)) + " " + fullname.c_str() : fullname; // prefix tag
|
||||
}
|
||||
|
||||
if (dv.tag != DeviceValueTAG::TAG_NONE) {
|
||||
json["circuit"] = tag_to_mqtt(dv.tag);
|
||||
}
|
||||
|
||||
char val[10];
|
||||
switch (dv.type) {
|
||||
case DeviceValueType::ENUM: {
|
||||
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
|
||||
if (EMSESP::system_.enum_format() == ENUM_FORMAT_INDEX) {
|
||||
json[value] = (uint8_t)(*(uint8_t *)(dv.value_p));
|
||||
} else {
|
||||
json[value] = Helpers::translated_word(dv.options[*(uint8_t *)(dv.value_p)]); // text
|
||||
}
|
||||
}
|
||||
json[type] = F_(enum);
|
||||
JsonArray enum_ = json[F_(enum)].to<JsonArray>();
|
||||
for (uint8_t i = 0; i < dv.options_size; i++) {
|
||||
enum_.add(Helpers::translated_word(dv.options[i]));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case DeviceValueType::UINT16:
|
||||
if (Helpers::hasValue(*(uint16_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(uint16_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::UINT8:
|
||||
if (Helpers::hasValue(*(uint8_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(uint8_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::INT16:
|
||||
if (Helpers::hasValue(*(int16_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(int16_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::INT8:
|
||||
if (Helpers::hasValue(*(int8_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(int8_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::UINT24:
|
||||
case DeviceValueType::UINT32:
|
||||
if (Helpers::hasValue(*(uint32_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(uint32_t *)(dv.value_p), dv.numeric_operator));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::BOOL:
|
||||
if (Helpers::hasValue(*(uint8_t *)(dv.value_p), EMS_VALUE_BOOL)) {
|
||||
auto value_b = (bool)*(uint8_t *)(dv.value_p);
|
||||
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||
json[value] = value_b;
|
||||
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||
json[value] = value_b ? 1 : 0;
|
||||
} else {
|
||||
char s[12];
|
||||
json[value] = Helpers::render_boolean(s, value_b);
|
||||
}
|
||||
}
|
||||
json[type] = ("boolean");
|
||||
break;
|
||||
|
||||
case DeviceValueType::TIME:
|
||||
if (Helpers::hasValue(*(uint32_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(uint32_t *)(dv.value_p), dv.numeric_operator));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::STRING:
|
||||
if (Helpers::hasValue((char *)(dv.value_p))) {
|
||||
json[value] = (char *)(dv.value_p);
|
||||
}
|
||||
json[type] = ("string");
|
||||
break;
|
||||
|
||||
case DeviceValueType::CMD:
|
||||
if (dv.options_size > 1) {
|
||||
json[type] = F_(enum);
|
||||
JsonArray enum_ = json[F_(enum)].to<JsonArray>();
|
||||
for (uint8_t i = 0; i < dv.options_size; i++) {
|
||||
enum_.add(Helpers::translated_word(dv.options[i]));
|
||||
}
|
||||
} else if (dv.uom != DeviceValueUOM::NONE) {
|
||||
json[type] = F_(number);
|
||||
} else {
|
||||
json[type] = F_(command);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
json[type] = Helpers::translated_word(FL_(unknown));
|
||||
break;
|
||||
}
|
||||
|
||||
// set the min and max only for commands
|
||||
if (dv.has_cmd) {
|
||||
int16_t dv_set_min;
|
||||
uint32_t dv_set_max;
|
||||
if (dv.get_min_max(dv_set_min, dv_set_max)) {
|
||||
json["min"] = dv_set_min;
|
||||
json["max"] = dv_set_max;
|
||||
}
|
||||
}
|
||||
|
||||
// add uom if it's not a " " (single space)
|
||||
if (dv.uom != DeviceValueUOM::NONE) {
|
||||
json["uom"] = uom_to_string(dv.uom);
|
||||
}
|
||||
|
||||
json["readable"] = !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE);
|
||||
json["writeable"] = dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY);
|
||||
json["visible"] = !dv.has_state(DeviceValueState::DV_WEB_EXCLUDE);
|
||||
|
||||
// TODO refactor to remove containsKey as it's costly and not advisable to use it
|
||||
// https://arduinojson.org/v7/api/jsonobject/containskey/#avoid
|
||||
|
||||
// commented out, leads to issues if type is set to number
|
||||
// if there is no value, mention it
|
||||
// if (!json.containsKey(value)) {
|
||||
// json[value] = "not set";
|
||||
// }
|
||||
|
||||
if (cmd == Helpers::toLower(dv.short_name) && (tag <= 0 || tag == dv.tag)) {
|
||||
get_value_json(output, dv);
|
||||
// if we're filtering on an attribute, go find it
|
||||
if (attribute_s) {
|
||||
if (json.containsKey(attribute_s)) {
|
||||
std::string data = json[attribute_s].as<std::string>();
|
||||
output.clear();
|
||||
output["api_data"] = data; // always as string
|
||||
return true;
|
||||
}
|
||||
return EMSESP::return_not_found(output, attribute_s, command_s); // not found
|
||||
return Command::set_attirbute(output, cmd, attribute_s);
|
||||
}
|
||||
}
|
||||
return false; // not found, but don't return a message error yet
|
||||
}
|
||||
|
||||
// build the json for a specific entity
|
||||
void EMSdevice::get_value_json(JsonObject json, DeviceValue & dv) {
|
||||
uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (dv.uom == DeviceValueUOM::DEGREES) ? 2 : (dv.uom == DeviceValueUOM::DEGREES_R) ? 1 : 0;
|
||||
|
||||
const char * type = "type";
|
||||
const char * value = "value";
|
||||
|
||||
json["name"] = dv.short_name;
|
||||
|
||||
auto fullname = dv.get_fullname();
|
||||
if (!fullname.empty()) {
|
||||
json["fullname"] = dv.has_tag() ? fullname + " " + tag_to_string(dv.tag) : fullname; // suffix tag
|
||||
|
||||
// TAG https://github.com/emsesp/EMS-ESP32/issues/1338
|
||||
json["fullname"] = dv.has_tag() ? std::string(tag_to_string(dv.tag)) + " " + fullname.c_str() : fullname; // prefix tag
|
||||
}
|
||||
|
||||
if (dv.tag != DeviceValueTAG::TAG_NONE) {
|
||||
json["circuit"] = tag_to_mqtt(dv.tag);
|
||||
}
|
||||
|
||||
char val[10];
|
||||
switch (dv.type) {
|
||||
case DeviceValueType::ENUM: {
|
||||
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
|
||||
json["index"] = (uint8_t)(*(uint8_t *)(dv.value_p));
|
||||
json["enum"] = Helpers::translated_word(dv.options[*(uint8_t *)(dv.value_p)]); // text
|
||||
if (EMSESP::system_.enum_format() == ENUM_FORMAT_INDEX) {
|
||||
json[value] = (uint8_t)(*(uint8_t *)(dv.value_p));
|
||||
} else {
|
||||
json[value] = Helpers::translated_word(dv.options[*(uint8_t *)(dv.value_p)]); // text
|
||||
}
|
||||
return true;
|
||||
}
|
||||
json[type] = F_(enum);
|
||||
JsonArray enum_ = json[F_(enum)].to<JsonArray>();
|
||||
for (uint8_t i = 0; i < dv.options_size; i++) {
|
||||
enum_.add(Helpers::translated_word(dv.options[i]));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case DeviceValueType::UINT16:
|
||||
if (Helpers::hasValue(*(uint16_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(uint16_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::UINT8:
|
||||
if (Helpers::hasValue(*(uint8_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(uint8_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::INT16:
|
||||
if (Helpers::hasValue(*(int16_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(int16_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::INT8:
|
||||
if (Helpers::hasValue(*(int8_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(int8_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::UINT24:
|
||||
case DeviceValueType::UINT32:
|
||||
if (Helpers::hasValue(*(uint32_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(uint32_t *)(dv.value_p), dv.numeric_operator));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::BOOL:
|
||||
if (Helpers::hasValue(*(uint8_t *)(dv.value_p), EMS_VALUE_BOOL)) {
|
||||
auto value_b = (bool)*(uint8_t *)(dv.value_p);
|
||||
json["bool"] = value_b;
|
||||
json["index"] = value_b ? 1 : 0;
|
||||
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||
json[value] = value_b;
|
||||
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||
json[value] = value_b ? 1 : 0;
|
||||
} else {
|
||||
char s[12];
|
||||
json[value] = Helpers::render_boolean(s, value_b);
|
||||
}
|
||||
}
|
||||
json[type] = ("boolean");
|
||||
break;
|
||||
|
||||
case DeviceValueType::TIME:
|
||||
if (Helpers::hasValue(*(uint32_t *)(dv.value_p))) {
|
||||
json[value] = serialized(Helpers::render_value(val, *(uint32_t *)(dv.value_p), dv.numeric_operator));
|
||||
}
|
||||
json[type] = F_(number);
|
||||
break;
|
||||
|
||||
case DeviceValueType::STRING:
|
||||
if (Helpers::hasValue((char *)(dv.value_p))) {
|
||||
json[value] = (char *)(dv.value_p);
|
||||
}
|
||||
json[type] = ("string");
|
||||
break;
|
||||
|
||||
case DeviceValueType::CMD:
|
||||
if (dv.options_size > 1) {
|
||||
json[type] = F_(enum);
|
||||
JsonArray enum_ = json[F_(enum)].to<JsonArray>();
|
||||
for (uint8_t i = 0; i < dv.options_size; i++) {
|
||||
enum_.add(Helpers::translated_word(dv.options[i]));
|
||||
}
|
||||
} else if (dv.uom != DeviceValueUOM::NONE) {
|
||||
json[type] = F_(number);
|
||||
} else {
|
||||
json[type] = F_(command);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
json[type] = Helpers::translated_word(FL_(unknown));
|
||||
break;
|
||||
}
|
||||
|
||||
// set the min and max only for commands
|
||||
if (dv.has_cmd) {
|
||||
int16_t dv_set_min;
|
||||
uint32_t dv_set_max;
|
||||
if (dv.get_min_max(dv_set_min, dv_set_max)) {
|
||||
json["min"] = dv_set_min;
|
||||
json["max"] = dv_set_max;
|
||||
}
|
||||
}
|
||||
|
||||
return false; // not found, but don't return a message error yet
|
||||
// add uom if it's not a " " (single space)
|
||||
if (dv.uom != DeviceValueUOM::NONE) {
|
||||
json["uom"] = uom_to_string(dv.uom);
|
||||
}
|
||||
|
||||
json["readable"] = !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE);
|
||||
json["writeable"] = dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY);
|
||||
json["visible"] = !dv.has_state(DeviceValueState::DV_WEB_EXCLUDE);
|
||||
}
|
||||
|
||||
// mqtt publish all single values from one device (used for time schedule)
|
||||
|
||||
@@ -214,7 +214,7 @@ class EMSdevice {
|
||||
void show_telegram_handlers(uuid::console::Shell & shell) const;
|
||||
char * show_telegram_handlers(char * result, const size_t len, const uint8_t handlers);
|
||||
void show_mqtt_handlers(uuid::console::Shell & shell) const;
|
||||
void list_device_entries(JsonObject output) const;
|
||||
// void list_device_entries(JsonObject output) const;
|
||||
void add_handlers_ignored(const uint16_t handler);
|
||||
|
||||
void set_climate_minmax(int8_t tag, int16_t min, uint32_t max);
|
||||
@@ -227,6 +227,7 @@ class EMSdevice {
|
||||
std::string get_value_uom(const std::string & shortname) const;
|
||||
|
||||
bool get_value_info(JsonObject root, const char * cmd, const int8_t id);
|
||||
void get_value_json(JsonObject output, DeviceValue & dv);
|
||||
void get_dv_info(JsonObject json);
|
||||
|
||||
enum OUTPUT_TARGET : uint8_t { API_VERBOSE, API_SHORTNAMES, MQTT, CONSOLE };
|
||||
@@ -353,8 +354,7 @@ class EMSdevice {
|
||||
ANALOGSENSOR, // for internal analog sensors
|
||||
SCHEDULER, // for internal schedule
|
||||
CUSTOM, // for user defined entities
|
||||
|
||||
BOILER,
|
||||
BOILER, // frome here on enum the ems-devices
|
||||
THERMOSTAT,
|
||||
MIXER,
|
||||
SOLAR,
|
||||
|
||||
@@ -1343,38 +1343,12 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, const
|
||||
// Print to LOG showing we've added a new device
|
||||
LOG_INFO("Recognized new %s with deviceID 0x%02X", EMSdevice::device_type_2_device_name(device_type), device_id);
|
||||
|
||||
// add commands 'info', 'commands', 'values', 'entities' for all EMS devices
|
||||
// and register the MQTT subscribe topic for this device
|
||||
// register the MQTT subscribe topic for this device
|
||||
// except for connect, controller and gateway
|
||||
if ((device_type == DeviceType::CONNECT) || (device_type == DeviceType::CONTROLLER) || (device_type == DeviceType::GATEWAY)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Command::add(
|
||||
device_type,
|
||||
F_(info),
|
||||
[device_type](const char * value, const int8_t id, JsonObject output) {
|
||||
return EMSdevice::export_values(device_type, output, id, EMSdevice::OUTPUT_TARGET::API_VERBOSE);
|
||||
},
|
||||
FL_(info_cmd));
|
||||
Command::add(
|
||||
device_type,
|
||||
F_(values),
|
||||
[device_type](const char * value, const int8_t id, JsonObject output) {
|
||||
return EMSdevice::export_values(device_type, output, id, EMSdevice::OUTPUT_TARGET::API_SHORTNAMES);
|
||||
},
|
||||
FL_(values_cmd));
|
||||
Command::add(
|
||||
device_type,
|
||||
F_(commands),
|
||||
[device_type](const char * value, const int8_t id, JsonObject output) { return command_commands(device_type, output, id); },
|
||||
FL_(commands_cmd));
|
||||
Command::add(
|
||||
device_type,
|
||||
F_(entities),
|
||||
[device_type](const char * value, const int8_t id, JsonObject output) { return command_entities(device_type, output, id); },
|
||||
FL_(entities_cmd));
|
||||
|
||||
// MQTT subscribe to the device e.g. "ems-esp/boiler/#"
|
||||
auto topic = std::string(EMSdevice::device_type_2_device_name(device_type)) + "/#";
|
||||
Mqtt::subscribe(device_type, topic, nullptr);
|
||||
@@ -1382,25 +1356,6 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, const
|
||||
return true;
|
||||
}
|
||||
|
||||
// list device entities
|
||||
bool EMSESP::command_entities(uint8_t device_type, JsonObject output, const int8_t id) {
|
||||
JsonObject node;
|
||||
|
||||
for (const auto & emsdevice : emsdevices) {
|
||||
if (emsdevice && (emsdevice->device_type() == device_type)) {
|
||||
emsdevice->list_device_entries(output);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// list all available commands, return as json
|
||||
bool EMSESP::command_commands(uint8_t device_type, JsonObject output, const int8_t id) {
|
||||
return Command::list(device_type, output);
|
||||
}
|
||||
|
||||
// send a read request, passing it into to the Tx Service, with optional offset and length
|
||||
void EMSESP::send_read_request(const uint16_t type_id, const uint8_t dest, const uint8_t offset, const uint8_t length, const bool front) {
|
||||
txservice_.read_request(type_id, dest, offset, length, front);
|
||||
|
||||
@@ -251,8 +251,6 @@ class EMSESP {
|
||||
static void process_version(std::shared_ptr<const Telegram> telegram);
|
||||
static void publish_response(std::shared_ptr<const Telegram> telegram);
|
||||
static void publish_all_loop();
|
||||
static bool command_commands(uint8_t device_type, JsonObject output, const int8_t id);
|
||||
static bool command_entities(uint8_t device_type, JsonObject output, const int8_t id);
|
||||
|
||||
static constexpr uint32_t EMS_FETCH_FREQUENCY = 60000; // check every minute
|
||||
static constexpr uint8_t EMS_WAIT_KM_TIMEOUT = 60; // wait one minute
|
||||
|
||||
@@ -349,6 +349,7 @@ MAKE_ENUM(enum_control1, FL_(rc310), FL_(rc200), FL_(rc100), FL_(rc100h), FL_(tc
|
||||
MAKE_ENUM(enum_control2, FL_(off), FL_(dash), FL_(rc100), FL_(rc100h), FL_(dash), FL_(rc120rf), FL_(rc220), FL_(single)) // BC400
|
||||
|
||||
MAKE_ENUM(enum_switchmode, FL_(off), FL_(eco), FL_(comfort), FL_(heat))
|
||||
MAKE_ENUM(enum_switchProgMode, FL_(level), FL_(absolute))
|
||||
|
||||
MAKE_ENUM(enum_dayOfWeek, FL_(day_mo), FL_(day_tu), FL_(day_we), FL_(day_th), FL_(day_fr), FL_(day_sa), FL_(day_su), FL_(all))
|
||||
MAKE_ENUM(enum_progMode2, FL_(own_1), FL_(family), FL_(morning), FL_(evening), FL_(am), FL_(pm), FL_(midday), FL_(singles), FL_(seniors), FL_(new), FL_(own_2))
|
||||
|
||||
@@ -279,6 +279,8 @@ MAKE_WORD_TRANSLATION(functioning_mode, "functioning mode", "Funktionsweise", "f
|
||||
MAKE_WORD_TRANSLATION(unmixed, "unmixed", "ungemischt", "", "", "niezmieszany", "", "", "", "", "") // TODO translate
|
||||
MAKE_WORD_TRANSLATION(unmixedIPM, "unmixed IPM", "ungemischt IPM", "", "", "niezmieszany IPM", "", "", "", "", "") // TODO translate
|
||||
MAKE_WORD_TRANSLATION(mixed, "mixed IPM", "gemischt IPM", "", "", "zmieszany IPM", "", "", "", "", "") // TODO translate
|
||||
MAKE_WORD_TRANSLATION(level, "level", "Level") // TODO translate
|
||||
MAKE_WORD_TRANSLATION(absolute, "absolute", "Absolut") // TODO translate
|
||||
|
||||
// mixer
|
||||
MAKE_WORD_TRANSLATION(stopped, "stopped", "gestoppt", "gestopt", "stoppad", "zatrzymany", "stoppet", "arrêté", "durdu", "fermato", "zastavený")
|
||||
|
||||
126
src/system.cpp
126
src/system.cpp
@@ -108,31 +108,40 @@ bool System::command_response(const char * value, const int8_t id, JsonObject ou
|
||||
return true;
|
||||
}
|
||||
|
||||
// output all the EMS devices and their values, plus the sensors and any custom entities
|
||||
// not scheduler as these are records with no output data
|
||||
// output all the devices and the values
|
||||
// not system info
|
||||
bool System::command_allvalues(const char * value, const int8_t id, JsonObject output) {
|
||||
JsonDocument doc;
|
||||
JsonObject device_output;
|
||||
// default to values
|
||||
if (value == nullptr || strlen(value) == 0) {
|
||||
value = F_(values);
|
||||
}
|
||||
|
||||
// System Entities
|
||||
// device_output = output["System"].to<JsonObject>();
|
||||
// get_value_info(device_output, value);
|
||||
|
||||
// EMS-Device Entities
|
||||
for (const auto & emsdevice : EMSESP::emsdevices) {
|
||||
std::string title = emsdevice->device_type_2_device_name_translated() + std::string(" ") + emsdevice->to_string();
|
||||
device_output = output[title].to<JsonObject>();
|
||||
emsdevice->generate_values(device_output, DeviceValueTAG::TAG_NONE, true, EMSdevice::OUTPUT_TARGET::API_VERBOSE); // use nested for id -1 and 0
|
||||
emsdevice->get_value_info(device_output, value, DeviceValueTAG::TAG_NONE);
|
||||
}
|
||||
|
||||
// Custom Entities
|
||||
device_output = output["Custom Entities"].to<JsonObject>();
|
||||
EMSESP::webCustomEntityService.get_value_info(device_output, "");
|
||||
EMSESP::webCustomEntityService.get_value_info(device_output, value);
|
||||
|
||||
// Scheduler
|
||||
device_output = output["Scheduler"].to<JsonObject>();
|
||||
EMSESP::webSchedulerService.get_value_info(device_output, "");
|
||||
EMSESP::webSchedulerService.get_value_info(device_output, value);
|
||||
|
||||
// Sensors
|
||||
device_output = output["Analog Sensors"].to<JsonObject>();
|
||||
EMSESP::analogsensor_.get_value_info(device_output, "values");
|
||||
EMSESP::analogsensor_.get_value_info(device_output, value);
|
||||
device_output = output["Temperature Sensors"].to<JsonObject>();
|
||||
EMSESP::temperaturesensor_.get_value_info(device_output, "values");
|
||||
EMSESP::temperaturesensor_.get_value_info(device_output, value);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -848,8 +857,6 @@ void System::commands_init() {
|
||||
#endif
|
||||
|
||||
// these commands will return data in JSON format
|
||||
Command::add(EMSdevice::DeviceType::SYSTEM, F_(info), System::command_info, FL_(system_info_cmd));
|
||||
Command::add(EMSdevice::DeviceType::SYSTEM, F_(commands), System::command_commands, FL_(commands_cmd));
|
||||
Command::add(EMSdevice::DeviceType::SYSTEM, F("response"), System::command_response, FL_(commands_response));
|
||||
Command::add(EMSdevice::DeviceType::SYSTEM, F("allvalues"), System::command_allvalues, FL_(allvalues_cmd));
|
||||
|
||||
@@ -1250,11 +1257,6 @@ bool System::check_upgrade(bool factory_settings) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// list commands
|
||||
bool System::command_commands(const char * value, const int8_t id, JsonObject output) {
|
||||
return Command::list(EMSdevice::DeviceType::SYSTEM, output);
|
||||
}
|
||||
|
||||
// convert settings file into json object
|
||||
void System::extractSettings(const char * filename, const char * section, JsonObject output) {
|
||||
#ifndef EMSESP_STANDALONE
|
||||
@@ -1292,27 +1294,41 @@ bool System::saveSettings(const char * filename, const char * section, JsonObjec
|
||||
}
|
||||
|
||||
// return back a system value
|
||||
bool System::get_value_info(JsonObject root, const char * command) {
|
||||
if (command == nullptr || strlen(command) == 0) {
|
||||
bool System::get_value_info(JsonObject output, const char * cmd) {
|
||||
if (cmd == nullptr || strlen(cmd) == 0) {
|
||||
LOG_ERROR("empty system command");
|
||||
return false;
|
||||
}
|
||||
|
||||
// cmd is lower case of the command
|
||||
char cmd[COMMAND_MAX_LENGTH];
|
||||
strlcpy(cmd, Helpers::toLower(command).c_str(), sizeof(cmd));
|
||||
// check for hardcoded "info"/"value"
|
||||
if (!strcmp(cmd, F_(info)) || !strcmp(cmd, F_(values))) {
|
||||
return command_info("", 0, output);
|
||||
}
|
||||
|
||||
// fetch all the data from the system
|
||||
// fetch all the data from the system in a different json
|
||||
JsonDocument doc;
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
(void)command_info("", 0, root);
|
||||
|
||||
// check for hardcoded "info"
|
||||
if (!strcmp(cmd, F_(info)) || !strcmp(cmd, F_(value))) {
|
||||
// list all entities
|
||||
if (!strcmp(cmd, F_(entities))) {
|
||||
for (JsonPair p : root) {
|
||||
if (p.value().is<JsonObject>()) {
|
||||
// String prefix = p.key().c_str();
|
||||
for (JsonPair p1 : p.value().as<JsonObject>()) {
|
||||
JsonObject entity = output[String(p.key().c_str()) + '.' + p1.key().c_str()].to<JsonObject>();
|
||||
get_value_json(entity, p.key().c_str(), p1.key().c_str(), p1.value());
|
||||
}
|
||||
} // else { // we don't have pairs in json root object
|
||||
// get_value_json(entity, "", p.key().c_str(), p.value());
|
||||
// }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
char * val = strstr(cmd, "/value");
|
||||
if (val) {
|
||||
val[0] = '\0';
|
||||
*val = '\0';
|
||||
}
|
||||
|
||||
char * slash = strchr(cmd, '/');
|
||||
@@ -1321,40 +1337,58 @@ bool System::get_value_info(JsonObject root, const char * command) {
|
||||
slash++;
|
||||
}
|
||||
|
||||
std::string s;
|
||||
// list values for a jsonObject in system, e.g. /api/system/network
|
||||
if (!slash || !strcmp(slash, F_(info)) || !strcmp(slash, F_(values))) {
|
||||
for (JsonPair p : root) {
|
||||
if (Helpers::toLower(p.key().c_str()) == cmd && p.value().is<JsonObject>()) {
|
||||
for (JsonPair p1 : p.value().as<JsonObject>()) {
|
||||
output[p1.key().c_str()] = p1.value().as<std::string>();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// value info or api_data for a single value
|
||||
// Loop through all the key-value pairs in root to find the key, case independent
|
||||
if (slash) { // search the top level first
|
||||
for (JsonPair p : root) {
|
||||
if (p.value().is<JsonObject>() && Helpers::toLower(p.key().c_str()) == cmd) {
|
||||
for (JsonPair p1 : p.value().as<JsonObject>()) {
|
||||
if (Helpers::toLower(p1.key().c_str()) == slash && !p1.value().is<JsonObject>()) {
|
||||
s = p1.value().as<std::string>();
|
||||
break;
|
||||
if (val) {
|
||||
output["api_data"] = p1.value().as<std::string>();
|
||||
return true;
|
||||
}
|
||||
get_value_json(output, p.key().c_str(), p1.key().c_str(), p1.value());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // else skipt, but we don't have value pairs in system root
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void System::get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val) {
|
||||
output["name"] = name;
|
||||
if (circuit.length()) {
|
||||
output["circuit"] = circuit;
|
||||
}
|
||||
output["readable"] = true;
|
||||
output["writable"] = false;
|
||||
output["visible"] = true;
|
||||
if (val.is<bool>()) {
|
||||
output["value"] = val.as<bool>();
|
||||
output["type"] = "boolean";
|
||||
} else if (val.is<String>() || val.is<const char *>() || val.is<std::string>()) {
|
||||
output["value"] = val.as<std::string>();
|
||||
output["type"] = "string";
|
||||
} else {
|
||||
for (JsonPair p : root) {
|
||||
if (Helpers::toLower(p.key().c_str()) == cmd && !p.value().is<JsonObject>()) {
|
||||
s = p.value().as<std::string>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
output["value"] = val.as<float>();
|
||||
output["type"] = "number";
|
||||
}
|
||||
|
||||
root.clear(); // clear json, we only one a single value
|
||||
|
||||
if (!s.empty()) {
|
||||
if (val) {
|
||||
root["api_data"] = s;
|
||||
} else {
|
||||
root["value"] = s;
|
||||
}
|
||||
return true; // found
|
||||
}
|
||||
|
||||
return false; // not found
|
||||
}
|
||||
|
||||
// export status information including the device information
|
||||
|
||||
@@ -59,10 +59,10 @@ class System {
|
||||
static bool command_watch(const char * value, const int8_t id);
|
||||
static bool command_message(const char * value, const int8_t id);
|
||||
static bool command_info(const char * value, const int8_t id, JsonObject output);
|
||||
static bool command_commands(const char * value, const int8_t id, JsonObject output);
|
||||
static bool command_response(const char * value, const int8_t id, JsonObject output);
|
||||
static bool command_allvalues(const char * value, const int8_t id, JsonObject output);
|
||||
static bool get_value_info(JsonObject root, const char * cmd);
|
||||
static void get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val);
|
||||
|
||||
#if defined(EMSESP_TEST)
|
||||
static bool command_test(const char * value, const int8_t id);
|
||||
|
||||
@@ -343,75 +343,43 @@ bool TemperatureSensor::updated_values() {
|
||||
|
||||
// called from emsesp.cpp for commands
|
||||
bool TemperatureSensor::get_value_info(JsonObject output, const char * cmd, const int8_t id) {
|
||||
// check of it a 'commands' command
|
||||
if (Helpers::toLower(cmd) == F_(commands)) {
|
||||
return Command::list(EMSdevice::DeviceType::TEMPERATURESENSOR, output);
|
||||
}
|
||||
|
||||
// return empty json if there are no sensors
|
||||
if (sensors_.empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t show_all = 0;
|
||||
if (Helpers::hasValue(cmd)) {
|
||||
show_all = (strncmp(cmd, F_(info), 4) == 0) ? 1 : (strncmp(cmd, F_(values), 6) == 0) ? 2 : 0;
|
||||
}
|
||||
|
||||
// see if we're showing all sensors
|
||||
if (show_all) {
|
||||
if (!strcmp(cmd, F_(info)) || !strcmp(cmd, F_(values))) {
|
||||
for (const auto & sensor : sensors_) {
|
||||
if (show_all == 1) {
|
||||
// info
|
||||
JsonObject dataSensor = output[sensor.name()].to<JsonObject>();
|
||||
addSensorJson(dataSensor, sensor);
|
||||
} else {
|
||||
// values, shortname version. Also used in 'system allvalues'
|
||||
if (Helpers::hasValue(sensor.temperature_c)) {
|
||||
char val[10];
|
||||
output[sensor.name()] = serialized(Helpers::render_value(val, sensor.temperature_c, 10, EMSESP::system_.fahrenheit() ? 2 : 0));
|
||||
}
|
||||
if (Helpers::hasValue(sensor.temperature_c)) {
|
||||
char val[10];
|
||||
output[sensor.name()] = serialized(Helpers::render_value(val, sensor.temperature_c, 10, EMSESP::system_.fahrenheit() ? 2 : 0));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// this is for a specific sensor
|
||||
// make a copy of the string command for parsing, and lowercase it
|
||||
char sensor_name[COMMAND_MAX_LENGTH] = {'\0'};
|
||||
char * attribute_s = nullptr;
|
||||
strlcpy(sensor_name, Helpers::toLower(cmd).c_str(), sizeof(sensor_name));
|
||||
|
||||
// check for a specific attribute to fetch instead of the complete record
|
||||
char * breakp = strchr(sensor_name, '/');
|
||||
if (breakp) {
|
||||
*breakp = '\0';
|
||||
attribute_s = breakp + 1;
|
||||
if (!strcmp(cmd, F_(entities))) {
|
||||
for (const auto & sensor : sensors_) {
|
||||
get_value_json(output[sensor.name()].to<JsonObject>(), sensor);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// this is for a specific sensor
|
||||
const char * attribute_s = Command::get_attribute(cmd);
|
||||
|
||||
for (const auto & sensor : sensors_) {
|
||||
// match custom name or sensor ID
|
||||
if (sensor_name == Helpers::toLower(sensor.name()) || sensor_name == Helpers::toLower(sensor.id())) {
|
||||
// add all the data elements
|
||||
addSensorJson(output, sensor);
|
||||
// if we're filtering on an attribute, go find it
|
||||
if (attribute_s) {
|
||||
if (output.containsKey(attribute_s)) {
|
||||
std::string data = output[attribute_s].as<std::string>();
|
||||
output.clear();
|
||||
output["api_data"] = data; // always as string
|
||||
return true;
|
||||
}
|
||||
return EMSESP::return_not_found(output, attribute_s, sensor_name); // not found
|
||||
}
|
||||
return true; // found a match, exit
|
||||
if (cmd == Helpers::toLower(sensor.name()) || cmd == Helpers::toLower(sensor.id())) {
|
||||
get_value_json(output, sensor);
|
||||
return Command::set_attirbute(output, cmd, attribute_s);
|
||||
}
|
||||
}
|
||||
|
||||
return false; // not found
|
||||
}
|
||||
|
||||
void TemperatureSensor::addSensorJson(JsonObject output, const Sensor & sensor) {
|
||||
void TemperatureSensor::get_value_json(JsonObject output, const Sensor & sensor) {
|
||||
output["id"] = sensor.id();
|
||||
output["name"] = sensor.name();
|
||||
if (Helpers::hasValue(sensor.temperature_c)) {
|
||||
|
||||
@@ -151,8 +151,8 @@ class TemperatureSensor {
|
||||
bool temperature_convert_complete();
|
||||
int16_t get_temperature_c(const uint8_t addr[]);
|
||||
uint64_t get_id(const uint8_t addr[]);
|
||||
void get_value_json(JsonObject output, const Sensor & sensor);
|
||||
void remove_ha_topic(const std::string & id);
|
||||
void addSensorJson(JsonObject output, const Sensor & sensor);
|
||||
|
||||
std::vector<Sensor> sensors_; // our list of active sensors
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
#define EMSESP_APP_VERSION "3.7.0-dev.28"
|
||||
#define EMSESP_APP_VERSION "3.7.0-dev.29"
|
||||
|
||||
@@ -256,20 +256,6 @@ void WebCustomEntityService::show_values(JsonObject output) {
|
||||
|
||||
// process json output for info/commands and value_info
|
||||
bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd) {
|
||||
// check of it a 'commands' command
|
||||
if (Helpers::toLower(cmd) == F_(commands)) {
|
||||
output[F_(info)] = Helpers::translated_word(FL_(info_cmd));
|
||||
output[F_(commands)] = Helpers::translated_word(FL_(commands_cmd));
|
||||
output[F_(values)] = Helpers::translated_word(FL_(values_cmd));
|
||||
|
||||
for (const auto & entity : *customEntityItems_) {
|
||||
if (entity.writeable) {
|
||||
output[entity.name] = "custom entity";
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// if no custom entries, return empty json
|
||||
// even if we're looking for a specific entity
|
||||
// https://github.com/emsesp/EMS-ESP32/issues/1297
|
||||
@@ -278,7 +264,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
|
||||
}
|
||||
|
||||
// if it's info or values...
|
||||
if (strlen(cmd) == 0 || Helpers::toLower(cmd) == F_(values) || Helpers::toLower(cmd) == F_(info)) {
|
||||
if (!strlen(cmd) || !strcmp(cmd, F_(values)) || !strcmp(cmd, F_(info))) {
|
||||
// list all names
|
||||
for (const CustomEntityItem & entity : *customEntityItems_) {
|
||||
render_value(output, entity);
|
||||
@@ -286,59 +272,50 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
|
||||
return true;
|
||||
}
|
||||
|
||||
char command_s[COMMAND_MAX_LENGTH];
|
||||
strlcpy(command_s, Helpers::toLower(cmd).c_str(), sizeof(command_s));
|
||||
char * attribute_s = nullptr;
|
||||
|
||||
// check specific attribute to fetch instead of the complete record
|
||||
char * breakp = strchr(command_s, '/');
|
||||
if (breakp) {
|
||||
*breakp = '\0';
|
||||
attribute_s = breakp + 1;
|
||||
// list all entities
|
||||
if (!strcmp(cmd, F_(entities))) {
|
||||
for (const auto & entity : *customEntityItems_) {
|
||||
auto nest = output[entity.name].to<JsonObject>();
|
||||
get_value_json(nest, entity);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// specific value info
|
||||
const char * attribute_s = Command::get_attribute(cmd);
|
||||
for (const auto & entity : *customEntityItems_) {
|
||||
if (Helpers::toLower(entity.name) == command_s) {
|
||||
output["name"] = entity.name;
|
||||
output["ram"] = entity.ram;
|
||||
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number);
|
||||
if (entity.uom > 0) {
|
||||
output["uom"] = EMSdevice::uom_to_string(entity.uom);
|
||||
}
|
||||
output["readable"] = true;
|
||||
output["writeable"] = entity.writeable;
|
||||
output["visible"] = true;
|
||||
if (entity.ram == 0) {
|
||||
output["device_id"] = Helpers::hextoa(entity.device_id);
|
||||
output["type_id"] = Helpers::hextoa(entity.type_id);
|
||||
output["offset"] = entity.offset;
|
||||
if (entity.value_type != DeviceValueType::BOOL && entity.value_type != DeviceValueType::STRING) {
|
||||
output["factor"] = entity.factor;
|
||||
} else if (entity.value_type == DeviceValueType::STRING) {
|
||||
output["bytes"] = (uint8_t)entity.factor;
|
||||
}
|
||||
}
|
||||
render_value(output, entity, true); // create the "value" field
|
||||
|
||||
if (attribute_s) {
|
||||
if (output.containsKey(attribute_s)) {
|
||||
std::string data = output[attribute_s].as<std::string>();
|
||||
output.clear();
|
||||
output["api_data"] = data; // always as string
|
||||
return true;
|
||||
}
|
||||
return EMSESP::return_not_found(output, attribute_s, command_s); // not found
|
||||
}
|
||||
}
|
||||
|
||||
if (output.size()) {
|
||||
return true;
|
||||
if (Helpers::toLower(entity.name) == cmd) {
|
||||
get_value_json(output, entity);
|
||||
return Command::set_attirbute(output, cmd, attribute_s);
|
||||
}
|
||||
}
|
||||
|
||||
return false; // not found
|
||||
}
|
||||
|
||||
// build the json for specific entity
|
||||
void WebCustomEntityService::get_value_json(JsonObject output, const CustomEntityItem & entity) {
|
||||
output["name"] = entity.name;
|
||||
output["storage"] = entity.ram ? "ram" : "ems";
|
||||
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number);
|
||||
if (entity.uom > 0) {
|
||||
output["uom"] = EMSdevice::uom_to_string(entity.uom);
|
||||
}
|
||||
output["readable"] = true;
|
||||
output["writeable"] = entity.writeable;
|
||||
output["visible"] = true;
|
||||
if (entity.ram == 0) {
|
||||
output["device_id"] = Helpers::hextoa(entity.device_id);
|
||||
output["type_id"] = Helpers::hextoa(entity.type_id);
|
||||
output["offset"] = entity.offset;
|
||||
if (entity.value_type != DeviceValueType::BOOL && entity.value_type != DeviceValueType::STRING) {
|
||||
output["factor"] = entity.factor;
|
||||
} else if (entity.value_type == DeviceValueType::STRING) {
|
||||
output["bytes"] = (uint8_t)entity.factor;
|
||||
}
|
||||
}
|
||||
render_value(output, entity, true); // create the "value" field
|
||||
}
|
||||
|
||||
// publish single value
|
||||
void WebCustomEntityService::publish_single(const CustomEntityItem & entity) {
|
||||
if (!Mqtt::enabled() || !Mqtt::publish_single()) {
|
||||
|
||||
@@ -58,6 +58,7 @@ class WebCustomEntityService : public StatefulService<WebCustomEntity> {
|
||||
void publish(const bool force = false);
|
||||
bool command_setvalue(const char * value, const int8_t id, const char * name);
|
||||
bool get_value_info(JsonObject output, const char * cmd);
|
||||
void get_value_json(JsonObject output, const CustomEntityItem & entity);
|
||||
bool get_value(std::shared_ptr<const Telegram> telegram);
|
||||
void fetch();
|
||||
void render_value(JsonObject output, CustomEntityItem entity, const bool useVal = false, const bool web = false, const bool add_uom = false);
|
||||
|
||||
@@ -51,9 +51,9 @@ void WebScheduler::read(WebScheduler & webScheduler, JsonObject root) {
|
||||
for (const ScheduleItem & scheduleItem : webScheduler.scheduleItems) {
|
||||
JsonObject si = schedule.add<JsonObject>();
|
||||
si["id"] = counter++; // id is only used to render the table and must be unique
|
||||
si["active"] = scheduleItem.flags ? scheduleItem.active : false;
|
||||
si["flags"] = scheduleItem.flags;
|
||||
si["time"] = scheduleItem.time;
|
||||
si["active"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.active : false;
|
||||
si["time"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.time : "";
|
||||
si["cmd"] = scheduleItem.cmd;
|
||||
si["value"] = scheduleItem.value;
|
||||
si["name"] = scheduleItem.name;
|
||||
@@ -76,7 +76,7 @@ StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webSchedu
|
||||
auto si = ScheduleItem();
|
||||
si.active = schedule["active"];
|
||||
si.flags = schedule["flags"];
|
||||
si.time = schedule["time"].as<std::string>();
|
||||
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>();
|
||||
@@ -132,26 +132,11 @@ bool WebSchedulerService::command_setvalue(const char * value, const int8_t id,
|
||||
|
||||
// process json output for info/commands and value_info
|
||||
bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) {
|
||||
// check of it a 'commands' command
|
||||
if (Helpers::toLower(cmd) == F_(commands)) {
|
||||
output[F_(info)] = Helpers::translated_word(FL_(info_cmd));
|
||||
output[F_(commands)] = Helpers::translated_word(FL_(commands_cmd));
|
||||
output[F_(values)] = Helpers::translated_word(FL_(values_cmd));
|
||||
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (!scheduleItem.name.empty()) {
|
||||
output[scheduleItem.name] = "activate schedule";
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (scheduleItems_->size() == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strlen(cmd) == 0 || Helpers::toLower(cmd) == F_(values) || Helpers::toLower(cmd) == F_(info)) {
|
||||
if (!strlen(cmd) || !strcmp(cmd, F_(values)) || !strcmp(cmd, F_(info))) {
|
||||
// list all names
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (!scheduleItem.name.empty()) {
|
||||
@@ -165,68 +150,59 @@ bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char command_s[COMMAND_MAX_LENGTH];
|
||||
strlcpy(command_s, Helpers::toLower(cmd).c_str(), sizeof(command_s));
|
||||
char * attribute_s = nullptr;
|
||||
const char * attribute_s = Command::get_attribute(cmd);
|
||||
|
||||
// check specific attribute to fetch instead of the complete record
|
||||
char * breakp = strchr(command_s, '/');
|
||||
if (breakp) {
|
||||
*breakp = '\0';
|
||||
attribute_s = breakp + 1;
|
||||
if (!strcmp(cmd, F_(entities))) {
|
||||
uint8_t i = 0;
|
||||
char name[30];
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
strlcpy(name, scheduleItem.name == "" ? Helpers::smallitoa(name, i++) : scheduleItem.name.c_str(), sizeof(name));
|
||||
get_value_json(output[name].to<JsonObject>(), scheduleItem);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
if (Helpers::toLower(scheduleItem.name) == command_s) {
|
||||
output["name"] = 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);
|
||||
}
|
||||
if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_CONDITION) {
|
||||
output["condition"] = scheduleItem.time;
|
||||
} else if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_ONCHANGE) {
|
||||
output["onchange"] = scheduleItem.time;
|
||||
} else if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER) {
|
||||
output["timer"] = scheduleItem.time;
|
||||
} else if (scheduleItem.flags != 0) {
|
||||
output["time"] = scheduleItem.time;
|
||||
}
|
||||
output["command"] = scheduleItem.cmd;
|
||||
output["cmd_data"] = scheduleItem.value;
|
||||
output["readable"] = true;
|
||||
output["writeable"] = true;
|
||||
output["visible"] = true;
|
||||
break;
|
||||
if (Helpers::toLower(scheduleItem.name) == cmd) {
|
||||
get_value_json(output, scheduleItem);
|
||||
return Command::set_attirbute(output, cmd, attribute_s);
|
||||
}
|
||||
}
|
||||
|
||||
if (attribute_s) {
|
||||
if (output.containsKey(attribute_s)) {
|
||||
std::string data = output[attribute_s].as<std::string>();
|
||||
output.clear();
|
||||
output["api_data"] = data; // always as a string
|
||||
return true;
|
||||
}
|
||||
return EMSESP::return_not_found(output, attribute_s, command_s); // not found
|
||||
}
|
||||
|
||||
if (output.size()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // not found
|
||||
}
|
||||
|
||||
// build the json for specific entity
|
||||
void WebSchedulerService::get_value_json(JsonObject output, const ScheduleItem & scheduleItem) {
|
||||
output["name"] = 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);
|
||||
}
|
||||
if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_CONDITION) {
|
||||
output["condition"] = scheduleItem.time;
|
||||
} else if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_ONCHANGE) {
|
||||
output["onchange"] = scheduleItem.time;
|
||||
} else if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER) {
|
||||
output["timer"] = scheduleItem.time;
|
||||
} else if (scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE) {
|
||||
output["time"] = scheduleItem.time;
|
||||
}
|
||||
output["command"] = scheduleItem.cmd;
|
||||
output["cmd_data"] = scheduleItem.value;
|
||||
bool hasName = scheduleItem.name != "";
|
||||
output["readable"] = hasName;
|
||||
output["writeable"] = hasName;
|
||||
output["visible"] = hasName;
|
||||
}
|
||||
|
||||
// publish single value
|
||||
void WebSchedulerService::publish_single(const char * name, const bool state) {
|
||||
if (!Mqtt::enabled() || !Mqtt::publish_single() || name == nullptr || name[0] == '\0') {
|
||||
@@ -346,6 +322,8 @@ bool WebSchedulerService::has_commands() {
|
||||
return false;
|
||||
}
|
||||
|
||||
#include "shuntingYard.hpp"
|
||||
|
||||
// execute scheduled command
|
||||
bool WebSchedulerService::command(const char * name, const std::string & command, const std::string & data) {
|
||||
std::string cmd = Helpers::toLower(command);
|
||||
@@ -355,13 +333,19 @@ bool WebSchedulerService::command(const char * name, const std::string & command
|
||||
// shelly(get): http://<shellyIP>/relais/0?turn=on
|
||||
// parse json
|
||||
JsonDocument doc;
|
||||
if (DeserializationError::Ok == deserializeJson(doc, cmd)) {
|
||||
if (deserializeJson(doc, cmd) == DeserializationError::Ok) {
|
||||
HTTPClient http;
|
||||
int httpResult = 0;
|
||||
String url = doc["url"];
|
||||
if (http.begin(url)) {
|
||||
// It's an HTTP call
|
||||
|
||||
String url = doc["url"] | "";
|
||||
// for a GET with parameters replace commands with values
|
||||
auto q = url.indexOf('?');
|
||||
if (q != -1) {
|
||||
auto s = url.substring(q + 1);
|
||||
std::string v = s.c_str();
|
||||
commands(v, false);
|
||||
url.replace(s, v.c_str());
|
||||
}
|
||||
if (url.startsWith("http") && http.begin(url)) {
|
||||
// add any given headers
|
||||
for (JsonPair p : doc["header"].as<JsonObject>()) {
|
||||
http.addHeader(p.key().c_str(), p.value().as<String>().c_str());
|
||||
@@ -448,8 +432,6 @@ bool WebSchedulerService::onChange(const char * cmd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#include "shuntingYard.hpp"
|
||||
|
||||
// handle condition schedules, parse string stored in schedule.time field
|
||||
void WebSchedulerService::condition() {
|
||||
for (ScheduleItem & scheduleItem : *scheduleItems_) {
|
||||
|
||||
@@ -68,6 +68,7 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
|
||||
bool has_commands();
|
||||
bool command_setvalue(const char * value, const int8_t id, const char * name);
|
||||
bool get_value_info(JsonObject output, const char * cmd);
|
||||
void get_value_json(JsonObject output, const ScheduleItem & scheduleItem);
|
||||
void ha_reset() {
|
||||
ha_registered_ = false;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
class Token {
|
||||
public:
|
||||
enum class Type {
|
||||
enum class Type : uint8_t {
|
||||
Unknown,
|
||||
Number,
|
||||
String,
|
||||
@@ -332,12 +332,13 @@ bool isnum(const std::string & s) {
|
||||
|
||||
|
||||
// replace commands like "<device>/<hc>/<cmd>" with its value"
|
||||
std::string commands(std::string & expr) {
|
||||
std::string commands(std::string & expr, bool quotes = true) {
|
||||
for (uint8_t device = 0; device < emsesp::EMSdevice::DeviceType::UNKNOWN; device++) {
|
||||
const char * d = emsesp::EMSdevice::device_type_2_device_name(device);
|
||||
auto f = expr.find(d);
|
||||
while (f != std::string::npos) {
|
||||
auto e = expr.find_first_of(")=<>|&+-*!", f);
|
||||
// entity names are alphanumeric or _
|
||||
auto e = expr.find_first_not_of("/._abcdefghijklmnopqrstuvwxyz0123456789", f);
|
||||
if (e == std::string::npos) {
|
||||
e = expr.length();
|
||||
}
|
||||
@@ -361,7 +362,7 @@ std::string commands(std::string & expr) {
|
||||
emsesp::Command::process(cmd_s.c_str(), true, input, output);
|
||||
if (output.containsKey("api_data")) {
|
||||
std::string data = output["api_data"].as<std::string>();
|
||||
if (!isnum(data)) {
|
||||
if (!isnum(data) && quotes) {
|
||||
data.insert(data.begin(), '"');
|
||||
data.insert(data.end(), '"');
|
||||
}
|
||||
@@ -596,7 +597,7 @@ std::string compute(const std::string & expr) {
|
||||
auto expr_new = emsesp::Helpers::toLower(expr);
|
||||
|
||||
// search json with url:
|
||||
auto f = expr_new.find_first_of("{");
|
||||
auto f = expr_new.find_first_of('{');
|
||||
while (f != std::string::npos) {
|
||||
auto e = f + 1;
|
||||
for (uint8_t i = 1; i > 0; e++) {
|
||||
@@ -612,8 +613,8 @@ std::string compute(const std::string & expr) {
|
||||
JsonDocument doc;
|
||||
if (DeserializationError::Ok == deserializeJson(doc, cmd)) {
|
||||
HTTPClient http;
|
||||
String url = doc["url"];
|
||||
if (http.begin(url)) {
|
||||
String url = doc["url"] | "";
|
||||
if (url.startsWith("http") && http.begin(url)) {
|
||||
int httpResult = 0;
|
||||
for (JsonPair p : doc["header"].as<JsonObject>()) {
|
||||
http.addHeader(p.key().c_str(), p.value().as<std::string>().c_str());
|
||||
@@ -647,20 +648,22 @@ std::string compute(const std::string & expr) {
|
||||
}
|
||||
|
||||
// positions: q-questionmark, c-colon
|
||||
auto q = expr_new.find_first_of("?");
|
||||
auto q = expr_new.find_first_of('?');
|
||||
while (q != std::string::npos) {
|
||||
// find corresponding colon
|
||||
auto c1 = expr_new.find_first_of(":", q + 1);
|
||||
auto q1 = expr_new.find_first_of("?", q + 1);
|
||||
auto c1 = expr_new.find_first_of(':', q + 1);
|
||||
auto q1 = expr_new.find_first_of('?', q + 1);
|
||||
while (q1 < c1 && q1 != std::string::npos && c1 != std::string::npos) {
|
||||
q1 = expr_new.find_first_of("?", q1 + 1);
|
||||
c1 = expr_new.find_first_of(":", c1 + 1);
|
||||
q1 = expr_new.find_first_of('?', q1 + 1);
|
||||
c1 = expr_new.find_first_of(':', c1 + 1);
|
||||
}
|
||||
if (c1 == std::string::npos) {
|
||||
return ""; // error: missing colon
|
||||
}
|
||||
std::string cond = calculate(expr_new.substr(0, q));
|
||||
if (cond[0] == '1') {
|
||||
if (cond.length() == 0) {
|
||||
return "";
|
||||
} else if (cond[0] == '1') {
|
||||
expr_new.erase(c1); // remove second expression after colon
|
||||
expr_new.erase(0, q + 1); // remove condition before questionmark
|
||||
} else if (cond[0] == '0') {
|
||||
@@ -668,7 +671,7 @@ std::string compute(const std::string & expr) {
|
||||
} else {
|
||||
return ""; // error
|
||||
}
|
||||
q = expr_new.find_first_of("?"); // search next instance
|
||||
q = expr_new.find_first_of('?'); // search next instance
|
||||
}
|
||||
|
||||
return calculate(expr_new);
|
||||
|
||||
Reference in New Issue
Block a user