-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.go
83 lines (70 loc) · 1.96 KB
/
helpers.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
package main
import (
"context"
"fmt"
"log"
"math"
keylight "github.com/endocrimes/keylight-go"
texttable "github.com/syohex/go-texttable"
)
// discover all Keylights in your network and return a channel with results
func discoverLights() (<-chan *keylight.Device, error) {
discovery, err := keylight.NewDiscovery()
if err != nil {
log.Println("failed to initialize keylight discovery: ", err.Error())
return nil, err
}
go func() {
err := discovery.Run(context.Background())
if err != nil {
log.Fatalln("Failed to discover lights: ", err.Error())
}
}()
return discovery.ResultsCh(), nil
}
// convert powerstate from int to string
func getPowerState(state int) string {
if state == 0 {
return "off"
}
return "on"
}
// toggle the power state
func togglePowerState(toggleSwitchOn bool, lightOn int) int {
if toggleSwitchOn || lightOn == 0 {
return 1
}
return 0
}
// create a pretty table
func createTable() *texttable.TextTable {
tbl := &texttable.TextTable{}
tbl.SetHeader("Name", "Power State", "Brightness", "Temperature", "Address")
return tbl
}
// add a row to Table to pretty print
func addTableRow(tbl *texttable.TextTable, d *keylight.Device, group *keylight.LightGroup) {
tbl.AddRow(
d.Name,
getPowerState(group.Lights[0].On),
fmt.Sprintf("%d", group.Lights[0].Brightness),
fmt.Sprintf("%d (%d K)", group.Lights[0].Temperature, convertToKelvin(group.Lights[0].Temperature)),
fmt.Sprintf("%s:%d", d.DNSAddr, d.Port),
)
}
// draw pretty table
func drawTable(tbl *texttable.TextTable, count int) {
if count > 0 {
fmt.Println(tbl.Draw())
} else {
fmt.Println("Either timedout or no lights found! Try again")
}
}
// converts the Elgato API temperatures to Kelvin.
func convertToKelvin(temp int) int {
return int(math.Round(995007 * math.Pow(float64(temp), -1)))
}
// converts Kelvin temperatures to those of the Elgato API.
func convertFromKelvin(kelvin int) int {
return int(math.Floor(987007 * math.Pow(float64(kelvin), -0.999)))
}