-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
305 lines (255 loc) · 6.51 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
package main
import (
"flag"
"log"
"strings"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/2tvenom/golifx"
"errors"
)
type (
silent bool
)
var (
lookupBulbs bool
showLabel bool
showHostInfo bool
showHostFirmware bool
showWifiInfo bool
showWifiFirmware bool
showPowerState bool
showBulbVersion bool
showInfo bool
showLocation bool
showGroup bool
showColor bool
mac string
turnOn bool
turnOff bool
duration int
hue int
saturation int
brightness int
kelvin int
jsonFormat bool
isSilent silent = silent(*flag.Bool("silent", false, "Silent"))
)
const (
_MAX = 65535
)
func init() {
flag.BoolVar(&lookupBulbs, "lookup", false, "Lookup bulbs")
flag.BoolVar(&showLabel, "label", false, "Get bulb label. Use with --lookup")
flag.BoolVar(&showHostInfo, "hostinfo", false, "Get bulb host info. Use with --lookup")
flag.BoolVar(&showHostFirmware, "hostfirmware", false, "Get bulb host firmware. Use with --lookup")
flag.BoolVar(&showWifiInfo, "wifiinfo", false, "Get bulb Wi-Fi info. Use with --lookup")
flag.BoolVar(&showWifiFirmware, "wififirmware", false, "Get bulb Wi-Fi firmware. Use with --lookup")
flag.BoolVar(&showPowerState, "powerstate", false, "Get bulb power state. Use with --lookup")
flag.BoolVar(&showBulbVersion, "bulbversion", false, "Get bulb version. Use with --lookup")
flag.BoolVar(&showInfo, "info", false, "Get bulb info. Use with --lookup")
flag.BoolVar(&showLocation, "location", false, "Get bulb location. Use with --lookup")
flag.BoolVar(&showGroup, "group", false, "Get bulb group. Use with --lookup")
flag.BoolVar(&showColor, "color", false, "Get bulb color, label, power state. Use with --lookup")
flag.StringVar(&mac, "bulb", "", "Set bulb MAC address for set color and power state. MAC format like 56:84:7a:fe:97:99")
flag.BoolVar(&turnOn, "on", false, "Turn on bulb. Use with --bulb")
flag.BoolVar(&turnOff, "off", false, "Turn off bulb. Use with --bulb")
flag.IntVar(&duration, "duration", 0, "Turn off/Turn on duration. Use with --bulb and (--on or --off)")
flag.IntVar(&hue, "hue", -1, "Set color hue. Range 0 to 65535. Use with --bulb")
flag.IntVar(&saturation, "saturation", -1, "Set color saturation. Range 0 to 65535. Use with --bulb")
flag.IntVar(&brightness, "brightness", -1, "Set color brightness. Range 0 to 65535. Use with --bulb")
flag.IntVar(&kelvin, "kelvin", -1, "Set color kelvin. Range 2500° (warm) to 9000° (cool). Use with --bulb")
flag.BoolVar(&jsonFormat, "json", false, "JSON format output")
}
func main() {
flag.Parse()
if mac != "" {
hwAddr, err := parseMac(mac)
if err != nil {
isSilent.error(err)
}
bulb := &golifx.Bulb{}
bulb.SetHardwareAddress(hwAddr)
if turnOn {
if duration == 0 {
bulb.SetPowerState(true)
} else {
bulb.SetPowerDurationState(true, uint32(duration))
}
return
}
if turnOff {
if duration == 0 {
bulb.SetPowerState(false)
} else {
bulb.SetPowerDurationState(false, uint32(duration))
}
return
}
if hue != -1 && (hue < 0 || hue > _MAX) {
isSilent.error(errors.New("Incorrect hue"))
}
if saturation != -1 && (saturation < 0 || saturation > _MAX) {
isSilent.error(errors.New("Incorrect saturation"))
}
if brightness != -1 && (brightness < 0 || brightness > _MAX) {
isSilent.error(errors.New("Incorrect brightness"))
}
if kelvin != -1 && (kelvin < 2500 || kelvin > 9000) {
isSilent.error(errors.New("Incorrect kelvin"))
}
if hue != -1 || saturation != -1 || brightness != -1 || kelvin != -1 {
var err error
var color *golifx.HSBK
if hue != -1 && saturation != -1 && brightness != -1 && kelvin != -1 {
color = &golifx.HSBK{
Hue: uint16(hue),
Saturation: uint16(saturation),
Brightness: uint16(brightness),
Kelvin: uint16(kelvin),
}
} else {
bulbState, err := bulb.GetColorState()
color = bulbState.Color
if err != nil {
isSilent.error(err)
}
if hue != -1 {
color.Hue = uint16(hue)
}
if saturation != -1 {
color.Saturation = uint16(saturation)
}
if brightness != -1 {
color.Brightness = uint16(brightness)
}
if kelvin != -1 {
color.Kelvin = uint16(kelvin)
}
}
bulbState, err := bulb.SetColorStateWithResponse(color, uint32(duration))
if err != nil {
isSilent.error(err)
}
if !isSilent {
if jsonFormat {
b, _ := json.Marshal(bulbState)
fmt.Println(string(b))
} else {
fmt.Println(bulbState)
}
}
}
return
}
if lookupBulbs {
bulbs, err := golifx.LookupBulbs()
if err != nil {
isSilent.error(err)
}
for _, bulb := range bulbs {
if showLabel {
_, err := bulb.GetLabel()
if err != nil {
isSilent.error(err)
}
}
if showHostInfo {
_, err := bulb.GetStateHostInfo()
if err != nil {
isSilent.error(err)
}
}
if showHostFirmware {
_, err := bulb.GetHostFirmware()
if err != nil {
isSilent.error(err)
}
}
if showWifiInfo {
_, err := bulb.GetWifiInfo()
if err != nil {
isSilent.error(err)
}
}
if showWifiFirmware {
_, err := bulb.GetWifiFirmware()
if err != nil {
isSilent.error(err)
}
}
if showPowerState {
_, err := bulb.GetPowerState()
if err != nil {
isSilent.error(err)
}
}
if showBulbVersion {
_, err := bulb.GetVersion()
if err != nil {
isSilent.error(err)
}
}
if showInfo {
_, err := bulb.GetInfo()
if err != nil {
isSilent.error(err)
}
}
if showLocation {
_, err := bulb.GetLocation()
if err != nil {
isSilent.error(err)
}
}
if showGroup {
_, err := bulb.GetGroup()
if err != nil {
isSilent.error(err)
}
}
if showColor {
_, err := bulb.GetColorState()
if err != nil {
isSilent.error(err)
}
}
}
if jsonFormat {
b, _ := json.Marshal(bulbs)
fmt.Println(string(b))
} else {
for id, bulb := range bulbs {
fmt.Print(bulb)
if id < len(bulbs)-1 {
fmt.Println("----------------")
}
}
}
return
}
}
func (s silent) error(err error) {
if !s {
log.Fatalf("Error: %s\n", err)
}
}
func parseMac(mac string) (uint64, error) {
mac = strings.Replace(mac, ":", "", -1)
hex, err := hex.DecodeString(mac)
if err != nil {
return 0, err
}
hex = append(hex, []byte{0,0}...)
var digit uint64
readUint64(hex, &digit)
return digit, nil
}
func readUint64(buff []byte, dest *uint64) error {
*dest = 0
for i := 0; i < 8; i++ {
*dest += uint64(buff[i]&0xFF) << uint(i*8)
}
return nil
}