Skip to content

Commit

Permalink
Merge pull request #36 from TheThingsNetwork/feature/eui-for-id
Browse files Browse the repository at this point in the history
  • Loading branch information
htdvisser authored Oct 12, 2021
2 parents 627e4a9 + f43fdfc commit 7f5e866
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Added

- `--set-eui-as-id` flag to re-map the ID of end devices. Using this flag will set the device ID to the DevEUI (if it has a DevEUI). When combined with the `--dev-id-prefix` flag, the ID will consist of the prefix and the DevEUI.

### Changed

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
ctx = log.NewContext(context.Background(), logger)

exportCfg.devIDPrefix, _ = cmd.Flags().GetString("dev-id-prefix")
exportCfg.euiForID, _ = cmd.Flags().GetBool("set-eui-as-id")

rpclog.ReplaceGrpcLogger(logger)
return nil
Expand All @@ -68,5 +69,6 @@ func init() {
rootCmd.PersistentFlags().Bool("verbose", false, "Verbose output")
rootCmd.PersistentFlags().Bool("dry-run", false, "Do everything except resetting root and session keys of exported devices")
rootCmd.PersistentFlags().String("frequency-plans-url", "https://raw.githubusercontent.com/TheThingsNetwork/lorawan-frequency-plans/master", "URL for fetching frequency plans")
rootCmd.PersistentFlags().Bool("set-eui-as-id", false, "Use the DevEUI as ID")
rootCmd.PersistentFlags().String("dev-id-prefix", "", "(optional) value to be prefixed to the resulting device IDs")
}
18 changes: 15 additions & 3 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
)

type exportConfig struct {
euiForID bool
devIDPrefix string
}

Expand All @@ -54,17 +55,28 @@ func (cfg exportConfig) exportDev(s source.Source, devID string) error {
if err != nil {
return errExport.WithAttributes("device_id", devID).WithCause(err)
}
oldID := dev.DeviceId

if cfg.euiForID && dev.DevEui != nil {
dev.DeviceId = strings.ToLower(dev.DevEui.String())
}
if cfg.devIDPrefix != "" {
dev.DeviceId = fmt.Sprintf("%s-%s", cfg.devIDPrefix, dev.DeviceId)
}
// V3 does not allow any underscores in identifiers
dev.DeviceId = sanitizeID.Replace(dev.DeviceId)
dev.ApplicationId = sanitizeID.Replace(dev.ApplicationId)

dev.DeviceId = sanitizeID.Replace(dev.DeviceId)
if len(dev.DeviceId) > maxIDLength {
return errDevIDExceedsMaxLength.WithAttributes("id", dev.DeviceId)
}

if dev.DeviceId != oldID {
if dev.Attributes == nil {
dev.Attributes = make(map[string]string)
}
dev.Attributes["old-id"] = oldID
}

dev.ApplicationId = sanitizeID.Replace(dev.ApplicationId)
if len(dev.ApplicationId) > maxIDLength {
return errAppIDExceedsMaxLength.WithAttributes("id", dev.ApplicationId)
}
Expand Down

0 comments on commit 7f5e866

Please sign in to comment.