Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Started refactoring to move to client v2
  • Loading branch information
rjtokenring committed Jun 14, 2024
commit a78ee2609666d58bd88798a56555a75c5c61fcda
4 changes: 2 additions & 2 deletions command/dashboard/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)

// Name passed as parameter has priority over name from template
if params.Name != nil {
dashboard.Name = *params.Name
dashboard.Name = params.Name
}
// If name is not specified in the template, it should be passed as parameter
if dashboard.Name == "" {
if dashboard.Name == nil || *dashboard.Name == "" {
return nil, errors.New("dashboard name not specified")
}

Expand Down
4 changes: 3 additions & 1 deletion command/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ type DashboardInfo struct {
func getDashboardInfo(dashboard *iotclient.ArduinoDashboardv2) *DashboardInfo {
var widgets []string
for _, w := range dashboard.Widgets {
widgets = append(widgets, w.Name)
if w.Name != nil {
widgets = append(widgets, *w.Name)
}
}
info := &DashboardInfo{
Name: dashboard.Name,
Expand Down
9 changes: 8 additions & 1 deletion command/device/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)
ID: dev.Id,
Board: dev.Type,
Serial: dev.Serial,
FQBN: dev.Fqbn,
FQBN: dereferenceString(dev.Fqbn),
}
return devInfo, nil
}

func dereferenceString(s *string) string {
if s == nil {
return ""
}
return *s
}
4 changes: 2 additions & 2 deletions command/device/creategeneric.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ func CreateGeneric(ctx context.Context, params *CreateGenericParams, cred *confi
ID: dev.Id,
Board: dev.Type,
Serial: dev.Serial,
FQBN: dev.Fqbn,
FQBN: dereferenceString(dev.Fqbn),
},
Password: pass.SuggestedPassword,
Password: dereferenceString(pass.SuggestedPassword),
}
return devInfo, nil
}
2 changes: 1 addition & 1 deletion command/device/createlora.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func getDeviceLoraInfo(ctx context.Context, iotClient *iot.Client, loraDev *iotc
ID: dev.Id,
Board: dev.Type,
Serial: dev.Serial,
FQBN: dev.Fqbn,
FQBN: dereferenceString(dev.Fqbn),
},
AppEUI: loraDev.AppEui,
AppKey: loraDev.AppKey,
Expand Down
2 changes: 1 addition & 1 deletion command/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func getDeviceInfo(device *iotclient.ArduinoDevicev2) (*DeviceInfo, error) {
ID: device.Id,
Board: device.Type,
Serial: device.Serial,
FQBN: device.Fqbn,
FQBN: dereferenceString(device.Fqbn),
Tags: tags,
}
return dev, nil
Expand Down
2 changes: 1 addition & 1 deletion command/device/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (p provision) configBoard(ctx context.Context) error {
}

logrus.Info("Sending certificate authority key")
b, err = hex.DecodeString(cert.AuthorityKeyIdentifier)
b, err = hex.DecodeString(dereferenceString(cert.AuthorityKeyIdentifier))
if err != nil {
return fmt.Errorf("decoding certificate authority key id: %w", err)
}
Expand Down
7 changes: 7 additions & 0 deletions command/ota/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ func Generate(binFile string, outFile string, fqbn string) error {

return nil
}

func dereferenceString(s *string) string {
if s == nil {
return ""
}
return *s
}
7 changes: 4 additions & 3 deletions command/ota/massupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"context"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"os"
"path/filepath"

"github.com/sirupsen/logrus"

"github.com/arduino/arduino-cloud-cli/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
"github.com/arduino/arduino-cloud-cli/internal/ota"
Expand Down Expand Up @@ -179,8 +180,8 @@ func validateDevices(ctx context.Context, lister deviceLister, ids []string, fqb
continue
}
// Device FQBN doesn't match the passed one
if found.Fqbn != fqbn {
inv := Result{ID: id, Err: fmt.Errorf("has FQBN '%s' instead of '%s'", found.Fqbn, fqbn)}
if dereferenceString(found.Fqbn) != fqbn {
inv := Result{ID: id, Err: fmt.Errorf("has FQBN '%s' instead of '%s'", dereferenceString(found.Fqbn), fqbn)}
invalid = append(invalid, inv)
continue
}
Expand Down
6 changes: 3 additions & 3 deletions command/ota/massupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func TestValidateDevices(t *testing.T) {

mockDeviceList := deviceListerTest{
list: []iotclient.ArduinoDevicev2{
{Id: idCorrect1, Fqbn: correctFQBN},
{Id: idCorrect2, Fqbn: correctFQBN},
{Id: idNotValid, Fqbn: wrongFQBN},
{Id: idCorrect1, Fqbn: &correctFQBN},
{Id: idCorrect2, Fqbn: &correctFQBN},
{Id: idNotValid, Fqbn: &wrongFQBN},
},
}

Expand Down
2 changes: 1 addition & 1 deletion command/ota/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
otaFile = filepath.Join(otaDir, "temp.ota")
defer os.RemoveAll(otaDir)

err = Generate(params.File, otaFile, dev.Fqbn)
err = Generate(params.File, otaFile, dereferenceString(dev.Fqbn))
if err != nil {
return fmt.Errorf("%s: %w", "cannot generate .ota file", err)
}
Expand Down
9 changes: 8 additions & 1 deletion command/thing/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Bind(ctx context.Context, params *BindParams, cred *config.Credentials) err
}

thing := &iotclient.ThingUpdate{
DeviceId: params.DeviceID,
DeviceId: &params.DeviceID,
}

err = iotClient.ThingUpdate(ctx, params.ID, thing, true)
Expand All @@ -52,3 +52,10 @@ func Bind(ctx context.Context, params *BindParams, cred *config.Credentials) err

return nil
}

func dereferenceString(s *string) string {
if s == nil {
return ""
}
return *s
}
2 changes: 1 addition & 1 deletion command/thing/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Clone(ctx context.Context, params *CloneParams, cred *config.Credentials) (
return nil, err
}

thing.Name = params.Name
thing.Name = &params.Name
force := true
newThing, err := iotClient.ThingCreate(ctx, thing, force)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions command/thing/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)

// Name passed as parameter has priority over name from template
if params.Name != nil {
thing.Name = *params.Name
thing.Name = params.Name
}
// If name is not specified in the template, it should be passed as parameter
if thing.Name == "" {
if dereferenceString(thing.Name) == "" {
return nil, errors.New("thing name not specified")
}

Expand Down
2 changes: 1 addition & 1 deletion command/thing/thing.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func getThingInfo(thing *iotclient.ArduinoThing) (*ThingInfo, error) {
info := &ThingInfo{
Name: thing.Name,
ID: thing.Id,
DeviceID: thing.DeviceId,
DeviceID: dereferenceString(thing.DeviceId),
Variables: vars,
Tags: tags,
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ require (
github.com/arduino/arduino-cli v0.0.0-20221116144942-76251df9241a
github.com/arduino/go-paths-helper v1.7.0
github.com/arduino/go-win32-utils v1.0.0
github.com/arduino/iot-client-go v1.4.4
github.com/arduino/iot-client-go v1.4.5-0.20240416140423-fb8fda8a47af
github.com/gofrs/uuid v4.2.0+incompatible
github.com/google/go-cmp v0.6.0
github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6
github.com/icza/bitio v1.1.0
github.com/manifoldco/promptui v0.9.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.3.0
Expand Down Expand Up @@ -43,7 +44,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/icza/bitio v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ github.com/arduino/go-properties-orderedmap v1.7.1 h1:HQ9Pn/mk3+XyfrE39EEvaZwJkr
github.com/arduino/go-properties-orderedmap v1.7.1/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
github.com/arduino/go-win32-utils v1.0.0 h1:/cXB86sOJxOsCHP7sQmXGLkdValwJt56mIwOHYxgQjQ=
github.com/arduino/go-win32-utils v1.0.0/go.mod h1:0jqM7doGEAs6DaJCxxhLBUDS5OawrqF48HqXkcEie/Q=
github.com/arduino/iot-client-go v1.4.4 h1:FICCXD5uCZ0scGG6RioOlpZamDqgSD1l/fQlFwKR284=
github.com/arduino/iot-client-go v1.4.4/go.mod h1:gYvpMt7Qw+OSScTLyIlCnpbvy9y96ey/2zhB4w6FoK0=
github.com/arduino/iot-client-go v1.4.5-0.20240416140423-fb8fda8a47af h1:/dbu5I3q09kOePrWbBVMrKEPDM8KKUIGATQpwWc/wWo=
github.com/arduino/iot-client-go v1.4.5-0.20240416140423-fb8fda8a47af/go.mod h1:aKaQRceeYIeMpyFyEUGJj4/5WFETZD1CrarYGn4v5Nc=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-metrics v0.3.10/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
Expand Down Expand Up @@ -287,6 +287,7 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/icza/bitio v1.1.0 h1:ysX4vtldjdi3Ygai5m1cWy4oLkhWTAi+SyO6HC8L9T0=
github.com/icza/bitio v1.1.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k=
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
29 changes: 19 additions & 10 deletions internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func NewClient(cred *config.Credentials) (*Client, error) {
return cl, nil
}

func toStringPointer(s string) *string {
return &s
}

// DeviceCreate allows to create a new device on Arduino IoT Cloud.
// It returns the newly created device, and an error.
func (cl *Client) DeviceCreate(ctx context.Context, fqbn, name, serial, dType string, cType *string) (*iotclient.ArduinoDevicev2, error) {
Expand All @@ -57,22 +61,24 @@ func (cl *Client) DeviceCreate(ctx context.Context, fqbn, name, serial, dType st
}

payload := iotclient.CreateDevicesV2Payload{
Fqbn: fqbn,
Name: name,
Serial: serial,
Fqbn: toStringPointer(fqbn),
Name: toStringPointer(name),
Serial: toStringPointer(serial),
Type: dType,
}

if cType != nil {
payload.ConnectionType = *cType
payload.ConnectionType = cType
}

dev, _, err := cl.api.DevicesV2Api.DevicesV2Create(ctx, payload, nil)
req := cl.api.DevicesV2Api.DevicesV2Create(ctx)
req = req.CreateDevicesV2Payload(payload)
dev, _, err := cl.api.DevicesV2Api.DevicesV2CreateExecute(req)
if err != nil {
err = fmt.Errorf("creating device, %w", errorDetail(err))
return nil, err
}
return &dev, nil
return dev, nil
}

// DeviceLoraCreate allows to create a new LoRa device on Arduino IoT Cloud.
Expand All @@ -88,17 +94,19 @@ func (cl *Client) DeviceLoraCreate(ctx context.Context, name, serial, devType, e
Eui: eui,
FrequencyPlan: freq,
Name: name,
Serial: serial,
Serial: toStringPointer(serial),
Type: devType,
UserId: "me",
}

dev, _, err := cl.api.LoraDevicesV1Api.LoraDevicesV1Create(ctx, payload, nil)
req := cl.api.LoraDevicesV1Api.LoraDevicesV1Create(ctx)
req = req.CreateLoraDevicesV1Payload(payload)
dev, _, err := cl.api.LoraDevicesV1Api.LoraDevicesV1CreateExecute(req)
if err != nil {
err = fmt.Errorf("creating lora device: %w", errorDetail(err))
return nil, err
}
return &dev, nil
return dev, nil
}

// DevicePassSet sets the device password to the one suggested by Arduino IoT Cloud.
Expand Down Expand Up @@ -135,7 +143,8 @@ func (cl *Client) DeviceDelete(ctx context.Context, id string) error {
return err
}

_, err = cl.api.DevicesV2Api.DevicesV2Delete(ctx, id, nil)
req := cl.api.DevicesV2Api.DevicesV2Delete(ctx, id)
_, err = cl.api.DevicesV2Api.DevicesV2DeleteExecute(req)
if err != nil {
err = fmt.Errorf("deleting device: %w", errorDetail(err))
return err
Expand Down
9 changes: 8 additions & 1 deletion internal/template/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,17 @@ func getVariableID(ctx context.Context, thingID string, variableName string, fet
}

for _, v := range thing.Properties {
if v.VariableName == variableName {
if v.VariableName == &variableName {
return v.Id, nil
}
}

return "", fmt.Errorf("thing with id %s doesn't have variable with name %s : %w", thingID, variableName, err)
}

func dereferenceString(s *string) string {
if s == nil {
return ""
}
return *s
}
2 changes: 1 addition & 1 deletion internal/template/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func FromDashboard(dashboard *iotclient.ArduinoDashboardv2) map[string]interface
widget["x"] = w.X
widget["y"] = w.Y

if w.WidthMobile != 0 && w.HeightMobile != 0 {
if w.WidthMobile != nil && w.HeightMobile != nil && *w.WidthMobile != 0 && *w.HeightMobile != 0 {
widget["width_mobile"] = w.WidthMobile
widget["height_mobile"] = w.HeightMobile
widget["x_mobile"] = w.XMobile
Expand Down
Loading