-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices_register.go
109 lines (94 loc) · 2.91 KB
/
devices_register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cmd
import (
"strings"
"github.com/TheThingsNetwork/api"
"github.com/TheThingsNetwork/api/handler"
"github.com/TheThingsNetwork/api/protocol/lorawan"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/go-utils/pseudorandom"
"github.com/TheThingsNetwork/go-utils/random"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/spf13/cobra"
)
var devicesRegisterCmd = &cobra.Command{
Use: "register [Device ID] [DevEUI] [AppKey] [Lat,Long]",
Short: "Register a new device",
Long: `ttnctl devices register can be used to register a new device.`,
Example: `$ ttnctl devices register test
INFO Using Application AppEUI=70B3D57EF0000024 AppID=test
INFO Generating random DevEUI...
INFO Generating random AppKey...
INFO Discovering Handler...
INFO Connecting with Handler...
INFO Registered device AppEUI=70B3D57EF0000024 AppID=test AppKey=EBD2E2810A4307263FE5EF78E2EF589D DevEUI=0001D544B2936FCE DevID=test
`,
Run: func(cmd *cobra.Command, args []string) {
assertArgsLength(cmd, args, 1, 4)
var err error
devID := strings.ToLower(args[0])
if err := api.NotEmptyAndValidID(devID, "Device ID"); err != nil {
ctx.Fatal(err.Error())
}
appID := util.GetAppID(ctx)
appEUI := util.GetAppEUI(ctx)
var devEUI types.DevEUI
if len(args) > 1 {
devEUI, err = types.ParseDevEUI(args[1])
if err != nil {
ctx.Fatalf("Invalid DevEUI: %s", err)
}
} else {
ctx.Info("Generating random DevEUI...")
pseudorandom.FillBytes(devEUI[1:])
}
var appKey types.AppKey
if len(args) > 2 {
appKey, err = types.ParseAppKey(args[2])
if err != nil {
ctx.Fatalf("Invalid AppKey: %s", err)
}
} else {
ctx.Info("Generating random AppKey...")
random.FillBytes(appKey[:])
}
device := &handler.Device{
AppID: appID,
DevID: devID,
Device: &handler.Device_LoRaWANDevice{LoRaWANDevice: &lorawan.Device{
AppID: appID,
DevID: devID,
AppEUI: appEUI,
DevEUI: devEUI,
AppKey: &appKey,
Uses32BitFCnt: true,
}},
}
if len(args) > 3 {
location, err := util.ParseLocation(args[3])
if err != nil {
ctx.WithError(err).Fatal("Invalid location")
}
device.Latitude = float32(location.Latitude)
device.Longitude = float32(location.Longitude)
}
conn, manager := util.GetHandlerManager(ctx, appID)
defer conn.Close()
err = manager.SetDevice(device)
if err != nil {
ctx.WithError(err).Fatal("Could not register Device")
}
ctx.WithFields(ttnlog.Fields{
"AppID": appID,
"DevID": devID,
"AppEUI": appEUI,
"DevEUI": devEUI,
"AppKey": appKey,
}).Info("Registered device")
},
}
func init() {
devicesCmd.AddCommand(devicesRegisterCmd)
}