]> git.basschouten.com Git - openhab-addons.git/blob
2eb1bbcaa68c7ec11a20cc28b62e3ed49172ad00
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.wifiled.internal.handler;
14
15 import java.io.IOException;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18
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;
33
34 /**
35  * The {@link WiFiLEDHandler} is responsible for handling commands, which are
36  * sent to one of the channels.
37  *
38  * @author Osman Basha - Initial contribution
39  * @author Ries van Twisk
40  */
41 public class WiFiLEDHandler extends BaseThingHandler {
42
43     private static final int INC_DEC_STEP = 10;
44
45     private Logger logger = LoggerFactory.getLogger(WiFiLEDHandler.class);
46     private AbstractWiFiLEDDriver driver;
47     private ScheduledFuture<?> pollingJob;
48
49     public WiFiLEDHandler(Thing thing) {
50         super(thing);
51     }
52
53     @Override
54     public void initialize() {
55         logger.debug("Initializing WiFiLED handler '{}'", getThing().getUID());
56
57         WiFiLEDConfig config = getConfigAs(WiFiLEDConfig.class);
58
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());
62
63         switch (driverName) {
64             case CLASSIC:
65                 driver = new ClassicWiFiLEDDriver(this, config.getIp(), port, protocol);
66                 break;
67
68             case FADING:
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);
75                 break;
76         }
77
78         try {
79             driver.init();
80
81             logger.debug("Found a WiFi LED device '{}'", getThing().getUID());
82         } catch (IOException e) {
83             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, e.getMessage());
84             return;
85         }
86         updateStatus(ThingStatus.ONLINE);
87
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());
92         }
93     }
94
95     @Override
96     public void dispose() {
97         logger.debug("Disposing WiFiLED handler '{}'", getThing().getUID());
98
99         if (pollingJob != null) {
100             pollingJob.cancel(true);
101             pollingJob = null;
102         }
103         driver.shutdown();
104         driver = null;
105     }
106
107     @Override
108     public void handleCommand(ChannelUID channelUID, Command command) {
109         logger.debug("Handle command '{}' for {}", command, channelUID);
110
111         try {
112             if (command == RefreshType.REFRESH) {
113                 update();
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);
127             }
128         } catch (IOException e) {
129             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
130         }
131     }
132
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);
144             } else {
145                 driver.decBrightness(INC_DEC_STEP);
146             }
147         }
148     }
149
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);
157             } else {
158                 driver.setWhite(PercentType.ZERO);
159             }
160         } else if (command instanceof IncreaseDecreaseType) {
161             IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
162             if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
163                 driver.incWhite(INC_DEC_STEP);
164             } else {
165                 driver.decWhite(INC_DEC_STEP);
166             }
167         }
168     }
169
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);
177             } else {
178                 driver.setWhite2(PercentType.ZERO);
179             }
180         } else if (command instanceof IncreaseDecreaseType) {
181             IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
182             if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
183                 driver.incWhite2(INC_DEC_STEP);
184             } else {
185                 driver.decWhite2(INC_DEC_STEP);
186             }
187         }
188     }
189
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);
197             } else {
198                 driver.setProgramSpeed(PercentType.ZERO);
199             }
200         } else if (command instanceof IncreaseDecreaseType) {
201             IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
202             if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
203                 driver.incProgramSpeed(INC_DEC_STEP);
204             } else {
205                 driver.decProgramSpeed(INC_DEC_STEP);
206             }
207         }
208     }
209
210     private synchronized void update() {
211         logger.debug("Updating WiFiLED data '{}'", getThing().getUID());
212
213         try {
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());
222
223             if (getThing().getStatus().equals(ThingStatus.OFFLINE)) {
224                 updateStatus(ThingStatus.ONLINE);
225             }
226         } catch (IOException e) {
227             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
228         }
229     }
230
231     public void reportCommunicationError(IOException e) {
232         this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
233     }
234 }