@@ -116,6 +116,9 @@ bool _otaCheckFailed = false; /* Set by onError during checkForUpdate(
116116int _otaCheckErrorCode = 0 ; /* Error code captured by onError during checkForUpdate() */
117117JsonDocument _otaPendingDoc;
118118static char _otaCurrentPartition[8 ] = " " ;
119+ static char _otaLatestVersion[32 ] = " " ;
120+ static char _otaReleaseUrl[256 ] = " " ;
121+ static int _otaLastPublishedPercentage = -1 ;
119122uint64_t _otaLastCheckedTime = 0 ;
120123
121124fs::LittleFSFS uiFS;
@@ -3584,6 +3587,25 @@ void setup_OtaFirmware(){
35843587 eventLog.createEvent (msg, EventLog::LOG_LEVEL_INFO );
35853588 }
35863589 oled.setProgressBar ((float )written / (float )total);
3590+ int pct = (int )((float )written / (float )total * 100 );
3591+ if (pct != _otaLastPublishedPercentage && deviceIdentity.enabled && mqttClient.connected ()){
3592+ _otaLastPublishedPercentage = pct;
3593+ JsonDocument mqttDoc;
3594+ mqttDoc[" installed_version" ] = VERSION ;
3595+ mqttDoc[" latest_version" ] = _otaLatestVersion;
3596+ if (strlen (_otaReleaseUrl) > 0 ){ mqttDoc[" release_url" ] = _otaReleaseUrl; }
3597+ mqttDoc[" in_progress" ] = true ;
3598+ mqttDoc[" update_percentage" ] = pct;
3599+ char title[32 ];
3600+ snprintf (title, sizeof (title), " Updating %s" , _otaCurrentPartition);
3601+ mqttDoc[" title" ] = title;
3602+ char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
3603+ snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
3604+ char buffer[384 ];
3605+ serializeJson (mqttDoc, buffer, sizeof (buffer));
3606+ mqttClient.publish (topic, buffer);
3607+ }
3608+ mqttClient.loop ();
35873609 });
35883610
35893611 otaFirmware.onPartitionComplete ([](const char * partition, bool success){
@@ -3614,6 +3636,9 @@ void setup_OtaFirmware(){
36143636 });
36153637
36163638 otaFirmware.onAvailable ([](const char * version, const char * app, const char * releaseUrl){
3639+ strlcpy (_otaLatestVersion, version, sizeof (_otaLatestVersion));
3640+ strlcpy (_otaReleaseUrl, releaseUrl ? releaseUrl : " " , sizeof (_otaReleaseUrl));
3641+ _otaLastPublishedPercentage = -1 ;
36173642 if (deviceIdentity.enabled == false ){ return ; }
36183643 if (!mqttClient.connected ()){ return ; }
36193644 eventLog.createEvent (" OTA update available" );
@@ -3690,6 +3715,25 @@ void otaFirmware_checkPending(){
36903715 eventLog.createEvent (msg, EventLog::LOG_LEVEL_INFO );
36913716 }
36923717 oled.setProgressBar ((float )written / (float )total);
3718+ int pct = (int )((float )written / (float )total * 100 );
3719+ if (pct != _otaLastPublishedPercentage && deviceIdentity.enabled && mqttClient.connected ()){
3720+ _otaLastPublishedPercentage = pct;
3721+ JsonDocument mqttDoc;
3722+ mqttDoc[" installed_version" ] = VERSION ;
3723+ mqttDoc[" latest_version" ] = _otaLatestVersion;
3724+ if (strlen (_otaReleaseUrl) > 0 ){ mqttDoc[" release_url" ] = _otaReleaseUrl; }
3725+ mqttDoc[" in_progress" ] = true ;
3726+ mqttDoc[" update_percentage" ] = pct;
3727+ char title[32 ];
3728+ snprintf (title, sizeof (title), " Updating %s" , _otaCurrentPartition);
3729+ mqttDoc[" title" ] = title;
3730+ char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
3731+ snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
3732+ char buffer[384 ];
3733+ serializeJson (mqttDoc, buffer, sizeof (buffer));
3734+ mqttClient.publish (topic, buffer);
3735+ }
3736+ mqttClient.loop ();
36933737 });
36943738
36953739 otaFirmware.onPartitionComplete ([](const char * partition, bool success){
@@ -3720,16 +3764,20 @@ void otaFirmware_checkPending(){
37203764 });
37213765
37223766 eventLog.createEvent (" OTA update available" );
3767+ const char * targetVersion = _otaPendingDoc[" version" ] | VERSION ;
3768+ const char * releaseUrl = _otaPendingDoc[" release_url" ] | " " ;
3769+ strlcpy (_otaLatestVersion, targetVersion, sizeof (_otaLatestVersion));
3770+ strncat (_otaLatestVersion, " (Forced)" , sizeof (_otaLatestVersion) - strlen (_otaLatestVersion) - 1 );
3771+ strlcpy (_otaReleaseUrl, releaseUrl, sizeof (_otaReleaseUrl));
3772+ _otaLastPublishedPercentage = -1 ;
37233773 if (deviceIdentity.enabled && mqttClient.connected ()){
37243774 JsonDocument mqttDoc;
37253775 mqttDoc[" installed_version" ] = VERSION ;
3726- const char * targetVersion = _otaPendingDoc[" version" ] | VERSION ;
37273776 mqttDoc[" latest_version" ] = targetVersion;
3728- const char * releaseUrl = _otaPendingDoc[" release_url" ] | " " ;
37293777 if (releaseUrl && strlen (releaseUrl) > 0 ){
37303778 mqttDoc[" release_url" ] = releaseUrl;
37313779 }
3732- mqttDoc[" in_progress" ] = false ;
3780+ mqttDoc[" in_progress" ] = true ;
37333781 char topic[MQTT_TOPIC_UPDATE_STATE_PATTERN_LENGTH +1 ];
37343782 snprintf (topic, sizeof (topic), MQTT_TOPIC_UPDATE_STATE_PATTERN , deviceIdentity.data .uuid );
37353783 mqttClient.beginPublish (topic, measureJson (mqttDoc), true );
0 commit comments