-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathColorChannel.go
137 lines (109 loc) · 3.78 KB
/
ColorChannel.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
package main
import (
"fmt"
"math"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/ninjasphere/go-ninja/channels"
"github.com/ninjasphere/go-zigbee/gateway"
)
type ColorChannel struct {
Channel
channel *channels.ColorChannel
}
// -------- Color Protocol --------
func (c *ColorChannel) init() error {
log.Debugf("Initialising color channel of device %d", *c.device.deviceInfo.IeeeAddress)
/*clusterID := uint32(0x06)
attributeID := uint32(0)
minReportInterval := uint32(1)
maxReportInterval := uint32(120)
request := &gateway.GwSetAttributeReportingReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
},
ClusterId: &clusterID,
AttributeReportList: []*gateway.GwAttributeReportT{{
AttributeId: &attributeID,
AttributeType: gateway.GwZclAttributeDataTypesT_ZCL_DATATYPE_BOOLEAN.Enum(),
MinReportInterval: &minReportInterval,
MaxReportInterval: &maxReportInterval,
}},
}
response := &gateway.GwSetAttributeReportingRspInd{}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 20*time.Second)
if err != nil {
log.Errorf("Error enabling on/off reporting: %s", err)
} else if response.Status.String() != "STATUS_SUCCESS" {
log.Errorf("Failed to enable on/off reporting. status: %s", response.Status.String())
}*/
//mosquitto_pub -m '{"id":123, "params": [0.1],"jsonrpc": "2.0","method":"set","time":132123123}' -t '$device/26b4f71484/channel/11-8'
c.channel = channels.NewColorChannel(c)
err := c.device.driver.Conn.ExportChannel(c.device, c.channel, c.ID)
if err != nil {
log.Fatalf("Failed to announce color channel: %s", err)
}
go func() {
for {
log.Debugf("Polling for color")
err := c.fetchState()
if err != nil {
log.Errorf("Failed to poll for color state %s", err)
}
time.Sleep(10 * time.Second)
}
}()
return nil
}
func (c *ColorChannel) SetColor(state *channels.ColorState) error {
if state.Mode != "hue" {
return fmt.Errorf("TODO: Only color mode 'hue' is supported atm.")
}
spew.Dump("setting color", state)
hue := uint32(*state.Hue * float64(math.MaxUint8-1))
saturation := uint32(*state.Saturation * float64(math.MaxUint8-1))
request := &gateway.DevSetColorReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
},
HueValue: &hue,
SaturationValue: &saturation,
}
spew.Dump(request)
response := &gateway.GwZigbeeGenericRspInd{}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 2*time.Second)
if err != nil {
return fmt.Errorf("Error setting color state : %s", err)
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Failed to set color state. status: %s", response.Status.String())
}
return c.fetchState()
}
func (c *ColorChannel) fetchState() error {
request := &gateway.DevGetColorReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
},
}
response := &gateway.DevGetColorRspInd{}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 10*time.Second)
if err != nil {
return fmt.Errorf("Error getting color state : %s", err)
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Failed to get color state. status: %s", response.Status.String())
}
saturation := float64(float64(*response.SatValue) / float64(math.MaxUint8-1))
hue := float64(float64(*response.HueValue) / float64(math.MaxUint8-1))
state := &channels.ColorState{
Mode: "Hue",
Saturation: &saturation,
Hue: &hue,
}
c.channel.SendState(state)
return nil
}