2 * Copyright (c) 2010-2023 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.HSBType;
24 import org.openhab.core.library.types.IncreaseDecreaseType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.binding.BaseThingHandler;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.RefreshType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link WiFiLEDHandler} is responsible for handling commands, which are
40 * sent to one of the channels.
42 * @author Osman Basha - Initial contribution
43 * @author Ries van Twisk
45 public class WiFiLEDHandler extends BaseThingHandler {
47 private static final int INC_DEC_STEP = 10;
49 private Logger logger = LoggerFactory.getLogger(WiFiLEDHandler.class);
50 private AbstractWiFiLEDDriver driver;
51 private ScheduledFuture<?> pollingJob;
53 public WiFiLEDHandler(Thing thing) {
58 public void initialize() {
59 logger.debug("Initializing WiFiLED handler '{}'", getThing().getUID());
61 WiFiLEDConfig config = getConfigAs(WiFiLEDConfig.class);
63 int port = (config.getPort() == null) ? AbstractWiFiLEDDriver.DEFAULT_PORT : config.getPort();
64 Protocol protocol = config.getProtocol() == null ? Protocol.LD382A : Protocol.valueOf(config.getProtocol());
65 Driver driverName = config.getDriver() == null ? Driver.CLASSIC : Driver.valueOf(config.getDriver());
69 driver = new ClassicWiFiLEDDriver(this, config.getIp(), port, protocol);
73 int fadeDurationInMs = config.getFadeDurationInMs() == null
74 ? FadingWiFiLEDDriver.DEFAULT_FADE_DURATION_IN_MS
75 : config.getFadeDurationInMs();
76 int fadeSteps = config.getFadeSteps() == null ? FadingWiFiLEDDriver.DEFAULT_FADE_STEPS
77 : config.getFadeSteps();
78 driver = new FadingWiFiLEDDriver(config.getIp(), port, protocol, fadeDurationInMs, fadeSteps);
85 logger.debug("Found a WiFi LED device '{}'", getThing().getUID());
86 } catch (IOException e) {
87 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, e.getMessage());
90 updateStatus(ThingStatus.ONLINE);
92 int pollingPeriod = (config.getPollingPeriod() == null) ? 30 : config.getPollingPeriod();
93 if (pollingPeriod > 0) {
94 pollingJob = scheduler.scheduleWithFixedDelay(() -> update(), 0, pollingPeriod, TimeUnit.SECONDS);
95 logger.debug("Polling job scheduled to run every {} sec. for '{}'", pollingPeriod, getThing().getUID());
100 public void dispose() {
101 logger.debug("Disposing WiFiLED handler '{}'", getThing().getUID());
103 if (pollingJob != null) {
104 pollingJob.cancel(true);
112 public void handleCommand(ChannelUID channelUID, Command command) {
113 logger.debug("Handle command '{}' for {}", command, channelUID);
116 if (command == RefreshType.REFRESH) {
118 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_POWER)) {
119 handleColorCommand(command);
120 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_COLOR)) {
121 handleColorCommand(command);
122 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_WHITE)) {
123 handleWhiteCommand(command);
124 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_WHITE2)) {
125 handleWhite2Command(command);
126 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_PROGRAM)
127 && (command instanceof StringType)) {
128 driver.setProgram((StringType) command);
129 } else if (channelUID.getId().equals(WiFiLEDBindingConstants.CHANNEL_PROGRAM_SPEED)) {
130 handleProgramSpeedCommand(command);
132 } catch (IOException e) {
133 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
137 private void handleColorCommand(Command command) throws IOException {
138 if (command instanceof HSBType) {
139 driver.setColor((HSBType) command);
140 } else if (command instanceof PercentType) {
141 driver.setBrightness((PercentType) command);
142 } else if (command instanceof OnOffType) {
143 driver.setPower((OnOffType) command);
144 } else if (command instanceof IncreaseDecreaseType) {
145 IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
146 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
147 driver.incBrightness(INC_DEC_STEP);
149 driver.decBrightness(INC_DEC_STEP);
154 private void handleWhiteCommand(Command command) throws IOException {
155 if (command instanceof PercentType) {
156 driver.setWhite((PercentType) command);
157 } else if (command instanceof OnOffType) {
158 OnOffType onOffCommand = (OnOffType) command;
159 if (onOffCommand.equals(OnOffType.ON)) {
160 driver.setWhite(PercentType.HUNDRED);
162 driver.setWhite(PercentType.ZERO);
164 } else if (command instanceof IncreaseDecreaseType) {
165 IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
166 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
167 driver.incWhite(INC_DEC_STEP);
169 driver.decWhite(INC_DEC_STEP);
174 private void handleWhite2Command(Command command) throws IOException {
175 if (command instanceof PercentType) {
176 driver.setWhite2((PercentType) command);
177 } else if (command instanceof OnOffType) {
178 OnOffType onOffCommand = (OnOffType) command;
179 if (onOffCommand.equals(OnOffType.ON)) {
180 driver.setWhite2(PercentType.HUNDRED);
182 driver.setWhite2(PercentType.ZERO);
184 } else if (command instanceof IncreaseDecreaseType) {
185 IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
186 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
187 driver.incWhite2(INC_DEC_STEP);
189 driver.decWhite2(INC_DEC_STEP);
194 private void handleProgramSpeedCommand(Command command) throws IOException {
195 if (command instanceof PercentType) {
196 driver.setProgramSpeed((PercentType) command);
197 } else if (command instanceof OnOffType) {
198 OnOffType onOffCommand = (OnOffType) command;
199 if (onOffCommand.equals(OnOffType.ON)) {
200 driver.setProgramSpeed(PercentType.HUNDRED);
202 driver.setProgramSpeed(PercentType.ZERO);
204 } else if (command instanceof IncreaseDecreaseType) {
205 IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
206 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
207 driver.incProgramSpeed(INC_DEC_STEP);
209 driver.decProgramSpeed(INC_DEC_STEP);
214 private synchronized void update() {
215 logger.debug("Updating WiFiLED data '{}'", getThing().getUID());
218 LEDStateDTO ledState = driver.getLEDStateDTO();
219 HSBType color = ledState.getHSB();
220 updateState(WiFiLEDBindingConstants.CHANNEL_POWER, ledState.getPower());
221 updateState(WiFiLEDBindingConstants.CHANNEL_COLOR, color);
222 updateState(WiFiLEDBindingConstants.CHANNEL_WHITE, ledState.getWhite());
223 updateState(WiFiLEDBindingConstants.CHANNEL_WHITE2, ledState.getWhite2());
224 updateState(WiFiLEDBindingConstants.CHANNEL_PROGRAM, ledState.getProgram());
225 updateState(WiFiLEDBindingConstants.CHANNEL_PROGRAM_SPEED, ledState.getProgramSpeed());
227 if (getThing().getStatus().equals(ThingStatus.OFFLINE)) {
228 updateStatus(ThingStatus.ONLINE);
230 } catch (IOException e) {
231 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
235 public void reportCommunicationError(IOException e) {
236 this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());