|
| 1 | +package thing |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + |
| 9 | + iotclient "github.com/arduino/iot-client-go" |
| 10 | + "github.com/arduino/iot-cloud-cli/internal/config" |
| 11 | + "github.com/arduino/iot-cloud-cli/internal/iot" |
| 12 | +) |
| 13 | + |
| 14 | +// ExtractParams contains the parameters needed to |
| 15 | +// extract a thing from Arduino IoT Cloud and save it on local storage. |
| 16 | +// Output indicates the destination path of the extraction. |
| 17 | +type ExtractParams struct { |
| 18 | + ID string |
| 19 | + Outfile *string |
| 20 | +} |
| 21 | + |
| 22 | +// Extract command is used to extract a thing template |
| 23 | +// from a thing on Arduino IoT Cloud. |
| 24 | +func Extract(params *ExtractParams) error { |
| 25 | + conf, err := config.Retrieve() |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + iotClient, err := iot.NewClient(conf.Client, conf.Secret) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + thing, err := iotClient.GetThing(params.ID) |
| 35 | + if err != nil { |
| 36 | + err = fmt.Errorf("%s: %w", "cannot extract thing: ", err) |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + template, err := templateFromThing(thing) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + if params.Outfile == nil { |
| 46 | + outfile := thing.Name + "-template.json" |
| 47 | + params.Outfile = &outfile |
| 48 | + } |
| 49 | + err = ioutil.WriteFile(*params.Outfile, template, os.FileMode(0644)) |
| 50 | + if err != nil { |
| 51 | + err = fmt.Errorf("%s: %w", "cannot write outfile: ", err) |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func templateFromThing(thing *iotclient.ArduinoThing) ([]byte, error) { |
| 59 | + template := make(map[string]interface{}) |
| 60 | + template["properties_count"] = thing.PropertiesCount |
| 61 | + |
| 62 | + var props []map[string]interface{} |
| 63 | + for _, p := range thing.Properties { |
| 64 | + prop := make(map[string]interface{}) |
| 65 | + prop["max_value"] = p.MaxValue |
| 66 | + prop["min_value"] = p.MinValue |
| 67 | + prop["name"] = p.Name |
| 68 | + prop["permission"] = p.Permission |
| 69 | + prop["persist"] = p.Persist |
| 70 | + prop["tag"] = p.Tag |
| 71 | + prop["type"] = p.Type |
| 72 | + prop["update_parameter"] = p.UpdateParameter |
| 73 | + prop["update_strategy"] = p.UpdateStrategy |
| 74 | + prop["variable_name"] = p.VariableName |
| 75 | + props = append(props, prop) |
| 76 | + } |
| 77 | + template["properties"] = props |
| 78 | + |
| 79 | + // Extract json template from thing structure |
| 80 | + file, err := json.MarshalIndent(template, "", " ") |
| 81 | + if err != nil { |
| 82 | + err = fmt.Errorf("%s: %w", "thing marshal failure: ", err) |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + return file, nil |
| 86 | +} |
0 commit comments