Skip to content

Commit b58f5c6

Browse files
committed
Update the go client and manage connection parameter for device create
1 parent b50f460 commit b58f5c6

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

cli/device/create.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ import (
3232
)
3333

3434
type createFlags struct {
35-
port string
36-
name string
37-
fqbn string
35+
port string
36+
name string
37+
fqbn string
38+
ctype string
3839
}
3940

4041
func initCreateCommand() *cobra.Command {
@@ -53,6 +54,7 @@ func initCreateCommand() *cobra.Command {
5354
createCommand.Flags().StringVarP(&flags.port, "port", "p", "", "Device port")
5455
createCommand.Flags().StringVarP(&flags.name, "name", "n", "", "Device name")
5556
createCommand.Flags().StringVarP(&flags.fqbn, "fqbn", "b", "", "Device fqbn")
57+
createCommand.Flags().StringVarP(&flags.ctype, "connection", "c", "", "Device connection type")
5658
createCommand.MarkFlagRequired("name")
5759
return createCommand
5860
}
@@ -68,6 +70,9 @@ func runCreateCommand(flags *createFlags) error {
6870
params := &device.CreateParams{
6971
Name: flags.name,
7072
}
73+
if flags.ctype != "" {
74+
params.ConnectionType = &flags.ctype
75+
}
7176
if flags.port != "" {
7277
params.Port = &flags.port
7378
}

command/device/create.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ import (
3131
// CreateParams contains the parameters needed
3232
// to find the device to be provisioned.
3333
type CreateParams struct {
34-
Name string // Device name
35-
Port *string // Serial port - Optional - If omitted then each serial port is analyzed
36-
FQBN *string // Board FQBN - Optional - If omitted then the first device found gets selected
34+
Name string // Device name
35+
Port *string // Serial port - Optional - If omitted then each serial port is analyzed
36+
FQBN *string // Board FQBN - Optional - If omitted then the first device found gets selected
37+
ConnectionType *string // Connection type - Optional - If omitted then the default connection type (depends on the board type) get selected
3738
}
3839

3940
// Create command is used to provision a new arduino device
@@ -70,7 +71,7 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)
7071
}
7172

7273
logrus.Info("Creating a new device on the cloud")
73-
dev, err := iotClient.DeviceCreate(ctx, board.fqbn, params.Name, board.serial, board.dType)
74+
dev, err := iotClient.DeviceCreate(ctx, board.fqbn, params.Name, board.serial, board.dType, params.ConnectionType)
7475
if err != nil {
7576
return nil, err
7677
}

command/device/creategeneric.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func CreateGeneric(ctx context.Context, params *CreateGenericParams, cred *confi
5050
return nil, err
5151
}
5252

53-
dev, err := iotClient.DeviceCreate(ctx, params.FQBN, params.Name, "", genericDType)
53+
dev, err := iotClient.DeviceCreate(ctx, params.FQBN, params.Name, "", genericDType, nil)
5454
if err != nil {
5555
return nil, err
5656
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/arduino/board-discovery v0.0.0-20211020061712-fd83c2e3c908 // indirect
1010
github.com/arduino/go-paths-helper v1.7.0
1111
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b
12-
github.com/arduino/iot-client-go v1.4.2
12+
github.com/arduino/iot-client-go v1.4.4
1313
github.com/gofrs/uuid v4.2.0+incompatible
1414
github.com/google/go-cmp v0.5.6
1515
github.com/h2non/filetype v1.1.3 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b h1:3PjgYG5g
8080
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b/go.mod h1:iIPnclBMYm1g32Q5kXoqng4jLhMStReIP7ZxaoUC2y8=
8181
github.com/arduino/iot-client-go v1.4.2 h1:uZC8A26ytxSkuxNBFL2b/4w0TYjsR3ny2BbB5aVe4kw=
8282
github.com/arduino/iot-client-go v1.4.2/go.mod h1:gYvpMt7Qw+OSScTLyIlCnpbvy9y96ey/2zhB4w6FoK0=
83+
github.com/arduino/iot-client-go v1.4.4 h1:FICCXD5uCZ0scGG6RioOlpZamDqgSD1l/fQlFwKR284=
84+
github.com/arduino/iot-client-go v1.4.4/go.mod h1:gYvpMt7Qw+OSScTLyIlCnpbvy9y96ey/2zhB4w6FoK0=
8385
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
8486
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
8587
github.com/armon/go-metrics v0.3.10/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=

internal/iot/client.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewClient(cred *config.Credentials) (*Client, error) {
4848

4949
// DeviceCreate allows to create a new device on Arduino IoT Cloud.
5050
// It returns the newly created device, and an error.
51-
func (cl *Client) DeviceCreate(ctx context.Context, fqbn, name, serial, dType string) (*iotclient.ArduinoDevicev2, error) {
51+
func (cl *Client) DeviceCreate(ctx context.Context, fqbn, name, serial, dType string, cType *string) (*iotclient.ArduinoDevicev2, error) {
5252
ctx, err := ctxWithToken(ctx, cl.token)
5353
if err != nil {
5454
return nil, err
@@ -60,6 +60,11 @@ func (cl *Client) DeviceCreate(ctx context.Context, fqbn, name, serial, dType st
6060
Serial: serial,
6161
Type: dType,
6262
}
63+
64+
if cType != nil {
65+
payload.ConnectionType = *cType
66+
}
67+
6368
dev, _, err := cl.api.DevicesV2Api.DevicesV2Create(ctx, payload, nil)
6469
if err != nil {
6570
err = fmt.Errorf("creating device, %w", errorDetail(err))
@@ -85,7 +90,8 @@ func (cl *Client) DeviceLoraCreate(ctx context.Context, name, serial, devType, e
8590
Type: devType,
8691
UserId: "me",
8792
}
88-
dev, _, err := cl.api.LoraDevicesV1Api.LoraDevicesV1Create(ctx, payload)
93+
94+
dev, _, err := cl.api.LoraDevicesV1Api.LoraDevicesV1Create(ctx, payload, nil)
8995
if err != nil {
9096
err = fmt.Errorf("creating lora device: %w", errorDetail(err))
9197
return nil, err

0 commit comments

Comments
 (0)