2 * Copyright (c) 2010-2024 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 stringCommand)) {
128 driver.setProgram(stringCommand);
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 hsbCommand) {
139 driver.setColor(hsbCommand);
140 } else if (command instanceof PercentType percentCommand) {
141 driver.setBrightness(percentCommand);
142 } else if (command instanceof OnOffType onOffCommand) {
143 driver.setPower(onOffCommand);
144 } else if (command instanceof IncreaseDecreaseType increaseDecreaseType) {
145 if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
146 driver.incBrightness(INC_DEC_STEP);
148 driver.decBrightness(INC_DEC_STEP);
153 private void handleWhiteCommand(Command command) throws IOException {
154 if (command instanceof PercentType percentCommand) {
155 driver.setWhite(percentCommand);
156 } else if (command instanceof OnOffType onOffCommand) {
157 if (onOffCommand.equals(OnOffType.ON)) {
158 driver.setWhite(PercentType.HUNDRED);
160 driver.setWhite(PercentType.ZERO);
162 } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
163 if (increaseDecreaseCommand.equals(IncreaseDecreaseType.INCREASE)) {
164 driver.incWhite(INC_DEC_STEP);
166 driver.decWhite(INC_DEC_STEP);
171 private void handleWhite2Command(Command command) throws IOException {
172 if (command instanceof PercentType percentCommand) {
173 driver.setWhite2(percentCommand);
174 } else if (command instanceof OnOffType onOffCommand) {
175 if (onOffCommand.equals(OnOffType.ON)) {
176 driver.setWhite2(PercentType.HUNDRED);
178 driver.setWhite2(PercentType.ZERO);
180 } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
181 if (increaseDecreaseCommand.equals(IncreaseDecreaseType.INCREASE)) {
182 driver.incWhite2(INC_DEC_STEP);
184 driver.decWhite2(INC_DEC_STEP);
189 private void handleProgramSpeedCommand(Command command) throws IOException {
190 if (command instanceof PercentType percentCommand) {
191 driver.setProgramSpeed(percentCommand);
192 } else if (command instanceof OnOffType onOffCommand) {
193 if (onOffCommand.equals(OnOffType.ON)) {
194 driver.setProgramSpeed(PercentType.HUNDRED);
196 driver.setProgramSpeed(PercentType.ZERO);
198 } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
199 if (increaseDecreaseCommand.equals(IncreaseDecreaseType.INCREASE)) {
200 driver.incProgramSpeed(INC_DEC_STEP);
202 driver.decProgramSpeed(INC_DEC_STEP);
207 private synchronized void update() {
208 logger.debug("Updating WiFiLED data '{}'", getThing().getUID());
211 LEDStateDTO ledState = driver.getLEDStateDTO();
212 HSBType color = ledState.getHSB();
213 updateState(WiFiLEDBindingConstants.CHANNEL_POWER, ledState.getPower());
214 updateState(WiFiLEDBindingConstants.CHANNEL_COLOR, color);
215 updateState(WiFiLEDBindingConstants.CHANNEL_WHITE, ledState.getWhite());
216 updateState(WiFiLEDBindingConstants.CHANNEL_WHITE2, ledState.getWhite2());
217 updateState(WiFiLEDBindingConstants.CHANNEL_PROGRAM, ledState.getProgram());
218 updateState(WiFiLEDBindingConstants.CHANNEL_PROGRAM_SPEED, ledState.getProgramSpeed());
220 if (getThing().getStatus().equals(ThingStatus.OFFLINE)) {
221 updateStatus(ThingStatus.ONLINE);
223 } catch (IOException e) {
224 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
228 public void reportCommunicationError(IOException e) {
229 this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());