Skip to content

Commit

Permalink
Enabling / Disabling Hardware now require a restart
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Dec 26, 2024
1 parent 3263580 commit 1ee1c29
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 172 deletions.
12 changes: 8 additions & 4 deletions include/YaSolRWebsite.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace YaSolR {
void _boolConfig(dash::SwitchCard& card, const char* key) {
card.onChange([key, &card, this](bool value) {
config.setBool(key, value);
card.setValue(config.getBool(key) ? 1 : 0);
card.setValue(config.getBool(key));
dashboard.refresh(card);
dashboardInitTask.resume();
});
Expand Down Expand Up @@ -165,9 +165,13 @@ namespace YaSolR {
void _status(dash::FeedbackSwitchCard& card, const char* key, bool enabled, bool active = true, const char* err = "") {
const bool configEnabled = config.getBool(key);
card.setValue(configEnabled);
if (!configEnabled)
card.setFeedback(YASOLR_LBL_115, dash::Status::IDLE);
else if (!enabled)
if (!configEnabled) {
if (enabled) {
card.setFeedback(YASOLR_LBL_185, dash::Status::DANGER);
} else {
card.setFeedback(YASOLR_LBL_115, dash::Status::IDLE);
}
} else if (!enabled)
card.setFeedback(YASOLR_LBL_124, dash::Status::DANGER);
else if (!active)
card.setFeedback(err, dash::Status::WARNING);
Expand Down
2 changes: 1 addition & 1 deletion include/i18n/en.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
#define YASOLR_LBL_182 YASOLR_LBL_070 " " YASOLR_LBL_048 " MQTT Topic"
#define YASOLR_LBL_183 YASOLR_LBL_046 " Dimmer Min/Max Remapping"
#define YASOLR_LBL_184 YASOLR_LBL_070 " Dimmer Min/Max Remapping"
// #define YASOLR_LBL_185
#define YASOLR_LBL_185 "Restart required!"
#define YASOLR_LBL_186 "Resistance Value Detection"
// #define YASOLR_LBL_187
#define YASOLR_LBL_030 "Relay: " YASOLR_LBL_046 " Bypass Relay Switch Count"
Expand Down
2 changes: 1 addition & 1 deletion include/i18n/fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
#define YASOLR_LBL_182 "Topic MQTT pour la température sortie 2"
#define YASOLR_LBL_183 "Redéfinition Min/Max Variateur Sortie 1"
#define YASOLR_LBL_184 "Redéfinition Min/Max Variateur Sortie 2"
// #define YASOLR_LBL_185
#define YASOLR_LBL_185 "Redémarrage requis!"
#define YASOLR_LBL_186 "Détection valeur résistances"
// #define YASOLR_LBL_187
#define YASOLR_LBL_188 "Adresse IP statique"
Expand Down
85 changes: 45 additions & 40 deletions src/init/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,6 @@
Mycila::Task initConfigTask("Init Config", [](void* params) {
logger.warn(TAG, "Configuring %s", Mycila::AppInfo.nameModelVersion.c_str());

// coreTaskManager
calibrationTask.setEnabledWhen([]() { return router.isCalibrationRunning(); });
calibrationTask.setInterval(1 * Mycila::TaskDuration::SECONDS);
carouselTask.setEnabledWhen([]() { return display.isEnabled(); });
carouselTask.setInterval(config.getLong(KEY_DISPLAY_SPEED) * Mycila::TaskDuration::SECONDS);
dashboardInitTask.setEnabledWhen([]() { return espConnect.isConnected() && !dashboard.isAsyncAccessInProgress(); });
dashboardUpdateTask.setEnabledWhen([]() { return espConnect.isConnected() && !dashboard.isAsyncAccessInProgress(); });
dashboardUpdateTask.setInterval(1 * Mycila::TaskDuration::SECONDS);
debugTask.setEnabledWhen([]() { return config.getBool(KEY_ENABLE_DEBUG); });
debugTask.setInterval(20 * Mycila::TaskDuration::SECONDS);
displayTask.setEnabledWhen([]() { return display.isEnabled(); });
displayTask.setInterval(500 * Mycila::TaskDuration::MILLISECONDS);
ds18Task.setEnabledWhen([]() { return ds18Sys.isEnabled() || ds18O1.isEnabled() || ds18O2.isEnabled(); });
ds18Task.setInterval(10 * Mycila::TaskDuration::SECONDS);
lightsTask.setInterval(200 * Mycila::TaskDuration::MILLISECONDS);
networkManagerTask.setInterval(200 * Mycila::TaskDuration::MILLISECONDS);
relayTask.setEnabledWhen([]() { return !router.isCalibrationRunning() && (routerRelay1.isAutoRelayEnabled() || routerRelay2.isAutoRelayEnabled()); });
relayTask.setInterval(7 * Mycila::TaskDuration::SECONDS);
routerTask.setEnabledWhen([]() { return !router.isCalibrationRunning(); });
routerTask.setInterval(500 * Mycila::TaskDuration::MILLISECONDS);
zcdTask.setInterval(1 * Mycila::TaskDuration::SECONDS);
#ifdef APP_MODEL_TRIAL
trialTask.setInterval(30 * Mycila::TaskDuration::SECONDS);
#endif

// unsafeTaskManager
mqttPublishTask.setEnabledWhen([]() { return mqtt.isConnected(); });
mqttPublishTask.setInterval(config.getLong(KEY_MQTT_PUBLISH_INTERVAL) * Mycila::TaskDuration::SECONDS);
mqttPublishStaticTask.setEnabledWhen([]() { return mqtt.isConnected(); });
mqttPublishConfigTask.setEnabledWhen([]() { return mqtt.isConnected(); });

// jsyTaskManager
jsyTask.setEnabledWhen([]() { return jsy.isEnabled(); });

// pzemTaskManager
pzemTask.setEnabledWhen([]() { return (pzemO1.isEnabled() || pzemO2.isEnabled()) && pzemO1PairingTask.isPaused() && pzemO2PairingTask.isPaused(); });

// Grid
grid.localMetrics().setExpiration(10000); // local is fast
grid.remoteMetrics().setExpiration(10000); // remote JSY is fast
Expand Down Expand Up @@ -185,13 +148,55 @@ Mycila::Task initConfigTask("Init Config", [](void* params) {
espConnect.setBlocking(false);
espConnect.begin(Mycila::AppInfo.defaultHostname.c_str(), Mycila::AppInfo.defaultSSID.c_str(), config.get(KEY_ADMIN_PASSWORD), {config.get(KEY_WIFI_SSID), config.get(KEY_WIFI_PASSWORD), config.getBool(KEY_ENABLE_AP_MODE)});

// ZCD + Dimmers
pulseAnalyzer.onZeroCross(Mycila::Dimmer::onZeroCross);
// Dimmers
dimmerO1.setDutyCycleMin(config.getFloat(KEY_OUTPUT1_DIMMER_MIN) / 100);
dimmerO1.setDutyCycleMax(config.getFloat(KEY_OUTPUT1_DIMMER_MAX) / 100);
dimmerO1.setDutyCycleLimit(config.getFloat(KEY_OUTPUT1_DIMMER_LIMIT) / 100);
dimmerO2.setDutyCycleMin(config.getFloat(KEY_OUTPUT2_DIMMER_MIN) / 100);
dimmerO2.setDutyCycleMax(config.getFloat(KEY_OUTPUT2_DIMMER_MAX) / 100);
dimmerO2.setDutyCycleLimit(config.getFloat(KEY_OUTPUT2_DIMMER_LIMIT) / 100);
zcdTask.forceRun();

// ZCD
if (config.getBool(KEY_ENABLE_ZCD)) {
pulseAnalyzer.onZeroCross(Mycila::Dimmer::onZeroCross);
pulseAnalyzer.begin(config.getLong(KEY_PIN_ZCD));
zcdTask.resume();
}

// coreTaskManager
calibrationTask.setEnabledWhen([]() { return router.isCalibrationRunning(); });
calibrationTask.setInterval(1 * Mycila::TaskDuration::SECONDS);
carouselTask.setEnabled(display.isEnabled());
carouselTask.setInterval(config.getLong(KEY_DISPLAY_SPEED) * Mycila::TaskDuration::SECONDS);
dashboardInitTask.setEnabledWhen([]() { return espConnect.isConnected() && !dashboard.isAsyncAccessInProgress(); });
dashboardUpdateTask.setEnabledWhen([]() { return espConnect.isConnected() && !dashboard.isAsyncAccessInProgress(); });
dashboardUpdateTask.setInterval(1 * Mycila::TaskDuration::SECONDS);
debugTask.setEnabledWhen([]() { return config.getBool(KEY_ENABLE_DEBUG); });
debugTask.setInterval(20 * Mycila::TaskDuration::SECONDS);
displayTask.setEnabled(display.isEnabled());
displayTask.setInterval(500 * Mycila::TaskDuration::MILLISECONDS);
ds18Task.setEnabled(ds18Sys.isEnabled() || ds18O1.isEnabled() || ds18O2.isEnabled());
ds18Task.setInterval(10 * Mycila::TaskDuration::SECONDS);
lightsTask.setInterval(200 * Mycila::TaskDuration::MILLISECONDS);
networkManagerTask.setInterval(200 * Mycila::TaskDuration::MILLISECONDS);
relayTask.setEnabledWhen([]() { return !router.isCalibrationRunning() && (routerRelay1.isAutoRelayEnabled() || routerRelay2.isAutoRelayEnabled()); });
relayTask.setInterval(7 * Mycila::TaskDuration::SECONDS);
routerTask.setEnabledWhen([]() { return !router.isCalibrationRunning(); });
routerTask.setInterval(500 * Mycila::TaskDuration::MILLISECONDS);
zcdTask.setEnabled(pulseAnalyzer.isEnabled());
#ifdef APP_MODEL_TRIAL
trialTask.setInterval(30 * Mycila::TaskDuration::SECONDS);
#endif

// unsafeTaskManager
mqttPublishTask.setEnabledWhen([]() { return mqtt.isConnected(); });
mqttPublishTask.setInterval(config.getLong(KEY_MQTT_PUBLISH_INTERVAL) * Mycila::TaskDuration::SECONDS);
mqttPublishStaticTask.setEnabledWhen([]() { return mqtt.isConnected(); });
mqttPublishConfigTask.setEnabledWhen([]() { return mqtt.isConnected(); });

// jsyTaskManager
jsyTask.setEnabledWhen([]() { return jsy.isEnabled(); });

// pzemTaskManager
pzemTask.setEnabledWhen([]() { return (pzemO1.isEnabled() || pzemO2.isEnabled()) && pzemO1PairingTask.isPaused() && pzemO2PairingTask.isPaused(); });
});
84 changes: 0 additions & 84 deletions src/init/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,90 +128,6 @@ Mycila::Task initEventsTask("Init Events", [](void* params) {
} else if (key == KEY_HA_DISCOVERY_TOPIC) {
haDiscovery.setDiscoveryTopic(config.get(KEY_HA_DISCOVERY_TOPIC));

} else if (key == KEY_ENABLE_LIGHTS) {
lights.end();
if (config.getBool(KEY_ENABLE_LIGHTS))
lights.begin(config.getLong(KEY_PIN_LIGHTS_GREEN), config.getLong(KEY_PIN_LIGHTS_YELLOW), config.getLong(KEY_PIN_LIGHTS_RED));

} else if (key == KEY_ENABLE_DS18_SYSTEM) {
ds18Sys.end();
if (config.getBool(KEY_ENABLE_DS18_SYSTEM))
ds18Sys.begin(config.getLong(KEY_PIN_ROUTER_DS18));

} else if (key == KEY_ENABLE_OUTPUT1_DS18) {
ds18O1.end();
if (config.getBool(KEY_ENABLE_OUTPUT1_DS18))
ds18O1.begin(config.getLong(KEY_PIN_OUTPUT1_DS18));

} else if (key == KEY_ENABLE_OUTPUT2_DS18) {
ds18O2.end();
if (config.getBool(KEY_ENABLE_OUTPUT2_DS18))
ds18O2.begin(config.getLong(KEY_PIN_OUTPUT2_DS18));

} else if (key == KEY_ENABLE_MQTT) {
mqttConfigTask.resume();

} else if (key == KEY_ENABLE_ZCD) {
zcdTask.requestEarlyRun();

} else if (key == KEY_ENABLE_OUTPUT1_DIMMER) {
zcdTask.requestEarlyRun();

} else if (key == KEY_ENABLE_OUTPUT2_DIMMER) {
zcdTask.requestEarlyRun();

} else if (key == KEY_ENABLE_OUTPUT1_RELAY) {
bypassRelayO1.end();
if (config.getBool(KEY_ENABLE_OUTPUT1_RELAY))
bypassRelayO1.begin(config.getLong(KEY_PIN_OUTPUT1_RELAY), config.isEqual(KEY_OUTPUT1_RELAY_TYPE, YASOLR_RELAY_TYPE_NC) ? Mycila::RelayType::NC : Mycila::RelayType::NO);

} else if (key == KEY_ENABLE_OUTPUT2_RELAY) {
bypassRelayO2.end();
if (config.getBool(KEY_ENABLE_OUTPUT2_RELAY))
bypassRelayO2.begin(config.getLong(KEY_PIN_OUTPUT2_RELAY), config.isEqual(KEY_OUTPUT2_RELAY_TYPE, YASOLR_RELAY_TYPE_NC) ? Mycila::RelayType::NC : Mycila::RelayType::NO);

} else if (key == KEY_ENABLE_RELAY1) {
relay1.end();
if (config.getBool(KEY_ENABLE_RELAY1))
relay1.begin(config.getLong(KEY_PIN_RELAY1), config.isEqual(KEY_RELAY1_TYPE, YASOLR_RELAY_TYPE_NC) ? Mycila::RelayType::NC : Mycila::RelayType::NO);

} else if (key == KEY_ENABLE_RELAY2) {
relay2.end();
if (config.getBool(KEY_ENABLE_RELAY2))
relay2.begin(config.getLong(KEY_PIN_RELAY2), config.isEqual(KEY_RELAY2_TYPE, YASOLR_RELAY_TYPE_NC) ? Mycila::RelayType::NC : Mycila::RelayType::NO);

} else if (key == KEY_ENABLE_JSY) {
jsy.end();
if (config.getBool(KEY_ENABLE_JSY)) {
jsy.begin(YASOLR_JSY_SERIAL, config.getLong(KEY_PIN_JSY_RX), config.getLong(KEY_PIN_JSY_TX));
if (jsy.isEnabled() && jsy.getBaudRate() != jsy.getMaxAvailableBaudRate())
jsy.setBaudRate(jsy.getMaxAvailableBaudRate());
}

} else if (key == KEY_ENABLE_OUTPUT1_PZEM) {
pzemO1.end();
if (config.getBool(KEY_ENABLE_OUTPUT1_PZEM))
pzemO1.begin(YASOLR_PZEM_SERIAL, config.getLong(KEY_PIN_PZEM_RX), config.getLong(KEY_PIN_PZEM_TX), YASOLR_PZEM_ADDRESS_OUTPUT1);

} else if (key == KEY_ENABLE_OUTPUT2_PZEM) {
pzemO2.end();
if (config.getBool(KEY_ENABLE_OUTPUT2_PZEM))
pzemO2.begin(YASOLR_PZEM_SERIAL, config.getLong(KEY_PIN_PZEM_RX), config.getLong(KEY_PIN_PZEM_TX), YASOLR_PZEM_ADDRESS_OUTPUT2);

} else if (key == KEY_ENABLE_DISPLAY) {
display.end();
if (config.getBool(KEY_ENABLE_DISPLAY)) {
const std::string& displayType = config.getString(KEY_DISPLAY_TYPE);
if (displayType == "SSD1306")
display.begin(Mycila::EasyDisplayType::SSD1306, config.getLong(KEY_PIN_DISPLAY_SCL), config.getLong(KEY_PIN_DISPLAY_SDA), config.getLong(KEY_DISPLAY_ROTATION));
else if (displayType == "SH1107")
display.begin(Mycila::EasyDisplayType::SH1107, config.getLong(KEY_PIN_DISPLAY_SCL), config.getLong(KEY_PIN_DISPLAY_SDA), config.getLong(KEY_DISPLAY_ROTATION));
else if (displayType == "SH1106")
display.begin(Mycila::EasyDisplayType::SH1106, config.getLong(KEY_PIN_DISPLAY_SCL), config.getLong(KEY_PIN_DISPLAY_SDA), config.getLong(KEY_DISPLAY_ROTATION));
display.clearDisplay();
display.setActive(true);
}

} else if (key == KEY_PID_KP || key == KEY_PID_KI || key == KEY_PID_KD || key == KEY_PID_OUT_MIN || key == KEY_PID_OUT_MAX || key == KEY_PID_P_MODE || key == KEY_PID_D_MODE || key == KEY_PID_IC_MODE || key == KEY_PID_SETPOINT) {
pidController.setProportionalMode((Mycila::PID::ProportionalMode)config.getLong(KEY_PID_P_MODE));
pidController.setDerivativeMode((Mycila::PID::DerivativeMode)config.getLong(KEY_PID_D_MODE));
Expand Down
48 changes: 6 additions & 42 deletions src/tasks/ZCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,31 @@
#include <thyristor.h>

Mycila::Task zcdTask("ZCD", [](void* params) {
const bool zcdSwitched = config.getBool(KEY_ENABLE_ZCD);
const bool dimmer1Switched = config.getBool(KEY_ENABLE_OUTPUT1_DIMMER);
const bool dimmer2Switched = config.getBool(KEY_ENABLE_OUTPUT2_DIMMER);
const bool online = pulseAnalyzer.isOnline();

// Check dimmers

if (!(dimmer1Switched && online) && dimmerO1.isEnabled()) {
dimmerO1.end();
}

if (!(dimmer2Switched && online) && dimmerO2.isEnabled()) {
dimmerO2.end();
}

// Check ZCD

if (!zcdSwitched) {
pulseAnalyzer.end();
dimmerO1.end();
dimmerO2.end();
Thyristor::end();
Thyristor::setSemiPeriod(0);
return;
}

// => ZCD switch turned on

// turn on Pulse Analyzer if off
if (!pulseAnalyzer.isEnabled()) {
logger.info(TAG, "Starting Pulse Analyzer");
pulseAnalyzer.begin(config.getLong(KEY_PIN_ZCD));
return;
}

// => ZCD switch turned on + Pulse Analyzer running

// check if ZCD is online (connected to the grid)
// this is required for dimmers to work
if (!online) {
if (!pulseAnalyzer.isOnline()) {
logger.debug(TAG, "No electricity detected by ZCD module");
zcdTask.resume(2 * Mycila::TaskDuration::SECONDS); // retry in 2 seconds
return;
}

// => ZCD switch turned on + Pulse Analyzer online

if (!Thyristor::getSemiPeriod() || (dimmer1Switched && !dimmerO1.isEnabled()) || (dimmer2Switched && !dimmerO2.isEnabled())) {
if (!Thyristor::getSemiPeriod() || !dimmerO1.isEnabled() || !dimmerO2.isEnabled()) {
float frequency = detectGridFrequency();
uint16_t semiPeriod = 1000000 / 2 / frequency;
logger.info(TAG, "Detected grid frequency: %.2f Hz", frequency);

if (!Thyristor::getSemiPeriod()) {
logger.info(TAG, "Starting Thyristor with semi-period: %" PRIu16 " us", semiPeriod);
Thyristor::setSemiPeriod(semiPeriod);
Thyristor::begin();
}

if (dimmer1Switched && !dimmerO1.isEnabled()) {
if (!dimmerO1.isEnabled() && config.getBool(KEY_ENABLE_OUTPUT1_DIMMER)) {
dimmerO1.begin(config.getLong(KEY_PIN_OUTPUT1_DIMMER), semiPeriod);
}

if (dimmer2Switched && !dimmerO2.isEnabled()) {
if (!dimmerO2.isEnabled() && config.getBool(KEY_ENABLE_OUTPUT2_DIMMER)) {
dimmerO2.begin(config.getLong(KEY_PIN_OUTPUT2_DIMMER), semiPeriod);
}
}
Expand Down

0 comments on commit 1ee1c29

Please sign in to comment.