2 * Copyright (c) 2010-2020 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.openwebnet.handler;
15 import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_SHUTTER;
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.openwebnet.OpenWebNetBindingConstants;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.StopMoveType;
29 import org.openhab.core.library.types.UpDownType;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.UnDefType;
37 import org.openwebnet4j.OpenGateway;
38 import org.openwebnet4j.communication.OWNException;
39 import org.openwebnet4j.message.Automation;
40 import org.openwebnet4j.message.BaseOpenMessage;
41 import org.openwebnet4j.message.FrameException;
42 import org.openwebnet4j.message.GatewayMgmt;
43 import org.openwebnet4j.message.Where;
44 import org.openwebnet4j.message.Who;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * The {@link OpenWebNetAutomationHandler} is responsible for handling commands/messages for an Automation OpenWebNet
50 * device. It extends the abstract {@link OpenWebNetThingHandler}.
52 * @author Massimo Valla - Initial contribution
55 public class OpenWebNetAutomationHandler extends OpenWebNetThingHandler {
57 private final Logger logger = LoggerFactory.getLogger(OpenWebNetAutomationHandler.class);
59 private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("ss.SSS");
61 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.AUTOMATION_SUPPORTED_THING_TYPES;
64 public static final int MOVING_STATE_STOPPED = 0;
65 public static final int MOVING_STATE_MOVING_UP = 1;
66 public static final int MOVING_STATE_MOVING_DOWN = 2;
67 public static final int MOVING_STATE_UNKNOWN = -1;
70 public static final int CALIBRATION_INACTIVE = -1;
71 public static final int CALIBRATION_ACTIVATED = 0;
72 public static final int CALIBRATION_GOING_UP = 1;
73 public static final int CALIBRATION_GOING_DOWN = 2;
76 public static final int POSITION_MAX_STEPS = 100;
77 public static final int POSITION_DOWN = 100;
78 public static final int POSITION_UP = 0;
79 public static final int POSITION_UNKNOWN = -1;
81 public static final int SHUTTER_RUN_UNDEFINED = -1;
82 private int shutterRun = SHUTTER_RUN_UNDEFINED;
83 private static final String AUTO_CALIBRATION = "AUTO";
85 private long startedMovingAt = -1;
86 private int movingState = MOVING_STATE_UNKNOWN;
87 private int positionEstimation = POSITION_UNKNOWN;
88 private @Nullable ScheduledFuture<?> moveSchedule;
89 private int positionRequested = POSITION_UNKNOWN;
90 private int calibrating = CALIBRATION_INACTIVE;
91 private static final int MIN_STEP_TIME_MSEC = 50;
92 private @Nullable Command commandRequestedWhileMoving = null;
94 public OpenWebNetAutomationHandler(Thing thing) {
99 public void initialize() {
101 Object shutterRunConfig = getConfig().get(OpenWebNetBindingConstants.CONFIG_PROPERTY_SHUTTER_RUN);
103 if (shutterRunConfig == null) {
104 shutterRunConfig = AUTO_CALIBRATION;
105 logger.debug("shutterRun null --> default to AUTO");
106 } else if (shutterRunConfig instanceof String) {
107 if (AUTO_CALIBRATION.equalsIgnoreCase(((String) shutterRunConfig))) {
108 logger.debug("shutterRun set to AUTO via configuration");
109 shutterRun = SHUTTER_RUN_UNDEFINED; // reset shutterRun
110 } else { // try to parse int>=1000
111 int shutterRunInt = Integer.parseInt((String) shutterRunConfig);
112 if (shutterRunInt < 1000) {
113 throw new NumberFormatException();
115 shutterRun = shutterRunInt;
116 logger.debug("shutterRun set to {} via configuration", shutterRun);
119 throw new NumberFormatException();
121 } catch (NumberFormatException e) {
122 logger.debug("Wrong configuration: {} setting must be {} or an integer >= 1000",
123 OpenWebNetBindingConstants.CONFIG_PROPERTY_SHUTTER_RUN, AUTO_CALIBRATION);
124 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
125 "@text/offline.wrong-configuration");
126 shutterRun = SHUTTER_RUN_UNDEFINED;
128 updateState(CHANNEL_SHUTTER, UnDefType.UNDEF);
129 positionEstimation = POSITION_UNKNOWN;
133 protected void requestChannelState(ChannelUID channel) {
134 logger.debug("requestChannelState() thingUID={} channel={}", thing.getUID(), channel.getId());
135 Where w = deviceWhere;
138 send(Automation.requestStatus(w.value()));
139 } catch (OWNException e) {
140 logger.debug("Exception while requesting channel {} state: {}", channel, e.getMessage(), e);
146 protected void handleChannelCommand(ChannelUID channel, Command command) {
147 switch (channel.getId()) {
148 case CHANNEL_SHUTTER:
149 handleShutterCommand(command);
152 logger.info("Unsupported channel UID {}", channel);
158 * Handles Automation Roller shutter command (UP/DOWN, STOP/MOVE, PERCENT xx%)
160 private void handleShutterCommand(Command command) {
161 Where w = deviceWhere;
163 calibrating = CALIBRATION_INACTIVE; // cancel calibration if we receive a command
164 commandRequestedWhileMoving = null;
166 if (StopMoveType.STOP.equals(command)) {
167 send(Automation.requestStop(w.value()));
168 } else if (command instanceof UpDownType || command instanceof PercentType) {
169 if (movingState == MOVING_STATE_MOVING_UP || movingState == MOVING_STATE_MOVING_DOWN) { // already
171 logger.debug("# {} # already moving, STOP then defer command", deviceWhere);
172 commandRequestedWhileMoving = command;
173 sendHighPriority(Automation.requestStop(w.value()));
176 if (command instanceof UpDownType) {
177 if (UpDownType.UP.equals(command)) {
178 send(Automation.requestMoveUp(w.value()));
180 send(Automation.requestMoveDown(w.value()));
182 } else if (command instanceof PercentType) {
183 handlePercentCommand((PercentType) command, w.value());
187 logger.debug("Unsupported command {} for thing {}", command, thing.getUID());
189 } catch (OWNException e) {
190 logger.debug("Exception while sending request for command {}: {}", command, e.getMessage(), e);
196 * Handles Automation PERCENT xx% command
198 private void handlePercentCommand(PercentType command, String w) {
199 int percent = command.intValue();
200 if (percent == positionEstimation) {
201 logger.debug("# {} # handlePercentCommand() Command {}% == positionEstimation -> nothing to do", w,
206 if (percent == POSITION_DOWN) { // GO TO 100%
207 send(Automation.requestMoveDown(w));
208 } else if (percent == POSITION_UP) { // GO TO 0%
209 send(Automation.requestMoveUp(w));
210 } else { // GO TO XX%
211 logger.debug("# {} # {}% requested", deviceWhere, percent);
212 if (shutterRun == SHUTTER_RUN_UNDEFINED) {
213 logger.debug("& {} & CALIBRATION - shutterRun not configured, starting CALIBRATION...",
215 calibrating = CALIBRATION_ACTIVATED;
216 send(Automation.requestMoveUp(w));
217 positionRequested = percent;
218 } else if (shutterRun >= 1000 && positionEstimation != POSITION_UNKNOWN) {
219 // these two must be known to calculate moveTime.
220 // Calculate how much time we have to move and set a deadline to stop after that time
222 .round(((float) Math.abs(percent - positionEstimation) / POSITION_MAX_STEPS * shutterRun));
223 logger.debug("# {} # target moveTime={}", deviceWhere, moveTime);
224 if (moveTime > MIN_STEP_TIME_MSEC) {
225 ScheduledFuture<?> mSch = moveSchedule;
226 if (mSch != null && !mSch.isDone()) {
227 // a moveSchedule was already scheduled and is not done... let's cancel the schedule
229 logger.debug("# {} # new XX% requested, old moveSchedule cancelled", deviceWhere);
231 // send a requestFirmwareVersion message to BUS gateways to wake up the CMD connection before
232 // sending further cmds
233 OpenWebNetBridgeHandler h = bridgeHandler;
234 if (h != null && h.isBusGateway()) {
235 OpenGateway gw = h.gateway;
237 if (!gw.isCmdConnectionReady()) {
238 logger.debug("# {} # waking-up CMD connection...", deviceWhere);
239 send(GatewayMgmt.requestFirmwareVersion());
243 // REMINDER: start the schedule BEFORE sending the command, because the synch command waits for
244 // ACK and can take some 300ms
245 logger.debug("# {} # Starting schedule...", deviceWhere);
246 moveSchedule = scheduler.schedule(() -> {
247 logger.debug("# {} # moveSchedule expired, sending STOP...", deviceWhere);
249 sendHighPriority(Automation.requestStop(w));
250 } catch (OWNException ex) {
251 logger.debug("Exception while sending request for command {}: {}", command,
252 ex.getMessage(), ex);
254 }, moveTime, TimeUnit.MILLISECONDS);
255 logger.debug("# {} # ...schedule started, now sending highPriority command...", deviceWhere);
256 if (percent < positionEstimation) {
257 sendHighPriority(Automation.requestMoveUp(w));
259 sendHighPriority(Automation.requestMoveDown(w));
261 logger.debug("# {} # ...gateway.sendHighPriority() returned", deviceWhere);
263 logger.debug("# {} # moveTime <= MIN_STEP_TIME_MSEC ---> do nothing", deviceWhere);
267 "Command {} cannot be executed: unknown position or shutterRun configuration params not/wrongly set (thing={})",
268 command, thing.getUID());
271 } catch (OWNException e) {
272 logger.debug("Exception while sending request for command {}: {}", command, e.getMessage(), e);
277 protected String ownIdPrefix() {
278 return Who.AUTOMATION.value().toString();
282 protected void handleMessage(BaseOpenMessage msg) {
283 updateAutomationState((Automation) msg);
284 // REMINDER: update state, then update thing status in the super method, to avoid delays
285 super.handleMessage(msg);
289 * Updates automation device state based on the Automation message received from OWN network
291 * @param msg the Automation message
293 private void updateAutomationState(Automation msg) {
294 logger.debug("updateAutomationState() - msg={} what={}", msg, msg.getWhat());
296 if (msg.isCommandTranslation()) {
297 logger.debug("msg is command translation, ignoring it");
300 } catch (FrameException fe) {
301 logger.warn("Exception while checking WHERE command translation for frame {}: {}, ignoring it", msg,
305 updateMovingState(MOVING_STATE_MOVING_UP);
306 if (calibrating == CALIBRATION_ACTIVATED) {
307 calibrating = CALIBRATION_GOING_UP;
308 logger.debug("& {} & CALIBRATION - started going ALL UP...", deviceWhere);
310 } else if (msg.isDown()) {
311 updateMovingState(MOVING_STATE_MOVING_DOWN);
312 if (calibrating == CALIBRATION_ACTIVATED) {
313 calibrating = CALIBRATION_GOING_DOWN;
314 logger.debug("& {} & CALIBRATION - started going ALL DOWN...", deviceWhere);
316 } else if (msg.isStop()) {
317 long stoppedAt = System.currentTimeMillis();
318 if (calibrating == CALIBRATION_GOING_DOWN && shutterRun == SHUTTER_RUN_UNDEFINED) {
319 shutterRun = (int) (stoppedAt - startedMovingAt);
320 logger.debug("& {} & CALIBRATION - reached DOWN ---> shutterRun={}", deviceWhere, shutterRun);
321 updateMovingState(MOVING_STATE_STOPPED);
322 logger.debug("& {} & CALIBRATION - COMPLETED, now going to {}%", deviceWhere, positionRequested);
323 handleShutterCommand(new PercentType(positionRequested));
324 Configuration configuration = editConfiguration();
325 configuration.put(OpenWebNetBindingConstants.CONFIG_PROPERTY_SHUTTER_RUN, Integer.toString(shutterRun));
326 updateConfiguration(configuration);
327 logger.debug("& {} & CALIBRATION - configuration updated: shutterRun = {}ms", deviceWhere, shutterRun);
328 } else if (calibrating == CALIBRATION_GOING_UP) {
329 updateMovingState(MOVING_STATE_STOPPED);
330 logger.debug("& {} & CALIBRATION - reached UP, now sending DOWN command...", deviceWhere);
331 calibrating = CALIBRATION_ACTIVATED;
332 Where dw = deviceWhere;
334 String w = dw.value();
336 send(Automation.requestMoveDown(w));
337 } catch (OWNException e) {
338 logger.debug("Exception while sending DOWN command during calibration: {}", e.getMessage(), e);
339 calibrating = CALIBRATION_INACTIVE;
343 updateMovingState(MOVING_STATE_STOPPED);
344 // do deferred command, if present
345 Command cmd = commandRequestedWhileMoving;
347 handleShutterCommand(cmd);
351 logger.debug("Frame {} not supported for thing {}, ignoring it.", msg, thing.getUID());
356 * Updates movingState to newState
358 private void updateMovingState(int newState) {
359 if (movingState == MOVING_STATE_STOPPED) {
360 if (newState != MOVING_STATE_STOPPED) { // moving after stop
361 startedMovingAt = System.currentTimeMillis();
362 synchronized (DATE_FORMATTER) {
363 logger.debug("# {} # MOVING {} - startedMovingAt={} - {}", deviceWhere, newState, startedMovingAt,
364 DATE_FORMATTER.format(new Date(startedMovingAt)));
367 } else { // we were moving
369 if (newState != MOVING_STATE_STOPPED) { // moving after moving, take new timestamp
370 startedMovingAt = System.currentTimeMillis();
371 synchronized (DATE_FORMATTER) {
372 logger.debug("# {} # MOVING {} - startedMovingAt={} - {}", deviceWhere, newState, startedMovingAt,
373 DATE_FORMATTER.format(new Date(startedMovingAt)));
376 // cancel the schedule
377 ScheduledFuture<?> mSc = moveSchedule;
378 if (mSc != null && !mSc.isDone()) {
382 movingState = newState;
383 logger.debug("# {} # movingState={} positionEstimation={} - calibrating={} shutterRun={}", deviceWhere,
384 movingState, positionEstimation, calibrating, shutterRun);
388 * Updates positionEstimation and then channel state based on movedTime and current movingState
390 private void updatePosition() {
391 int newPos = POSITION_UNKNOWN;
392 if (shutterRun > 0) {// we have shutterRun defined, let's calculate new positionEstimation
393 long movedTime = System.currentTimeMillis() - startedMovingAt;
394 logger.debug("# {} # current positionEstimation={} movedTime={}", deviceWhere, positionEstimation,
396 int movedSteps = Math.round((float) movedTime / shutterRun * POSITION_MAX_STEPS);
397 logger.debug("# {} # movedSteps: {} {}", deviceWhere, movedSteps,
398 (movingState == MOVING_STATE_MOVING_DOWN) ? "DOWN(+)" : "UP(-)");
399 if (positionEstimation == POSITION_UNKNOWN && movedSteps >= POSITION_MAX_STEPS) { // we did a full run
400 newPos = (movingState == MOVING_STATE_MOVING_DOWN) ? POSITION_DOWN : POSITION_UP;
401 } else if (positionEstimation != POSITION_UNKNOWN) {
402 newPos = positionEstimation
403 + ((movingState == MOVING_STATE_MOVING_DOWN) ? movedSteps : -1 * movedSteps);
404 logger.debug("# {} # {} {} {} = {}", deviceWhere, positionEstimation,
405 (movingState == MOVING_STATE_MOVING_DOWN) ? "+" : "-", movedSteps, newPos);
406 if (newPos > POSITION_DOWN) {
407 newPos = POSITION_DOWN;
408 } else if (newPos < POSITION_UP) {
409 newPos = POSITION_UP;
413 if (newPos != POSITION_UNKNOWN) {
414 if (newPos != positionEstimation) {
415 updateState(CHANNEL_SHUTTER, new PercentType(newPos));
418 updateState(CHANNEL_SHUTTER, UnDefType.UNDEF);
420 positionEstimation = newPos;
424 public void dispose() {
425 ScheduledFuture<?> mSc = moveSchedule;