github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/command/device/creategeneric.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  
    24  	"github.com/arduino/arduino-cloud-cli/config"
    25  	"github.com/arduino/arduino-cloud-cli/internal/iot"
    26  )
    27  
    28  const (
    29  	genericDType = "login_and_secretkey_wifi"
    30  )
    31  
    32  // CreateGenericParams contains the parameters needed
    33  // to create a new generic device.
    34  type CreateGenericParams struct {
    35  	Name string // Device name
    36  	FQBN string // Board FQBN
    37  }
    38  
    39  // DeviceGenericInfo contains the most interesting
    40  // parameters of a generic Arduino IoT Cloud device.
    41  type DeviceGenericInfo struct {
    42  	DeviceInfo
    43  	Password string `json:"secret_key"`
    44  }
    45  
    46  // CreateGeneric command is used to add a new generic device to Arduino IoT Cloud.
    47  func CreateGeneric(ctx context.Context, params *CreateGenericParams, cred *config.Credentials) (*DeviceGenericInfo, error) {
    48  	iotClient, err := iot.NewClient(cred)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	dev, err := iotClient.DeviceCreate(ctx, params.FQBN, params.Name, "", genericDType, nil)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	pass, err := iotClient.DevicePassSet(ctx, dev.Id)
    59  	if err != nil {
    60  		// Don't use the passed context for the cleanup because it could be cancelled.
    61  		if errDel := iotClient.DeviceDelete(context.Background(), dev.Id); errDel != nil {
    62  			return nil, fmt.Errorf(
    63  				"device was successfully created on IoT-API but " +
    64  					"now we can't set its secret key nor delete it - please check " +
    65  					"it on the web application.\n\nFetch error: " + err.Error() +
    66  					"\nDeletion error: " + errDel.Error(),
    67  			)
    68  		}
    69  		return nil, fmt.Errorf("cannot create generic device: %w", err)
    70  	}
    71  
    72  	devInfo := &DeviceGenericInfo{
    73  		DeviceInfo: DeviceInfo{
    74  			Name:   dev.Name,
    75  			ID:     dev.Id,
    76  			Board:  dev.Type,
    77  			Serial: dev.Serial,
    78  			FQBN:   dev.Fqbn,
    79  		},
    80  		Password: pass.SuggestedPassword,
    81  	}
    82  	return devInfo, nil
    83  }