]> git.basschouten.com Git - openhab-addons.git/blob
cdd32ed65b3bf9c85100ab8641e84c0f900065d0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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)) {
128                 driver.setProgram((StringType) command);
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) {
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);
148             } else {
149                 driver.decBrightness(INC_DEC_STEP);
150             }
151         }
152     }
153
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);
161             } else {
162                 driver.setWhite(PercentType.ZERO);
163             }
164         } else if (command instanceof IncreaseDecreaseType) {
165             IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
166             if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
167                 driver.incWhite(INC_DEC_STEP);
168             } else {
169                 driver.decWhite(INC_DEC_STEP);
170             }
171         }
172     }
173
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);
181             } else {
182                 driver.setWhite2(PercentType.ZERO);
183             }
184         } else if (command instanceof IncreaseDecreaseType) {
185             IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
186             if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
187                 driver.incWhite2(INC_DEC_STEP);
188             } else {
189                 driver.decWhite2(INC_DEC_STEP);
190             }
191         }
192     }
193
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);
201             } else {
202                 driver.setProgramSpeed(PercentType.ZERO);
203             }
204         } else if (command instanceof IncreaseDecreaseType) {
205             IncreaseDecreaseType increaseDecreaseType = (IncreaseDecreaseType) command;
206             if (increaseDecreaseType.equals(IncreaseDecreaseType.INCREASE)) {
207                 driver.incProgramSpeed(INC_DEC_STEP);
208             } else {
209                 driver.decProgramSpeed(INC_DEC_STEP);
210             }
211         }
212     }
213
214     private synchronized void update() {
215         logger.debug("Updating WiFiLED data '{}'", getThing().getUID());
216
217         try {
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());
226
227             if (getThing().getStatus().equals(ThingStatus.OFFLINE)) {
228                 updateStatus(ThingStatus.ONLINE);
229             }
230         } catch (IOException e) {
231             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
232         }
233     }
234
235     public void reportCommunicationError(IOException e) {
236         this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
237     }
238 }