Skip to content

Commit 7a0850d

Browse files
committed
Implemented support for percentage state display for ota progress
1 parent 5aebb3d commit 7a0850d

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

command/ota/status.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ func PrintOtaStatus(otaid, otaids, device string, cred *config.Credentials, limi
2727
res, err := otapi.GetOtaStatusByOtaID(otaid, limit, order)
2828
if err == nil && res != nil {
2929
feedback.PrintResult(otaapi.OtaStatusDetail{
30-
Ota: res.Ota,
31-
Details: res.States,
30+
FirmwareSize: res.FirmwareSize,
31+
Ota: res.Ota,
32+
Details: res.States,
3233
})
3334
} else if err != nil {
3435
return err

internal/ota-api/dto.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package otaapi
1919

2020
import (
21+
"strconv"
2122
"strings"
2223
"time"
2324

@@ -28,8 +29,9 @@ import (
2829

2930
type (
3031
OtaStatusResponse struct {
31-
Ota Ota `json:"ota"`
32-
States []State `json:"states,omitempty"`
32+
FirmwareSize *int64 `json:"firmware_size,omitempty"`
33+
Ota Ota `json:"ota"`
34+
States []State `json:"states,omitempty"`
3335
}
3436

3537
OtaStatusList struct {
@@ -53,8 +55,9 @@ type (
5355
}
5456

5557
OtaStatusDetail struct {
56-
Ota Ota `json:"ota"`
57-
Details []State `json:"details,omitempty"`
58+
FirmwareSize *int64 `json:"firmware_size,omitempty"`
59+
Ota Ota `json:"ota"`
60+
Details []State `json:"details,omitempty"`
5861
}
5962
)
6063

@@ -155,14 +158,40 @@ func (r OtaStatusDetail) String() string {
155158
t = table.New()
156159
t.SetHeader("Time", "Status", "Detail")
157160
for _, s := range r.Details {
158-
t.AddRow(formatHumanReadableTs(s.Timestamp), upperCaseFirst(s.State), s.StateData)
161+
stateData := formatStateData(s.State, s.StateData, r.FirmwareSize, hasReachedFlashState(r.Details))
162+
t.AddRow(formatHumanReadableTs(s.Timestamp), upperCaseFirst(s.State), stateData)
159163
}
160164
output += "\nDetails:\n" + t.Render()
161165
}
162166

163167
return output
164168
}
165169

170+
func hasReachedFlashState(states []State) bool {
171+
for _, s := range states {
172+
if s.State == "flash" {
173+
return true
174+
}
175+
}
176+
return false
177+
}
178+
179+
func formatStateData(state, data string, firmware_size *int64, hasReceivedFlashState bool) string {
180+
if state == "fetch" {
181+
// This is the state 'fetch' of OTA progress. This contains a numer that represents the number of bytes fetched
182+
if hasReceivedFlashState {
183+
return "100%"
184+
}
185+
actualDownloadedData, err := strconv.Atoi(data)
186+
if err != nil || firmware_size == nil || actualDownloadedData <= 0 || *firmware_size <= 0 { // Sanitize and avoid division by zero
187+
return data
188+
}
189+
percentage := (float64(actualDownloadedData) / float64(*firmware_size)) * 100
190+
return strconv.FormatFloat(percentage, 'f', 2, 64) + "%"
191+
}
192+
return data
193+
}
194+
166195
func upperCaseFirst(s string) string {
167196
if len(s) > 0 {
168197
s = strings.ReplaceAll(s, "_", " ")

0 commit comments

Comments
 (0)