/
ZigBeeConverterTemperature.java
175 lines (152 loc) · 6.88 KB
/
ZigBeeConverterTemperature.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.zigbee.internal.converter;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.zigbee.ZigBeeBindingConstants;
import org.openhab.binding.zigbee.converter.ZigBeeBaseChannelConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zsmartsystems.zigbee.CommandResult;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.zcl.ZclAttribute;
import com.zsmartsystems.zigbee.zcl.ZclAttributeListener;
import com.zsmartsystems.zigbee.zcl.clusters.ZclTemperatureMeasurementCluster;
import com.zsmartsystems.zigbee.zcl.protocol.ZclClusterType;
/**
* Converter for the temperature channel
*
* @author Chris Jackson - Initial Contribution
*
*/
public class ZigBeeConverterTemperature extends ZigBeeBaseChannelConverter implements ZclAttributeListener {
private Logger logger = LoggerFactory.getLogger(ZigBeeConverterTemperature.class);
private ZclTemperatureMeasurementCluster cluster;
private ZclTemperatureMeasurementCluster clusterClient;
private ZclAttribute attribute;
private ZclAttribute attributeClient;
@Override
public Set<Integer> getImplementedClientClusters() {
return Collections.singleton(ZclTemperatureMeasurementCluster.CLUSTER_ID);
}
@Override
public Set<Integer> getImplementedServerClusters() {
return Collections.singleton(ZclTemperatureMeasurementCluster.CLUSTER_ID);
}
@Override
public boolean initializeDevice() {
ZclTemperatureMeasurementCluster clientCluster = (ZclTemperatureMeasurementCluster) endpoint
.getInputCluster(ZclTemperatureMeasurementCluster.CLUSTER_ID);
if (clientCluster == null) {
// Nothing to do, but we still return success
return true;
}
ZclTemperatureMeasurementCluster serverCluster = (ZclTemperatureMeasurementCluster) endpoint
.getInputCluster(ZclTemperatureMeasurementCluster.CLUSTER_ID);
if (serverCluster == null) {
logger.error("{}: Error opening device temperature measurement cluster", endpoint.getIeeeAddress());
return false;
}
try {
CommandResult bindResponse = bind(serverCluster).get();
if (bindResponse.isSuccess()) {
// Configure reporting
ZclAttribute attribute = serverCluster
.getAttribute(ZclTemperatureMeasurementCluster.ATTR_MEASUREDVALUE);
CommandResult reportingResponse = attribute.setReporting(1, REPORTING_PERIOD_DEFAULT_MAX, 0.1).get();
handleReportingResponse(reportingResponse, POLLING_PERIOD_DEFAULT, REPORTING_PERIOD_DEFAULT_MAX);
} else {
logger.debug("{}: Failed to bind temperature measurement cluster", endpoint.getIeeeAddress());
}
} catch (InterruptedException | ExecutionException e) {
logger.error("{}: Exception setting reporting ", endpoint.getIeeeAddress(), e);
return false;
}
return true;
}
@Override
public boolean initializeConverter() {
cluster = (ZclTemperatureMeasurementCluster) endpoint
.getInputCluster(ZclTemperatureMeasurementCluster.CLUSTER_ID);
if (cluster != null) {
attribute = cluster.getAttribute(ZclTemperatureMeasurementCluster.ATTR_MEASUREDVALUE);
// Add a listener
cluster.addAttributeListener(this);
} else {
clusterClient = (ZclTemperatureMeasurementCluster) endpoint
.getOutputCluster(ZclTemperatureMeasurementCluster.CLUSTER_ID);
attributeClient = clusterClient.getLocalAttribute(ZclTemperatureMeasurementCluster.ATTR_MEASUREDVALUE);
attributeClient.setImplemented(true);
}
if (cluster == null && clusterClient == null) {
logger.error("{}: Error opening device temperature measurement cluster", endpoint.getIeeeAddress());
return false;
}
return true;
}
@Override
public void disposeConverter() {
if (cluster != null) {
cluster.removeAttributeListener(this);
}
}
@Override
public void handleRefresh() {
if (attribute != null) {
attribute.readValue(0);
}
}
@Override
public void handleCommand(final Command command) {
if (attributeClient == null) {
logger.warn("{}: Temperature measurement update but remote client not set", endpoint.getIeeeAddress(),
command, command.getClass().getSimpleName());
return;
}
Integer value = temperatureToValue(command);
if (value == null) {
logger.warn("{}: Temperature measurement update {} [{}] was not processed", endpoint.getIeeeAddress(),
command, command.getClass().getSimpleName());
return;
}
attributeClient.setValue(value);
attributeClient.reportValue(value);
}
@Override
public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) {
if (endpoint.getOutputCluster(ZclTemperatureMeasurementCluster.CLUSTER_ID) == null
&& endpoint.getInputCluster(ZclTemperatureMeasurementCluster.CLUSTER_ID) == null) {
logger.trace("{}: Temperature measurement cluster not found", endpoint.getIeeeAddress());
return null;
}
return ChannelBuilder
.create(createChannelUID(thingUID, endpoint, ZigBeeBindingConstants.CHANNEL_NAME_TEMPERATURE_VALUE),
ZigBeeBindingConstants.ITEM_TYPE_NUMBER_TEMPERATURE)
.withType(ZigBeeBindingConstants.CHANNEL_TEMPERATURE_VALUE)
.withLabel(ZigBeeBindingConstants.CHANNEL_LABEL_TEMPERATURE_VALUE)
.withProperties(createProperties(endpoint)).build();
}
@Override
public void attributeUpdated(ZclAttribute attribute, Object val) {
logger.debug("{}: ZigBee attribute reports {}", endpoint.getIeeeAddress(), attribute);
if (attribute.getCluster() == ZclClusterType.TEMPERATURE_MEASUREMENT
&& attribute.getId() == ZclTemperatureMeasurementCluster.ATTR_MEASUREDVALUE) {
updateChannelState(valueToTemperature((Integer) val));
}
}
}