-
Notifications
You must be signed in to change notification settings - Fork 25
/
gateway.go
156 lines (134 loc) · 3.46 KB
/
gateway.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
package migateway
import (
"errors"
"image/color"
"time"
mcolor "github.com/bingbaba/tool/color"
)
const (
MODEL_GATEWAY = "gateway"
FIELD_GATEWAY_RGB = "rgb"
FIELD_GATEWAY_ILLUMINATION = "illumination"
FIELD_GATEWAY_IP = "ip"
FLASHING_WEIGHT_WEAK = "1"
FLASHING_WEIGHT_NORMAL = "2"
FLASHING_WEIGHT_STRONG = "3"
)
//GateWay Status
type GateWay struct {
*Device
IP string
Port string
State GatewayState
}
type GatewayState struct {
RGB uint32
Illumination float64 // Unit: percentage (0-100%)
}
type GatewayStateChange struct {
ID string
From GatewayState
To GatewayState
}
func (g GatewayStateChange) IsChanged() bool {
return g.From.Illumination != g.To.Illumination || g.From.RGB != g.To.RGB
}
func NewGateWay(dev *Device) *GateWay {
g := &GateWay{Device: dev, State: GatewayState{RGB: 0, Illumination: 0.0}}
g.Set(dev)
return g
}
func convertIlluminationToPercentage(illumination int) float64 {
illumination = (illumination - 300)
if illumination < 0 {
illumination = 0
} else if illumination > 1000 {
illumination = 1000
}
return float64(illumination) / 10.0
}
func (g *GateWay) Set(dev *Device) {
change := &GatewayStateChange{ID: g.Sid, From: g.State, To: g.State}
if dev.hasField(FIELD_GATEWAY_IP) {
g.IP = dev.GetData(FIELD_GATEWAY_IP)
}
if dev.hasField(FIELD_GATEWAY_RGB) {
g.State.RGB = dev.GetDataAsUint32(FIELD_GATEWAY_RGB)
}
if dev.hasField(FIELD_GATEWAY_ILLUMINATION) {
illumination := dev.GetDataAsInt(FIELD_GATEWAY_ILLUMINATION)
g.State.Illumination = convertIlluminationToPercentage(illumination)
}
change.To = g.State
if change.IsChanged() || g.shouldPushUpdates() {
g.Aqara.StateMessages <- change
}
if dev.Token != "" {
g.setToken(dev.Token)
}
if dev.ShortID > 0 {
g.ShortID = dev.ShortID
}
}
func (g *GateWay) setToken(token string) {
g.Token = token
g.GatewayConnection.token = token
}
func (gwd *GateWay) ChangeColor(c color.Color) error {
r, g, b, a := c.RGBA()
LOGGER.Info("r:%d,g:%d,b:%d,a:%d", r&0xff, g&0xff, b&0xff, a&0xff)
data := map[string]interface{}{FIELD_GATEWAY_RGB: RGBNumber(c)}
return gwd.GatewayConnection.Control(gwd.Device, data)
}
func (gwd *GateWay) ChangeBrightness(b int) error {
if b < 0 || b > 100 {
return errors.New("Brightness should be between 0~100")
}
if b == 0 {
return gwd.TurnOff()
}
rgbNum := uint32(gwd.State.RGB & 0xff)
rgb := NewRGB(rgbNum)
rgb.A = uint8(100 - b)
return gwd.ChangeColor(rgb)
}
func (gwd *GateWay) TurnOff() error {
return gwd.ChangeColor(mcolor.COLOR_BLACK)
}
func (gwd *GateWay) TurnOn() error {
rgb := NewRGB(gwd.State.RGB)
if gwd.State.RGB == 0 {
rgb = mcolor.COLOR_WHITE
}
r, g, b, a := rgb.RGBA_8bit()
LOGGER.Warn("Change Color %d,%d,%d,%d", r, g, b, a)
return gwd.ChangeColor(rgb)
}
func (gwd *GateWay) Flashing(c color.Color) error {
interval := 500 * time.Millisecond
err := gwd.flashingOnce(c, interval)
if err != nil {
return err
}
go func() {
for {
time.Sleep(interval)
gwd.flashingOnce(c, interval)
}
}()
return nil
}
func (gwd *GateWay) flashingOnce(c color.Color, interval time.Duration) error {
updata := map[string]interface{}{FIELD_GATEWAY_RGB: RGBNumber(c)}
downdata := map[string]interface{}{FIELD_GATEWAY_RGB: RGBNumber(mcolor.COLOR_BLACK)}
err := gwd.GatewayConnection.Control(gwd.Device, updata)
if err != nil {
return err
}
time.Sleep(interval)
err = gwd.GatewayConnection.Control(gwd.Device, downdata)
if err != nil {
return err
}
return nil
}