Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e5c6d9e

Browse files
committedNov 12, 2021
Validate devices given the fqbn
1 parent 0f5c408 commit e5c6d9e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
 

‎command/ota/upload.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,38 @@ func idsGivenTags(iotClient iot.Client, tags map[string]string) ([]string, error
115115
return devices, nil
116116
}
117117

118-
func run(iotClient iot.Client, ids []string, file *os.File, expiration int) error {
118+
func validateDevices(iotClient iot.Client, ids []string, fqbn string) (valid, invalid, details []string, err error) {
119+
devs, err := iotClient.DeviceList(nil)
120+
if err != nil {
121+
return nil, nil, nil, fmt.Errorf("%s: %w", "cannot retrieve devices from cloud", err)
122+
}
123+
124+
for _, id := range ids {
125+
var found *iotclient.ArduinoDevicev2
126+
for _, d := range devs {
127+
if d.Id == id {
128+
found = &d
129+
break
130+
}
131+
}
132+
// Device not found on the cloud
133+
if found == nil {
134+
invalid = append(invalid, id)
135+
details = append(details, fmt.Sprintf("%s : not found", id))
136+
continue
137+
}
138+
// Device FQBN doesn't match the passed one
139+
if found.Fqbn != fqbn {
140+
invalid = append(invalid, id)
141+
details = append(details, fmt.Sprintf("%s : has FQBN `%s` instead of `%s`", found.Id, found.Fqbn, fqbn))
142+
continue
143+
}
144+
valid = append(valid, id)
145+
}
146+
return valid, invalid, details, nil
147+
}
148+
149+
func run(iotClient iot.Client, ids []string, file *os.File, expiration int) (updated, failed, errors []string) {
119150
targets := make(chan string, len(ids))
120151
type result struct {
121152
id string

‎command/ota/upload_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,27 @@ func TestRun(t *testing.T) {
3333
fmt.Println(len(failed), failed)
3434
t.Error("two updates should have failed")
3535
}
36+
if len(good) != 3 {
37+
t.Error("two updates should have succeded")
38+
}
39+
}
40+
41+
func TestValidateDevices(t *testing.T) {
42+
mockClient := &mocks.Client{}
43+
mockDeviceList := func(tags map[string]string) []iotclient.ArduinoDevicev2 {
44+
return []iotclient.ArduinoDevicev2{
45+
{Id: "xxxx", Fqbn: "samd"},
46+
{Id: "yyyy", Fqbn: "samd"},
47+
{Id: "zzzz", Fqbn: "avr"},
48+
}
49+
}
50+
mockClient.On("DeviceList", mock.Anything).Return(mockDeviceList, nil)
51+
52+
ids := []string{
53+
"xxxx",
54+
"aaaa",
55+
"zzzz",
56+
}
57+
v, i, d, err := validateDevices(mockClient, ids, "samd")
58+
fmt.Println("valid: ", v, "inv: ", i, "det: ", d, "err: ", err)
3659
}

0 commit comments

Comments
 (0)
Please sign in to comment.