-
Notifications
You must be signed in to change notification settings - Fork 7
/
HumidityChannel.go
91 lines (75 loc) · 2.54 KB
/
HumidityChannel.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
package main
import (
"fmt"
"time"
"github.com/ninjasphere/go-ninja/channels"
"github.com/ninjasphere/go-zigbee/gateway"
)
type HumidityChannel struct {
Channel
channel *channels.HumidityChannel
}
func (c *HumidityChannel) init() error {
log.Debugf("Initialising Humidity channel of device %d", *c.device.deviceInfo.IeeeAddress)
clusterID := ClusterIDHumidity
instantaneousDemandAttributeID := uint32(0x0400)
minReportInterval := uint32(10)
maxReportInterval := uint32(120)
reportableChange := uint32(1)
request := &gateway.GwSetAttributeReportingReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
EndpointId: c.endpoint.EndpointId,
},
ClusterId: &clusterID,
AttributeReportList: []*gateway.GwAttributeReportT{{
AttributeId: &instantaneousDemandAttributeID,
AttributeType: gateway.GwZclAttributeDataTypesT_ZCL_DATATYPE_INT24.Enum(),
MinReportInterval: &minReportInterval,
MaxReportInterval: &maxReportInterval,
ReportableChange: &reportableChange,
}},
}
response := &gateway.GwSetAttributeReportingRspInd{}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 20*time.Second)
if err != nil {
log.Errorf("Error enabling Humidity reporting: %s", err)
} else if response.Status.String() != "STATUS_SUCCESS" {
log.Errorf("Failed to enable Humidity reporting. status: %s", response.Status.String())
}
c.channel = channels.NewHumidityChannel(c)
err = c.device.driver.Conn.ExportChannel(c.device, c.channel, c.ID)
if err != nil {
log.Fatalf("Failed to announce Humidity channel: %s", err)
}
go func() {
for {
err := c.fetchState()
if err != nil {
log.Errorf("Failed to poll for Humidity %s", err)
}
time.Sleep(1 * time.Minute)
}
}()
return nil
}
func (c *HumidityChannel) fetchState() error {
request := &gateway.DevGetHumidityReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: c.device.deviceInfo.IeeeAddress,
},
}
response := &gateway.DevGetHumidityRspInd{}
err := c.device.driver.gatewayConn.SendAsyncCommand(request, response, 10*time.Second)
if err != nil {
return fmt.Errorf("Error getting Humidity level : %s", err)
}
if response.Status.String() != "STATUS_SUCCESS" {
return fmt.Errorf("Failed to get Humidity level. status: %s", response.Status.String())
}
log.Debugf("Got Humidity value %d", *response.HumidityValue)
c.channel.SendState(float64(*response.HumidityValue) / 0x2710)
return nil
}