-
Notifications
You must be signed in to change notification settings - Fork 1
/
AirZoneBaseThingHandler.java
280 lines (239 loc) · 11.7 KB
/
AirZoneBaseThingHandler.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Copyright (c) 2023-2024 Olivier Sannier
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* you can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/
package com.obones.binding.airzone.internal.handler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.ThingStatusInfo;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.binding.BridgeHandler;
import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.binding.builder.ThingBuilder;
import org.openhab.core.thing.type.AutoUpdatePolicy;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.obones.binding.airzone.internal.api.AirZoneApiManager;
import com.obones.binding.airzone.internal.api.AirZoneDetailedErrors;
import com.obones.binding.airzone.internal.api.model.AirZoneError;
import com.obones.binding.airzone.internal.utils.Localization;
/***
* The{@link AirZoneBaseThingHandler} the base class for things in this binding. It provides default
* mechanisms for handling commands, which are sent via {@link AirZoneBridgeHandler} to one of the channels.
*
* @author Olivier Sannier - Initial contribution
*/
@NonNullByDefault
public abstract class AirZoneBaseThingHandler extends BaseThingHandler {
private @NonNullByDefault({}) final Logger logger = LoggerFactory.getLogger(AirZoneBaseThingHandler.class);
private @NonNullByDefault({}) Set<String> channelsInActionCommand = Collections
.synchronizedSet(new HashSet<String>());
protected Localization localization;
protected static final Gson gson = new Gson();
public AirZoneBaseThingHandler(Thing thing, Localization localization) {
super(thing);
this.localization = localization;
logger.trace("AirZoneBaseThingHandler(thing={},localization={}) constructor called.", thing, localization);
}
@Override
public void initialize() {
logger.trace("initialize() called.");
Bridge thisBridge = getBridge();
logger.debug("initialize(): Initializing thing {} in combination with bridge {}.", getThing().getUID(),
thisBridge);
if (thisBridge == null) {
logger.trace("initialize() updating ThingStatus to OFFLINE/CONFIGURATION_PENDING.");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING);
} else if (thisBridge.getStatus() == ThingStatus.ONLINE) {
logger.trace("initialize() updating ThingStatus to ONLINE.");
createOptionalChannels();
initializeProperties();
updateStatus(ThingStatus.ONLINE);
} else {
logger.trace("initialize() updating ThingStatus to OFFLINE/BRIDGE_OFFLINE.");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
}
logger.trace("initialize() done.");
}
protected synchronized void initializeProperties() {
Bridge bridge = getBridge();
if (bridge != null) {
AirZoneBridgeHandler bridgeHandler = (AirZoneBridgeHandler) bridge.getHandler();
if (bridgeHandler != null) {
initializeProperties(bridgeHandler);
}
}
logger.trace("initializeProperties() done.");
}
protected abstract void initializeProperties(AirZoneBridgeHandler bridgeHandler);
protected synchronized void createOptionalChannels() {
Bridge bridge = getBridge();
if (bridge != null) {
AirZoneBridgeHandler bridgeHandler = (AirZoneBridgeHandler) bridge.getHandler();
if (bridgeHandler == null) {
logger.warn("createOptionalChannels: Could not get bridge handler");
return;
}
createOptionalChannels(bridgeHandler);
}
}
protected abstract void createOptionalChannels(AirZoneBridgeHandler bridgeHandler);
protected ThingBuilder createOptionalChannel(ThingHandlerCallback callback, ThingBuilder builder, ThingUID thingUID,
String ChannelId, ChannelTypeUID channelTypeUID, AutoUpdatePolicy autoUpdatePolicy,
@Nullable String labelKey, @Nullable String descriptionKey) {
ChannelUID channelUID = new ChannelUID(thing.getUID(), ChannelId);
ChannelBuilder channelBuilder = callback.createChannelBuilder(channelUID, channelTypeUID);
channelBuilder.withAutoUpdatePolicy(autoUpdatePolicy);
if (labelKey != null)
channelBuilder = channelBuilder.withLabel(localization.getText(labelKey));
if (descriptionKey != null)
channelBuilder = channelBuilder.withDescription(localization.getText(descriptionKey));
Channel channel = channelBuilder.build();
// Add channel (remove it beforehand to avoid duplicates, it's a no-op if it did not exist)
return builder.withoutChannel(channelUID).withChannel(channel);
}
protected ThingBuilder createOptionalChannel(ThingHandlerCallback callback, ThingBuilder builder, ThingUID thingUID,
String ChannelId, ChannelTypeUID channelTypeUID, AutoUpdatePolicy autoUpdatePolicy) {
return createOptionalChannel(callback, builder, thingUID, ChannelId, channelTypeUID, autoUpdatePolicy, null,
null);
}
protected ThingBuilder createOptionalChannel(ThingHandlerCallback callback, ThingBuilder builder, ThingUID thingUID,
String ChannelId, ChannelTypeUID channelTypeUID, @Nullable String labelKey,
@Nullable String descriptionKey) {
return createOptionalChannel(callback, builder, thingUID, ChannelId, channelTypeUID, AutoUpdatePolicy.DEFAULT,
labelKey, descriptionKey);
}
protected ThingBuilder createOptionalChannel(ThingHandlerCallback callback, ThingBuilder builder, ThingUID thingUID,
String ChannelId, ChannelTypeUID channelTypeUID) {
return createOptionalChannel(callback, builder, thingUID, ChannelId, channelTypeUID, null, null);
}
@Override
public void dispose() {
logger.trace("dispose() called.");
super.dispose();
}
@Override
public void channelLinked(ChannelUID channelUID) {
logger.trace("channelLinked({}) called.", channelUID.getAsString());
if (thing.getStatus() == ThingStatus.ONLINE) {
handleCommand(channelUID, RefreshType.REFRESH);
}
}
@Override
public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
if (isInitialized()) { // prevents change of address
validateConfigurationParameters(configurationParameters);
Configuration configuration = editConfiguration();
for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
logger.trace("handleConfigurationUpdate(): found modified config entry {}.",
configurationParameter.getKey());
configuration.put(configurationParameter.getKey(), configurationParameter.getValue());
}
// persist new configuration and reinitialize handler
dispose();
updateConfiguration(configuration);
initialize();
} else {
super.handleConfigurationUpdate(configurationParameters);
}
}
@Override
public void bridgeStatusChanged(ThingStatusInfo info) {
switch (info.getStatus()) {
case OFFLINE:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
break;
case ONLINE:
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
break;
default:
super.bridgeStatusChanged(info);
}
}
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("handleCommand({},{}) initiated by {}.", channelUID.getAsString(), command,
Thread.currentThread());
Bridge bridge = getBridge();
if (bridge == null) {
logger.trace("handleCommand() nothing yet to do as there is no bridge available.");
} else {
BridgeHandler handler = bridge.getHandler();
if (handler == null) {
logger.trace("handleCommand() nothing yet to do as thing is not initialized.");
} else {
AirZoneBridgeHandler bridgeHandler = (AirZoneBridgeHandler) handler;
AirZoneApiManager apiManager = bridgeHandler.getApiManager();
boolean commandHandled = false;
if (command instanceof RefreshType) {
commandHandled = refreshChannel(channelUID, apiManager);
} else {
channelsInActionCommand.add(channelUID.getAsString());
try {
commandHandled = handleActionCommand(channelUID, command, apiManager);
} finally {
channelsInActionCommand.remove(channelUID.getAsString());
}
}
if (!commandHandled)
bridgeHandler.handleCommand(channelUID, command);
}
}
}
protected abstract boolean handleActionCommand(ChannelUID channelUID, Command command,
AirZoneApiManager apiManager);
public abstract boolean refreshChannel(ChannelUID channelUID, AirZoneApiManager apiManager);
public abstract void refreshChannelsAndProperties(AirZoneApiManager apiManager, Set<ChannelUID> linkedChannelsUIDs);
protected State getErrorsToState(AirZoneError @Nullable [] airZoneErrors) {
var errors = new ArrayList<String>();
if (airZoneErrors != null) {
for (var airZoneError : airZoneErrors) {
String systemValue = airZoneError.getSystem();
String zoneValue = airZoneError.getZone();
boolean isSystem = systemValue != null;
boolean isZone = zoneValue != null;
String originName = isSystem ? "System" : (isZone ? "Zone" : "unknown");
String errorCode = isSystem ? systemValue : (isZone ? zoneValue : "unexpected");
String errorMessage = originName + ": " + errorCode;
@Nullable
String detailedErrorMessage = AirZoneDetailedErrors.getDetailedErrorMessage(errorCode, localization);
if (detailedErrorMessage != null)
errorMessage += " - " + detailedErrorMessage;
errors.add(errorMessage);
}
}
return new StringType(gson.toJson(errors.toArray()));
}
protected boolean channelIsInActionCommand(ChannelUID channelUID) {
return channelsInActionCommand.contains(channelUID.getAsString());
}
}