-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
554 lines (508 loc) · 12 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package main
import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"gopkg.in/ini.v1"
)
const defaultConfigFile = ".tronrc"
const defaultCertDir = ".config/tron/certs"
//go:generate bash get_versions.sh
//go:embed tmp/version.txt
var Version string
//go:embed tmp/go_version.txt
var GoVersion string
//go:embed tmp/commit_hash.txt
var CommitHash string
var verbose = flag.Bool("v", false, "Verbose")
func usage() {
fmt.Println("usage: tron [-v] <command>")
fmt.Println()
fmt.Println("Commands:")
fmt.Println()
fmt.Println(" version Print tron version")
fmt.Println()
fmt.Println(" pair Pair with a Lutron Caséta controller")
fmt.Println(" ping Ping paired controller")
fmt.Println()
fmt.Println(" get Query controller endpoints")
fmt.Println(" post Send data to controller endpoints")
fmt.Println()
fmt.Println(" area Control areas")
fmt.Println(" device Control Lutron devices")
fmt.Println(" server Control Lutron controllers")
fmt.Println(" service Control 3rd-party services")
fmt.Println(" zone Control zones")
fmt.Println()
os.Exit(1)
}
func main() {
flag.Parse()
usr, err := user.Current()
if err != nil {
fmt.Println("error: failed to fetch current user:", err)
os.Exit(1)
}
dir := usr.HomeDir
configFilePath := filepath.Join(dir, defaultConfigFile)
cfg, err := ini.Load(configFilePath)
if err != nil {
fmt.Println("error: failed to read file:", err)
os.Exit(1)
}
client := Client{
Host: cfg.Section("").Key("host").String(),
CACertPath: filepath.Join(dir, defaultCertDir, "ca.crt"),
ClientCertPath: filepath.Join(dir, defaultCertDir, "client.crt"),
ClientKeyPath: filepath.Join(dir, defaultCertDir, "client.key"),
Verbose: *verbose,
}
if *verbose {
os.Stderr.WriteString(fmt.Sprintf("Host: %s\n\n", client.Host))
}
if flag.NArg() > 0 {
cmd := flag.Arg(0)
switch cmd {
case "pair":
err := client.Pair()
if err != nil {
fmt.Println("error: failed to pair controller:", err)
os.Exit(1)
}
case "area":
doAreaCommand(client, flag.Args()[1:])
case "device":
doDeviceCommand(client, flag.Args()[1:])
case "server":
doServerCommand(client, flag.Args()[1:])
case "service":
doServiceCommand(client, flag.Args()[1:])
case "zone":
doZoneCommand(client, flag.Args()[1:])
case "get":
doGetCommand(client, flag.Args()[1:])
case "post":
doPostCommand(client, flag.Args()[1:])
case "ping":
res, err := client.Ping()
if err != nil {
fmt.Println("error: failed to ping controller:", err)
os.Exit(1)
}
fmt.Printf("OK (LEAP version %0.3f)\n", res.LEAPVersion)
case "version":
fmt.Printf("tron %s (%s) go/%s\n", Version, CommitHash, GoVersion)
default:
usage()
}
} else {
usage()
}
}
func doAreaCommand(client Client, args []string) {
printArea := func(area AreaDefinition) {
fmt.Println("Name: ", area.Name)
fmt.Println("Category:", area.Category.Type)
fmt.Println("Path: ", area.Href)
fmt.Println("Parent: ", area.Parent.Href)
fmt.Println()
fmt.Println("Devices:")
for _, d := range area.AssociatedDevices {
fmt.Println("-", d.Href)
}
fmt.Println()
fmt.Println("Daylighting Gain Settings:", area.DaylightingGainSettings.Href)
fmt.Println("Load Shedding: ", area.LoadShedding.Href)
fmt.Println("Occupancy Settings: ", area.OccupancySettings.Href)
fmt.Println("Occupancy Sensor Settings:", area.OccupancySensorSettings.Href)
fmt.Println()
fmt.Println("Occupancy Groups:")
for _, og := range area.AssociatedOccupancyGroups {
fmt.Println("-", og.Href)
}
}
usage := func() {
fmt.Println("usage: tron area list")
fmt.Println(" tron area info <id>")
os.Exit(1)
}
if len(args) < 1 {
usage()
}
command := args[0]
switch command {
case "info":
if len(args) < 2 {
usage()
}
id := args[1]
area, err := client.Area(id)
if err != nil {
fmt.Println("error: failed to retrieve area info:", err)
os.Exit(1)
}
printArea(area)
case "list":
list, err := client.Areas()
if err != nil {
fmt.Println("error: failed retrieve area list:", err)
os.Exit(1)
}
first := true
for _, area := range list {
if first {
first = false
} else {
fmt.Println("==========")
fmt.Println()
}
printArea(area)
fmt.Println()
}
default:
usage()
}
}
func doDeviceCommand(client Client, args []string) {
printDevice := func(device DeviceDefinition) {
fmt.Println("Name: ", strings.Join(device.FullyQualifiedName, " "))
fmt.Println("Path: ", device.Href)
fmt.Println("Type: ", device.DeviceType)
fmt.Println("Model Number: ", device.ModelNumber)
fmt.Println("Serial Number:", device.SerialNumber)
fmt.Println()
fmt.Println("Addressed State:", device.AddressedState)
fmt.Println("Associated Area:", device.AssociatedArea.Href)
fmt.Println("Parent Path: ", device.Parent.Href)
fmt.Println()
if len(device.LocalZones) > 0 {
fmt.Println("Local Zones:")
for _, lz := range device.LocalZones {
fmt.Println("-", lz.Href)
}
}
fmt.Println()
if len(device.ButtonGroups) > 0 {
fmt.Println("Button Groups:")
for _, bg := range device.ButtonGroups {
fmt.Println("-", bg.Href)
}
}
fmt.Println()
if len(device.DeviceRules) > 0 {
fmt.Println("Device Rules:")
for _, dr := range device.DeviceRules {
fmt.Println("-", dr.Href)
}
}
fmt.Println()
if len(device.LinkNodes) > 0 {
fmt.Println("Link Nodes:")
for _, ln := range device.LinkNodes {
fmt.Println("-", ln.Href)
}
}
}
usage := func() {
fmt.Println("usage: tron device list")
fmt.Println(" tron device info <id>")
os.Exit(1)
}
if len(args) < 1 {
usage()
}
command := args[0]
switch command {
case "info":
if len(args) < 2 {
usage()
}
id := args[1]
device, err := client.Device(id)
if err != nil {
fmt.Println("error: failed to retrieve device info:", err)
os.Exit(1)
}
printDevice(device)
case "list":
list, err := client.Devices()
if err != nil {
fmt.Println("error: failed retrieve device list:", err)
os.Exit(1)
}
first := true
for _, device := range list {
if first {
first = false
} else {
fmt.Println("==========")
fmt.Println()
}
printDevice(device)
fmt.Println()
}
default:
usage()
}
}
func doServerCommand(client Client, args []string) {
printServer := func(server ServerDefinition) {
fmt.Println("Path: ", server.Href)
fmt.Println("Type: ", server.Type)
fmt.Printf("Enabled: %v\n", server.EnableState == "Enabled")
fmt.Println()
fmt.Println("Protocol Version:", server.ProtocolVersion)
fmt.Println()
fmt.Println("LEAP:")
fmt.Println(" Pairing List:", server.LEAPProperties.PairingList.Href)
fmt.Println()
fmt.Println("Endpoints:")
for _, ep := range server.Endpoints {
fmt.Printf("- %d (%s)\n", ep.Port, ep.Protocol)
}
fmt.Println()
fmt.Println("Network Interfaces:")
for _, iface := range server.NetworkInterfaces {
fmt.Println("-", iface.Href)
}
}
usage := func() {
fmt.Println("usage: tron server list")
fmt.Println("usage: tron server info [id]")
os.Exit(1)
}
if len(args) < 1 {
usage()
}
command := args[0]
switch command {
case "info":
id := "1"
if len(args) >= 2 {
id = args[1]
}
server, err := client.Server(id)
if err != nil {
fmt.Println("error: failed to retrieve server info:", err)
os.Exit(1)
}
printServer(server)
case "list":
list, err := client.Servers()
if err != nil {
fmt.Println("error: failed to retrieve server list:", err)
os.Exit(1)
}
for _, server := range list {
printServer(server)
}
default:
usage()
}
}
func doServiceCommand(client Client, args []string) {
usage := func() {
fmt.Println("usage: tron service list")
os.Exit(1)
}
if len(args) < 1 {
usage()
}
command := args[0]
switch command {
case "list":
list, err := client.Services()
if err != nil {
fmt.Println("error: failed retrieve service list:", err)
os.Exit(1)
}
for _, service := range list {
fmt.Printf("%s (%s)\n", service.Type, service.Href)
}
default:
usage()
}
}
func doZoneCommand(client Client, args []string) {
printZone := func(zone ZoneDefinition) {
fmt.Println("Name:", zone.Name)
fmt.Println("Path:", zone.Href)
fmt.Println("Type:", zone.ControlType)
if zone.Category.Type != "" {
fmt.Println("Category:")
fmt.Println(" Type: ", zone.Category.Type)
fmt.Println(" Is Light:", zone.Category.IsLight)
}
fmt.Println("Device Path:", zone.Device.Href)
}
usage := func() {
fmt.Println("usage: tron zone list")
fmt.Println("usage: tron zone info <id>")
fmt.Println("usage: tron zone status <id>")
fmt.Println("usage: tron zone on <id> [duration] [delay]")
fmt.Println("usage: tron zone off <id> [duration] [delay]")
fmt.Println("usage: tron zone dim <id> <level> [duration] [delay]")
os.Exit(1)
}
if len(args) < 1 {
usage()
}
command := args[0]
switch command {
case "dim":
if len(args) < 3 {
usage()
}
id := args[1]
level, err := strconv.Atoi(args[2])
if err != nil {
fmt.Println("error: invalid level:", err)
os.Exit(1)
}
options := DimOptions{
Level: level,
}
if len(args) >= 4 {
options.Duration = args[3]
}
if len(args) >= 5 {
options.Delay = args[4]
}
_, err = client.ZoneDim(id, options)
if err != nil {
fmt.Println("error: failed to dim zone:", err)
os.Exit(1)
}
case "info":
if len(args) < 2 {
usage()
}
id := args[1]
zone, err := client.Zone(id)
if err != nil {
fmt.Println("error: failed to retrieve zone info:", err)
os.Exit(1)
}
printZone(zone)
case "list":
list, err := client.Zones()
if err != nil {
fmt.Println("error: failed retrieve zone list:", err)
os.Exit(1)
}
for _, zone := range list {
printZone(zone)
fmt.Println()
}
case "on":
if len(args) < 2 {
usage()
}
id := args[1]
options := DimOptions{
Level: 100,
}
if len(args) >= 3 {
options.Duration = args[2]
}
if len(args) >= 4 {
options.Delay = args[3]
}
_, err := client.ZoneDim(id, options)
if err != nil {
fmt.Println("error: failed to dim zone:", err)
os.Exit(1)
}
case "off":
if len(args) < 2 {
usage()
}
id := args[1]
options := DimOptions{
Level: 0,
}
if len(args) >= 3 {
options.Duration = args[2]
}
if len(args) >= 4 {
options.Delay = args[3]
}
_, err := client.ZoneDim(id, options)
if err != nil {
fmt.Println("error: failed to dim zone:", err)
os.Exit(1)
}
case "status":
if len(args) < 2 {
usage()
}
id := args[1]
zoneStatus, err := client.ZoneStatus(id)
if err != nil {
fmt.Println("error: failed to retrieve zone status:", err)
os.Exit(1)
}
fmt.Println("Level: ", zoneStatus.Level)
fmt.Println("Accuracy:", zoneStatus.StatusAccuracy)
fmt.Println()
fmt.Println("Status Path:", zoneStatus.Href)
fmt.Println("Zone Path: ", zoneStatus.Zone.Href)
default:
usage()
}
}
func doGetCommand(client Client, args []string) {
usage := func() {
fmt.Println("usage: tron get <path>")
os.Exit(1)
}
if len(args) < 1 {
usage()
}
path := args[0]
res, err := client.Get(path)
if err != nil {
fmt.Println("error: request failed:", err)
os.Exit(1)
}
out, err := json.MarshalIndent(res, "", " ")
if err != nil {
fmt.Println("error: failed to format response as JSON:", err)
os.Exit(1)
}
fmt.Println(string(out))
}
func doPostCommand(client Client, args []string) {
usage := func() {
fmt.Println("usage: tron post <path> <json>")
os.Exit(1)
}
if len(args) < 2 {
usage()
}
path := args[0]
raw := args[1]
var o map[string]any
err := json.Unmarshal([]byte(raw), &o)
if err != nil {
fmt.Println("error: failed to parse input as JSON:", err)
os.Exit(1)
}
res, err := client.Post(path, o)
if err != nil {
fmt.Println("error: request failed:", err)
os.Exit(1)
}
out, err := json.MarshalIndent(res, "", " ")
if err != nil {
fmt.Println("error: failed to format response as JSON:", err)
os.Exit(1)
}
fmt.Println(string(out))
}