2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hdpowerview.internal.handler;
15 import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
16 import static org.openhab.binding.hdpowerview.internal.api.CoordinateSystem.*;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import javax.ws.rs.NotSupportedException;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
27 import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
28 import org.openhab.binding.hdpowerview.internal.api.CoordinateSystem;
29 import org.openhab.binding.hdpowerview.internal.api.Firmware;
30 import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
31 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
32 import org.openhab.binding.hdpowerview.internal.api.responses.Survey;
33 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration;
34 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
35 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
36 import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
37 import org.openhab.binding.hdpowerview.internal.exceptions.HubInvalidResponseException;
38 import org.openhab.binding.hdpowerview.internal.exceptions.HubMaintenanceException;
39 import org.openhab.binding.hdpowerview.internal.exceptions.HubProcessingException;
40 import org.openhab.core.library.types.DecimalType;
41 import org.openhab.core.library.types.OnOffType;
42 import org.openhab.core.library.types.PercentType;
43 import org.openhab.core.library.types.QuantityType;
44 import org.openhab.core.library.types.StopMoveType;
45 import org.openhab.core.library.types.StringType;
46 import org.openhab.core.library.types.UpDownType;
47 import org.openhab.core.library.unit.Units;
48 import org.openhab.core.thing.Bridge;
49 import org.openhab.core.thing.ChannelUID;
50 import org.openhab.core.thing.Thing;
51 import org.openhab.core.thing.ThingStatus;
52 import org.openhab.core.thing.ThingStatusDetail;
53 import org.openhab.core.types.Command;
54 import org.openhab.core.types.RefreshType;
55 import org.openhab.core.types.UnDefType;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * Handles commands for an HD PowerView Shade
62 * @author Andy Lintner - Initial contribution
63 * @author Andrew Fiddian-Green - Added support for secondary rail positions
66 public class HDPowerViewShadeHandler extends AbstractHubbedThingHandler {
68 private enum RefreshKind {
74 private static final String COMMAND_CALIBRATE = "CALIBRATE";
76 private final Logger logger = LoggerFactory.getLogger(HDPowerViewShadeHandler.class);
77 private final ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
79 private @Nullable ScheduledFuture<?> refreshPositionFuture = null;
80 private @Nullable ScheduledFuture<?> refreshSignalFuture = null;
81 private @Nullable ScheduledFuture<?> refreshBatteryLevelFuture = null;
82 private @Nullable Capabilities capabilities;
84 private boolean isDisposing;
86 public HDPowerViewShadeHandler(Thing thing) {
91 public void initialize() {
93 shadeId = getConfigAs(HDPowerViewShadeConfiguration.class).id;
94 logger.debug("Initializing shade handler for shade {}", shadeId);
96 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
97 "@text/offline.conf-error.invalid-id");
100 Bridge bridge = getBridge();
101 if (bridge == null) {
102 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
105 if (!(bridge.getHandler() instanceof HDPowerViewHubHandler)) {
106 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
107 "@text/offline.conf-error.invalid-bridge-handler");
110 ThingStatus bridgeStatus = bridge.getStatus();
111 if (bridgeStatus == ThingStatus.ONLINE) {
112 updateStatus(ThingStatus.UNKNOWN);
114 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
119 public void dispose() {
120 logger.debug("Disposing shade handler for shade {}", shadeId);
122 ScheduledFuture<?> future = refreshPositionFuture;
123 if (future != null) {
126 refreshPositionFuture = null;
127 future = refreshSignalFuture;
128 if (future != null) {
131 refreshSignalFuture = null;
132 future = refreshBatteryLevelFuture;
133 if (future != null) {
136 refreshBatteryLevelFuture = null;
141 public void handleCommand(ChannelUID channelUID, Command command) {
142 String channelId = channelUID.getId();
144 if (RefreshType.REFRESH == command) {
146 case CHANNEL_SHADE_POSITION:
147 case CHANNEL_SHADE_SECONDARY_POSITION:
148 case CHANNEL_SHADE_VANE:
149 requestRefreshShadePosition();
151 case CHANNEL_SHADE_LOW_BATTERY:
152 case CHANNEL_SHADE_BATTERY_LEVEL:
153 case CHANNEL_SHADE_BATTERY_VOLTAGE:
154 requestRefreshShadeBatteryLevel();
156 case CHANNEL_SHADE_SIGNAL_STRENGTH:
157 requestRefreshShadeSurvey();
163 HDPowerViewHubHandler bridge = getBridgeHandler();
164 if (bridge == null) {
165 logger.warn("Missing bridge handler");
168 HDPowerViewWebTargets webTargets = bridge.getWebTargets();
169 if (webTargets == null) {
170 logger.warn("Web targets not initialized");
174 handleShadeCommand(channelId, command, webTargets, shadeId);
175 } catch (HubInvalidResponseException e) {
176 Throwable cause = e.getCause();
178 logger.warn("Bridge returned a bad JSON response: {}", e.getMessage());
180 logger.warn("Bridge returned a bad JSON response: {} -> {}", e.getMessage(), cause.getMessage());
182 } catch (HubMaintenanceException e) {
183 // exceptions are logged in HDPowerViewWebTargets
184 } catch (HubException e) {
185 // ScheduledFutures will be cancelled by dispose(), naturally causing InterruptedException in invoke()
186 // for any ongoing requests. Logging this would only cause confusion.
188 logger.warn("Unexpected error: {}", e.getMessage());
193 private void handleShadeCommand(String channelId, Command command, HDPowerViewWebTargets webTargets, int shadeId)
194 throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
196 case CHANNEL_SHADE_POSITION:
197 if (command instanceof PercentType) {
198 moveShade(PRIMARY_POSITION, ((PercentType) command).intValue(), webTargets, shadeId);
199 } else if (command instanceof UpDownType) {
200 moveShade(PRIMARY_POSITION, UpDownType.UP == command ? 0 : 100, webTargets, shadeId);
201 } else if (command instanceof StopMoveType) {
202 if (StopMoveType.STOP == command) {
203 stopShade(webTargets, shadeId);
205 logger.warn("Unexpected StopMoveType command");
210 case CHANNEL_SHADE_VANE:
211 if (command instanceof PercentType) {
212 moveShade(VANE_TILT_POSITION, ((PercentType) command).intValue(), webTargets, shadeId);
213 } else if (command instanceof OnOffType) {
214 moveShade(VANE_TILT_POSITION, OnOffType.ON == command ? 100 : 0, webTargets, shadeId);
218 case CHANNEL_SHADE_SECONDARY_POSITION:
219 if (command instanceof PercentType) {
220 moveShade(SECONDARY_POSITION, ((PercentType) command).intValue(), webTargets, shadeId);
221 } else if (command instanceof UpDownType) {
222 moveShade(SECONDARY_POSITION, UpDownType.UP == command ? 0 : 100, webTargets, shadeId);
223 } else if (command instanceof StopMoveType) {
224 if (StopMoveType.STOP == command) {
225 stopShade(webTargets, shadeId);
227 logger.warn("Unexpected StopMoveType command");
232 case CHANNEL_SHADE_COMMAND:
233 if (command instanceof StringType) {
234 if (COMMAND_CALIBRATE.equals(((StringType) command).toString())) {
235 logger.debug("Calibrate shade {}", shadeId);
236 calibrateShade(webTargets, shadeId);
239 logger.warn("Unsupported command: {}. Supported commands are: " + COMMAND_CALIBRATE, command);
246 * Update the state of the channels based on the ShadeData provided.
248 * @param shadeData the ShadeData to be used; may be null.
250 protected void onReceiveUpdate(@Nullable ShadeData shadeData) {
251 if (shadeData != null) {
252 updateStatus(ThingStatus.ONLINE);
253 updateCapabilities(shadeData);
254 updateSoftProperties(shadeData);
255 updateFirmwareProperties(shadeData);
256 ShadePosition shadePosition = shadeData.positions;
257 if (shadePosition != null) {
258 updatePositionStates(shadePosition);
260 updateBatteryLevelStates(shadeData.batteryStatus);
261 updateState(CHANNEL_SHADE_BATTERY_VOLTAGE,
262 shadeData.batteryStrength > 0 ? new QuantityType<>(shadeData.batteryStrength / 10, Units.VOLT)
264 updateState(CHANNEL_SHADE_SIGNAL_STRENGTH, new DecimalType(shadeData.signalStrength));
266 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
270 private void updateCapabilities(ShadeData shade) {
271 if (capabilities != null) {
275 Integer value = shade.capabilities;
277 int valueAsInt = value.intValue();
278 logger.debug("Caching capabilities {} for shade {}", valueAsInt, shade.id);
279 capabilities = db.getCapabilities(valueAsInt);
281 logger.debug("Capabilities not included in shade response");
285 private Capabilities getCapabilitiesOrDefault() {
286 Capabilities capabilities = this.capabilities;
287 if (capabilities == null) {
288 return new Capabilities();
294 * Update the Thing's properties based on the contents of the provided ShadeData.
296 * Checks the database of known Shade 'types' and 'capabilities' and logs any unknown or incompatible values, so
297 * that developers can be kept updated about the potential need to add support for that type resp. capabilities.
301 private void updateSoftProperties(ShadeData shadeData) {
302 final Map<String, String> properties = getThing().getProperties();
303 boolean propChanged = false;
305 // update 'type' property
306 final int type = shadeData.type;
307 String propKey = HDPowerViewBindingConstants.PROPERTY_SHADE_TYPE;
308 String propOldVal = properties.getOrDefault(propKey, "");
309 String propNewVal = db.getType(type).toString();
310 if (!propNewVal.equals(propOldVal)) {
312 getThing().setProperty(propKey, propNewVal);
313 if ((type > 0) && !db.isTypeInDatabase(type)) {
314 db.logTypeNotInDatabase(type);
318 // update 'capabilities' property
319 final Integer temp = shadeData.capabilities;
320 final int capabilitiesVal = temp != null ? temp.intValue() : -1;
321 Capabilities capabilities = db.getCapabilities(capabilitiesVal);
322 propKey = HDPowerViewBindingConstants.PROPERTY_SHADE_CAPABILITIES;
323 propOldVal = properties.getOrDefault(propKey, "");
324 propNewVal = capabilities.toString();
325 if (!propNewVal.equals(propOldVal)) {
327 getThing().setProperty(propKey, propNewVal);
328 if ((capabilitiesVal >= 0) && !db.isCapabilitiesInDatabase(capabilitiesVal)) {
329 db.logCapabilitiesNotInDatabase(type, capabilitiesVal);
333 if (propChanged && db.isCapabilitiesInDatabase(capabilitiesVal) && db.isTypeInDatabase(type)
334 && (capabilitiesVal != db.getType(type).getCapabilities())) {
335 db.logCapabilitiesMismatch(type, capabilitiesVal);
339 private void updateFirmwareProperties(ShadeData shadeData) {
340 Map<String, String> properties = editProperties();
341 Firmware shadeFirmware = shadeData.firmware;
342 Firmware motorFirmware = shadeData.motor;
343 if (shadeFirmware != null) {
344 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, shadeFirmware.toString());
346 if (motorFirmware != null) {
347 properties.put(PROPERTY_MOTOR_FIRMWARE_VERSION, motorFirmware.toString());
349 updateProperties(properties);
353 * After a hard refresh, update the Thing's properties based on the contents of the provided ShadeData.
355 * Checks if the secondary support capabilities in the database of known Shade 'types' and 'capabilities' matches
356 * that implied by the ShadeData and logs any incompatible values, so that developers can be kept updated about the
357 * potential need to add support for that type resp. capabilities.
361 private void updateHardProperties(ShadeData shadeData) {
362 final ShadePosition positions = shadeData.positions;
363 if (positions == null) {
366 Capabilities capabilities = getCapabilitiesOrDefault();
367 final Map<String, String> properties = getThing().getProperties();
369 // update 'secondary rail detected' property
370 String propKey = HDPowerViewBindingConstants.PROPERTY_SECONDARY_RAIL_DETECTED;
371 String propOldVal = properties.getOrDefault(propKey, "");
372 boolean propNewBool = positions.secondaryRailDetected();
373 String propNewVal = String.valueOf(propNewBool);
374 if (!propNewVal.equals(propOldVal)) {
375 getThing().setProperty(propKey, propNewVal);
376 if (propNewBool != capabilities.supportsSecondary()) {
377 db.logPropertyMismatch(propKey, shadeData.type, capabilities.getValue(), propNewBool);
381 // update 'tilt anywhere detected' property
382 propKey = HDPowerViewBindingConstants.PROPERTY_TILT_ANYWHERE_DETECTED;
383 propOldVal = properties.getOrDefault(propKey, "");
384 propNewBool = positions.tiltAnywhereDetected();
385 propNewVal = String.valueOf(propNewBool);
386 if (!propNewVal.equals(propOldVal)) {
387 getThing().setProperty(propKey, propNewVal);
388 if (propNewBool != capabilities.supportsTiltAnywhere()) {
389 db.logPropertyMismatch(propKey, shadeData.type, capabilities.getValue(), propNewBool);
394 private void updatePositionStates(ShadePosition shadePos) {
395 Capabilities capabilities = this.capabilities;
396 if (capabilities == null) {
397 logger.debug("The 'shadeCapabilities' field has not yet been initialized");
398 updateState(CHANNEL_SHADE_POSITION, UnDefType.UNDEF);
399 updateState(CHANNEL_SHADE_VANE, UnDefType.UNDEF);
400 updateState(CHANNEL_SHADE_SECONDARY_POSITION, UnDefType.UNDEF);
403 updateState(CHANNEL_SHADE_POSITION, shadePos.getState(capabilities, PRIMARY_POSITION));
404 updateState(CHANNEL_SHADE_VANE, shadePos.getState(capabilities, VANE_TILT_POSITION));
405 updateState(CHANNEL_SHADE_SECONDARY_POSITION, shadePos.getState(capabilities, SECONDARY_POSITION));
408 private void updateBatteryLevelStates(int batteryStatus) {
410 switch (batteryStatus) {
418 case 4: // Plugged in
421 default: // No status available (0) or invalid
422 updateState(CHANNEL_SHADE_LOW_BATTERY, UnDefType.UNDEF);
423 updateState(CHANNEL_SHADE_BATTERY_LEVEL, UnDefType.UNDEF);
426 updateState(CHANNEL_SHADE_LOW_BATTERY, batteryStatus == 1 ? OnOffType.ON : OnOffType.OFF);
427 updateState(CHANNEL_SHADE_BATTERY_LEVEL, new DecimalType(mappedValue));
430 private void moveShade(CoordinateSystem coordSys, int newPercent, HDPowerViewWebTargets webTargets, int shadeId)
431 throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
432 ShadePosition newPosition = null;
433 // (try to) read the positions from the hub
434 ShadeData shadeData = webTargets.getShade(shadeId);
435 updateCapabilities(shadeData);
436 newPosition = shadeData.positions;
437 // if no positions returned, then create a new position
438 if (newPosition == null) {
439 newPosition = new ShadePosition();
441 Capabilities capabilities = getCapabilitiesOrDefault();
442 // set the new position value, and write the positions to the hub
443 shadeData = webTargets.moveShade(shadeId, newPosition.setPosition(capabilities, coordSys, newPercent));
444 updateShadePositions(shadeData);
447 private void stopShade(HDPowerViewWebTargets webTargets, int shadeId)
448 throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
449 updateShadePositions(webTargets.stopShade(shadeId));
450 // Positions in response from stop motion is not updated to to actual positions yet,
451 // so we need to request hard refresh.
452 requestRefreshShadePosition();
455 private void calibrateShade(HDPowerViewWebTargets webTargets, int shadeId)
456 throws HubInvalidResponseException, HubProcessingException, HubMaintenanceException {
457 updateShadePositions(webTargets.calibrateShade(shadeId));
460 private void updateShadePositions(ShadeData shadeData) {
461 ShadePosition shadePosition = shadeData.positions;
462 if (shadePosition == null) {
465 updateCapabilities(shadeData);
466 updatePositionStates(shadePosition);
470 * Request that the shade shall undergo a 'hard' refresh for querying its current position
472 protected synchronized void requestRefreshShadePosition() {
473 if (refreshPositionFuture == null) {
474 refreshPositionFuture = scheduler.schedule(this::doRefreshShadePosition, 0, TimeUnit.SECONDS);
479 * Request that the shade shall undergo a 'hard' refresh for querying its survey data
481 protected synchronized void requestRefreshShadeSurvey() {
482 if (refreshSignalFuture == null) {
483 refreshSignalFuture = scheduler.schedule(this::doRefreshShadeSignal, 0, TimeUnit.SECONDS);
488 * Request that the shade shall undergo a 'hard' refresh for querying its battery level state
490 protected synchronized void requestRefreshShadeBatteryLevel() {
491 if (refreshBatteryLevelFuture == null) {
492 refreshBatteryLevelFuture = scheduler.schedule(this::doRefreshShadeBatteryLevel, 0, TimeUnit.SECONDS);
496 private void doRefreshShadePosition() {
497 this.doRefreshShade(RefreshKind.POSITION);
498 refreshPositionFuture = null;
501 private void doRefreshShadeSignal() {
502 this.doRefreshShade(RefreshKind.SURVEY);
503 refreshSignalFuture = null;
506 private void doRefreshShadeBatteryLevel() {
507 this.doRefreshShade(RefreshKind.BATTERY_LEVEL);
508 refreshBatteryLevelFuture = null;
511 private void doRefreshShade(RefreshKind kind) {
513 HDPowerViewHubHandler bridge;
514 if ((bridge = getBridgeHandler()) == null) {
515 throw new HubProcessingException("Missing bridge handler");
517 HDPowerViewWebTargets webTargets = bridge.getWebTargets();
518 if (webTargets == null) {
519 throw new HubProcessingException("Web targets not initialized");
524 shadeData = webTargets.refreshShadePosition(shadeId);
527 Survey survey = webTargets.getShadeSurvey(shadeId);
528 if (survey.surveyData != null) {
529 logger.debug("Survey response for shade {}: {}", survey.shadeId, survey.toString());
531 logger.warn("No response from shade {} survey", shadeId);
535 shadeData = webTargets.refreshShadeBatteryLevel(shadeId);
538 throw new NotSupportedException("Unsupported refresh kind " + kind.toString());
540 if (Boolean.TRUE.equals(shadeData.timedOut)) {
541 logger.warn("Shade {} wireless refresh time out", shadeId);
542 } else if (kind == RefreshKind.POSITION) {
543 updateShadePositions(shadeData);
544 updateHardProperties(shadeData);
546 } catch (HubInvalidResponseException e) {
547 Throwable cause = e.getCause();
549 logger.warn("Bridge returned a bad JSON response: {}", e.getMessage());
551 logger.warn("Bridge returned a bad JSON response: {} -> {}", e.getMessage(), cause.getMessage());
553 } catch (HubMaintenanceException e) {
554 // exceptions are logged in HDPowerViewWebTargets
555 } catch (HubException e) {
556 // ScheduledFutures will be cancelled by dispose(), naturally causing InterruptedException in invoke()
557 // for any ongoing requests. Logging this would only cause confusion.
559 logger.warn("Unexpected error: {}", e.getMessage());