github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/device/createlora.go (about)

     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  	"context"
    22  	"fmt"
    23  	"os"
    24  
    25  	"github.com/arduino/arduino-cli/cli/errorcodes"
    26  	"github.com/arduino/arduino-cli/cli/feedback"
    27  	"github.com/arduino/arduino-cloud-cli/command/device"
    28  	"github.com/arduino/arduino-cloud-cli/config"
    29  	"github.com/sirupsen/logrus"
    30  	"github.com/spf13/cobra"
    31  	"go.bug.st/cleanup"
    32  )
    33  
    34  type createLoraFlags struct {
    35  	port          string
    36  	name          string
    37  	fqbn          string
    38  	frequencyPlan string
    39  }
    40  
    41  func initCreateLoraCommand() *cobra.Command {
    42  	flags := &createLoraFlags{}
    43  	createLoraCommand := &cobra.Command{
    44  		Use:   "create-lora",
    45  		Short: "Create a LoRa device",
    46  		Long:  "Create a LoRa device for Arduino IoT Cloud",
    47  		Run: func(cmd *cobra.Command, args []string) {
    48  			if err := runCreateLoraCommand(flags); err != nil {
    49  				feedback.Errorf("Error during device create-lora: %v", err)
    50  				os.Exit(errorcodes.ErrGeneric)
    51  			}
    52  		},
    53  	}
    54  	createLoraCommand.Flags().StringVarP(&flags.port, "port", "p", "", "Device port")
    55  	createLoraCommand.Flags().StringVarP(&flags.name, "name", "n", "", "Device name")
    56  	createLoraCommand.Flags().StringVarP(&flags.fqbn, "fqbn", "b", "", "Device fqbn")
    57  	createLoraCommand.Flags().StringVarP(&flags.frequencyPlan, "frequency-plan", "f", "",
    58  		"ID of the LoRa frequency plan to use. Run the 'device list-frequency-plans' command to obtain a list of valid plans.")
    59  	createLoraCommand.MarkFlagRequired("name")
    60  	createLoraCommand.MarkFlagRequired("frequency-plan")
    61  	return createLoraCommand
    62  }
    63  
    64  func runCreateLoraCommand(flags *createLoraFlags) error {
    65  	logrus.Infof("Creating LoRa device with name %s", flags.name)
    66  
    67  	cred, err := config.RetrieveCredentials()
    68  	if err != nil {
    69  		return fmt.Errorf("retrieving credentials: %w", err)
    70  	}
    71  
    72  	params := &device.CreateLoraParams{
    73  		CreateParams: device.CreateParams{
    74  			Name: flags.name,
    75  		},
    76  		FrequencyPlan: flags.frequencyPlan,
    77  	}
    78  	if flags.port != "" {
    79  		params.Port = &flags.port
    80  	}
    81  	if flags.fqbn != "" {
    82  		params.FQBN = &flags.fqbn
    83  	}
    84  
    85  	ctx, cancel := cleanup.InterruptableContext(context.Background())
    86  	defer cancel()
    87  
    88  	dev, err := device.CreateLora(ctx, params, cred)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	feedback.PrintResult(createLoraResult{dev})
    94  	return nil
    95  }
    96  
    97  type createLoraResult struct {
    98  	device *device.DeviceLoraInfo
    99  }
   100  
   101  func (r createLoraResult) Data() interface{} {
   102  	return r.device
   103  }
   104  
   105  func (r createLoraResult) String() string {
   106  	return fmt.Sprintf(
   107  		"name: %s\nid: %s\nboard: %s\nserial_number: %s\nfqbn: %s"+
   108  			"\napp_eui: %s\napp_key: %s\neui: %s",
   109  		r.device.Name,
   110  		r.device.ID,
   111  		r.device.Board,
   112  		r.device.Serial,
   113  		r.device.FQBN,
   114  		r.device.AppEUI,
   115  		r.device.AppKey,
   116  		r.device.EUI,
   117  	)
   118  }