-
Notifications
You must be signed in to change notification settings - Fork 2
/
bulb.go
207 lines (175 loc) · 4.55 KB
/
bulb.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
package yeelight
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"sync"
)
// notes:
// max 4 parallel opened TCP connections
// quota: 60 commands per minute (for one device)
// quota: 144 commands per minute for all devices
// TODO: Returns response objects too, not only error
// TODO: Export interface only, not whole struct
type Bulb struct {
standardCommands
commonCommands
// Namespace to control "background" capabilities (device must support it)
Bg backgroundLightCommands
Ip string
Port int
conn net.Conn
results map[int]chan Response
resultsMtx sync.Mutex
}
func (b *Bulb) Connect() error {
destination := fmt.Sprintf("%s:%d", b.Ip, b.Port)
conn, err := net.Dial("tcp", destination)
if err != nil {
return err
}
go b.responseProcessor()
b.conn = conn
return nil
}
func (b *Bulb) Disconnect() error {
err := b.conn.Close()
if err != nil {
return err
}
return nil
}
// NewBulb creates Bulb instance, default protocol port: 55443
func NewBulb(ip string) *Bulb {
bulb := &Bulb{
standardCommands{},
commonCommands{},
backgroundLightCommands{},
ip,
55443, // 55443 is a constant protocol port
nil,
make(map[int]chan Response),
sync.Mutex{},
}
// I know It looks badly, but "It is working? It is working"
bulb.standardCommands.commander = bulb
bulb.commonCommands.commander = bulb
bulb.Bg.commander = bulb
bulb.Bg.prefix = "bg_"
return bulb
}
func (b *Bulb) executeCommand(c partialCommand) error {
respChan := make(chan Response)
// preparing request ID to be able to monitor and wait for response
b.resultsMtx.Lock()
id, err := b.findFirstFreeIntKey()
if err != nil {
b.resultsMtx.Unlock()
return err
}
b.results[id] = respChan
b.resultsMtx.Unlock()
defer func(ch chan Response, id int) {
close(ch)
delete(b.results, id)
}(respChan, id)
realCommand := newCompleteCommand(c, id)
message, err := json.Marshal(realCommand)
if err != nil {
return err
}
log.Printf("[%s] request: %s\n", b.Ip, message)
message = append(message, CR, LF)
_, err = b.conn.Write(message)
if err != nil {
return err
}
// waiting for response on that request
resp := <-respChan
return resp.ok()
}
func openSocket(host string, min, max int) (net.Listener, int, error) {
if min > max {
return nil, 0, errors.New("min value cannot be greather than max value")
}
if min < 0 || max > 65535 {
return nil, 0, errors.New("port number must be in range 0 - 65535")
}
for port := min; port <= max; port++ {
var ip = "" // binding on all interfaces
address := fmt.Sprintf("%s:%d", ip, port)
listener, err := net.Listen("tcp", address)
if err != nil {
continue
}
return listener, port, nil
}
return nil, 0, errors.New("no available free ports in given range")
}
// keysExists returns a bool when given map contains all of given key names
func keysExists(m map[string]interface{}, keys ...string) bool {
var matches int
for k1, _ := range m {
for _, k2 := range keys {
if k1 == k2 {
matches += 1
}
}
}
return matches == len(keys)
}
// responseProcessor is run internally by Connect() function.
// Tt's responsible for monitoring command responses and notifications
func (b *Bulb) responseProcessor() {
var buff = make([]byte, 512)
var resp map[string]interface{}
for {
n, err := b.conn.Read(buff)
if err != nil {
break
}
responses := bytes.Split(buff[:n], []byte{CR, LF})
for _, r := range responses[:len(responses)-1] {
resp = make(map[string]interface{})
err = json.Unmarshal(r, &resp)
if err != nil {
log.Printf("OKResponse err: %s\n", r)
continue
}
switch {
case keysExists(resp, "id", "result"): // Command success
var unmarshaled OKResponse
err = json.Unmarshal(r, &unmarshaled)
if err != nil {
log.Printf("second unmarshal error: %s\n", r)
}
b.results[unmarshaled.id()] <- &unmarshaled
case keysExists(resp, "id", "error"): // Command failed
var unmarshaled ERRResponse
err = json.Unmarshal(r, &unmarshaled)
if err != nil {
log.Printf("second unmarshal error: %s\n", r)
}
b.results[unmarshaled.id()] <- &unmarshaled
case keysExists(resp, "method", "params"): // Notification
// log.Printf("state change%s\n", r)
default:
log.Printf("unhandled response: %s\n", r)
}
}
}
log.Printf("response processor exited\n")
}
// findFirstFreeIntKey finds available (unique) id which will be used as command identifier
func (b *Bulb) findFirstFreeIntKey() (int, error) {
for i := 0; i < 100; i++ {
_, ok := b.results[i]
if !ok {
return i, nil
}
}
return 0, errors.New("not available")
}