-
Notifications
You must be signed in to change notification settings - Fork 3
Support Migration from Wanesy #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
69ceec6
util: Add wanesy boilerplate
KrishnaIyer ed4e547
util: Complete device
KrishnaIyer 777a52e
util: Export all from CSV
KrishnaIyer d682e70
util: Require JoinEUI only for OTAA
KrishnaIyer 25d3d8e
util: Update README.md
KrishnaIyer d2be08a
dev: Finalize parsing of fields
KrishnaIyer 0a9a67f
dev: Add unit tests
KrishnaIyer 7a17539
dev: Add changelog
KrishnaIyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ | |
# Build files | ||
/build/ | ||
/dist/ | ||
|
||
# macOS related | ||
*.DS_store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright © 2023 The Things Network Foundation, The Things Industries B.V. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package wanesy | ||
|
||
import ( | ||
"go.thethings.network/lorawan-stack-migrate/pkg/commands" | ||
_ "go.thethings.network/lorawan-stack-migrate/pkg/source/wanesy" | ||
) | ||
|
||
const sourceName = "wanesy" | ||
|
||
// WanesyCmd represents the Wanesy source. | ||
var WanesyCmd = commands.Source(sourceName, | ||
"Migrate from Wanesy Management Center", | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright © 2024 The Things Network Foundation, The Things Industries B.V. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package wanesy | ||
|
||
import ( | ||
"encoding/csv" | ||
"encoding/json" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"go.thethings.network/lorawan-stack-migrate/pkg/source" | ||
"go.thethings.network/lorawan-stack/v3/pkg/fetch" | ||
"go.thethings.network/lorawan-stack/v3/pkg/frequencyplans" | ||
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb" | ||
) | ||
|
||
type Config struct { | ||
src source.Config | ||
|
||
appID string | ||
frequencyPlanID string | ||
csvPath string | ||
all bool | ||
|
||
derivedMacVersion ttnpb.MACVersion | ||
derivedPhyVersion ttnpb.PHYVersion | ||
|
||
flags *pflag.FlagSet | ||
fpStore *frequencyplans.Store | ||
} | ||
|
||
// NewConfig returns a new Wanesy configuration. | ||
func NewConfig() *Config { | ||
config := &Config{ | ||
flags: &pflag.FlagSet{}, | ||
} | ||
config.flags.StringVar(&config.frequencyPlanID, | ||
"frequency-plan-id", | ||
"", | ||
"Frequency Plan ID for the exported devices") | ||
config.flags.StringVar(&config.appID, | ||
"app-id", | ||
"", | ||
"Application ID for the exported devices") | ||
config.flags.StringVar(&config.csvPath, | ||
"csv-path", | ||
"", | ||
"Path to the CSV file exported from Wanesy Management Center") | ||
config.flags.BoolVar(&config.all, | ||
"all", | ||
false, | ||
"Export all devices in the CSV. This is only used by the application command") | ||
return config | ||
} | ||
|
||
// Initialize the configuration. | ||
func (c *Config) Initialize(src source.Config) error { | ||
c.src = src | ||
|
||
if c.appID = os.Getenv("APP_ID"); c.appID == "" { | ||
return errNoAppID.New() | ||
} | ||
if c.frequencyPlanID = os.Getenv("FREQUENCY_PLAN_ID"); c.frequencyPlanID == "" { | ||
return errNoFrequencyPlanID.New() | ||
} | ||
if c.csvPath = os.Getenv("CSV_PATH"); c.csvPath == "" { | ||
return errNoCSVFileProvided.New() | ||
} | ||
|
||
fpFetcher, err := fetch.FromHTTP(http.DefaultClient, src.FrequencyPlansURL) | ||
if err != nil { | ||
return err | ||
} | ||
c.fpStore = frequencyplans.NewStore(fpFetcher) | ||
|
||
return nil | ||
} | ||
|
||
// Flags returns the flags for the configuration. | ||
func (c *Config) Flags() *pflag.FlagSet { | ||
return c.flags | ||
} | ||
|
||
// ImportDevices imports the devices from the provided CSV file. | ||
func ImportDevices(csvPath string) (Devices, error) { | ||
raw, err := os.ReadFile(csvPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reader := csv.NewReader(strings.NewReader(string(raw))) | ||
readValues, err := reader.ReadAll() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(readValues) < 2 { | ||
return nil, errNoValuesInCSV.New() | ||
} | ||
values := make([]map[string]string, 0) | ||
for i := 1; i < len(readValues); i++ { | ||
keys := readValues[0] | ||
value := make(map[string]string) | ||
for j := 0; j < len(keys); j++ { | ||
noOfcolumns := len(readValues[i]) | ||
if j >= noOfcolumns { | ||
value[keys[j]] = "" // Fill empty columns. | ||
continue | ||
} | ||
value[keys[j]] = readValues[i][j] | ||
} | ||
values = append(values, value) | ||
} | ||
j, err := json.Marshal(values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
devices := make(Devices) | ||
err = devices.UnmarshalJSON(j) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return devices, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.