|
| 1 | +// Copyright © 2024 The Things Network Foundation, The Things Industries B.V. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package wanesy |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/csv" |
| 19 | + "encoding/json" |
| 20 | + "net/http" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/spf13/pflag" |
| 25 | + |
| 26 | + "go.thethings.network/lorawan-stack-migrate/pkg/source" |
| 27 | + "go.thethings.network/lorawan-stack/v3/pkg/fetch" |
| 28 | + "go.thethings.network/lorawan-stack/v3/pkg/frequencyplans" |
| 29 | + "go.thethings.network/lorawan-stack/v3/pkg/ttnpb" |
| 30 | +) |
| 31 | + |
| 32 | +type Config struct { |
| 33 | + src source.Config |
| 34 | + |
| 35 | + appID string |
| 36 | + frequencyPlanID string |
| 37 | + csvPath string |
| 38 | + all bool |
| 39 | + |
| 40 | + derivedMacVersion ttnpb.MACVersion |
| 41 | + derivedPhyVersion ttnpb.PHYVersion |
| 42 | + |
| 43 | + flags *pflag.FlagSet |
| 44 | + fpStore *frequencyplans.Store |
| 45 | +} |
| 46 | + |
| 47 | +// NewConfig returns a new Wanesy configuration. |
| 48 | +func NewConfig() *Config { |
| 49 | + config := &Config{ |
| 50 | + flags: &pflag.FlagSet{}, |
| 51 | + } |
| 52 | + config.flags.StringVar(&config.frequencyPlanID, |
| 53 | + "frequency-plan-id", |
| 54 | + "", |
| 55 | + "Frequency Plan ID for the exported devices") |
| 56 | + config.flags.StringVar(&config.appID, |
| 57 | + "app-id", |
| 58 | + "", |
| 59 | + "Application ID for the exported devices") |
| 60 | + config.flags.StringVar(&config.csvPath, |
| 61 | + "csv-path", |
| 62 | + "", |
| 63 | + "Path to the CSV file exported from Wanesy Management Center") |
| 64 | + config.flags.BoolVar(&config.all, |
| 65 | + "all", |
| 66 | + false, |
| 67 | + "Export all devices in the CSV. This is only used by the application command") |
| 68 | + return config |
| 69 | +} |
| 70 | + |
| 71 | +// Initialize the configuration. |
| 72 | +func (c *Config) Initialize(src source.Config) error { |
| 73 | + c.src = src |
| 74 | + |
| 75 | + if c.appID = os.Getenv("APP_ID"); c.appID == "" { |
| 76 | + return errNoAppID.New() |
| 77 | + } |
| 78 | + if c.frequencyPlanID = os.Getenv("FREQUENCY_PLAN_ID"); c.frequencyPlanID == "" { |
| 79 | + return errNoFrequencyPlanID.New() |
| 80 | + } |
| 81 | + if c.csvPath = os.Getenv("CSV_PATH"); c.csvPath == "" { |
| 82 | + return errNoCSVFileProvided.New() |
| 83 | + } |
| 84 | + |
| 85 | + fpFetcher, err := fetch.FromHTTP(http.DefaultClient, src.FrequencyPlansURL) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + c.fpStore = frequencyplans.NewStore(fpFetcher) |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +// Flags returns the flags for the configuration. |
| 95 | +func (c *Config) Flags() *pflag.FlagSet { |
| 96 | + return c.flags |
| 97 | +} |
| 98 | + |
| 99 | +// ImportDevices imports the devices from the provided CSV file. |
| 100 | +func ImportDevices(csvPath string) (Devices, error) { |
| 101 | + raw, err := os.ReadFile(csvPath) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + reader := csv.NewReader(strings.NewReader(string(raw))) |
| 106 | + readValues, err := reader.ReadAll() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + if len(readValues) < 2 { |
| 111 | + return nil, errNoValuesInCSV.New() |
| 112 | + } |
| 113 | + values := make([]map[string]string, 0) |
| 114 | + for i := 1; i < len(readValues); i++ { |
| 115 | + keys := readValues[0] |
| 116 | + value := make(map[string]string) |
| 117 | + for j := 0; j < len(keys); j++ { |
| 118 | + noOfcolumns := len(readValues[i]) |
| 119 | + if j >= noOfcolumns { |
| 120 | + value[keys[j]] = "" // Fill empty columns. |
| 121 | + continue |
| 122 | + } |
| 123 | + value[keys[j]] = readValues[i][j] |
| 124 | + } |
| 125 | + values = append(values, value) |
| 126 | + } |
| 127 | + j, err := json.Marshal(values) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + devices := make(Devices) |
| 132 | + err = devices.UnmarshalJSON(j) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + return devices, nil |
| 137 | +} |
0 commit comments