|
| 1 | +package thing |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + |
| 9 | + "errors" |
| 10 | + |
| 11 | + iotclient "github.com/arduino/iot-client-go" |
| 12 | + "github.com/arduino/iot-cloud-cli/internal/config" |
| 13 | + "github.com/arduino/iot-cloud-cli/internal/iot" |
| 14 | +) |
| 15 | + |
| 16 | +// CreateParams contains the parameters needed to create a new thing. |
| 17 | +type CreateParams struct { |
| 18 | + // Mandatory - contains the name of the thing |
| 19 | + Name string |
| 20 | + // Optional - contains the ID of the device to be bound to the thing |
| 21 | + DeviceID string |
| 22 | + // Mandatory if device is empty - contains the path of the template file |
| 23 | + Template string |
| 24 | + // Mandatory if template is empty- name of things to be cloned |
| 25 | + CloneID string |
| 26 | +} |
| 27 | + |
| 28 | +// Create allows to create a new thing |
| 29 | +func Create(params *CreateParams) (string, error) { |
| 30 | + if params.Template == "" && params.CloneID == "" { |
| 31 | + return "", fmt.Errorf("%s", "provide either a thing(ID) to clone (--clone) or a thing template file (--template)\n") |
| 32 | + } |
| 33 | + |
| 34 | + conf, err := config.Retrieve() |
| 35 | + if err != nil { |
| 36 | + return "", err |
| 37 | + } |
| 38 | + iotClient, err := iot.NewClient(conf.Client, conf.Secret) |
| 39 | + if err != nil { |
| 40 | + return "", err |
| 41 | + } |
| 42 | + |
| 43 | + var thing *iotclient.Thing |
| 44 | + |
| 45 | + if params.CloneID != "" { |
| 46 | + thing, err = clone(iotClient, params.CloneID) |
| 47 | + if err != nil { |
| 48 | + return "", err |
| 49 | + } |
| 50 | + |
| 51 | + } else if params.Template != "" { |
| 52 | + thing, err = loadTemplate(params.Template) |
| 53 | + if err != nil { |
| 54 | + return "", err |
| 55 | + } |
| 56 | + |
| 57 | + } else { |
| 58 | + return "", errors.New("provide either a thing(ID) to clone (--clone) or a thing template file (--template)") |
| 59 | + } |
| 60 | + |
| 61 | + thing.Name = params.Name |
| 62 | + force := true |
| 63 | + if params.DeviceID != "" { |
| 64 | + thing.DeviceId = params.DeviceID |
| 65 | + } |
| 66 | + thingID, err := iotClient.AddThing(thing, force) |
| 67 | + if err != nil { |
| 68 | + return "", err |
| 69 | + } |
| 70 | + |
| 71 | + return thingID, nil |
| 72 | +} |
| 73 | + |
| 74 | +func clone(client iot.Client, thingID string) (*iotclient.Thing, error) { |
| 75 | + clone, err := client.GetThing(thingID) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("%s: %w", "retrieving the thing to be cloned", err) |
| 78 | + } |
| 79 | + |
| 80 | + thing := &iotclient.Thing{} |
| 81 | + |
| 82 | + // Copy device id |
| 83 | + if clone.DeviceId != "" { |
| 84 | + thing.DeviceId = clone.DeviceId |
| 85 | + } |
| 86 | + |
| 87 | + // Copy properties |
| 88 | + for _, p := range clone.Properties { |
| 89 | + thing.Properties = append(thing.Properties, iotclient.Property{ |
| 90 | + Name: p.Name, |
| 91 | + MinValue: p.MinValue, |
| 92 | + MaxValue: p.MaxValue, |
| 93 | + Permission: p.Permission, |
| 94 | + UpdateParameter: p.UpdateParameter, |
| 95 | + UpdateStrategy: p.UpdateStrategy, |
| 96 | + Type: p.Type, |
| 97 | + VariableName: p.VariableName, |
| 98 | + Persist: p.Persist, |
| 99 | + Tag: p.Tag, |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + return thing, nil |
| 104 | +} |
| 105 | + |
| 106 | +func loadTemplate(file string) (*iotclient.Thing, error) { |
| 107 | + templateFile, err := os.Open(file) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + defer templateFile.Close() |
| 112 | + |
| 113 | + templateBytes, err := ioutil.ReadAll(templateFile) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + thing := &iotclient.Thing{} |
| 119 | + err = json.Unmarshal([]byte(templateBytes), thing) |
| 120 | + if err != nil { |
| 121 | + return nil, fmt.Errorf("%s: %w", "reading template file: template not valid: ", err) |
| 122 | + } |
| 123 | + |
| 124 | + return thing, nil |
| 125 | +} |
0 commit comments