-
-
Notifications
You must be signed in to change notification settings - Fork 4
Support for ota download progress #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7a0850d
0488894
16b5fab
75dbb48
fa3deee
d02c1c1
ef4f1db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
package otaapi | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -26,10 +27,13 @@ import ( | |
"github.com/arduino/arduino-cli/table" | ||
) | ||
|
||
const progressBarMultiplier = 2 | ||
|
||
type ( | ||
OtaStatusResponse struct { | ||
Ota Ota `json:"ota"` | ||
States []State `json:"states,omitempty"` | ||
FirmwareSize *int64 `json:"firmware_size,omitempty"` | ||
Ota Ota `json:"ota"` | ||
States []State `json:"states,omitempty"` | ||
} | ||
|
||
OtaStatusList struct { | ||
|
@@ -53,8 +57,9 @@ type ( | |
} | ||
|
||
OtaStatusDetail struct { | ||
Ota Ota `json:"ota"` | ||
Details []State `json:"details,omitempty"` | ||
FirmwareSize *int64 `json:"firmware_size,omitempty"` | ||
Ota Ota `json:"ota"` | ||
Details []State `json:"details,omitempty"` | ||
} | ||
) | ||
|
||
|
@@ -154,15 +159,62 @@ func (r OtaStatusDetail) String() string { | |
if len(r.Details) > 0 { | ||
t = table.New() | ||
t.SetHeader("Time", "Status", "Detail") | ||
fwSize := int64(0) | ||
if r.FirmwareSize != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can also remove the pointer from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a pointer because is doc it is stated that firmware size may not be present. In case it is missing, is it rmeoved from api response or returned as a 0 value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn't be present in the response, but anyway, for Golang that doesn't make any difference you will get a 0 either if the field is not present or if it is an actual 0. |
||
fwSize = *r.FirmwareSize | ||
} | ||
for _, s := range r.Details { | ||
t.AddRow(formatHumanReadableTs(s.Timestamp), upperCaseFirst(s.State), s.StateData) | ||
stateData := formatStateData(s.State, s.StateData, fwSize, hasReachedFlashState(r.Details)) | ||
t.AddRow(formatHumanReadableTs(s.Timestamp), upperCaseFirst(s.State), stateData) | ||
} | ||
output += "\nDetails:\n" + t.Render() | ||
} | ||
|
||
return output | ||
} | ||
|
||
func hasReachedFlashState(states []State) bool { | ||
for _, s := range states { | ||
if s.State == "flash" || s.State == "reboot" { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func formatStateData(state, data string, firmware_size int64, hasReceivedFlashState bool) string { | ||
if data == "" || data == "Unknown" { | ||
return "" | ||
} | ||
if state == "fetch" { | ||
// This is the state 'fetch' of OTA progress. This contains a number that represents the number of bytes fetched | ||
actualDownloadedData, err := strconv.Atoi(data) | ||
if err != nil || actualDownloadedData <= 0 || firmware_size <= 0 { // Sanitize and avoid division by zero | ||
return data | ||
} | ||
if hasReceivedFlashState { | ||
return buildSimpleProgressBar(float64(100)) | ||
} | ||
percentage := (float64(actualDownloadedData) / float64(firmware_size)) * 100 | ||
return buildSimpleProgressBar(percentage) | ||
} | ||
return data | ||
} | ||
|
||
func buildSimpleProgressBar(progress float64) string { | ||
progressInt := int(progress) / 10 | ||
progressInt = progressInt * progressBarMultiplier | ||
maxProgress := 10 * progressBarMultiplier | ||
var bar strings.Builder | ||
bar.WriteString("[") | ||
bar.WriteString(strings.Repeat("=", progressInt)) | ||
bar.WriteString(strings.Repeat(" ", maxProgress-progressInt)) | ||
bar.WriteString("] ") | ||
bar.WriteString(strconv.FormatFloat(progress, 'f', 2, 64)) | ||
bar.WriteString("%") | ||
return bar.String() | ||
} | ||
|
||
func upperCaseFirst(s string) string { | ||
if len(s) > 0 { | ||
s = strings.ReplaceAll(s, "_", " ") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package otaapi | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProgressBar_notCompletePct(t *testing.T) { | ||
firmwareSize := int64(25665 * 2) | ||
bar := formatStateData("fetch", "25665", firmwareSize, false) | ||
assert.Equal(t, "[========== ] 50.00%", bar) | ||
} | ||
|
||
func TestProgressBar_ifFlashState_goTo100Pct(t *testing.T) { | ||
firmwareSize := int64(25665 * 2) | ||
bar := formatStateData("fetch", "25665", firmwareSize, true) // If in flash status, go to 100% | ||
assert.Equal(t, "[====================] 100.00%", bar) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.