This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
devices_list.go
74 lines (61 loc) · 1.92 KB
/
devices_list.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
// 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 (
"fmt"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
)
var devicesListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List all devices for the current application",
Long: `ttnctl devices list can be used to list all devices for the current application.`,
Example: `$ ttnctl devices list
INFO Using Application AppID=test
INFO Discovering Handler...
INFO Connecting with Handler...
DevID AppEUI DevEUI DevAddr
test 70B3D57EF0000024 0001D544B2936FCE 26001ADA
INFO Listed 1 devices AppID=test
`,
Run: func(cmd *cobra.Command, args []string) {
assertArgsLength(cmd, args, 0, 0)
appID := util.GetAppID(ctx)
conn, manager := util.GetHandlerManager(ctx, appID)
defer conn.Close()
devices, err := manager.GetDevicesForApplication(appID, 0, 0)
if err != nil {
ctx.WithError(err).Fatal("Could not get devices.")
}
table := uitable.New()
table.MaxColWidth = 70
table.AddRow("DevID", "AppEUI", "DevEUI", "DevAddr", "Description")
for _, dev := range devices {
if lorawan := dev.GetLoRaWANDevice(); lorawan != nil {
devAddr := lorawan.DevAddr
if devAddr.IsEmpty() {
devAddr = nil
}
var devEUI interface{} = lorawan.DevEUI
if lorawan.DevEUI.IsEmpty() {
devEUI = "register on join"
}
table.AddRow(dev.DevID, lorawan.AppEUI, devEUI, devAddr, crop(dev.Description, 40))
} else {
table.AddRow(dev.DevID)
}
}
fmt.Println()
fmt.Println(table)
fmt.Println()
ctx.WithFields(ttnlog.Fields{
"AppID": appID,
}).Infof("Listed %d devices", len(devices))
},
}
func init() {
devicesCmd.AddCommand(devicesListCmd)
}