Skip to content

Commit fa3deee

Browse files
committed
handling Unknown state data
1 parent 75dbb48 commit fa3deee

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/ota-api/dto.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,27 @@ func hasReachedFlashState(states []State) bool {
177177
}
178178

179179
func formatStateData(state, data string, firmware_size *int64, hasReceivedFlashState bool) string {
180-
if data == "" || strings.ToLower(data) == "unknown" {
180+
if data == "" {
181181
return ""
182182
}
183183
if state == "fetch" {
184-
// This is the state 'fetch' of OTA progress. This contains a numer that represents the number of bytes fetched
184+
// This is the state 'fetch' of OTA progress. This contains a number that represents the number of bytes fetched
185185
if hasReceivedFlashState {
186186
return buildSimpleProgressBar(float64(100))
187187
}
188+
if strings.ToLower(data) == "unknown" {
189+
return ""
190+
}
188191
actualDownloadedData, err := strconv.Atoi(data)
189192
if err != nil || firmware_size == nil || actualDownloadedData <= 0 || *firmware_size <= 0 { // Sanitize and avoid division by zero
190193
return data
191194
}
192195
percentage := (float64(actualDownloadedData) / float64(*firmware_size)) * 100
193196
return buildSimpleProgressBar(percentage)
194197
}
198+
if strings.ToLower(data) == "unknown" {
199+
return ""
200+
}
195201
return data
196202
}
197203

internal/ota-api/dto_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ func TestProgressBar_ifFlashState_goTo100Pct(t *testing.T) {
1717
bar := formatStateData("fetch", "25665", &firmwareSize, true) // If in flash status, go to 100%
1818
assert.Equal(t, "[==========] 100.00%", bar)
1919
}
20+
21+
func TestProgressBar_ifFlashStateAndUnknown_goTo100Pct(t *testing.T) {
22+
firmwareSize := int64(25665 * 2)
23+
bar := formatStateData("fetch", "Unknown", &firmwareSize, true) // If in flash status, go to 100%
24+
assert.Equal(t, "[==========] 100.00%", bar)
25+
}

0 commit comments

Comments
 (0)