-
Notifications
You must be signed in to change notification settings - Fork 7
/
OnOffSwitchCluster.go
75 lines (56 loc) · 1.89 KB
/
OnOffSwitchCluster.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
package main
import (
"time"
"github.com/davecgh/go-spew/spew"
"github.com/ninjasphere/go-zigbee/nwkmgr"
)
type OnOffSwitchCluster struct {
Channel
SendEvent func(event string, payload ...interface{}) error
}
func (c *OnOffSwitchCluster) SetEventHandler(handler func(event string, payload ...interface{}) error) {
c.SendEvent = handler
}
func (c *OnOffSwitchCluster) GetProtocol() string {
return "button-momentary"
}
func (c *OnOffSwitchCluster) init() error {
log.Debugf("Initialising on/off button cluster of device %d", *c.device.deviceInfo.IeeeAddress)
clusterID := uint32(0x06)
dstEndpoint := uint32(5)
bindReq := &nwkmgr.NwkSetBindingEntryReq{
SrcAddr: &nwkmgr.NwkAddressStructT{
AddressType: nwkmgr.NwkAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
EndpointId: c.endpoint.EndpointId,
},
ClusterId: &clusterID,
DstAddr: &nwkmgr.NwkAddressStructT{
AddressType: nwkmgr.NwkAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.driver.localDevice.IeeeAddress,
EndpointId: &dstEndpoint,
},
BindingMode: nwkmgr.NwkBindingModeT_BIND.Enum(),
}
log.Infof("Binding on-off cluster %v", bindReq)
bindRes := &nwkmgr.NwkSetBindingEntryRspInd{}
err := c.device.driver.nwkmgrConn.SendAsyncCommand(bindReq, bindRes, time.Second*10)
if err != nil {
log.Errorf("Error binding on/off cluster: %s", err)
} else if bindRes.Status.String() != "STATUS_SUCCESS" {
log.Errorf("Failed to bind on/off cluster. status: %s", bindRes.Status.String())
}
update := c.device.driver.gatewayConn.OnBoundCluster(*c.device.deviceInfo.IeeeAddress, *c.endpoint.EndpointId, clusterID)
go func() {
for {
state := <-update
spew.Dump("Incoming on/off state:", state)
c.SendEvent("pressed", true)
}
}()
err = c.device.driver.Conn.ExportChannel(c.device, c, c.ID)
if err != nil {
log.Fatalf("Failed to announce on/off switch channel: %s", err)
}
return nil
}