Skip to content

Commit b2bc49e

Browse files
authored
Merge pull request #690 from BrentIO/fix/689
refactor: unify forced and non-forced OTA execution paths
2 parents 9140b1b + 9e698c1 commit b2bc49e

1 file changed

Lines changed: 33 additions & 100 deletions

File tree

Controller/Controller.ino

Lines changed: 33 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,6 +3598,7 @@ void setup_OtaFirmware(){
35983598
_otaUpdateInProcess = true;
35993599
if(strcmp(partition, _otaCurrentPartition) != 0){
36003600
strlcpy(_otaCurrentPartition, partition, sizeof(_otaCurrentPartition));
3601+
_otaLastPublishedPercentage = -1;
36013602
oled.setOTAPartition(partition);
36023603
oled.setPage(managerOled::PAGE_OTA_IN_PROGRESS);
36033604
char msg[OLED_CHARACTERS_PER_LINE+1];
@@ -3699,9 +3700,9 @@ void setup_OtaFirmware(){
36993700

37003701

37013702
/**
3702-
* Executes a pending OTA update. If _otaPendingDoc is empty the update was
3703-
* triggered from HA via MQTT (non-forced); otherwise it is a forced update
3704-
* with a specific document from the HTTP API.
3703+
* Executes a pending OTA update. If _otaPendingDoc is populated the update was
3704+
* triggered via the HTTP API (forced); otherwise it was triggered from HA via MQTT.
3705+
* Both paths share the same callbacks registered in setup_OtaFirmware().
37053706
*/
37063707
void otaFirmware_checkPending(){
37073708

@@ -3712,100 +3713,39 @@ void otaFirmware_checkPending(){
37123713
_otaUpdateInProcess = true;
37133714
_otaCurrentPartition[0] = '\0';
37143715

3715-
if(_otaPendingDoc.size() == 0){
3716-
_otaPendingRequest = false;
3717-
otaFirmware.execOTA();
3718-
_otaUpdateInProcess = false;
3719-
return;
3720-
}
3716+
bool isForced = (_otaPendingDoc.size() > 0);
37213717

3722-
otaFirmware.setApplicationName(_otaPendingDoc["application_name"]);
3718+
if(isForced){
3719+
otaFirmware.setApplicationName(_otaPendingDoc["application_name"]);
37233720

3724-
for(JsonVariant bin : _otaPendingDoc["binaries"].as<JsonArray>()){
3725-
if(String(bin["url"] | "").startsWith("https:")){
3726-
if(_certBundle != nullptr){
3727-
_otaHttpsClient.setCACert(_certBundle);
3728-
otaFirmware.setClient(&_otaHttpsClient);
3729-
} else {
3730-
otaFirmware.useBundledCerts();
3721+
for(JsonVariant bin : _otaPendingDoc["binaries"].as<JsonArray>()){
3722+
if(String(bin["url"] | "").startsWith("https:")){
3723+
if(_certBundle != nullptr){
3724+
_otaHttpsClient.setCACert(_certBundle);
3725+
otaFirmware.setClient(&_otaHttpsClient);
3726+
} else {
3727+
otaFirmware.useBundledCerts();
3728+
}
3729+
break;
37313730
}
3732-
break;
3733-
}
3734-
}
3735-
3736-
otaFirmware.onProgress([](const char* partition, size_t written, size_t total){
3737-
_otaUpdateInProcess = true;
3738-
if(strcmp(partition, _otaCurrentPartition) != 0){
3739-
strlcpy(_otaCurrentPartition, partition, sizeof(_otaCurrentPartition));
3740-
oled.setOTAPartition(partition);
3741-
oled.setPage(managerOled::PAGE_OTA_IN_PROGRESS);
3742-
char msg[OLED_CHARACTERS_PER_LINE+1];
3743-
snprintf(msg, sizeof(msg), "OTA %s update start", partition);
3744-
eventLog.createEvent(msg, EventLog::LOG_LEVEL_INFO);
3745-
}
3746-
oled.setProgressBar((float)written / (float)total);
3747-
int pct = (int)((float)written / (float)total * 100);
3748-
if(pct != _otaLastPublishedPercentage && deviceIdentity.enabled && mqttClient.connected()){
3749-
_otaLastPublishedPercentage = pct;
3750-
JsonDocument mqttDoc;
3751-
mqttDoc["installed_version"] = VERSION;
3752-
mqttDoc["latest_version"] = _otaLatestVersion;
3753-
if(strlen(_otaReleaseUrl) > 0){ mqttDoc["release_url"] = _otaReleaseUrl; }
3754-
mqttDoc["in_progress"] = true;
3755-
mqttDoc["update_percentage"] = pct;
3756-
char title[32];
3757-
snprintf(title, sizeof(title), "Updating %s", _otaCurrentPartition);
3758-
mqttDoc["title"] = title;
3759-
char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH+1];
3760-
snprintf(topic, sizeof(topic), MQTT_TOPIC_UPDATE_STATE_PATTERN, deviceIdentity.data.uuid);
3761-
char buffer[384];
3762-
serializeJson(mqttDoc, buffer, sizeof(buffer));
3763-
mqttClient.publish(topic, buffer);
3764-
}
3765-
mqttClient.loop();
3766-
});
3767-
3768-
otaFirmware.onPartitionComplete([](const char* partition, bool success){
3769-
char msg[OLED_CHARACTERS_PER_LINE+1];
3770-
snprintf(msg, sizeof(msg), "OTA %s %s", partition, success ? "finished" : "failed");
3771-
eventLog.createEvent(msg, success ? EventLog::LOG_LEVEL_INFO : EventLog::LOG_LEVEL_NOTIFICATION);
3772-
});
3773-
3774-
otaFirmware.onComplete([](bool success){
3775-
_otaUpdateInProcess = false;
3776-
_otaPendingRequest = false;
3777-
3778-
if(mqttClient.connected()){
3779-
char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH+1];
3780-
snprintf(topic, sizeof(topic), MQTT_TOPIC_UPDATE_STATE_PATTERN, deviceIdentity.data.uuid);
3781-
JsonDocument mqttDoc;
3782-
mqttDoc["in_progress"] = false;
3783-
char buffer[256];
3784-
serializeJson(mqttDoc, buffer);
3785-
mqttClient.publish(topic, buffer);
37863731
}
37873732

3788-
if(success){
3789-
eventLog.createEvent("Rebooting...", EventLog::LOG_LEVEL_NOTIFICATION);
3790-
delay(5000);
3791-
ESP.restart();
3792-
}
3793-
});
3733+
const char* targetVersion = _otaPendingDoc["version"] | VERSION;
3734+
const char* releaseUrl = _otaPendingDoc["release_url"] | "";
3735+
strlcpy(_otaLatestVersion, targetVersion, sizeof(_otaLatestVersion));
3736+
strncat(_otaLatestVersion, " (Forced)", sizeof(_otaLatestVersion) - strlen(_otaLatestVersion) - 1);
3737+
strlcpy(_otaReleaseUrl, releaseUrl, sizeof(_otaReleaseUrl));
3738+
eventLog.createEvent("OTA update available");
3739+
}
37943740

3795-
eventLog.createEvent("OTA update available");
3796-
const char* targetVersion = _otaPendingDoc["version"] | VERSION;
3797-
const char* releaseUrl = _otaPendingDoc["release_url"] | "";
3798-
strlcpy(_otaLatestVersion, targetVersion, sizeof(_otaLatestVersion));
3799-
strncat(_otaLatestVersion, " (Forced)", sizeof(_otaLatestVersion) - strlen(_otaLatestVersion) - 1);
3800-
strlcpy(_otaReleaseUrl, releaseUrl, sizeof(_otaReleaseUrl));
38013741
_otaLastPublishedPercentage = -1;
3742+
_otaPendingRequest = false;
3743+
38023744
if(deviceIdentity.enabled && mqttClient.connected()){
38033745
JsonDocument mqttDoc;
38043746
mqttDoc["installed_version"] = VERSION;
3805-
mqttDoc["latest_version"] = targetVersion;
3806-
if(releaseUrl && strlen(releaseUrl) > 0){
3807-
mqttDoc["release_url"] = releaseUrl;
3808-
}
3747+
mqttDoc["latest_version"] = _otaLatestVersion;
3748+
if(strlen(_otaReleaseUrl) > 0){ mqttDoc["release_url"] = _otaReleaseUrl; }
38093749
mqttDoc["in_progress"] = true;
38103750
char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH+1];
38113751
snprintf(topic, sizeof(topic), MQTT_TOPIC_UPDATE_STATE_PATTERN, deviceIdentity.data.uuid);
@@ -3816,10 +3756,14 @@ void otaFirmware_checkPending(){
38163756
mqttClient.endPublish();
38173757
}
38183758

3819-
otaFirmware.execOTA(_otaPendingDoc);
3759+
if(isForced){
3760+
otaFirmware.execOTA(_otaPendingDoc);
3761+
_otaPendingDoc.clear();
3762+
} else {
3763+
otaFirmware.execOTA();
3764+
}
38203765

38213766
/* Only reached on failure — onComplete handles restart on success */
3822-
_otaPendingDoc.clear();
38233767
_otaUpdateInProcess = false;
38243768
_otaPendingRequest = false;
38253769
}
@@ -4379,17 +4323,6 @@ void eventHandler_mqttMessageReceived(char* topic, byte* pl, unsigned int length
43794323

43804324
if(ms.Match(MQTT_TOPIC_UPDATE_SET_REGEX)){ //This is an update command request
43814325

4382-
char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH+1];
4383-
snprintf(topic, sizeof(topic), MQTT_TOPIC_UPDATE_STATE_PATTERN, deviceIdentity.data.uuid);
4384-
4385-
JsonDocument doc;
4386-
doc["in_progress"] = true;
4387-
4388-
char buffer[256];
4389-
serializeJson(doc, buffer);
4390-
4391-
mqttClient.publish(topic, buffer);
4392-
43934326
if(!_otaManifestUrl.isEmpty()){
43944327
_otaPendingRequest = true;
43954328
}

0 commit comments

Comments
 (0)