forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractNetatmoThingHandler.java
207 lines (181 loc) · 7.9 KB
/
AbstractNetatmoThingHandler.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* 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.netatmo.internal.handler;
import static org.eclipse.smarthome.core.library.unit.MetricPrefix.*;
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.measure.Unit;
import javax.measure.quantity.Angle;
import javax.measure.quantity.Dimensionless;
import javax.measure.quantity.Length;
import javax.measure.quantity.Pressure;
import javax.measure.quantity.Speed;
import javax.measure.quantity.Temperature;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.library.unit.SIUnits;
import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.type.ChannelKind;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.netatmo.internal.channelhelper.BatteryHelper;
import org.openhab.binding.netatmo.internal.channelhelper.RadioHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link AbstractNetatmoThingHandler} is the abstract class that handles
* common behaviors of all netatmo things
*
* @author Gaël L'hopital - Initial contribution OH2 version
* @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
*
*/
public abstract class AbstractNetatmoThingHandler extends BaseThingHandler {
private Logger logger = LoggerFactory.getLogger(AbstractNetatmoThingHandler.class);
// Units of measurement of the data delivered by the API
public static final Unit<Temperature> API_TEMPERATURE_UNIT = SIUnits.CELSIUS;
public static final Unit<Dimensionless> API_HUMIDITY_UNIT = SmartHomeUnits.PERCENT;
public static final Unit<Pressure> API_PRESSURE_UNIT = HECTO(SIUnits.PASCAL);
public static final Unit<Speed> API_WIND_SPEED_UNIT = SIUnits.KILOMETRE_PER_HOUR;
public static final Unit<Angle> API_WIND_DIRECTION_UNIT = SmartHomeUnits.DEGREE_ANGLE;
public static final Unit<Length> API_RAIN_UNIT = MILLI(SIUnits.METRE);
public static final Unit<Dimensionless> API_CO2_UNIT = SmartHomeUnits.PARTS_PER_MILLION;
public static final Unit<Dimensionless> API_NOISE_UNIT = SmartHomeUnits.DECIBEL;
protected final MeasurableChannels measurableChannels = new MeasurableChannels();
protected Optional<RadioHelper> radioHelper;
protected Optional<BatteryHelper> batteryHelper;
protected Configuration config;
protected NetatmoBridgeHandler bridgeHandler;
AbstractNetatmoThingHandler(@NonNull Thing thing) {
super(thing);
}
@Override
public void initialize() {
config = getThing().getConfiguration();
radioHelper = thing.getProperties().containsKey(PROPERTY_SIGNAL_LEVELS)
? Optional.of(new RadioHelper(thing.getProperties().get(PROPERTY_SIGNAL_LEVELS)))
: Optional.empty();
batteryHelper = thing.getProperties().containsKey(PROPERTY_BATTERY_LEVELS)
? Optional.of(new BatteryHelper(thing.getProperties().get(PROPERTY_BATTERY_LEVELS)))
: Optional.empty();
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Pending parent object initialization");
}
protected State getNAThingProperty(String channelId) {
Optional<State> result;
result = batteryHelper.flatMap(helper -> helper.getNAThingProperty(channelId));
if (result.isPresent()) {
return result.get();
}
result = radioHelper.flatMap(helper -> helper.getNAThingProperty(channelId));
if (result.isPresent()) {
return result.get();
}
result = measurableChannels.getNAThingProperty(channelId);
return result.orElse(UnDefType.UNDEF);
}
protected void updateChannels() {
getThing().getChannels().stream().filter(channel -> channel.getKind() != ChannelKind.TRIGGER)
.forEach(channel -> {
String channelId = channel.getUID().getId();
if (isLinked(channelId)) {
State state = getNAThingProperty(channelId);
if (state != null) {
updateState(channel.getUID(), state);
}
}
});
}
@Override
public void channelLinked(ChannelUID channelUID) {
super.channelLinked(channelUID);
measurableChannels.addChannel(channelUID);
}
@Override
public void channelUnlinked(ChannelUID channelUID) {
super.channelUnlinked(channelUID);
measurableChannels.removeChannel(channelUID);
}
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
if (command == RefreshType.REFRESH) {
logger.debug("Refreshing {}", channelUID);
updateChannels();
}
}
protected NetatmoBridgeHandler getBridgeHandler() {
if (bridgeHandler == null) {
Bridge bridge = getBridge();
if (bridge != null) {
bridgeHandler = (NetatmoBridgeHandler) bridge.getHandler();
}
}
return bridgeHandler;
}
public boolean matchesId(String searchedId) {
return searchedId != null && searchedId.equalsIgnoreCase(getId());
}
protected String getId() {
if (config != null) {
String equipmentId = (String) config.get(EQUIPMENT_ID);
return equipmentId.toLowerCase();
} else {
return null;
}
}
protected void updateProperties(Integer firmware, String modelId) {
Map<String, String> properties = editProperties();
if (firmware != null || modelId != null) {
properties.put(Thing.PROPERTY_VENDOR, VENDOR);
}
if (firmware != null) {
properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmware.toString());
}
if (modelId != null) {
properties.put(Thing.PROPERTY_MODEL_ID, modelId);
}
updateProperties(properties);
}
public void updateMeasurements() {
}
public void getMeasurements(NetatmoBridgeHandler handler, String device, @Nullable String module, String scale,
List<String> types, List<String> channels, Map<String, Float> channelMeasurements) {
if (types.size() != channels.size()) {
throw new IllegalArgumentException("types and channels lists are different sizes.");
}
List<Float> measurements = handler.getStationMeasureResponses(device, module, scale, types);
if (measurements.size() != types.size()) {
throw new IllegalArgumentException("types and measurements lists are different sizes.");
}
int i = 0;
for (Float measurement : measurements) {
channelMeasurements.put(channels.get(i++), measurement);
}
}
public void addMeasurement(List<String> channels, List<String> types, String channel, String type) {
if (isLinked(channel)) {
channels.add(channel);
types.add(type);
}
}
}