github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/internal/template/dashboard.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 template
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"fmt"
    24  
    25  	iotclient "github.com/arduino/iot-client-go"
    26  )
    27  
    28  type dashboardTemplate struct {
    29  	Name    string           `json:"name,omitempty" yaml:"name,omitempty"`
    30  	Widgets []widgetTemplate `json:"widgets,omitempty" yaml:"widgets,omitempty"`
    31  }
    32  
    33  type widgetTemplate struct {
    34  	Height       int64                  `json:"height" yaml:"height"`
    35  	HeightMobile int64                  `json:"height_mobile,omitempty" yaml:"height_mobile,omitempty"`
    36  	Id           string                 `json:"id" yaml:"id"`
    37  	Name         string                 `json:"name,omitempty" yaml:"name,omitempty"`
    38  	Options      map[string]interface{} `json:"options" yaml:"options"`
    39  	Type         string                 `json:"type" yaml:"type"`
    40  	Variables    []variableTemplate     `json:"variables,omitempty" yaml:"variables,omitempty"`
    41  	Width        int64                  `json:"width" yaml:"width"`
    42  	WidthMobile  int64                  `json:"width_mobile,omitempty" yaml:"width_mobile,omitempty"`
    43  	X            int64                  `json:"x" yaml:"x"`
    44  	XMobile      int64                  `json:"x_mobile,omitempty" yaml:"x_mobile,omitempty"`
    45  	Y            int64                  `json:"y" yaml:"y"`
    46  	YMobile      int64                  `json:"y_mobile,omitempty" yaml:"y_mobile,omitempty"`
    47  }
    48  
    49  type variableTemplate struct {
    50  	ThingID      string `json:"thing_id" yaml:"thing_id"`
    51  	VariableName string `json:"variable_id" yaml:"variable_id"`
    52  	VariableID   string
    53  }
    54  
    55  // MarshalJSON satisfies the Marshaler interface from json package.
    56  // With this, when a variableTemplate is marshaled, it only marshals
    57  // its VariableID. In this way, a widgetTemplate can be
    58  // marshaled and then unmarshaled into a iot.Widget struct.
    59  // In the same way, a dashboardTemplate can now be converted
    60  // into a iot.DashboardV2 leveraging the JSON marshal/unmarshal.
    61  func (v *variableTemplate) MarshalJSON() ([]byte, error) {
    62  	return json.Marshal(v.VariableID)
    63  }
    64  
    65  // ThingFetcher wraps the method to fetch a thing given its id.
    66  type ThingFetcher interface {
    67  	ThingShow(ctx context.Context, id string) (*iotclient.ArduinoThing, error)
    68  }
    69  
    70  // getVariableID returns the id of a variable, given its name and its thing id.
    71  // If the variable is not found, an error is returned.
    72  func getVariableID(ctx context.Context, thingID string, variableName string, fetcher ThingFetcher) (string, error) {
    73  	thing, err := fetcher.ThingShow(ctx, thingID)
    74  	if err != nil {
    75  		return "", fmt.Errorf("getting variables of thing %s: %w", thingID, err)
    76  	}
    77  
    78  	for _, v := range thing.Properties {
    79  		if v.VariableName == variableName {
    80  			return v.Id, nil
    81  		}
    82  	}
    83  
    84  	return "", fmt.Errorf("thing with id %s doesn't have variable with name %s : %w", thingID, variableName, err)
    85  }