]> git.basschouten.com Git - openhab-addons.git/blob
a1eb9febc914ee41cba9c69135045ec4fc2060b0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.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;
37
38 /**
39  * The {@link WiFiLEDHandler} is responsible for handling commands, which are
40  * sent to one of the channels.
41  *
42  * @author Osman Basha - Initial contribution
43  * @author Ries van Twisk
44  */
45 public class WiFiLEDHandler extends BaseThingHandler {
46
47     private static final int INC_DEC_STEP = 10;
48
49     private Logger logger = LoggerFactory.getLogger(WiFiLEDHandler.class);
50     private AbstractWiFiLEDDriver driver;
51     private ScheduledFuture<?> pollingJob;
52
53     public WiFiLEDHandler(Thing thing) {
54         super(thing);
55     }
56
57     @Override
58     public void initialize() {
59         logger.debug("Initializing WiFiLED handler '{}'", getThing().getUID());
60
61         WiFiLEDConfig config = getConfigAs(WiFiLEDConfig.class);
62
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());
66
67         switch (driverName) {
68             case CLASSIC:
69                 driver = new ClassicWiFiLEDDriver(this, config.getIp(), port, protocol);
70                 break;
71
72             case FADING:
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);
79                 break;
80         }
81
82         try {
83             driver.init();
84
85             logger.debug("Found a WiFi LED device '{}'", getThing().getUID());
86         } catch (IOException e) {
87             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, e.getMessage());
88             return;
89         }
90         updateStatus(ThingStatus.ONLINE);
91
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());
96         }
97     }
98
99     @Override
100     public void dispose() {
101         logger.debug("Disposing WiFiLED handler '{}'", getThing().getUID());
102
103         if (pollingJob != null) {
104             pollingJob.cancel(true);
105             pollingJob = null;
106         }
107         driver.shutdown();
108         driver = null;
109     }
110
111     @Override
112     public void handleCommand(ChannelUID channelUID, Command command) {
113         logger.debug("Handle command '{}' for {}", command, channelUID);
114
115         try {
116             if (command == RefreshType.REFRESH) {
117                 update();
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);
131             }
132         } catch (IOException e) {
133             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
134         }
135     }
136
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);
147             } else {
148                 driver.decBrightness(INC_DEC_STEP);
149             }
150         }
151     }
152
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);
159             } else {
160                 driver.setWhite(PercentType.ZERO);
161             }
162         } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
163             if (increaseDecreaseCommand.equals(IncreaseDecreaseType.INCREASE)) {
164                 driver.incWhite(INC_DEC_STEP);
165             } else {
166                 driver.decWhite(INC_DEC_STEP);
167             }
168         }
169     }
170
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);
177             } else {
178                 driver.setWhite2(PercentType.ZERO);
179             }
180         } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
181             if (increaseDecreaseCommand.equals(IncreaseDecreaseType.INCREASE)) {
182                 driver.incWhite2(INC_DEC_STEP);
183             } else {
184                 driver.decWhite2(INC_DEC_STEP);
185             }
186         }
187     }
188
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);
195             } else {
196                 driver.setProgramSpeed(PercentType.ZERO);
197             }
198         } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
199             if (increaseDecreaseCommand.equals(IncreaseDecreaseType.INCREASE)) {
200                 driver.incProgramSpeed(INC_DEC_STEP);
201             } else {
202                 driver.decProgramSpeed(INC_DEC_STEP);
203             }
204         }
205     }
206
207     private synchronized void update() {
208         logger.debug("Updating WiFiLED data '{}'", getThing().getUID());
209
210         try {
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());
219
220             if (getThing().getStatus().equals(ThingStatus.OFFLINE)) {
221                 updateStatus(ThingStatus.ONLINE);
222             }
223         } catch (IOException e) {
224             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
225         }
226     }
227
228     public void reportCommunicationError(IOException e) {
229         this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
230     }
231 }