From f43fdfcca36c189dfada59bc0a871dd76682a732 Mon Sep 17 00:00:00 2001 From: Hylke Visser Date: Fri, 8 Oct 2021 10:36:59 +0200 Subject: [PATCH] Add set-eui-as-id flag to re-map DeviceID to DevEUI --- CHANGELOG.md | 2 ++ cmd/root.go | 2 ++ cmd/util.go | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 991f3a9..eb89459 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cmd/root.go b/cmd/root.go index 76d52cc..91d83a2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -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") } diff --git a/cmd/util.go b/cmd/util.go index 4e04419..816f460 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -46,6 +46,7 @@ var ( ) type exportConfig struct { + euiForID bool devIDPrefix string } @@ -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) }