-
Notifications
You must be signed in to change notification settings - Fork 0
/
mh.go
132 lines (116 loc) · 2.11 KB
/
mh.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
package mh
import (
"net"
"time"
)
type Config struct {
IP net.IP
Timeout time.Duration
}
type Controller struct {
config Config
}
func NewController(config Config) *Controller {
controller := Controller{
config: config,
}
return &controller
}
func (c *Controller) GetState() ([]byte, error) {
request := []byte{0x81, 0x8a, 0x8b}
response := make([]byte, 14)
err := c.sendRequest(request, response)
if err != nil {
return nil, err
}
return response, nil
}
func (c *Controller) GetPower() (bool, error) {
state, err := c.GetState()
if err != nil {
return false, err
}
power := state[2] == 0x23
return power, nil
}
func (c *Controller) SetPower(power bool) error {
request := []byte{0x71, 0x23, 0x0f}
if !power {
request[1] = 0x24
}
err := c.sendRequest(request, nil)
if err != nil {
return err
}
return nil
}
type RGBW struct {
Red, Green, Blue, White float64
}
func (c *Controller) GetRGBW() (*RGBW, error) {
state, err := c.GetState()
if err != nil {
return nil, err
}
return &RGBW{
Red: float64(state[6]),
Green: float64(state[7]),
Blue: float64(state[8]),
White: float64(state[9]),
}, nil
}
func (c *Controller) SetRGBW(rgbw *RGBW) error {
err := c.SetPower(true)
if err != nil {
return err
}
request := []byte{
0x31,
uint8(rgbw.Red),
uint8(rgbw.Green),
uint8(rgbw.Blue),
uint8(rgbw.White),
0x00,
0x0f,
}
err = c.sendRequest(request, nil)
if err != nil {
return err
}
return nil
}
func (c *Controller) sendRequest(request []byte, response []byte) error {
connection, err := net.DialTimeout(
"tcp",
c.config.IP.String()+":5577",
c.config.Timeout,
)
if err != nil {
return err
}
checksum := calculateChecksum(request)
request = append(request, checksum)
_, err = connection.Write(request)
if err != nil {
return err
}
if response != nil {
_, err = connection.Read(response)
if err != nil {
return err
}
}
err = connection.Close()
if err != nil {
return err
}
return nil
}
func calculateChecksum(data []byte) byte {
checksum := uint8(0)
for _, b := range data {
checksum += b
}
checksum &= 0xFF
return checksum
}