forked from ninjasphere/driver-go-zigbee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnOffChannel.go
147 lines (118 loc) · 3.79 KB
/
OnOffChannel.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
package main
import (
"fmt"
"time"
"github.com/ninjasphere/go-ninja/channels"
"github.com/ninjasphere/go-zigbee/gateway"
)
type OnOffChannel struct {
Channel
lastState *bool
channel *channels.OnOffChannel
}
// -------- On/Off Protocol --------
func (c *OnOffChannel) TurnOn() error {
return c.setState(gateway.GwOnOffStateT_ON_STATE.Enum())
}
func (c *OnOffChannel) TurnOff() error {
return c.setState(gateway.GwOnOffStateT_OFF_STATE.Enum())
}
func (c *OnOffChannel) ToggleOnOff() error {
return c.setState(gateway.GwOnOffStateT_TOGGLE_STATE.Enum())
}
func (c *OnOffChannel) SetOnOff(state bool) error {
if state {
return c.TurnOn()
}
return c.TurnOff()
}
func (c *OnOffChannel) init() error {
log.Debugf("Initialising on/off 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())
}*/
c.channel = channels.NewOnOffChannel(c)
err = c.device.driver.Conn.ExportChannel(c.device, c.channel, c.ID)
if err != nil {
log.Fatalf("Failed to announce on/off channel: %s", err)
}
go func() {
for {
log.Debugf("Polling for on/off")
err := c.fetchState()
if err != nil {
log.Errorf("Failed to poll for on/off state %s", err)
}
time.Sleep(10 * time.Second)
}
}()
return nil
}
func (c *OnOffChannel) setState(state *gateway.GwOnOffStateT) error {
request := &gateway.DevSetOnOffStateReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
},
State: state,
}
response := &gateway.GwZigbeeGenericRspInd{}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 2*time.Second)
if err != nil {
return fmt.Errorf("Error setting on/off state : %s", err)
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Failed to set on/off state. status: %s", response.Status.String())
}
c.lastState = nil
return c.fetchState()
}
func (c *OnOffChannel) fetchState() error {
request := &gateway.DevGetOnOffStateReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
},
}
response := &gateway.DevGetOnOffStateRspInd{}
if c.device.driver == nil {
log.Fatalf("assertion failed: c.device.driver != nil")
}
if c.device.driver.gatewayConn == nil {
log.Fatalf("assertion failed: c.device.driver.gatewayConn != nil")
}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 10*time.Second)
if err != nil {
return fmt.Errorf("Error getting on/off state : %s", err)
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Failed to get on/off state. status: %s", response.Status.String())
}
state := *response.StateValue == gateway.GwOnOffStateValueT_ON
if c.lastState == nil || *c.lastState != state {
c.lastState = &state
c.channel.SendState(state)
}
return nil
}