mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
store relais states in nvs
This commit is contained in:
@@ -24,7 +24,7 @@ namespace emsesp {
|
|||||||
uuid::log::Logger AnalogSensor::logger_{F_(analogsensor), uuid::log::Facility::DAEMON};
|
uuid::log::Logger AnalogSensor::logger_{F_(analogsensor), uuid::log::Facility::DAEMON};
|
||||||
|
|
||||||
void AnalogSensor::start() {
|
void AnalogSensor::start() {
|
||||||
reload(); // fetch the list of sensors from our customization service
|
reload(true); // fetch the list of sensors from our customization service
|
||||||
|
|
||||||
if (!analog_enabled_) {
|
if (!analog_enabled_) {
|
||||||
return;
|
return;
|
||||||
@@ -47,7 +47,7 @@ void AnalogSensor::start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load settings from the customization file, sorts them and initializes the GPIOs
|
// load settings from the customization file, sorts them and initializes the GPIOs
|
||||||
void AnalogSensor::reload() {
|
void AnalogSensor::reload(bool get_nvs) {
|
||||||
EMSESP::webSettingsService.read([&](WebSettings & settings) { analog_enabled_ = settings.analog_enabled; });
|
EMSESP::webSettingsService.read([&](WebSettings & settings) { analog_enabled_ = settings.analog_enabled; });
|
||||||
|
|
||||||
#if defined(EMSESP_STANDALONE)
|
#if defined(EMSESP_STANDALONE)
|
||||||
@@ -195,6 +195,13 @@ void AnalogSensor::reload() {
|
|||||||
} else
|
} else
|
||||||
#endif
|
#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());
|
||||||
|
} else {
|
||||||
|
sensor.set_offset(EMSESP::nvs_.getChar(sensor.name().c_str()));
|
||||||
|
}
|
||||||
|
}
|
||||||
digitalWrite(sensor.gpio(), sensor.offset() * sensor.factor() > 0 ? 1 : 0);
|
digitalWrite(sensor.gpio(), sensor.offset() * sensor.factor() > 0 ? 1 : 0);
|
||||||
sensor.set_value(sensor.offset());
|
sensor.set_value(sensor.offset());
|
||||||
}
|
}
|
||||||
@@ -724,6 +731,8 @@ void AnalogSensor::addSensorJson(JsonObject output, const Sensor & sensor) {
|
|||||||
} else if (sensor.type() == AnalogType::DIGITAL_OUT) {
|
} else if (sensor.type() == AnalogType::DIGITAL_OUT) {
|
||||||
output["min"] = 0;
|
output["min"] = 0;
|
||||||
output["max"] = sensor.gpio() == 25 || sensor.gpio() == 26 ? 255 : 1;
|
output["max"] = sensor.gpio() == 25 || sensor.gpio() == 26 ? 255 : 1;
|
||||||
|
char state[][2] = {"?", "0", "1"};
|
||||||
|
output["start"] = state[sensor.uom()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -796,6 +805,9 @@ bool AnalogSensor::command_setvalue(const char * value, const int8_t gpio) {
|
|||||||
sensor.set_value(v);
|
sensor.set_value(v);
|
||||||
pinMode(sensor.gpio(), OUTPUT);
|
pinMode(sensor.gpio(), OUTPUT);
|
||||||
digitalWrite(sensor.gpio(), sensor.offset() * sensor.factor() > 0 ? 1 : 0);
|
digitalWrite(sensor.gpio(), sensor.offset() * sensor.factor() > 0 ? 1 : 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (sensor.type() >= AnalogType::PWM_0 && sensor.type() <= AnalogType::PWM_2) {
|
} else if (sensor.type() >= AnalogType::PWM_0 && sensor.type() <= AnalogType::PWM_2) {
|
||||||
if (val > 100) {
|
if (val > 100) {
|
||||||
@@ -815,18 +827,6 @@ bool AnalogSensor::command_setvalue(const char * value, const int8_t gpio) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (oldoffset != sensor.offset()) {
|
if (oldoffset != sensor.offset()) {
|
||||||
EMSESP::webCustomizationService.update([&](WebCustomization & settings) {
|
|
||||||
for (auto & AnalogCustomization : settings.analogCustomizations) {
|
|
||||||
if (AnalogCustomization.gpio == sensor.gpio() && AnalogCustomization.type == sensor.type()) {
|
|
||||||
AnalogCustomization.offset = sensor.offset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (sensor.type() == AnalogType::COUNTER || (sensor.type() == AnalogType::DIGITAL_OUT && sensor.uom() > 0)) {
|
|
||||||
return StateUpdateResult::UNCHANGED; // temporary change
|
|
||||||
} else {
|
|
||||||
return StateUpdateResult::CHANGED; // persist the change
|
|
||||||
}
|
|
||||||
});
|
|
||||||
publish_sensor(sensor);
|
publish_sensor(sensor);
|
||||||
changed_ = true;
|
changed_ = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ class AnalogSensor {
|
|||||||
void loop();
|
void loop();
|
||||||
void publish_sensor(const Sensor & sensor) const;
|
void publish_sensor(const Sensor & sensor) const;
|
||||||
void publish_values(const bool force);
|
void publish_values(const bool force);
|
||||||
void reload();
|
void reload(bool get_nvs = false);
|
||||||
bool updated_values();
|
bool updated_values();
|
||||||
|
|
||||||
// return back reference to the sensor list, used by other classes
|
// return back reference to the sensor list, used by other classes
|
||||||
|
|||||||
Reference in New Issue
Block a user