more to psram, names for sensors, schedule, custom as char[20]

This commit is contained in:
MichaelDvP
2025-12-11 16:00:24 +01:00
parent ac982cbb15
commit 7a683d3637
20 changed files with 371 additions and 439 deletions

View File

@@ -56,13 +56,12 @@ void IRAM_ATTR AnalogSensor::freqIrq2() {
#endif
void AnalogSensor::start(const bool factory_settings) {
// if (factory_settings && EMSESP::nvs_.getString("boot").equals("E32V2_2") && EMSESP::nvs_.getString("hwrevision").equals("3.0")) {
// if (factory_settings && EMSESP::nvs_.getString("boot").equals("E32V2_2")) {
if (factory_settings && analogReadMilliVolts(39) > 700) { // core voltage > 2.6V
EMSESP::webCustomizationService.update([&](WebCustomization & settings) {
auto newSensor = AnalogCustomization();
strcpy(newSensor.name, "core_voltage");
newSensor.gpio = 39;
newSensor.name = "core_voltage";
newSensor.offset = 0;
newSensor.factor = 0.00377136; // Divider 24k - 8,66k
newSensor.uom = DeviceValueUOM::VOLTS;
@@ -70,8 +69,8 @@ void AnalogSensor::start(const bool factory_settings) {
newSensor.is_system = true;
settings.analogCustomizations.push_back(newSensor);
strcpy(newSensor.name, "supply_voltage");
newSensor.gpio = 36;
newSensor.name = "supply_voltage";
newSensor.factor = 0.017; // Divider 24k - 1,5k
newSensor.is_system = true;
settings.analogCustomizations.push_back(newSensor);
@@ -135,8 +134,8 @@ void AnalogSensor::reload(bool get_nvs) {
sensor_.set_value(sensor.offset);
}
if ((sensor.type == AnalogType::COUNTER || (sensor.type >= AnalogType::CNT_0 && sensor.type <= AnalogType::CNT_2))
&& sensor_.offset() != sensor.offset && sensor.offset != EMSESP::nvs_.getDouble(sensor.name.c_str(), 0)) {
EMSESP::nvs_.putDouble(sensor.name.c_str(), sensor.offset);
&& sensor_.offset() != sensor.offset && sensor.offset != EMSESP::nvs_.getDouble(sensor.name, 0)) {
EMSESP::nvs_.putDouble(sensor.name, sensor.offset);
sensor_.set_value(sensor.offset);
}
sensor_.set_name(sensor.name);
@@ -180,7 +179,7 @@ void AnalogSensor::reload(bool get_nvs) {
|| sensor.type == AnalogType::RGB || sensor.type == AnalogType::PULSE || (sensor.type >= AnalogType::CNT_0 && sensor.type <= AnalogType::CNT_2)) {
Command::add(
EMSdevice::DeviceType::ANALOGSENSOR,
sensor.name.c_str(),
sensor.name,
[&](const char * value, const int8_t id) { return command_setvalue(value, sensor.gpio); },
sensor.type == AnalogType::COUNTER || (sensor.type >= AnalogType::CNT_0 && sensor.type <= AnalogType::CNT_2) ? FL_(counter)
: sensor.type == AnalogType::DIGITAL_OUT ? FL_(digital_out)
@@ -238,7 +237,7 @@ void AnalogSensor::reload(bool get_nvs) {
LOG_DEBUG("I/O Counter on GPIO %02d", sensor.gpio());
sensor.polltime_ = 0;
sensor.poll_ = digitalRead(sensor.gpio());
if (double_t val = EMSESP::nvs_.getDouble(sensor.name().c_str(), 0)) {
if (double_t val = EMSESP::nvs_.getDouble(sensor.name(), 0)) {
sensor.set_value(val);
}
publish_sensor(sensor);
@@ -263,7 +262,7 @@ void AnalogSensor::reload(bool get_nvs) {
} else if (sensor.type() >= AnalogType::CNT_0 && sensor.type() <= AnalogType::CNT_2) {
auto index = sensor.type() - AnalogType::CNT_0;
LOG_DEBUG("Counter %d on GPIO %02d", index, sensor.gpio());
if (double_t val = EMSESP::nvs_.getDouble(sensor.name().c_str(), 0)) {
if (double_t val = EMSESP::nvs_.getDouble(sensor.name(), 0)) {
sensor.set_value(val);
}
publish_sensor(sensor);
@@ -316,10 +315,10 @@ void AnalogSensor::reload(bool get_nvs) {
#endif
{
if (sensor.uom() == 0) { // set state from NVS
if (!get_nvs || EMSESP::nvs_.getChar(sensor.name().c_str(), -1) == -1) {
EMSESP::nvs_.putChar(sensor.name().c_str(), (int8_t)sensor.offset());
if (!get_nvs || EMSESP::nvs_.getChar(sensor.name(), -1) == -1) {
EMSESP::nvs_.putChar(sensor.name(), (int8_t)sensor.offset());
} else {
sensor.set_offset(EMSESP::nvs_.getChar(sensor.name().c_str()));
sensor.set_offset(EMSESP::nvs_.getChar(sensor.name()));
}
} else if (sensor.uom() > 1) {
sensor.set_uom(2);
@@ -458,7 +457,7 @@ void AnalogSensor::measure() {
} else if (!sensor.poll_) { // falling edge
if (sensor.type() == AnalogType::COUNTER) {
sensor.set_value(old_value + sensor.factor());
// EMSESP::nvs_.putDouble(sensor.name().c_str(), sensor.value());
// EMSESP::nvs_.putDouble(sensor.name(), sensor.value());
} else if (sensor.type() == AnalogType::RATE) { // default uom: Hz (1/sec) with factor 1
sensor.set_value(sensor.factor() * 1000 / (sensor.polltime_ - sensor.last_polltime_));
} else if (sensor.type() == AnalogType::TIMER) { // default seconds with factor 1
@@ -495,8 +494,8 @@ void AnalogSensor::measure() {
void AnalogSensor::store_counters() {
for (auto & sensor : sensors_) {
if (sensor.type() == AnalogType::COUNTER || (sensor.type() >= AnalogType::CNT_0 && sensor.type() <= AnalogType::CNT_2)) {
if (sensor.value() != EMSESP::nvs_.getDouble(sensor.name().c_str())) {
EMSESP::nvs_.putDouble(sensor.name().c_str(), sensor.value());
if (sensor.value() != EMSESP::nvs_.getDouble(sensor.name())) {
EMSESP::nvs_.putDouble(sensor.name(), sensor.value());
}
}
}
@@ -513,7 +512,13 @@ void AnalogSensor::loop() {
// update analog information name, offset, factor, uom, type, deleted, is_system
// a type value of -1 is used to delete the sensor
// the gpio is the key
bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, double factor, uint8_t uom, int8_t type, bool deleted, bool is_system) {
bool AnalogSensor::update(uint8_t gpio, const char * org_name, double offset, double factor, uint8_t uom, int8_t type, bool deleted, bool is_system) {
char name[20];
if (org_name[0] == '\0') {
snprintf(name, sizeof(name), "%s_%02d", FL_(list_sensortype)[type], gpio);
} else {
strlcpy(name, org_name, sizeof(name));
}
// first see if we can find the sensor in our customization list
bool found_sensor = false;
EMSESP::webCustomizationService.update([&](WebCustomization & settings) {
@@ -521,12 +526,7 @@ bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, doubl
if (AnalogCustomization.type == AnalogType::COUNTER
|| (AnalogCustomization.type >= AnalogType::DIGITAL_OUT && AnalogCustomization.type <= AnalogType::PWM_2)
|| AnalogCustomization.type == AnalogType::RGB || AnalogCustomization.type == AnalogType::PULSE) {
Command::erase_command(EMSdevice::DeviceType::ANALOGSENSOR, AnalogCustomization.name.c_str());
}
if (name.empty()) {
char n[20];
snprintf(n, sizeof(n), "%s_%02d", FL_(list_sensortype)[type], gpio);
name = n;
Command::erase_command(EMSdevice::DeviceType::ANALOGSENSOR, AnalogCustomization.name);
}
if (AnalogCustomization.gpio == gpio) {
found_sensor = true; // found the record
@@ -534,14 +534,14 @@ bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, doubl
if (deleted) {
LOG_DEBUG("Removing analog sensor GPIO %02d", gpio);
EMSESP::system_.remove_gpio(gpio); // remove from used list only
EMSESP::nvs_.remove(AnalogCustomization.name.c_str());
EMSESP::nvs_.remove(AnalogCustomization.name);
settings.analogCustomizations.remove(AnalogCustomization);
} else {
// update existing record
if (name != AnalogCustomization.name) {
EMSESP::nvs_.remove(AnalogCustomization.name.c_str());
if (!strcmp(name, AnalogCustomization.name)) {
EMSESP::nvs_.remove(AnalogCustomization.name);
}
AnalogCustomization.name = name;
strlcpy(AnalogCustomization.name, name, sizeof(AnalogCustomization.name));
AnalogCustomization.offset = offset;
AnalogCustomization.factor = factor;
AnalogCustomization.uom = uom;
@@ -566,7 +566,7 @@ bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, doubl
EMSESP::webCustomizationService.update([&](WebCustomization & settings) {
auto newSensor = AnalogCustomization();
newSensor.gpio = gpio;
newSensor.name = name;
strlcpy(newSensor.name, name, sizeof(newSensor.name));
newSensor.offset = offset;
newSensor.factor = factor;
newSensor.uom = uom;
@@ -606,9 +606,9 @@ void AnalogSensor::publish_sensor(const Sensor & sensor) const {
if (Mqtt::publish_single()) {
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
if (Mqtt::publish_single2cmd()) {
snprintf(topic, sizeof(topic), "%s/%s", F_(analogsensor), sensor.name().c_str());
snprintf(topic, sizeof(topic), "%s/%s", F_(analogsensor), sensor.name());
} else {
snprintf(topic, sizeof(topic), "%s%s/%s", F_(analogsensor), "_data", sensor.name().c_str());
snprintf(topic, sizeof(topic), "%s%s/%s", F_(analogsensor), "_data", sensor.name());
}
char result[12];
if (sensor.type() == AnalogType::DIGITAL_IN || sensor.type() == AnalogType::DIGITAL_OUT) {
@@ -619,7 +619,7 @@ void AnalogSensor::publish_sensor(const Sensor & sensor) const {
Mqtt::queue_publish(topic, result); // always publish as doubles
}
char cmd[COMMAND_MAX_LENGTH];
snprintf(cmd, sizeof(cmd), "%s/%s", F_(analogsensor), sensor.name().c_str());
snprintf(cmd, sizeof(cmd), "%s/%s", F_(analogsensor), sensor.name());
EMSESP::webSchedulerService.onChange(cmd);
}
@@ -634,6 +634,8 @@ void AnalogSensor::remove_ha_topic(const int8_t type, const uint8_t gpio) const
#if CONFIG_IDF_TARGET_ESP32
if (type == AnalogType::PULSE || (type == AnalogType::DIGITAL_OUT && gpio != 25 && gpio != 26)) {
#elif CONFIG_IDF_TARGET_ESP32S2
if (type == AnalogType::PULSE || (type == AnalogType::DIGITAL_OUT && gpio != 17 && gpio != 18)) {
#else
if (type == AnalogType::PULSE || type == AnalogType::DIGITAL_OUT) {
#endif
@@ -678,26 +680,12 @@ void AnalogSensor::publish_values(const bool force) {
#else
if (sensor.type() == AnalogType::PULSE || sensor.type() == AnalogType::DIGITAL_OUT) {
#endif
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
dataSensor["value"] = sensor.value() != 0;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
dataSensor["value"] = sensor.value() != 0 ? 1 : 0;
} else {
char result[12];
dataSensor["value"] = Helpers::render_boolean(result, sensor.value() != 0);
}
Mqtt::add_value_bool(doc.as<JsonObject>(), sensor.name(), sensor.value() != 0);
} else {
dataSensor["value"] = serialized(Helpers::render_value(s, sensor.value(), 2)); // double
}
} else if (sensor.type() == AnalogType::DIGITAL_IN || sensor.type() == AnalogType::DIGITAL_OUT || sensor.type() == AnalogType::PULSE) {
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
doc[sensor.name()] = sensor.value() != 0;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
doc[sensor.name()] = sensor.value() != 0 ? 1 : 0;
} else {
char result[12];
doc[sensor.name()] = Helpers::render_boolean(result, sensor.value() != 0);
}
Mqtt::add_value_bool(doc.as<JsonObject>(), sensor.name(), sensor.value() != 0);
} else {
char s[10];
doc[sensor.name()] = serialized(Helpers::render_value(s, sensor.value(), 2));
@@ -720,7 +708,7 @@ void AnalogSensor::publish_values(const bool force) {
snprintf(val_obj, sizeof(val_obj), "value_json['%02d']['value']", sensor.gpio());
snprintf(val_cond, sizeof(val_cond), "value_json['%02d'] is defined and %s is defined", sensor.gpio(), val_obj);
} else {
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", sensor.name().c_str());
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", sensor.name());
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
}
char sample_val[12] = "0";
@@ -743,10 +731,7 @@ void AnalogSensor::publish_values(const bool force) {
config["~"] = Mqtt::base();
config["uniq_id"] = uniq_s;
char name[50];
snprintf(name, sizeof(name), "%s", sensor.name().c_str());
config["name"] = name;
config["name"] = sensor.name();
if (sensor.uom() != DeviceValueUOM::NONE && sensor.type() != AnalogType::DIGITAL_OUT) {
config["unit_of_meas"] = EMSdevice::uom_to_string(sensor.uom());
@@ -758,16 +743,18 @@ void AnalogSensor::publish_values(const bool force) {
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
#if CONFIG_IDF_TARGET_ESP32
if (sensor.type() == AnalogType::PULSE || (sensor.type() == AnalogType::DIGITAL_OUT && sensor.gpio() != 25 && sensor.gpio() != 26)) {
#elif CONFIG_IDF_TARGET_ESP32S2
if (sensor.type() == AnalogType::PULSE || (sensor.type() == AnalogType::DIGITAL_OUT && sensor.gpio() != 17 && sensor.gpio() != 18)) {
#else
if (sensor.type() == AnalogType::PULSE || sensor.type() == AnalogType::DIGITAL_OUT) {
#endif
snprintf(topic, sizeof(topic), "switch/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name());
config["cmd_t"] = command_topic;
Mqtt::add_ha_bool(config.as<JsonObject>());
} else if (sensor.type() == AnalogType::DIGITAL_OUT) { // DAC
snprintf(topic, sizeof(topic), "number/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name());
config["cmd_t"] = command_topic;
config["min"] = 0;
config["max"] = 255;
@@ -775,7 +762,7 @@ void AnalogSensor::publish_values(const bool force) {
config["step"] = 1;
} else if (sensor.type() >= AnalogType::PWM_0 && sensor.type() <= AnalogType::PWM_2) {
snprintf(topic, sizeof(topic), "number/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name());
config["cmd_t"] = command_topic;
config["min"] = 0;
config["max"] = 100;
@@ -783,7 +770,7 @@ void AnalogSensor::publish_values(const bool force) {
config["step"] = 0.1;
} else if (sensor.type() == AnalogType::RGB) {
snprintf(topic, sizeof(topic), "number/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name());
config["cmd_t"] = command_topic;
config["min"] = 0;
config["max"] = 999999;
@@ -791,7 +778,7 @@ void AnalogSensor::publish_values(const bool force) {
config["step"] = 1;
} else if (sensor.type() == AnalogType::COUNTER || (sensor.type() >= AnalogType::CNT_0 && sensor.type() <= AnalogType::CNT_2)) {
snprintf(topic, sizeof(topic), "sensor/%s/%s_%02d/config", Mqtt::basename().c_str(), F_(analogsensor), sensor.gpio());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name().c_str());
snprintf(command_topic, sizeof(command_topic), "%s/%s/%s", Mqtt::base().c_str(), F_(analogsensor), sensor.name());
config["cmd_t"] = command_topic;
config["stat_cla"] = "total_increasing";
// config["mode"] = "box"; // auto, slider or box
@@ -901,6 +888,8 @@ void AnalogSensor::get_value_json(JsonObject output, const Sensor & sensor) {
output["min"] = 0;
#if CONFIG_IDF_TARGET_ESP32
output["max"] = sensor.gpio() == 25 || sensor.gpio() == 26 ? 255 : 1;
#elif CONFIG_IDF_TARGET_ESP32S2
output["max"] = sensor.gpio() == 17 || sensor.gpio() == 18 ? 255 : 1;
#else
output["max"] = 1;
#endif
@@ -912,20 +901,14 @@ void AnalogSensor::get_value_json(JsonObject output, const Sensor & sensor) {
}
// this creates the sensor, initializing everything
AnalogSensor::Sensor::Sensor(const uint8_t gpio,
const std::string & name,
const double offset,
const double factor,
const uint8_t uom,
const int8_t type,
const bool is_system)
AnalogSensor::Sensor::Sensor(const uint8_t gpio, const char * name, const double offset, const double factor, const uint8_t uom, const int8_t type, const bool is_system)
: gpio_(gpio)
, name_(name)
, offset_(offset)
, factor_(factor)
, uom_(uom)
, type_(type)
, is_system_(is_system) {
strlcpy(name_, name, sizeof(name_));
value_ = 0; // init value to 0 always
}
@@ -952,8 +935,8 @@ bool AnalogSensor::command_setvalue(const char * value, const int8_t gpio) {
sensor.set_value(val);
}
sensor.set_offset(sensor.value());
if (sensor.value() != EMSESP::nvs_.getDouble(sensor.name().c_str(), 0)) {
EMSESP::nvs_.putDouble(sensor.name().c_str(), sensor.value());
if (sensor.value() != EMSESP::nvs_.getDouble(sensor.name(), 0)) {
EMSESP::nvs_.putDouble(sensor.name(), sensor.value());
}
} else if (sensor.type() == AnalogType::ADC) {
sensor.set_offset(val);
@@ -998,8 +981,8 @@ bool AnalogSensor::command_setvalue(const char * value, const int8_t gpio) {
sensor.set_value(v);
pinMode(sensor.gpio(), OUTPUT);
digitalWrite(sensor.gpio(), (sensor.offset() == 0) ^ (sensor.factor() > 0));
if (sensor.uom() == 0 && EMSESP::nvs_.getChar(sensor.name().c_str()) != (int8_t)sensor.offset()) {
EMSESP::nvs_.putChar(sensor.name().c_str(), (int8_t)sensor.offset());
if (sensor.uom() == 0 && EMSESP::nvs_.getChar(sensor.name()) != (int8_t)sensor.offset()) {
EMSESP::nvs_.putChar(sensor.name(), (int8_t)sensor.offset());
}
}
} else if (sensor.type() >= AnalogType::PWM_0 && sensor.type() <= AnalogType::PWM_2) {

View File

@@ -29,25 +29,23 @@
namespace emsesp {
// names, same order as AnalogType, see list_sensortype in local_common.h
// MAKE_ENUM_FIXED(AnalogTypeName, "disabled", "dig_in", "counter", "adc", "timer", "rate", "dig_out", "pwm0", "pwm1", "pwm2")
class AnalogSensor {
public:
class Sensor {
public:
Sensor(const uint8_t gpio, const std::string & name, const double offset, const double factor, const uint8_t uom, const int8_t type, const bool is_system);
Sensor(const uint8_t gpio, const char * name, const double offset, const double factor, const uint8_t uom, const int8_t type, const bool is_system);
~Sensor() = default;
void set_offset(const double offset) {
offset_ = offset;
}
std::string name() const {
const char * name() const {
return name_;
}
void set_name(const std::string & name) {
name_ = name;
void set_name(const char * name) {
strlcpy(name_, name, sizeof(name_));
}
uint8_t gpio() const {
@@ -107,7 +105,7 @@ class AnalogSensor {
private:
uint8_t gpio_;
std::string name_;
char name_[20];
double offset_;
double factor_;
uint8_t uom_;
@@ -176,7 +174,7 @@ class AnalogSensor {
return sensors_.size();
}
bool update(uint8_t gpio, std::string & name, double offset, double factor, uint8_t uom, int8_t type, bool deleted, bool is_system);
bool update(uint8_t gpio, const char * name, double offset, double factor, uint8_t uom, int8_t type, bool deleted, bool is_system);
bool get_value_info(JsonObject output, const char * cmd, const int8_t id = -1);
void store_counters();
static std::vector<uint8_t> exclude_types() {

View File

@@ -24,7 +24,7 @@ namespace emsesp {
uuid::log::Logger Command::logger_{F_(command), uuid::log::Facility::DAEMON};
std::vector<Command::CmdFunction> Command::cmdfunctions_;
std::vector<Command::CmdFunction, AllocatorPSRAM<Command::CmdFunction>> Command::cmdfunctions_;
// takes a URI path and a json body, parses the data and calls the command
// the path is leading so if duplicate keys are in the input JSON it will be ignored

View File

@@ -22,6 +22,7 @@
#include <unordered_map>
#include "console.h"
#include <esp32-psram.h>
using uuid::console::Shell;
@@ -97,7 +98,7 @@ class Command {
}
};
static std::vector<CmdFunction> commands() {
static std::vector<CmdFunction, AllocatorPSRAM<CmdFunction>> commands() {
return cmdfunctions_;
}
@@ -145,7 +146,7 @@ class Command {
private:
static uuid::log::Logger logger_;
static std::vector<CmdFunction> cmdfunctions_; // the list of commands
static std::vector<CmdFunction, AllocatorPSRAM<CmdFunction>> cmdfunctions_; // the list of commands
static uint8_t json_message(uint8_t error_code, const char * message, JsonObject output, const char * object = nullptr);
};

View File

@@ -1637,14 +1637,7 @@ void EMSdevice::get_value_json(JsonObject json, DeviceValue & dv) {
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);
}
Mqtt::add_value_bool(json, value, value_b);
}
json[type] = ("boolean");
break;
@@ -1950,13 +1943,8 @@ bool EMSdevice::generate_values(JsonObject output, const int8_t tag_filter, cons
if (output_target == OUTPUT_TARGET::CONSOLE) {
char s[12];
json[name] = Helpers::render_boolean(s, value_b, true); // console use web settings
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
json[name] = value_b;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
json[name] = value_b ? 1 : 0;
} else {
char s[12];
json[name] = Helpers::render_boolean(s, value_b);
Mqtt::add_value_bool(json, name, value_b);
}
}
@@ -2003,12 +1991,12 @@ bool EMSdevice::generate_values(JsonObject output, const int8_t tag_filter, cons
char time_s[60];
snprintf(time_s,
sizeof(time_s),
"%lu %s %lu %s %lu %s",
(time_value / 1440),
"%d %s %d %s %d %s",
(int)(time_value / 1440),
Helpers::translated_word(FL_(days)),
((time_value % 1440) / 60),
(int)((time_value % 1440) / 60),
Helpers::translated_word(FL_(hours)),
(time_value % 60),
(int)(time_value % 60),
Helpers::translated_word(FL_(minutes)));
json[name] = time_s;
} else {

View File

@@ -518,19 +518,19 @@ void EMSESP::show_sensor_values(uuid::console::Shell & shell) {
for (const auto & sensor : temperaturesensor_.sensors()) {
if (Helpers::hasValue(sensor.temperature_c)) {
shell.printfln(" %s: %s%s °%c%s (Offset: %s, ID: %s, System: %s)",
sensor.name().c_str(),
sensor.name(),
COLOR_BRIGHT_GREEN,
Helpers::render_value(s, sensor.temperature_c, 10, fahrenheit),
(fahrenheit == 0) ? 'C' : 'F',
COLOR_RESET,
Helpers::render_value(s2, sensor.offset(), 10, fahrenheit),
sensor.id().c_str(),
sensor.id(),
sensor.is_system() ? "Yes" : "No");
} else {
shell.printfln(" %s (Offset: %s, ID: %s, System: %s)",
sensor.name().c_str(),
sensor.name(),
Helpers::render_value(s, sensor.offset(), 10, fahrenheit),
sensor.id().c_str(),
sensor.id(),
sensor.is_system() ? "Yes" : "No");
}
}
@@ -546,7 +546,7 @@ void EMSESP::show_sensor_values(uuid::console::Shell & shell) {
switch (sensor.type()) {
case AnalogSensor::AnalogType::ADC:
shell.printfln(" %s: %s%s %s%s (Type: ADC, Factor: %s, Offset: %s, System: %s)",
sensor.name().c_str(),
sensor.name(),
COLOR_BRIGHT_GREEN,
Helpers::render_value(s, sensor.value(), 2),
EMSdevice::uom_to_string(sensor.uom()),
@@ -559,7 +559,7 @@ void EMSESP::show_sensor_values(uuid::console::Shell & shell) {
// case AnalogSensor::AnalogType::DIGITAL_IN:
// case AnalogSensor::AnalogType::COUNTER:
shell.printfln(" %s: %s%d%s (Type: %s)",
sensor.name().c_str(),
sensor.name(),
COLOR_BRIGHT_GREEN,
(uint16_t)sensor.value(), // as int
COLOR_RESET,

View File

@@ -470,26 +470,25 @@ char * Helpers::utf8tolatin1(char * result, const char * c, const uint8_t len) {
*p = '\0'; // terminate result
return result;
}
// creates string of hex values from an array of bytes
std::string Helpers::data_to_hex(const uint8_t * data, const uint8_t length) {
if (length == 0) {
return "<empty>";
}
char str[length * 3];
memset(str, 0, sizeof(str));
std::string str;
str.reserve(length * 3 + 1);
char buffer[4];
char * p = &str[0];
for (uint8_t i = 0; i < length; i++) {
Helpers::hextoa(buffer, data[i]);
*p++ = buffer[0];
*p++ = buffer[1];
*p++ = ' '; // space
str.append(Helpers::hextoa(buffer, data[i]));
str.push_back(' ');
}
*--p = '\0'; // null terminate just in case, loosing the trailing space
return std::string(str);
if (!str.empty()) {
str.pop_back();
}
return str;
}
// takes a hex string and convert it to an unsigned 32bit number (max 8 hex digits)

View File

@@ -47,7 +47,7 @@ bool Mqtt::send_response_;
bool Mqtt::publish_single_;
bool Mqtt::publish_single2cmd_;
std::vector<Mqtt::MQTTSubFunction> Mqtt::mqtt_subfunctions_;
std::vector<Mqtt::MQTTSubFunction, AllocatorPSRAM<Mqtt::MQTTSubFunction>> Mqtt::mqtt_subfunctions_;
uint32_t Mqtt::mqtt_publish_fails_ = 0;
bool Mqtt::connecting_ = false;
@@ -119,7 +119,7 @@ void Mqtt::resubscribe() {
}
for (const auto & mqtt_subfunction : mqtt_subfunctions_) {
queue_subscribe_message(mqtt_subfunction.topic_);
queue_subscribe_message(mqtt_subfunction.topic_.c_str());
}
}
@@ -1511,4 +1511,16 @@ void Mqtt::add_ha_bool(JsonObject doc) {
}
}
// adds the bool depending on bool setting
void Mqtt::add_value_bool(JsonObject doc, const char * name, bool value) {
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
doc[name] = value;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
doc[name] = value ? 1 : 0;
} else {
char result[12];
doc[name] = Helpers::render_boolean(result, value);
}
}
} // namespace emsesp

View File

@@ -26,6 +26,7 @@
#include "console.h"
#include "command.h"
#include "emsdevicevalue.h"
#include <esp32-psram.h>
using uuid::console::Shell;
@@ -262,6 +263,7 @@ class Mqtt {
const char * cond2 = nullptr,
const char * negcond = nullptr);
static void add_ha_bool(JsonObject doc);
static void add_value_bool(JsonObject doc, const char * name, bool value);
private:
static uuid::log::Logger logger_;
@@ -279,18 +281,18 @@ class Mqtt {
// function handlers for MQTT subscriptions
struct MQTTSubFunction {
uint8_t device_type_; // which device type, from DeviceType::
const std::string topic_; // short topic name
const stringPSRAM topic_; // short topic name
mqtt_sub_function_p mqtt_subfunction_; // can be empty
// replaced &&topic with &topic in 3.7.0-dev.43, so we prevent the std:move later
MQTTSubFunction(uint8_t device_type, const std::string & topic, mqtt_sub_function_p mqtt_subfunction)
: device_type_(device_type)
, topic_(topic)
, topic_(topic.c_str())
, mqtt_subfunction_(mqtt_subfunction) {
}
};
static std::vector<MQTTSubFunction> mqtt_subfunctions_; // list of mqtt subscribe callbacks for all devices
static std::vector<MQTTSubFunction, AllocatorPSRAM<MQTTSubFunction>> mqtt_subfunctions_; // list of mqtt subscribe callbacks for all devices
uint32_t last_publish_boiler_ = 0;
uint32_t last_publish_thermostat_ = 0;

View File

@@ -94,13 +94,8 @@ PButton System::myPButton_;
bool System::test_set_all_active_ = false;
uint32_t System::max_alloc_mem_;
uint32_t System::heap_mem_;
#ifndef EMSESP_STANDALONE
std::vector<uint8_t, AllocatorPSRAM<uint8_t>> System::valid_system_gpios_;
std::vector<uint8_t, AllocatorPSRAM<uint8_t>> System::used_gpios_;
#else
std::vector<uint8_t> System::valid_system_gpios_;
std::vector<uint8_t> System::used_gpios_;
#endif
// find the index of the language
// 0 = EN, 1 = DE, etc...
@@ -2279,13 +2274,8 @@ uint8_t System::systemStatus() {
}
// takes a string range like "6-11, 1, 23, 24-48" which has optional ranges and single values and converts to a vector of ints
#ifndef EMSESP_STANDALONE
std::vector<uint8_t, AllocatorPSRAM<uint8_t>> System::string_range_to_vector(const std::string & range) {
std::vector<uint8_t, AllocatorPSRAM<uint8_t>> gpios;
#else
std::vector<uint8_t> System::string_range_to_vector(const std::string & range) {
std::vector<uint8_t> gpios;
#endif
std::string::size_type pos = 0;
std::string::size_type prev = 0;

View File

@@ -108,7 +108,7 @@ void TemperatureSensor::loop() {
last_activity_ = time_now;
}
} else if (state_ == State::READING) {
if (temperature_convert_complete() && (time_now - last_activity_ > CONVERSION_MS)) {
if (temperature_convert_complete(time_now - last_activity_)) {
#ifdef EMSESP_DEBUG_SENSOR
LOG_DEBUG("Scanning for temperature sensors");
#endif
@@ -181,12 +181,12 @@ void TemperatureSensor::loop() {
default:
sensorfails_++;
LOG_ERROR("Unknown sensor %s", Sensor(addr).id().c_str());
LOG_ERROR("Unknown sensor %s", Sensor(addr).id());
break;
}
} else {
sensorfails_++;
LOG_ERROR("Invalid sensor %s", Sensor(addr).id().c_str());
LOG_ERROR("Invalid sensor %s", Sensor(addr).id());
}
} else {
if (!parasite_) {
@@ -219,13 +219,13 @@ void TemperatureSensor::loop() {
s->set_name("gateway_temperature");
s->set_is_system(true); // mark as internal system temperature sensor
if (!EMSESP::nvs_.isKey("intTemp")) {
EMSESP::nvs_.putString("intTemp", s->id().c_str());
EMSESP::nvs_.putString("intTemp", s->id());
}
// LOG_NOTICE("Adding system sensor for gateway temperature %s", s->id().c_str());
EMSESP::webCustomizationService.update([&](WebCustomization & settings) {
auto newSensor = SensorCustomization();
newSensor.id = s->id();
newSensor.name = s->name();
strlcpy(newSensor.id, s->id(), sizeof(newSensor.id));
strlcpy(newSensor.name, s->name(), sizeof(newSensor.name));
newSensor.offset = 0;
newSensor.is_system = s->is_system(); // always true
settings.sensorCustomizations.push_back(newSensor);
@@ -244,21 +244,21 @@ void TemperatureSensor::loop() {
#endif
}
bool TemperatureSensor::temperature_convert_complete() {
bool TemperatureSensor::temperature_convert_complete(const uint32_t time) {
#ifndef EMSESP_STANDALONE
if (parasite_) {
return true; // don't care, use the minimum time in loop
return time > CONVERSION_MS; // don't care, use the datasheet time
}
return bus_.read_bit() == 1;
#else
return true;
return time > CONVERSION_MS;
#endif
}
int16_t TemperatureSensor::get_temperature_c(const uint8_t addr[]) {
#ifndef EMSESP_STANDALONE
if (!bus_.reset()) {
LOG_ERROR("Bus reset failed before reading scratchpad from %s", Sensor(addr).id().c_str());
LOG_ERROR("Bus reset failed before reading scratchpad from %s", Sensor(addr).id());
return EMS_VALUE_INT16_NOTSET;
}
YIELD;
@@ -270,7 +270,7 @@ int16_t TemperatureSensor::get_temperature_c(const uint8_t addr[]) {
YIELD;
if (!bus_.reset()) {
LOG_ERROR("Bus reset failed after reading scratchpad from %s", Sensor(addr).id().c_str());
LOG_ERROR("Bus reset failed after reading scratchpad from %s", Sensor(addr).id());
return EMS_VALUE_INT16_NOTSET;
}
YIELD;
@@ -286,7 +286,7 @@ int16_t TemperatureSensor::get_temperature_c(const uint8_t addr[]) {
scratchpad[6],
scratchpad[7],
scratchpad[8],
Sensor(addr).id().c_str());
Sensor(addr).id());
return EMS_VALUE_INT16_NOTSET;
}
@@ -319,10 +319,10 @@ int16_t TemperatureSensor::get_temperature_c(const uint8_t addr[]) {
}
// update temperature sensor information name and offset
bool TemperatureSensor::update(const std::string & id, const std::string & name, int16_t offset, bool is_system) {
bool TemperatureSensor::update(const char * id, const char * name, int16_t offset, bool hide, bool is_system) {
// find the sensor
for (auto & sensor : sensors_) {
if (sensor.id() == id) {
if (!strcmp(id, sensor.id())) {
// found a match, update the sensor object
// if HA is enabled then delete the old record
@@ -336,27 +336,27 @@ bool TemperatureSensor::update(const std::string & id, const std::string & name,
sensor.set_is_system(is_system);
// store the new name and offset in our configuration
EMSESP::webCustomizationService.update([&id, &name, &offset, &is_system, &sensor](WebCustomization & settings) {
EMSESP::webCustomizationService.update([&id, &name, &offset, &sensor, &hide, &is_system](WebCustomization & settings) {
// look it up to see if it exists
bool found = false;
for (auto & SensorCustomization : settings.sensorCustomizations) {
if (SensorCustomization.id == id) {
SensorCustomization.name = name;
if (!strcmp(id, SensorCustomization.id)) {
strlcpy(SensorCustomization.name, name, sizeof(SensorCustomization.name));
SensorCustomization.offset = offset;
SensorCustomization.is_system = is_system;
found = true;
LOG_DEBUG("Customizing existing sensor ID %s", id.c_str());
LOG_DEBUG("Customizing existing sensor ID %s", id);
break;
}
}
if (!found) {
auto newSensor = SensorCustomization();
newSensor.id = id;
newSensor.name = name;
strlcpy(newSensor.id, id, sizeof(newSensor.id));
strlcpy(newSensor.name, name, sizeof(newSensor.name));
newSensor.offset = offset;
newSensor.is_system = is_system; // is user defined, not system
settings.sensorCustomizations.push_back(newSensor);
LOG_DEBUG("Adding new customization for sensor ID %s", id.c_str());
LOG_DEBUG("Adding new customization for sensor ID %s", id);
}
sensor.ha_registered = false; // it's changed so we may need to recreate the HA config
return StateUpdateResult::CHANGED;
@@ -438,30 +438,27 @@ void TemperatureSensor::publish_sensor(const Sensor & sensor) {
if (Mqtt::enabled() && Mqtt::publish_single()) {
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
if (Mqtt::publish_single2cmd()) {
snprintf(topic, sizeof(topic), "%s/%s", F_(temperaturesensor), sensor.name().c_str());
snprintf(topic, sizeof(topic), "%s/%s", F_(temperaturesensor), sensor.name());
} else {
snprintf(topic, sizeof(topic), "%s%s/%s", F_(temperaturesensor), "_data", sensor.name().c_str());
snprintf(topic, sizeof(topic), "%s%s/%s", F_(temperaturesensor), "_data", sensor.name());
}
char payload[10];
Mqtt::queue_publish(topic, Helpers::render_value(payload, sensor.temperature_c, 10, EMSESP::system_.fahrenheit() ? 2 : 0));
}
char cmd[COMMAND_MAX_LENGTH];
snprintf(cmd, sizeof(cmd), "%s/%s", F_(temperaturesensor), sensor.name().c_str());
snprintf(cmd, sizeof(cmd), "%s/%s", F_(temperaturesensor), sensor.name());
EMSESP::webSchedulerService.onChange(cmd);
}
// send empty config topic to remove the entry from HA
void TemperatureSensor::remove_ha_topic(const std::string & id) {
void TemperatureSensor::remove_ha_topic(const char * id) {
if (!Mqtt::ha_enabled()) {
return;
}
LOG_DEBUG("Removing HA config for temperature sensor ID %s", id.c_str());
// use '_' as HA doesn't like '-' in the topic name
std::string sensorid = id;
std::replace(sensorid.begin(), sensorid.end(), '-', '_');
LOG_DEBUG("Removing HA config for temperature sensor ID %s", id);
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
snprintf(topic, sizeof(topic), "sensor/%s/temperaturesensor_%s/config", Mqtt::basename().c_str(), sensorid.c_str());
snprintf(topic, sizeof(topic), "sensor/%s/temperaturesensor_%s/config", Mqtt::basename().c_str(), id);
Mqtt::queue_remove_topic(topic);
}
@@ -504,7 +501,7 @@ void TemperatureSensor::publish_values(const bool force) {
remove_ha_topic(sensor.id());
sensor.ha_registered = false;
} else if (!sensor.ha_registered || force) {
LOG_DEBUG("Recreating HA config for sensor ID %s", sensor.id().c_str());
LOG_DEBUG("Recreating HA config for sensor ID %s", sensor.id());
JsonDocument config;
config["~"] = Mqtt::base();
@@ -520,10 +517,10 @@ void TemperatureSensor::publish_values(const bool force) {
char val_obj[70];
char val_cond[170];
if (Mqtt::is_nested()) {
snprintf(val_obj, sizeof(val_obj), "value_json['%s']['temp']", sensor.id().c_str());
snprintf(val_cond, sizeof(val_cond), "value_json['%s'] is defined and %s is defined", sensor.id().c_str(), val_obj);
snprintf(val_obj, sizeof(val_obj), "value_json['%s']['temp']", sensor.id());
snprintf(val_cond, sizeof(val_cond), "value_json['%s'] is defined and %s is defined", sensor.id(), val_obj);
} else {
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", sensor.name().c_str());
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", sensor.name());
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
}
@@ -537,17 +534,14 @@ void TemperatureSensor::publish_values(const bool force) {
char uniq_s[70];
if (Mqtt::entity_format() == Mqtt::entityFormat::MULTI_SHORT) {
snprintf(uniq_s, sizeof(uniq_s), "%s_%s_%s", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id().c_str());
snprintf(uniq_s, sizeof(uniq_s), "%s_%s_%s", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id());
} else {
snprintf(uniq_s, sizeof(uniq_s), "%s_%s", F_(temperaturesensor), sensor.id().c_str());
snprintf(uniq_s, sizeof(uniq_s), "%s_%s", F_(temperaturesensor), sensor.id());
}
config["uniq_id"] = uniq_s;
config["def_ent_id"] = (std::string) "sensor." + uniq_s;
char name[50];
snprintf(name, sizeof(name), "%s", sensor.name().c_str());
config["name"] = name;
config["name"] = sensor.name();
// see if we need to create the [devs] discovery section, as this needs only to be done once for all sensors
bool is_ha_device_created = false;
@@ -562,7 +556,7 @@ void TemperatureSensor::publish_values(const bool force) {
Mqtt::add_ha_avail_section(config.as<JsonObject>(), stat_t, !is_ha_device_created, val_cond);
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id().c_str());
snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id());
sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>());
}
@@ -579,16 +573,14 @@ TemperatureSensor::Sensor::Sensor(const uint8_t addr[])
: internal_id_(((uint64_t)addr[0] << 48) | ((uint64_t)addr[1] << 40) | ((uint64_t)addr[2] << 32) | ((uint64_t)addr[3] << 24) | ((uint64_t)addr[4] << 16)
| ((uint64_t)addr[5] << 8) | ((uint64_t)addr[6])) {
// create ID string
char id_s[20];
snprintf(id_s,
sizeof(id_s),
snprintf(id_,
sizeof(id_),
"%02X_%04X_%04X_%04X",
(unsigned int)(internal_id_ >> 48) & 0xFF,
(unsigned int)(internal_id_ >> 32) & 0xFFFF,
(unsigned int)(internal_id_ >> 16) & 0xFFFF,
(unsigned int)(internal_id_) & 0xFFFF);
id_ = std::string(id_s);
name_ = std::string{}; // name (alias) is empty
name_[0] = '\0';
offset_ = 0; // 0 degrees offset
}
@@ -599,8 +591,8 @@ uint64_t TemperatureSensor::get_id(const uint8_t addr[]) {
// find the name from the customization service
// if empty, return the ID as a string
std::string TemperatureSensor::Sensor::name() const {
if (name_.empty()) {
const char * TemperatureSensor::Sensor::name() const {
if (name_[0] == '\0') {
return id_;
}
return name_;
@@ -613,8 +605,8 @@ bool TemperatureSensor::Sensor::apply_customization() {
auto const & sensors = settings.sensorCustomizations;
if (!sensors.empty()) {
for (const auto & sensor : sensors) {
if (id_ == sensor.id) {
LOG_DEBUG("Loading customization for temperature sensor %s", sensor.id.c_str());
if (!strcmp(sensor.id, id_)) {
LOG_DEBUG("Loading customization for temperature sensor %s", id_);
set_name(sensor.name);
set_offset(sensor.offset);
set_is_system(sensor.is_system);

View File

@@ -27,11 +27,10 @@
#include <uuid/log.h>
#include <esp32-psram.h>
#ifndef EMSESP_STANDALONE
#include <OneWire.h>
#endif
#include <esp32-psram.h>
namespace emsesp {
@@ -53,7 +52,7 @@ class TemperatureSensor {
is_system_ = is_system;
}
std::string id() const {
const char * id() const {
return id_;
}
@@ -64,9 +63,10 @@ class TemperatureSensor {
offset_ = offset;
}
std::string name() const;
void set_name(const std::string & name) {
name_ = name;
const char * name() const;
void set_name(const char * name) {
strlcpy(name_, name, sizeof(name_));
}
bool apply_customization();
@@ -78,9 +78,10 @@ class TemperatureSensor {
private:
uint64_t internal_id_;
std::string id_;
std::string name_;
char id_[18];
char name_[20];
int16_t offset_;
bool is_system_;
};
@@ -96,11 +97,7 @@ class TemperatureSensor {
bool get_value_info(JsonObject output, const char * cmd, const int8_t id = -1);
// return back reference to the sensor list, used by other classes
#ifndef EMSESP_STANDALONE
std::vector<Sensor, AllocatorPSRAM<Sensor>> sensors() const {
#else
std::vector<Sensor> sensors() const {
#endif
return sensors_;
}
@@ -127,7 +124,7 @@ class TemperatureSensor {
return sensors_.size();
}
bool update(const std::string & id, const std::string & name, int16_t offset, bool is_system);
bool update(const char* id, const char* name, int16_t offset, bool hide = false, bool is_system = false);
#if defined(EMSESP_TEST)
void load_test_data();
@@ -165,22 +162,21 @@ class TemperatureSensor {
static uuid::log::Logger logger_;
bool temperature_convert_complete();
bool temperature_convert_complete(const uint32_t time);
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 remove_ha_topic(const char* id);
std::vector<Sensor, AllocatorPSRAM<Sensor>> sensors_; // our list of active sensors
#ifndef EMSESP_STANDALONE
std::vector<Sensor, AllocatorPSRAM<Sensor>> sensors_; // our list of active sensors
OneWire bus_;
uint32_t last_activity_ = uuid::get_uptime();
State state_ = State::IDLE;
int8_t scancnt_ = SCAN_START;
uint8_t firstscan_ = 0;
int8_t scanretry_ = 0;
#else
std::vector<Sensor> sensors_; // our list of active sensors
#endif
uint8_t dallas_gpio_ = 0;

View File

@@ -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;
}
@@ -225,18 +225,15 @@ bool WebCustomEntityService::command_setvalue(const char * value, const int8_t i
// 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;
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;
@@ -419,7 +416,7 @@ void WebCustomEntityService::publish(const bool force) {
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);

View File

@@ -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;

View File

@@ -54,8 +54,8 @@ 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["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
}
@@ -65,7 +65,7 @@ void WebCustomization::read(WebCustomization & customizations, JsonObject root)
for (const AnalogCustomization & sensor : customizations.analogCustomizations) {
JsonObject sensorJson = analogJson.add<JsonObject>();
sensorJson["gpio"] = sensor.gpio; // g
sensorJson["name"] = sensor.name; // n
sensorJson["name"] = (const char *)sensor.name; // n
sensorJson["offset"] = sensor.offset; // o
sensorJson["factor"] = sensor.factor; // f
sensorJson["uom"] = sensor.uom; // u
@@ -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());
}
}
}
@@ -99,14 +99,16 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c
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>();
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"];
if (sensor.id == sensor.name) {
sensor.name = ""; // no need to store id as name
}
sensor.is_system = sensorJson["is_system"] | false;
std::replace(sensor.id.begin(), sensor.id.end(), '-', '_'); // change old ids to v3.7 style
// 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();
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) {
@@ -383,22 +385,22 @@ void WebCustomizationService::load_test_data() {
webCustomization.sensorCustomizations.clear(); // delete all existing sensors
auto sensor1 = SensorCustomization();
sensor1.id = "01_0203_0405_0607";
sensor1.name = "test_tempsensor1";
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";
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";
strcpy(sensor3.id, "28_1767_7B13_2502");
strcpy(sensor3.name, "gateway_temperature");
sensor3.offset = 0;
sensor3.is_system = true;
webCustomization.sensorCustomizations.push_back(sensor3);
@@ -408,7 +410,7 @@ void WebCustomizationService::load_test_data() {
webCustomization.analogCustomizations.clear();
auto analog = AnalogCustomization();
analog.gpio = 36;
analog.name = "test_analogsensor1";
strcpy(analog.name, "test_analogsensor1");
analog.offset = 0;
analog.factor = 0.2;
analog.uom = 17;
@@ -418,7 +420,7 @@ void WebCustomizationService::load_test_data() {
analog = AnalogCustomization();
analog.gpio = 37;
analog.name = "test_analogsensor2";
strcpy(analog.name, "test_analogsensor2");
analog.offset = 0;
analog.factor = 1;
analog.uom = 0;
@@ -428,7 +430,7 @@ void WebCustomizationService::load_test_data() {
analog = AnalogCustomization();
analog.gpio = 38;
analog.name = "test_analogsensor3";
strcpy(analog.name, "test_analogsensor3");
analog.offset = 0;
analog.factor = 1;
analog.uom = 0;
@@ -438,7 +440,7 @@ void WebCustomizationService::load_test_data() {
analog = AnalogCustomization();
analog.gpio = 33;
analog.name = "test_analogsensor4";
strcpy(analog.name, "test_analogsensor4");
analog.offset = 0;
analog.factor = 1;
analog.uom = 0;
@@ -448,7 +450,7 @@ void WebCustomizationService::load_test_data() {
analog = AnalogCustomization();
analog.gpio = 39;
analog.name = "test_analogsensor5"; // core_voltage
strcpy(analog.name, "test_analogsensor5"); // core_voltage
analog.offset = 0;
analog.factor = 0.003771;
analog.uom = 23;

View File

@@ -35,8 +35,8 @@ namespace emsesp {
// Customization for temperature sensor
class SensorCustomization {
public:
std::string id;
std::string name;
char id[18];
char name[20];
uint16_t offset;
bool is_system; // if true, the customization is a system customization
};
@@ -44,7 +44,7 @@ class SensorCustomization {
class AnalogCustomization {
public:
uint8_t gpio;
std::string name;
char name[20];
double offset;
double factor;
uint8_t uom; // 0 is none

View File

@@ -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);
}
@@ -347,13 +347,13 @@ void WebDataService::write_analog_sensor(AsyncWebServerRequest * request, JsonVa
JsonObject analog = json;
uint8_t gpio = analog["gpio"];
std::string name = analog["name"];
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"];
bool is_system = analog["is_system"] | false;
ok = EMSESP::analogsensor_.update(gpio, name, offset, factor, uom, type, deleted, is_system);
}
@@ -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);

View File

@@ -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;

View File

@@ -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,26 +230,18 @@ 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();
@@ -273,7 +251,7 @@ void WebSchedulerService::publish(const bool force) {
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;
@@ -565,7 +541,7 @@ void WebSchedulerService::load_test_data() {
si.time = "12:00";
si.cmd = "system/fetch";
si.value = "10";
si.name = "test_scheduler";
strcpy(si.name, "test_scheduler");
si.elapsed_min = 0;
si.retry_cnt = 0xFF; // no startup retries
@@ -578,7 +554,7 @@ void WebSchedulerService::load_test_data() {
si.time = "13:00";
si.cmd = "system/message";
si.value = "20";
si.name = ""; // to make sure its excluded from Dashboard
strcpy(si.name, ""); // to make sure its excluded from Dashboard
si.elapsed_min = 0;
si.retry_cnt = 0xFF; // no startup retries

View File

@@ -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);
};