mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-26 16:49:11 +03:00
more to psram, names for sensors, schedule, custom as char[20]
This commit is contained in:
@@ -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;
|
||||
@@ -564,9 +564,9 @@ bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, doubl
|
||||
if (!found_sensor) {
|
||||
found_sensor = true;
|
||||
EMSESP::webCustomizationService.update([&](WebCustomization & settings) {
|
||||
auto newSensor = AnalogCustomization();
|
||||
newSensor.gpio = gpio;
|
||||
newSensor.name = name;
|
||||
auto newSensor = AnalogCustomization();
|
||||
newSensor.gpio = gpio;
|
||||
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) {
|
||||
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) {
|
||||
|
||||
@@ -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 {
|
||||
@@ -106,14 +104,14 @@ class AnalogSensor {
|
||||
uint32_t last_polltime_ = 0; // for timer
|
||||
|
||||
private:
|
||||
uint8_t gpio_;
|
||||
std::string name_;
|
||||
double offset_;
|
||||
double factor_;
|
||||
uint8_t uom_;
|
||||
double value_; // double because of the factor is a double
|
||||
int8_t type_; // one of the AnalogType enum
|
||||
bool is_system_; // if true, the sensor is a system sensor
|
||||
uint8_t gpio_;
|
||||
char name_[20];
|
||||
double offset_;
|
||||
double factor_;
|
||||
uint8_t uom_;
|
||||
double value_; // double because of the factor is a double
|
||||
int8_t type_; // one of the AnalogType enum
|
||||
bool is_system_; // if true, the sensor is a system sensor
|
||||
};
|
||||
|
||||
AnalogSensor() = default;
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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];
|
||||
char buffer[4];
|
||||
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)
|
||||
|
||||
@@ -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
|
||||
@@ -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;
|
||||
|
||||
@@ -90,17 +90,12 @@ uuid::syslog::SyslogService System::syslog_;
|
||||
uuid::log::Logger System::logger_{F_(system), uuid::log::Facility::KERN};
|
||||
|
||||
// init statics
|
||||
PButton System::myPButton_;
|
||||
bool System::test_set_all_active_ = false;
|
||||
uint32_t System::max_alloc_mem_;
|
||||
uint32_t System::heap_mem_;
|
||||
#ifndef EMSESP_STANDALONE
|
||||
PButton System::myPButton_;
|
||||
bool System::test_set_all_active_ = false;
|
||||
uint32_t System::max_alloc_mem_;
|
||||
uint32_t System::heap_mem_;
|
||||
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,15 +2274,10 @@ 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;
|
||||
std::string::size_type pos = 0;
|
||||
std::string::size_type prev = 0;
|
||||
|
||||
auto process_part = [&gpios](std::string part) {
|
||||
// trim whitespace
|
||||
|
||||
@@ -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();
|
||||
auto newSensor = SensorCustomization();
|
||||
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;
|
||||
auto newSensor = SensorCustomization();
|
||||
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,17 +573,15 @@ 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
|
||||
offset_ = 0; // 0 degrees offset
|
||||
name_[0] = '\0';
|
||||
offset_ = 0; // 0 degrees offset
|
||||
}
|
||||
|
||||
uint64_t TemperatureSensor::get_id(const uint8_t addr[]) {
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
@@ -77,11 +77,12 @@ class TemperatureSensor {
|
||||
bool ha_registered = false;
|
||||
|
||||
private:
|
||||
uint64_t internal_id_;
|
||||
std::string id_;
|
||||
std::string name_;
|
||||
int16_t offset_;
|
||||
bool is_system_;
|
||||
uint64_t internal_id_;
|
||||
char id_[18];
|
||||
char name_[20];
|
||||
int16_t offset_;
|
||||
|
||||
bool is_system_;
|
||||
};
|
||||
|
||||
TemperatureSensor() = default;
|
||||
@@ -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
|
||||
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;
|
||||
#endif
|
||||
|
||||
uint8_t dallas_gpio_ = 0;
|
||||
|
||||
Reference in New Issue
Block a user