-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.go
145 lines (127 loc) · 3.49 KB
/
device.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"github.com/nightrune/wrench/ei"
"github.com/nightrune/wrench/logging"
"os"
)
var cmdDevice = &Command{
UsageLine: "device",
Short: "Various ways to interact with electric imp devices",
Long: `Submcommand:
device - Shows usage
device list - Lists the current models
device assign <device_id> <model_id>
device restart <device_id>`,
}
func init() {
cmdDevice.Run = DeviceSubCommand
}
func ListDevices(client *ei.BuildClient) {
device_list, err := client.GetDeviceList()
if err != nil {
logging.Fatal("Failed to get device list %s", err.Error())
return
}
for _, model := range device_list {
fmt.Printf("Id: %s, Name: %s\n", model.Id, model.Name)
}
}
func StartDeviceLogging(client *ei.BuildClient, device_id string) {
logs, poll_url, err := client.GetDeviceLogs(device_id)
if err != nil {
logging.Fatal("Failed to get device logs %s", err.Error())
return
}
logging.Debug("Poll Url %s", poll_url)
for _, entry := range logs {
fmt.Printf("%s: %s: %s\n", entry.Timestamp, entry.Type, entry.Message)
}
for {
logs, err = client.ContinueDeviceLogs(poll_url)
if err != nil {
if _, ok := err.(*ei.Timeout); ok == false {
logging.Fatal("Failed to get device logs %s", err.Error())
return;
} else {
logging.Debug("Long poll timed out...")
}
} else {
for _, entry := range logs {
fmt.Printf("%s: %s: %s\n", entry.Timestamp, entry.Type, entry.Message)
}
}
}
}
func AssignDeviceToModel(client *ei.BuildClient, device_id string, model_id string) {
device, err := client.GetDevice(device_id)
if err != nil {
logging.Fatal("Failed to retrieve current model for model id: %s", model_id)
return
}
// Don't do anything if its already the same
if device.ModelId == model_id {
return;
}
device.ModelId = model_id;
updated_device, err := client.UpdateDevice(&device, device_id)
logging.Debug("Model and new Devices: %s", updated_device)
}
func RestartDevice(client *ei.BuildClient, device_id string) {
err := client.RestartDevice(device_id)
if err != nil {
logging.Fatal("Failed to restart device %s with error %s", device_id, err.Error())
return
}
fmt.Printf("Successfully restarted device and agent");
}
// TODO(sean) Rewrite this, and make the command list from wrench.go portable and resulable
func PrintDeviceHelp() {
fmt.Printf("Available sub-commands, list, log, assign, restart\n")
}
func DeviceSubCommand(cmd *Command, args []string) {
logging.Debug("In Device")
for i, s := range args {
logging.Debug("%d:%s", i, s)
}
client, err := CreateClient(cmd.settings.ApiKeyFile)
if err != nil {
os.Exit(1)
return
}
if len(args) < 2 {
PrintDeviceHelp()
os.Exit(1)
}
if args[1] == "list" {
ListDevices(client)
} else if args[1] == "log" {
if len(args) < 3 {
fmt.Printf("Usage: device log <device_id>\n")
PrintDeviceHelp()
} else {
logging.Debug("Attempting to start logging")
StartDeviceLogging(client, args[2])
}
} else if args[1] == "assign" {
if len(args) < 4 {
fmt.Printf("Usage: device assign <device_id> <model_id>\n");
PrintDeviceHelp();
} else {
logging.Debug("Attempting to assign device to model");
AssignDeviceToModel(client, args[2], args[3]);
}
} else if args[1] == "restart" {
if len(args) < 3 {
fmt.Printf("Usage: device restart <device_id>\n");
PrintDeviceHelp();
} else {
logging.Debug("Attempting to restart device");
RestartDevice(client, args[2]);
}
} else {
logging.Debug("Showing help")
PrintDeviceHelp()
os.Exit(1)
}
}