Skip to content

Add device list-frequency-plans command #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ Use this command to provision a device:

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

####LoRa

The list of supported LoRa frequency plans can be retrieved with:

`$ arduino-cloud-cli device list-frequency-plans`

## Device commands

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:
Expand Down
1 change: 1 addition & 0 deletions cli/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewCommand() *cobra.Command {
deviceCommand.AddCommand(initDeleteCommand())
deviceCommand.AddCommand(tag.InitCreateTagsCommand())
deviceCommand.AddCommand(tag.InitDeleteTagsCommand())
deviceCommand.AddCommand(initListFrequencyPlansCommand())

return deviceCommand
}
75 changes: 75 additions & 0 deletions cli/device/listfrequency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package device

import (
"os"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/table"
"github.com/arduino/arduino-cloud-cli/command/device"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func initListFrequencyPlansCommand() *cobra.Command {
listCommand := &cobra.Command{
Use: "list-frequency-plans",
Short: "List LoRa frequency plans",
Long: "List all supported LoRa frequency plans",
Run: runListFrequencyPlansCommand,
}
return listCommand
}

func runListFrequencyPlansCommand(cmd *cobra.Command, args []string) {
logrus.Info("Listing supported frequency plans")

freqs, err := device.ListFrequencyPlans()
if err != nil {
feedback.Errorf("Error during device list-frequency-plans: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

feedback.PrintResult(listFrequencyPlansResult{freqs})
}

type listFrequencyPlansResult struct {
freqs []device.FrequencyPlanInfo
}

func (r listFrequencyPlansResult) Data() interface{} {
return r.freqs
}

func (r listFrequencyPlansResult) String() string {
if len(r.freqs) == 0 {
return "No frequency plan found."
}
t := table.New()
t.SetHeader("ID", "Name", "Advanced")
for _, freq := range r.freqs {
t.AddRow(
freq.ID,
freq.Name,
freq.Advanced,
)
}
return t.Render()
}
62 changes: 62 additions & 0 deletions command/device/listfrequency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package device

import (
"fmt"

"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
)

// FrequencyPlanInfo describes a LoRa frequency plan.
type FrequencyPlanInfo struct {
Name string `json:"name"`
ID string `json:"id"`
Advanced string `json:"advanced"`
}

// ListFrequencyPlans command is used to list
// the supported LoRa frequency plans.
func ListFrequencyPlans() ([]FrequencyPlanInfo, error) {
conf, err := config.Retrieve()
if err != nil {
return nil, err
}
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
if err != nil {
return nil, err
}

foundFreqs, err := iotClient.LoraFrequencyPlansList()
if err != nil {
return nil, err
}

freqs := make([]FrequencyPlanInfo, 0, len(foundFreqs))
for _, f := range foundFreqs {
freq := FrequencyPlanInfo{
Name: f.Name,
ID: f.Id,
Advanced: fmt.Sprintf("%v",f.Advanced),
}
freqs = append(freqs, freq)
}

return freqs, nil
}
12 changes: 12 additions & 0 deletions internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Client interface {
DeviceOTA(id string, file *os.File, expireMins int) error
DeviceTagsCreate(id string, tags map[string]string) error
DeviceTagsDelete(id string, keys []string) error
LoraFrequencyPlansList() ([]iotclient.ArduinoLorafreqplanv1, error)
CertificateCreate(id, csr string) (*iotclient.ArduinoCompressedv2, error)
ThingCreate(thing *iotclient.ThingCreate, force bool) (*iotclient.ArduinoThing, error)
ThingUpdate(id string, thing *iotclient.ThingUpdate, force bool) error
Expand Down Expand Up @@ -166,6 +167,17 @@ func (cl *client) DeviceTagsDelete(id string, keys []string) error {
return nil
}

// LoraFrequencyPlansList retrieves and returns the list of all supported
// LoRa frequency plans.
func (cl *client) LoraFrequencyPlansList() ([]iotclient.ArduinoLorafreqplanv1, error) {
freqs, _, err := cl.api.LoraFreqPlanV1Api.LoraFreqPlanV1List(cl.ctx)
if err != nil {
err = fmt.Errorf("listing lora frequency plans: %w", errorDetail(err))
return nil, err
}
return freqs.FrequencyPlans, nil
}

// CertificateCreate allows to upload a certificate on Arduino IoT Cloud.
// It returns the certificate parameters populated by the cloud.
func (cl *client) CertificateCreate(id, csr string) (*iotclient.ArduinoCompressedv2, error) {
Expand Down
23 changes: 23 additions & 0 deletions internal/iot/mocks/Client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.