Skip to content

Commit cf5aaa4

Browse files
authored
Add thing bind command (#26)
Bind command is used to bind a thing to a device: $ iot-cloud-cli thing bind --id <thingID> --device-id <deviceID> * Add thing bind command * fix thing bind - improve flags * Update readme
1 parent e61334b commit cf5aaa4

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ Delete a thing with the following command:
7575
Extract a template from an existing thing:
7676

7777
`$ iot-cloud-cli thing extract --id <thingID> --outfile <templateFile.json>`
78+
79+
Bind a thing to an existing device:
80+
81+
`$ iot-cloud-cli thing bind --id <thingID> --device-id <deviceID>`

cli/thing/bind.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package thing
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/arduino/iot-cloud-cli/command/thing"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var bindFlags struct {
11+
id string
12+
deviceID string
13+
}
14+
15+
func initBindCommand() *cobra.Command {
16+
bindCommand := &cobra.Command{
17+
Use: "bind",
18+
Short: "Bind a thing to a device",
19+
Long: "Bind a thing to a device on Arduino IoT Cloud",
20+
RunE: runBindCommand,
21+
}
22+
bindCommand.Flags().StringVarP(&bindFlags.id, "id", "i", "", "Thing ID")
23+
bindCommand.Flags().StringVarP(&bindFlags.deviceID, "device-id", "d", "", "Device ID")
24+
bindCommand.MarkFlagRequired("id")
25+
bindCommand.MarkFlagRequired("device-id")
26+
return bindCommand
27+
}
28+
29+
func runBindCommand(cmd *cobra.Command, args []string) error {
30+
fmt.Printf("Binding thing %s to device%s\n", bindFlags.id, bindFlags.deviceID)
31+
32+
params := &thing.BindParams{
33+
ID: bindFlags.id,
34+
DeviceID: bindFlags.deviceID,
35+
}
36+
err := thing.Bind(params)
37+
if err != nil {
38+
return err
39+
}
40+
41+
fmt.Println("Thing-Device bound successfully updated")
42+
return nil
43+
}

cli/thing/thing.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func NewCommand() *cobra.Command {
1515
thingCommand.AddCommand(initListCommand())
1616
thingCommand.AddCommand(initDeleteCommand())
1717
thingCommand.AddCommand(initExtractCommand())
18+
thingCommand.AddCommand(initBindCommand())
1819

1920
return thingCommand
2021
}

command/thing/bind.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package thing
2+
3+
import (
4+
iotclient "github.com/arduino/iot-client-go"
5+
"github.com/arduino/iot-cloud-cli/internal/config"
6+
"github.com/arduino/iot-cloud-cli/internal/iot"
7+
)
8+
9+
// BindParams contains the parameters needed to
10+
// bind a thing to a device.
11+
// ID indicates the thing to bind.
12+
// deviceID indicates the device bind.
13+
type BindParams struct {
14+
ID string
15+
DeviceID string
16+
}
17+
18+
// Bind command is used to bind a thing to a device
19+
// on Arduino IoT Cloud.
20+
func Bind(params *BindParams) error {
21+
conf, err := config.Retrieve()
22+
if err != nil {
23+
return err
24+
}
25+
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
26+
if err != nil {
27+
return err
28+
}
29+
30+
thing := &iotclient.Thing{
31+
DeviceId: params.DeviceID,
32+
}
33+
34+
err = iotClient.UpdateThing(params.ID, thing, true)
35+
if err != nil {
36+
return err
37+
}
38+
39+
return nil
40+
}

internal/iot/client.go

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Client interface {
1616
ListDevices() ([]iotclient.ArduinoDevicev2, error)
1717
AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2, error)
1818
AddThing(thing *iotclient.Thing, force bool) (string, error)
19+
UpdateThing(id string, thing *iotclient.Thing, force bool) error
1920
DeleteThing(id string) error
2021
GetThing(id string) (*iotclient.ArduinoThing, error)
2122
ListThings(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error)
@@ -108,6 +109,16 @@ func (cl *client) AddThing(thing *iotclient.Thing, force bool) (string, error) {
108109
return newThing.Id, nil
109110
}
110111

112+
// AddThing updates a thing on Arduino IoT Cloud.
113+
func (cl *client) UpdateThing(id string, thing *iotclient.Thing, force bool) error {
114+
opt := &iotclient.ThingsV2UpdateOpts{Force: optional.NewBool(force)}
115+
_, _, err := cl.api.ThingsV2Api.ThingsV2Update(cl.ctx, id, *thing, opt)
116+
if err != nil {
117+
return fmt.Errorf("%s: %v", "updating thing", err)
118+
}
119+
return nil
120+
}
121+
111122
// DeleteThing deletes a thing from Arduino IoT Cloud.
112123
func (cl *client) DeleteThing(id string) error {
113124
_, err := cl.api.ThingsV2Api.ThingsV2Delete(cl.ctx, id, nil)

0 commit comments

Comments
 (0)