-
Notifications
You must be signed in to change notification settings - Fork 7
/
BrightnessChannel.go
125 lines (102 loc) · 3.49 KB
/
BrightnessChannel.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
package main
import (
"fmt"
"math"
"time"
"github.com/ninjasphere/go-ninja/channels"
"github.com/ninjasphere/go-zigbee/gateway"
)
type BrightnessChannel struct {
Channel
lastState *float64
channel *channels.BrightnessChannel
}
// -------- Brightness Protocol --------
func (c *BrightnessChannel) init() error {
log.Debugf("Initialising brightness 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.NewBrightnessChannel(c)
err := c.device.driver.Conn.ExportChannel(c.device, c.channel, c.ID)
if err != nil {
log.Fatalf("Failed to announce brightness channel: %s", err)
}
go func() {
for {
log.Debugf("Polling for brightness")
err := c.fetchState()
if err != nil {
log.Errorf("Failed to poll for brightness state %s", err)
}
time.Sleep(10 * time.Second)
}
}()
return nil
}
func (c *BrightnessChannel) SetBrightness(state float64) error {
level := uint32(state * float64(math.MaxUint8))
transition := uint32(10) // 1/100th seconds?
request := &gateway.DevSetLevelReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
},
LevelValue: &level,
TransitionTime: &transition,
}
response := &gateway.GwZigbeeGenericRspInd{}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 2*time.Second)
if err != nil {
return fmt.Errorf("Error setting brightness state : %s", err)
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Failed to set brightness state. status: %s", response.Status.String())
}
c.lastState = nil
return c.fetchState()
}
func (c *BrightnessChannel) fetchState() error {
request := &gateway.DevGetLevelReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
},
}
response := &gateway.DevGetLevelRspInd{}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 10*time.Second)
if err != nil {
return fmt.Errorf("Error getting brightness state : %s", err)
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Failed to get brightness state. status: %s", response.Status.String())
}
state := float64(*response.LevelValue) / float64(math.MaxUint8)
if c.lastState == nil || *c.lastState != state {
c.lastState = &state
c.channel.SendState(state)
}
return nil
}