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.io.homekit.internal;
15 import static org.openhab.io.homekit.internal.HomekitCommandType.*;
16 import static org.openhab.io.homekit.internal.HomekitDimmerMode.*;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.common.ThreadPoolManager;
27 import org.openhab.core.items.Item;
28 import org.openhab.core.library.items.ColorItem;
29 import org.openhab.core.library.items.DimmerItem;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.HSBType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.PercentType;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.UnDefType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
41 * Proxy class that can collect multiple commands for the same openHAB item and merge them to one command.
42 * e.g. Hue and Saturation update for Color Item
44 * @author Eugen Freiter - Initial contribution
48 public class HomekitOHItemProxy {
49 private final Logger logger = LoggerFactory.getLogger(HomekitOHItemProxy.class);
50 private static final int DEFAULT_DELAY = 50; // in ms
51 private final Item item;
52 private final Map<HomekitCommandType, State> commandCache = new ConcurrentHashMap<>();
53 private final ScheduledExecutorService scheduler = ThreadPoolManager
54 .getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
55 private @NonNullByDefault({}) ScheduledFuture<?> future;
56 private HomekitDimmerMode dimmerMode = DIMMER_MODE_NORMAL;
57 // delay, how long wait for further commands. in ms.
58 private int delay = DEFAULT_DELAY;
60 public HomekitOHItemProxy(Item item) {
64 public Item getItem() {
68 public void setDimmerMode(HomekitDimmerMode mode) {
72 public void setDelay(int delay) {
76 @SuppressWarnings("null")
77 private void sendCommand() {
78 if (!(item instanceof DimmerItem)) {
79 // currently supports only DimmerItem and ColorItem (which extends DimmerItem)
80 logger.debug("unexpected item type {}. Only DimmerItem and ColorItem are supported.", item);
83 final OnOffType on = (OnOffType) commandCache.remove(ON_COMMAND);
84 final PercentType brightness = (PercentType) commandCache.remove(BRIGHTNESS_COMMAND);
85 final DecimalType hue = (DecimalType) commandCache.remove(HUE_COMMAND);
86 final PercentType saturation = (PercentType) commandCache.remove(SATURATION_COMMAND);
87 final @Nullable OnOffType currentOnState = ((DimmerItem) item).getStateAs(OnOffType.class);
91 // - DIMMER_MODE_NONE is enabled OR
92 // - DIMMER_MODE_FILTER_BRIGHTNESS_100 is enabled OR
93 // - DIMMER_MODE_FILTER_ON_EXCEPT100 is not enabled and brightness is null or below 100
94 if ((on == OnOffType.OFF) || (dimmerMode == DIMMER_MODE_NORMAL)
95 || (dimmerMode == DIMMER_MODE_FILTER_BRIGHTNESS_100)
96 || ((dimmerMode == DIMMER_MODE_FILTER_ON_EXCEPT_BRIGHTNESS_100) && (currentOnState != OnOffType.ON)
97 && ((brightness == null) || (brightness.intValue() == 100)))) {
98 logger.trace("send OnOff command for item {} with value {}", item, on);
99 ((DimmerItem) item).send(on);
103 // if hue or saturation present, send an HSBType state update. no filter applied for HUE & Saturation
104 if ((hue != null) || (saturation != null)) {
105 if (item instanceof ColorItem) {
106 sendHSBCommand((ColorItem) item, hue, saturation, brightness);
108 } else if ((brightness != null) && (item instanceof DimmerItem)) {
110 // - DIMMER_MODE_NORMAL
111 // - DIMMER_MODE_FILTER_ON
112 // - other modes (DIMMER_MODE_FILTER_BRIGHTNESS_100 or DIMMER_MODE_FILTER_ON_EXCEPT_BRIGHTNESS_100) and
114 if ((dimmerMode == DIMMER_MODE_NORMAL) || (dimmerMode == DIMMER_MODE_FILTER_ON)
115 || (brightness.intValue() < 100) || (currentOnState == OnOffType.ON)) {
116 logger.trace("send Brightness command for item {} with value {}", item, brightness);
117 if (item instanceof ColorItem) {
118 sendHSBCommand((ColorItem) item, hue, saturation, brightness);
120 ((DimmerItem) item).send(brightness);
124 commandCache.clear();
127 private void sendHSBCommand(ColorItem item, @Nullable DecimalType hue, @Nullable PercentType saturation,
128 @Nullable PercentType brightness) {
129 final HSBType currentState = item.getState() instanceof UnDefType ? HSBType.BLACK : (HSBType) item.getState();
130 // logic for ColorItem = combine hue, saturation and brightness update to one command
131 final DecimalType targetHue = hue != null ? hue : currentState.getHue();
132 final PercentType targetSaturation = saturation != null ? saturation : currentState.getSaturation();
133 final PercentType targetBrightness = brightness != null ? brightness : currentState.getBrightness();
134 item.send(new HSBType(targetHue, targetSaturation, targetBrightness));
135 logger.trace("send HSB command for item {} with following values hue={} saturation={} brightness={}", item,
136 targetHue, targetSaturation, targetBrightness);
139 public synchronized void sendCommandProxy(HomekitCommandType commandType, State state) {
140 commandCache.put(commandType, state);
141 logger.trace("add command to command cache: item {}, command type {}, command state {}. cache state after: {}",
142 this, commandType, state, commandCache);
143 // if cache has already HUE+SATURATION or BRIGHTNESS+ON then we don't expect any further relevant command
144 if (((item instanceof ColorItem) && commandCache.containsKey(HUE_COMMAND)
145 && commandCache.containsKey(SATURATION_COMMAND))
146 || (commandCache.containsKey(BRIGHTNESS_COMMAND) && commandCache.containsKey(ON_COMMAND))) {
147 if (future != null) {
148 future.cancel(false);
153 // if timer is not already set, create a new one to ensure that the command command is send even if no follow up
154 // commands are received.
155 if (future == null || future.isDone()) {
156 future = scheduler.schedule(() -> {
157 logger.trace("timer of {} ms is over, sending the command", delay);
159 }, delay, TimeUnit.MILLISECONDS);