2 * Copyright (c) 2010-2021 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.wifiled.internal.handler;
15 import java.io.IOException;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
19 import org.openhab.binding.wifiled.internal.WiFiLEDBindingConstants;
20 import org.openhab.binding.wifiled.internal.configuration.WiFiLEDConfig;
21 import org.openhab.binding.wifiled.internal.handler.AbstractWiFiLEDDriver.Driver;
22 import org.openhab.binding.wifiled.internal.handler.AbstractWiFiLEDDriver.Protocol;
23 import org.openhab.core.library.types.*;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BaseThingHandler;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link WiFiLEDHandler} is responsible for handling commands, which are
36 * sent to one of the channels.
38 * @author Osman Basha - Initial contribution
39 * @author Ries van Twisk
41 public class WiFiLEDHandler extends BaseThingHandler {
43 private static final int INC_DEC_STEP = 10;
45 private Logger logger = LoggerFactory.getLogger(WiFiLEDHandler.class);
46 private AbstractWiFiLEDDriver driver;
47 private ScheduledFuture<?> pollingJob;
49 public WiFiLEDHandler(Thing thing) {
54 public void initialize() {
55 logger.debug("Initializing WiFiLED handler '{}'", getThing().getUID());
57 WiFiLEDConfig config = getConfigAs(WiFiLEDConfig.class);
59 int port = (config.getPort() == null) ? AbstractWiFiLEDDriver.DEFAULT_PORT : config.getPort();
60 Protocol protocol = config.getProtocol() == null ? Protocol.LD382A : Protocol.valueOf(config.getProtocol());
61 Driver driverName = config.getDriver() == null ? Driver.CLASSIC : Driver.valueOf(config.getDriver());
65 driver = new ClassicWiFiLEDDriver(this, config.getIp(), port, protocol);
69 int fadeDurationInMs = config.getFadeDurationInMs() == null
70 ? FadingWiFiLEDDriver.DEFAULT_FADE_DURATION_IN_MS
71 : config.getFadeDurationInMs();
72 int fadeSteps = config.getFadeSteps() == null ? FadingWiFiLEDDriver.DEFAULT_FADE_STEPS
73 : config.getFadeSteps();
74 driver = new FadingWiFiLEDDriver(config.getIp(), port, protocol, fadeDurationInMs, fadeSteps);
81 logger.debug("Found a WiFi LED device '{}'", getThing().getUID());
82 } catch (IOException e) {
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, e.getMessage());
86 updateStatus(ThingStatus.ONLINE);
88 int pollingPeriod = (config.getPollingPeriod() == null) ? 30 : config.getPollingPeriod();
89 if (pollingPeriod > 0) {
90 pollingJob = scheduler.scheduleWithFixedDelay(() -> update(), 0, pollingPeriod, TimeUnit.SECONDS);
91 logger.debug("Polling job scheduled to run every {} sec. for '{}'", pollingPeriod, getThing().getUID());
96 public void dispose() {
97 logger.debug("Disposing WiFiLED handler '{}'", getThing().getUID());
99 if (pollingJob != null) {
100 pollingJob.cancel(true);
108 public void handleCommand(ChannelUID channelUID, Command command) {
109 logger.debug("Handle command '{}' for {}", command, channelUID);
112 if (command == RefreshType.REFRESH) {
114 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_POWER)) {
115 handleColorCommand(command);
116 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_COLOR)) {
117 handleColorCommand(command);
118 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_WHITE)) {
119 handleWhiteCommand(command);
120 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_WHITE2)) {
121 handleWhite2Command(command);
122 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_PROGRAM)
123 && (command instanceof StringType)) {
124 driver.setProgram((StringType) command);
125 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_PROGRAM_SPEED)) {
126 handleProgramSpeedCommand(command);
128 } catch (IOException e) {
129 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
133 private void handleColorCommand(Command command) throws IOException {
134 if (command instanceof HSBType) {
135 driver.setColor((HSBType) command);
136 } else if (command instanceof PercentType) {
137 driver.setBrightness((PercentType) command);
138 } else if (command instanceof OnOffType) {
139 driver.setPower((OnOffType) command);
140 } else if (command instanceof IncreaseDecreaseType) {
141 IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
142 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
143 driver.incBrightness(INC_DEC_STEP);
145 driver.decBrightness(INC_DEC_STEP);
150 private void handleWhiteCommand(Command command) throws IOException {
151 if (command instanceof PercentType) {
152 driver.setWhite((PercentType) command);
153 } else if (command instanceof OnOffType) {
154 OnOffType onOffCommand = (OnOffType) command;
155 if (onOffCommand.equals(OnOffType.ON)) {
156 driver.setWhite(PercentType.HUNDRED);
158 driver.setWhite(PercentType.ZERO);
160 } else if (command instanceof IncreaseDecreaseType) {
161 IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
162 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
163 driver.incWhite(INC_DEC_STEP);
165 driver.decWhite(INC_DEC_STEP);
170 private void handleWhite2Command(Command command) throws IOException {
171 if (command instanceof PercentType) {
172 driver.setWhite2((PercentType) command);
173 } else if (command instanceof OnOffType) {
174 OnOffType onOffCommand = (OnOffType) command;
175 if (onOffCommand.equals(OnOffType.ON)) {
176 driver.setWhite2(PercentType.HUNDRED);
178 driver.setWhite2(PercentType.ZERO);
180 } else if (command instanceof IncreaseDecreaseType) {
181 IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
182 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
183 driver.incWhite2(INC_DEC_STEP);
185 driver.decWhite2(INC_DEC_STEP);
190 private void handleProgramSpeedCommand(Command command) throws IOException {
191 if (command instanceof PercentType) {
192 driver.setProgramSpeed((PercentType) command);
193 } else if (command instanceof OnOffType) {
194 OnOffType onOffCommand = (OnOffType) command;
195 if (onOffCommand.equals(OnOffType.ON)) {
196 driver.setProgramSpeed(PercentType.HUNDRED);
198 driver.setProgramSpeed(PercentType.ZERO);
200 } else if (command instanceof IncreaseDecreaseType) {
201 IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
202 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
203 driver.incProgramSpeed(INC_DEC_STEP);
205 driver.decProgramSpeed(INC_DEC_STEP);
210 private synchronized void update() {
211 logger.debug("Updating WiFiLED data '{}'", getThing().getUID());
214 LEDStateDTO ledState = driver.getLEDStateDTO();
215 HSBType color = ledState.getHSB();
216 updateState(WiFiLEDBindingConstants.CHANNEL_POWER, ledState.getPower());
217 updateState(WiFiLEDBindingConstants.CHANNEL_COLOR, color);
218 updateState(WiFiLEDBindingConstants.CHANNEL_WHITE, ledState.getWhite());
219 updateState(WiFiLEDBindingConstants.CHANNEL_WHITE2, ledState.getWhite2());
220 updateState(WiFiLEDBindingConstants.CHANNEL_PROGRAM, ledState.getProgram());
221 updateState(WiFiLEDBindingConstants.CHANNEL_PROGRAM_SPEED, ledState.getProgramSpeed());
223 if (getThing().getStatus().equals(ThingStatus.OFFLINE)) {
224 updateStatus(ThingStatus.ONLINE);
226 } catch (IOException e) {
227 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
231 public void reportCommunicationError(IOException e) {
232 this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());