18
18
package otaapi
19
19
20
20
import (
21
+ "strconv"
21
22
"strings"
22
23
"time"
23
24
@@ -28,8 +29,9 @@ import (
28
29
29
30
type (
30
31
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"`
33
35
}
34
36
35
37
OtaStatusList struct {
53
55
}
54
56
55
57
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"`
58
61
}
59
62
)
60
63
@@ -155,14 +158,40 @@ func (r OtaStatusDetail) String() string {
155
158
t = table .New ()
156
159
t .SetHeader ("Time" , "Status" , "Detail" )
157
160
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 )
159
163
}
160
164
output += "\n Details:\n " + t .Render ()
161
165
}
162
166
163
167
return output
164
168
}
165
169
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
+
166
195
func upperCaseFirst (s string ) string {
167
196
if len (s ) > 0 {
168
197
s = strings .ReplaceAll (s , "_" , " " )
0 commit comments