Skip to content

Commit 270e126

Browse files
Paolo Calaopolldo
Paolo Calao
authored andcommitted
Add device list-frequency-plans command (#63)
New command to list all the supported LoRa frequency plans has been introduced.
1 parent 79caebc commit 270e126

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ Use this command to provision a device:
6363

6464
`$ arduino-cloud-cli device create --name <deviceName> --port <port> --fqbn <deviceFqbn>`
6565

66+
####LoRa
67+
68+
The list of supported LoRa frequency plans can be retrieved with:
69+
70+
`$ arduino-cloud-cli device list-frequency-plans`
71+
6672
## Device commands
6773

6874
Devices can be deleted using the device delete command. This command accepts two mutually exclusive flags: `--id` and `--tags`. Only one of them must be passed. When the `--id` is passed, the device having such ID gets deleted:

cli/device/device.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func NewCommand() *cobra.Command {
3434
deviceCommand.AddCommand(initDeleteCommand())
3535
deviceCommand.AddCommand(tag.InitCreateTagsCommand())
3636
deviceCommand.AddCommand(tag.InitDeleteTagsCommand())
37+
deviceCommand.AddCommand(initListFrequencyPlansCommand())
3738

3839
return deviceCommand
3940
}

cli/device/listfrequency.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cli/table"
26+
"github.com/arduino/arduino-cloud-cli/command/device"
27+
"github.com/sirupsen/logrus"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func initListFrequencyPlansCommand() *cobra.Command {
32+
listCommand := &cobra.Command{
33+
Use: "list-frequency-plans",
34+
Short: "List LoRa frequency plans",
35+
Long: "List all supported LoRa frequency plans",
36+
Run: runListFrequencyPlansCommand,
37+
}
38+
return listCommand
39+
}
40+
41+
func runListFrequencyPlansCommand(cmd *cobra.Command, args []string) {
42+
logrus.Info("Listing supported frequency plans")
43+
44+
freqs, err := device.ListFrequencyPlans()
45+
if err != nil {
46+
feedback.Errorf("Error during device list-frequency-plans: %v", err)
47+
os.Exit(errorcodes.ErrGeneric)
48+
}
49+
50+
feedback.PrintResult(listFrequencyPlansResult{freqs})
51+
}
52+
53+
type listFrequencyPlansResult struct {
54+
freqs []device.FrequencyPlanInfo
55+
}
56+
57+
func (r listFrequencyPlansResult) Data() interface{} {
58+
return r.freqs
59+
}
60+
61+
func (r listFrequencyPlansResult) String() string {
62+
if len(r.freqs) == 0 {
63+
return "No frequency plan found."
64+
}
65+
t := table.New()
66+
t.SetHeader("ID", "Name", "Advanced")
67+
for _, freq := range r.freqs {
68+
t.AddRow(
69+
freq.ID,
70+
freq.Name,
71+
freq.Advanced,
72+
)
73+
}
74+
return t.Render()
75+
}

command/device/listfrequency.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package device
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/arduino/arduino-cloud-cli/internal/config"
24+
"github.com/arduino/arduino-cloud-cli/internal/iot"
25+
)
26+
27+
// FrequencyPlanInfo describes a LoRa frequency plan.
28+
type FrequencyPlanInfo struct {
29+
Name string `json:"name"`
30+
ID string `json:"id"`
31+
Advanced string `json:"advanced"`
32+
}
33+
34+
// ListFrequencyPlans command is used to list
35+
// the supported LoRa frequency plans.
36+
func ListFrequencyPlans() ([]FrequencyPlanInfo, error) {
37+
conf, err := config.Retrieve()
38+
if err != nil {
39+
return nil, err
40+
}
41+
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
foundFreqs, err := iotClient.LoraFrequencyPlansList()
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
freqs := make([]FrequencyPlanInfo, 0, len(foundFreqs))
52+
for _, f := range foundFreqs {
53+
freq := FrequencyPlanInfo{
54+
Name: f.Name,
55+
ID: f.Id,
56+
Advanced: fmt.Sprintf("%v",f.Advanced),
57+
}
58+
freqs = append(freqs, freq)
59+
}
60+
61+
return freqs, nil
62+
}

internal/iot/client.go

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Client interface {
3535
DeviceOTA(id string, file *os.File, expireMins int) error
3636
DeviceTagsCreate(id string, tags map[string]string) error
3737
DeviceTagsDelete(id string, keys []string) error
38+
LoraFrequencyPlansList() ([]iotclient.ArduinoLorafreqplanv1, error)
3839
CertificateCreate(id, csr string) (*iotclient.ArduinoCompressedv2, error)
3940
ThingCreate(thing *iotclient.ThingCreate, force bool) (*iotclient.ArduinoThing, error)
4041
ThingUpdate(id string, thing *iotclient.ThingUpdate, force bool) error
@@ -166,6 +167,17 @@ func (cl *client) DeviceTagsDelete(id string, keys []string) error {
166167
return nil
167168
}
168169

170+
// LoraFrequencyPlansList retrieves and returns the list of all supported
171+
// LoRa frequency plans.
172+
func (cl *client) LoraFrequencyPlansList() ([]iotclient.ArduinoLorafreqplanv1, error) {
173+
freqs, _, err := cl.api.LoraFreqPlanV1Api.LoraFreqPlanV1List(cl.ctx)
174+
if err != nil {
175+
err = fmt.Errorf("listing lora frequency plans: %w", errorDetail(err))
176+
return nil, err
177+
}
178+
return freqs.FrequencyPlans, nil
179+
}
180+
169181
// CertificateCreate allows to upload a certificate on Arduino IoT Cloud.
170182
// It returns the certificate parameters populated by the cloud.
171183
func (cl *client) CertificateCreate(id, csr string) (*iotclient.ArduinoCompressedv2, error) {

internal/iot/mocks/Client.go

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)