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.nanoleaf.internal.handler;
15 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.math.RoundingMode;
19 import java.util.Arrays;
20 import java.util.HashMap;
22 import java.util.concurrent.ScheduledFuture;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.jetty.client.HttpClient;
27 import org.eclipse.jetty.client.api.ContentResponse;
28 import org.eclipse.jetty.client.api.Request;
29 import org.eclipse.jetty.client.util.StringContentProvider;
30 import org.eclipse.jetty.http.HttpMethod;
31 import org.openhab.binding.nanoleaf.internal.NanoleafBadRequestException;
32 import org.openhab.binding.nanoleaf.internal.NanoleafException;
33 import org.openhab.binding.nanoleaf.internal.NanoleafNotFoundException;
34 import org.openhab.binding.nanoleaf.internal.NanoleafUnauthorizedException;
35 import org.openhab.binding.nanoleaf.internal.OpenAPIUtils;
36 import org.openhab.binding.nanoleaf.internal.config.NanoleafControllerConfig;
37 import org.openhab.binding.nanoleaf.internal.model.Effects;
38 import org.openhab.binding.nanoleaf.internal.model.Write;
39 import org.openhab.core.io.net.http.HttpClientFactory;
40 import org.openhab.core.library.types.HSBType;
41 import org.openhab.core.library.types.IncreaseDecreaseType;
42 import org.openhab.core.library.types.OnOffType;
43 import org.openhab.core.library.types.PercentType;
44 import org.openhab.core.thing.Bridge;
45 import org.openhab.core.thing.ChannelUID;
46 import org.openhab.core.thing.CommonTriggerEvents;
47 import org.openhab.core.thing.Thing;
48 import org.openhab.core.thing.ThingStatus;
49 import org.openhab.core.thing.ThingStatusDetail;
50 import org.openhab.core.thing.ThingStatusInfo;
51 import org.openhab.core.thing.binding.BaseThingHandler;
52 import org.openhab.core.thing.binding.BridgeHandler;
53 import org.openhab.core.types.Command;
54 import org.openhab.core.types.RefreshType;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import com.google.gson.Gson;
61 * The {@link NanoleafPanelHandler} is responsible for handling commands to the controller which
62 * affect an individual panels
64 * @author Martin Raepple - Initial contribution
65 * @author Stefan Höhn - Canvas Touch Support
68 public class NanoleafPanelHandler extends BaseThingHandler {
70 private static final PercentType MIN_PANEL_BRIGHTNESS = PercentType.ZERO;
71 private static final PercentType MAX_PANEL_BRIGHTNESS = PercentType.HUNDRED;
73 private final Logger logger = LoggerFactory.getLogger(NanoleafPanelHandler.class);
75 private HttpClient httpClient;
76 // JSON parser for API responses
77 private final Gson gson = new Gson();
79 // holds current color data per panel
80 private Map<String, HSBType> panelInfo = new HashMap<>();
82 private @NonNullByDefault({}) ScheduledFuture<?> singleTapJob;
83 private @NonNullByDefault({}) ScheduledFuture<?> doubleTapJob;
85 public NanoleafPanelHandler(Thing thing, HttpClientFactory httpClientFactory) {
87 this.httpClient = httpClientFactory.getCommonHttpClient();
91 public void initialize() {
92 logger.debug("Initializing handler for panel {}", getThing().getUID());
93 Bridge controller = getBridge();
94 if (controller == null) {
95 initializePanel(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, ""));
96 } else if (ThingStatus.OFFLINE.equals(controller.getStatus())) {
97 initializePanel(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
98 "@text/error.nanoleaf.panel.controllerOffline"));
100 initializePanel(controller.getStatusInfo());
105 public void bridgeStatusChanged(ThingStatusInfo controllerStatusInfo) {
106 logger.debug("Controller status changed to {} -- {}", controllerStatusInfo,
107 controllerStatusInfo.getDescription() + "/" + controllerStatusInfo.getStatus() + "/"
108 + controllerStatusInfo.hashCode());
109 if (controllerStatusInfo.getStatus().equals(ThingStatus.OFFLINE)) {
110 initializePanel(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
111 "@text/error.nanoleaf.panel.controllerOffline"));
113 initializePanel(controllerStatusInfo);
118 public void handleCommand(ChannelUID channelUID, Command command) {
119 logger.debug("Received command {} for channel {}", command, channelUID);
121 switch (channelUID.getId()) {
122 case CHANNEL_PANEL_COLOR:
123 sendRenderedEffectCommand(command);
126 logger.warn("Channel with id {} not handled", channelUID.getId());
129 } catch (NanoleafUnauthorizedException nae) {
130 logger.warn("Authorization for command {} for channelUID {} failed: {}", command, channelUID,
132 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
133 "@text/error.nanoleaf.controller.invalidToken");
134 } catch (NanoleafException ne) {
135 logger.warn("Handling command {} for channelUID {} failed: {}", command, channelUID, ne.getMessage());
136 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
137 "@text/error.nanoleaf.controller.communication");
142 public void handleRemoval() {
143 logger.debug("Nanoleaf panel {} removed", getThing().getUID());
144 super.handleRemoval();
148 public void dispose() {
149 logger.debug("Disposing handler for Nanoleaf panel {}", getThing().getUID());
154 private void stopAllJobs() {
155 if (singleTapJob != null && !singleTapJob.isCancelled()) {
156 logger.debug("Stop single touch job");
157 singleTapJob.cancel(true);
158 this.singleTapJob = null;
160 if (doubleTapJob != null && !doubleTapJob.isCancelled()) {
161 logger.debug("Stop double touch job");
162 doubleTapJob.cancel(true);
163 this.doubleTapJob = null;
167 private void initializePanel(ThingStatusInfo panelStatus) {
168 updateStatus(panelStatus.getStatus(), panelStatus.getStatusDetail());
169 logger.debug("Panel {} status changed to {}-{}", this.getThing().getUID(), panelStatus.getStatus(),
170 panelStatus.getStatusDetail());
173 private void sendRenderedEffectCommand(Command command) throws NanoleafException {
174 logger.debug("Command Type: {}", command.getClass());
175 HSBType currentPanelColor = getPanelColor();
176 if (currentPanelColor != null) {
177 logger.debug("currentPanelColor: {}", currentPanelColor.toString());
179 HSBType newPanelColor = new HSBType();
181 if (command instanceof HSBType) {
182 newPanelColor = (HSBType) command;
183 } else if (command instanceof OnOffType && (currentPanelColor != null)) {
184 if (OnOffType.ON.equals(command)) {
185 newPanelColor = new HSBType(currentPanelColor.getHue(), currentPanelColor.getSaturation(),
186 MAX_PANEL_BRIGHTNESS);
188 newPanelColor = new HSBType(currentPanelColor.getHue(), currentPanelColor.getSaturation(),
189 MIN_PANEL_BRIGHTNESS);
191 } else if (command instanceof PercentType && (currentPanelColor != null)) {
192 PercentType brightness = new PercentType(
193 Math.max(MIN_PANEL_BRIGHTNESS.intValue(), ((PercentType) command).intValue()));
194 newPanelColor = new HSBType(currentPanelColor.getHue(), currentPanelColor.getSaturation(), brightness);
195 } else if (command instanceof IncreaseDecreaseType && (currentPanelColor != null)) {
196 int brightness = currentPanelColor.getBrightness().intValue();
197 if (command.equals(IncreaseDecreaseType.INCREASE)) {
198 brightness = Math.min(MAX_PANEL_BRIGHTNESS.intValue(), brightness + BRIGHTNESS_STEP_SIZE);
200 brightness = Math.max(MIN_PANEL_BRIGHTNESS.intValue(), brightness - BRIGHTNESS_STEP_SIZE);
202 newPanelColor = new HSBType(currentPanelColor.getHue(), currentPanelColor.getSaturation(),
203 new PercentType(brightness));
204 } else if (command instanceof RefreshType) {
205 logger.debug("Refresh command received");
208 logger.warn("Unhandled command type: {}", command.getClass().getName());
211 // store panel's new HSB value
212 logger.trace("Setting new color {}", newPanelColor);
213 panelInfo.put(getThing().getConfiguration().get(CONFIG_PANEL_ID).toString(), newPanelColor);
215 PercentType[] rgbPercent = newPanelColor.toRGB();
216 logger.trace("Setting new rgbpercent {} {} {}", rgbPercent[0], rgbPercent[1], rgbPercent[2]);
217 int red = rgbPercent[0].toBigDecimal().divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP)
218 .multiply(new BigDecimal(255)).intValue();
219 int green = rgbPercent[1].toBigDecimal().divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP)
220 .multiply(new BigDecimal(255)).intValue();
221 int blue = rgbPercent[2].toBigDecimal().divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP)
222 .multiply(new BigDecimal(255)).intValue();
223 logger.trace("Setting new rgb {} {} {}", red, green, blue);
224 Bridge bridge = getBridge();
225 if (bridge != null) {
226 Effects effects = new Effects();
227 Write write = new Write();
228 write.setCommand("display");
229 write.setAnimType("static");
230 String panelID = this.thing.getConfiguration().get(CONFIG_PANEL_ID).toString();
232 BridgeHandler handler = bridge.getHandler();
233 if (handler != null) {
234 NanoleafControllerConfig config = ((NanoleafControllerHandler) handler).getControllerConfig();
235 // Light Panels and Canvas use different stream commands
236 if (config.deviceType.equals(CONFIG_DEVICE_TYPE_LIGHTPANELS)
237 || config.deviceType.equals(CONFIG_DEVICE_TYPE_CANVAS)) {
238 logger.trace("Anim Data rgb {} {} {} {}", panelID, red, green, blue);
239 write.setAnimData(String.format("1 %s 1 %d %d %d 0 10", panelID, red, green, blue));
241 // this is only used in special streaming situations with canvas which is not yet supported
242 int quotient = Integer.divideUnsigned(Integer.valueOf(panelID), 256);
243 int remainder = Integer.remainderUnsigned(Integer.valueOf(panelID), 256);
245 String.format("0 1 %d %d %d %d %d 0 0 10", quotient, remainder, red, green, blue));
247 write.setLoop(false);
248 effects.setWrite(write);
249 Request setNewRenderedEffectRequest = OpenAPIUtils.requestBuilder(httpClient, config, API_EFFECT,
251 String content = gson.toJson(effects);
252 logger.debug("sending effect command from panel {}: {}", getThing().getUID(), content);
253 setNewRenderedEffectRequest.content(new StringContentProvider(content), "application/json");
254 OpenAPIUtils.sendOpenAPIRequest(setNewRenderedEffectRequest);
256 logger.warn("Couldn't set rendering effect as Bridge-Handler {} is null", bridge.getUID());
261 public void updatePanelColorChannel() {
263 HSBType panelColor = getPanelColor();
264 logger.trace("updatePanelColorChannel: panelColor: {}", panelColor);
265 if (panelColor != null) {
266 updateState(CHANNEL_PANEL_COLOR, panelColor);
271 * Apply the gesture to the panel
273 * @param gesture Only 0=single tap and 1=double tap are supported
275 public void updatePanelGesture(int gesture) {
278 triggerChannel(CHANNEL_PANEL_TAP, CommonTriggerEvents.SHORT_PRESSED);
281 triggerChannel(CHANNEL_PANEL_TAP, CommonTriggerEvents.DOUBLE_PRESSED);
286 public String getPanelID() {
287 String panelID = getThing().getConfiguration().get(CONFIG_PANEL_ID).toString();
291 private @Nullable HSBType getPanelColor() {
292 String panelID = getPanelID();
294 // get panel color data from controller
296 Effects effects = new Effects();
297 Write write = new Write();
298 write.setCommand("request");
299 write.setAnimName("*Static*");
300 effects.setWrite(write);
301 Bridge bridge = getBridge();
302 if (bridge != null) {
303 NanoleafControllerHandler handler = (NanoleafControllerHandler) bridge.getHandler();
304 if (handler != null) {
305 NanoleafControllerConfig config = handler.getControllerConfig();
306 logger.debug("Sending Request from Panel for getColor()");
307 Request setPanelUpdateRequest = OpenAPIUtils.requestBuilder(httpClient, config, API_EFFECT,
309 setPanelUpdateRequest.content(new StringContentProvider(gson.toJson(effects)), "application/json");
310 ContentResponse panelData = OpenAPIUtils.sendOpenAPIRequest(setPanelUpdateRequest);
313 parsePanelData(panelID, config, panelData);
316 } catch (NanoleafNotFoundException nfe) {
317 logger.debug("Panel data could not be retrieved as no data was returned (static type missing?) : {}",
319 } catch (NanoleafBadRequestException nfe) {
321 "Panel data could not be retrieved as request not expected(static type missing / dynamic type on) : {}",
323 } catch (NanoleafException nue) {
324 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
325 "@text/error.nanoleaf.panel.communication");
326 logger.debug("Panel data could not be retrieved: {}", nue.getMessage());
329 return panelInfo.get(panelID);
332 void parsePanelData(String panelID, NanoleafControllerConfig config, ContentResponse panelData) {
333 // panelData is in format (numPanels, (PanelId, 1, R, G, B, W, TransitionTime) * numPanel)
335 Write response = gson.fromJson(panelData.getContentAsString(), Write.class);
336 if (response != null) {
337 String[] tokenizedData = response.getAnimData().split(" ");
338 if (config.deviceType.equals(CONFIG_DEVICE_TYPE_LIGHTPANELS)
339 || config.deviceType.equals(CONFIG_DEVICE_TYPE_CANVAS)) {
340 // panelData is in format (numPanels (PanelId 1 R G B W TransitionTime) * numPanel)
341 String[] panelDataPoints = Arrays.copyOfRange(tokenizedData, 1, tokenizedData.length);
342 for (int i = 0; i < panelDataPoints.length; i++) {
344 String id = panelDataPoints[i];
345 if (id.equals(panelID)) {
346 // found panel data - store it
347 panelInfo.put(panelID,
348 HSBType.fromRGB(Integer.parseInt(panelDataPoints[i + 2]),
349 Integer.parseInt(panelDataPoints[i + 3]),
350 Integer.parseInt(panelDataPoints[i + 4])));
355 // panelData is in format (0 numPanels (quotient(panelID) remainder(panelID) R G B W 0
356 // quotient(TransitionTime) remainder(TransitionTime)) * numPanel)
357 String[] panelDataPoints = Arrays.copyOfRange(tokenizedData, 2, tokenizedData.length);
358 for (int i = 0; i < panelDataPoints.length; i++) {
360 String idQuotient = panelDataPoints[i];
361 String idRemainder = panelDataPoints[i + 1];
362 Integer idNum = Integer.valueOf(idQuotient) * 256 + Integer.valueOf(idRemainder);
363 if (String.valueOf(idNum).equals(panelID)) {
364 // found panel data - store it
365 panelInfo.put(panelID,
366 HSBType.fromRGB(Integer.parseInt(panelDataPoints[i + 3]),
367 Integer.parseInt(panelDataPoints[i + 4]),
368 Integer.parseInt(panelDataPoints[i + 5])));