2 * Copyright (c) 2010-2021 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.WhereLightAutom;
45 import org.openwebnet4j.message.Who;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * The {@link OpenWebNetAutomationHandler} is responsible for handling commands/messages for an Automation OpenWebNet
51 * device. It extends the abstract {@link OpenWebNetThingHandler}.
53 * @author Massimo Valla - Initial contribution
56 public class OpenWebNetAutomationHandler extends OpenWebNetThingHandler {
58 private final Logger logger = LoggerFactory.getLogger(OpenWebNetAutomationHandler.class);
60 private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("ss.SSS");
62 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.AUTOMATION_SUPPORTED_THING_TYPES;
65 public static final int MOVING_STATE_STOPPED = 0;
66 public static final int MOVING_STATE_MOVING_UP = 1;
67 public static final int MOVING_STATE_MOVING_DOWN = 2;
68 public static final int MOVING_STATE_UNKNOWN = -1;
71 public static final int CALIBRATION_INACTIVE = -1;
72 public static final int CALIBRATION_ACTIVATED = 0;
73 public static final int CALIBRATION_GOING_UP = 1;
74 public static final int CALIBRATION_GOING_DOWN = 2;
77 public static final int POSITION_MAX_STEPS = 100;
78 public static final int POSITION_DOWN = 100;
79 public static final int POSITION_UP = 0;
80 public static final int POSITION_UNKNOWN = -1;
82 public static final int SHUTTER_RUN_UNDEFINED = -1;
83 private int shutterRun = SHUTTER_RUN_UNDEFINED;
84 private static final String AUTO_CALIBRATION = "AUTO";
86 private long startedMovingAt = -1;
87 private int movingState = MOVING_STATE_UNKNOWN;
88 private int positionEstimation = POSITION_UNKNOWN;
89 private @Nullable ScheduledFuture<?> moveSchedule;
90 private int positionRequested = POSITION_UNKNOWN;
91 private int calibrating = CALIBRATION_INACTIVE;
92 private static final int MIN_STEP_TIME_MSEC = 50;
93 private @Nullable Command commandRequestedWhileMoving = null;
95 public OpenWebNetAutomationHandler(Thing thing) {
100 public void initialize() {
102 Object shutterRunConfig = getConfig().get(OpenWebNetBindingConstants.CONFIG_PROPERTY_SHUTTER_RUN);
104 if (shutterRunConfig == null) {
105 shutterRunConfig = AUTO_CALIBRATION;
106 logger.debug("shutterRun null --> default to AUTO");
107 } else if (shutterRunConfig instanceof String) {
108 if (AUTO_CALIBRATION.equalsIgnoreCase(((String) shutterRunConfig))) {
109 logger.debug("shutterRun set to AUTO via configuration");
110 shutterRun = SHUTTER_RUN_UNDEFINED; // reset shutterRun
111 } else { // try to parse int>=1000
112 int shutterRunInt = Integer.parseInt((String) shutterRunConfig);
113 if (shutterRunInt < 1000) {
114 throw new NumberFormatException();
116 shutterRun = shutterRunInt;
117 logger.debug("shutterRun set to {} via configuration", shutterRun);
120 throw new NumberFormatException();
122 } catch (NumberFormatException e) {
123 logger.debug("Wrong configuration: {} setting must be {} or an integer >= 1000",
124 OpenWebNetBindingConstants.CONFIG_PROPERTY_SHUTTER_RUN, AUTO_CALIBRATION);
125 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
126 "@text/offline.wrong-configuration");
127 shutterRun = SHUTTER_RUN_UNDEFINED;
129 updateState(CHANNEL_SHUTTER, UnDefType.UNDEF);
130 positionEstimation = POSITION_UNKNOWN;
134 protected Where buildBusWhere(String wStr) throws IllegalArgumentException {
135 return new WhereLightAutom(wStr);
139 protected void requestChannelState(ChannelUID channel) {
140 logger.debug("requestChannelState() thingUID={} channel={}", thing.getUID(), channel.getId());
141 Where w = deviceWhere;
144 send(Automation.requestStatus(w.value()));
145 } catch (OWNException e) {
146 logger.debug("Exception while requesting channel {} state: {}", channel, e.getMessage(), e);
152 protected void handleChannelCommand(ChannelUID channel, Command command) {
153 switch (channel.getId()) {
154 case CHANNEL_SHUTTER:
155 handleShutterCommand(command);
158 logger.info("Unsupported channel UID {}", channel);
164 * Handles Automation Roller shutter command (UP/DOWN, STOP/MOVE, PERCENT xx%)
166 private void handleShutterCommand(Command command) {
167 Where w = deviceWhere;
169 calibrating = CALIBRATION_INACTIVE; // cancel calibration if we receive a command
170 commandRequestedWhileMoving = null;
172 if (StopMoveType.STOP.equals(command)) {
173 send(Automation.requestStop(w.value()));
174 } else if (command instanceof UpDownType || command instanceof PercentType) {
175 if (movingState == MOVING_STATE_MOVING_UP || movingState == MOVING_STATE_MOVING_DOWN) { // already
177 logger.debug("# {} # already moving, STOP then defer command", deviceWhere);
178 commandRequestedWhileMoving = command;
179 sendHighPriority(Automation.requestStop(w.value()));
182 if (command instanceof UpDownType) {
183 if (UpDownType.UP.equals(command)) {
184 send(Automation.requestMoveUp(w.value()));
186 send(Automation.requestMoveDown(w.value()));
188 } else if (command instanceof PercentType) {
189 handlePercentCommand((PercentType) command, w.value());
193 logger.debug("Unsupported command {} for thing {}", command, thing.getUID());
195 } catch (OWNException e) {
196 logger.debug("Exception while sending request for command {}: {}", command, e.getMessage(), e);
202 * Handles Automation PERCENT xx% command
204 private void handlePercentCommand(PercentType command, String w) {
205 int percent = command.intValue();
206 if (percent == positionEstimation) {
207 logger.debug("# {} # handlePercentCommand() Command {}% == positionEstimation -> nothing to do", w,
212 if (percent == POSITION_DOWN) { // GO TO 100%
213 send(Automation.requestMoveDown(w));
214 } else if (percent == POSITION_UP) { // GO TO 0%
215 send(Automation.requestMoveUp(w));
216 } else { // GO TO XX%
217 logger.debug("# {} # {}% requested", deviceWhere, percent);
218 if (shutterRun == SHUTTER_RUN_UNDEFINED) {
219 logger.debug("& {} & CALIBRATION - shutterRun not configured, starting CALIBRATION...",
221 calibrating = CALIBRATION_ACTIVATED;
222 send(Automation.requestMoveUp(w));
223 positionRequested = percent;
224 } else if (shutterRun >= 1000 && positionEstimation != POSITION_UNKNOWN) {
225 // these two must be known to calculate moveTime.
226 // Calculate how much time we have to move and set a deadline to stop after that time
228 .round(((float) Math.abs(percent - positionEstimation) / POSITION_MAX_STEPS * shutterRun));
229 logger.debug("# {} # target moveTime={}", deviceWhere, moveTime);
230 if (moveTime > MIN_STEP_TIME_MSEC) {
231 ScheduledFuture<?> mSch = moveSchedule;
232 if (mSch != null && !mSch.isDone()) {
233 // a moveSchedule was already scheduled and is not done... let's cancel the schedule
235 logger.debug("# {} # new XX% requested, old moveSchedule cancelled", deviceWhere);
237 // send a requestFirmwareVersion message to BUS gateways to wake up the CMD connection before
238 // sending further cmds
239 OpenWebNetBridgeHandler h = bridgeHandler;
240 if (h != null && h.isBusGateway()) {
241 OpenGateway gw = h.gateway;
243 if (!gw.isCmdConnectionReady()) {
244 logger.debug("# {} # waking-up CMD connection...", deviceWhere);
245 send(GatewayMgmt.requestFirmwareVersion());
249 // REMINDER: start the schedule BEFORE sending the command, because the synch command waits for
250 // ACK and can take some 300ms
251 logger.debug("# {} # Starting schedule...", deviceWhere);
252 moveSchedule = scheduler.schedule(() -> {
253 logger.debug("# {} # moveSchedule expired, sending STOP...", deviceWhere);
255 sendHighPriority(Automation.requestStop(w));
256 } catch (OWNException ex) {
257 logger.debug("Exception while sending request for command {}: {}", command,
258 ex.getMessage(), ex);
260 }, moveTime, TimeUnit.MILLISECONDS);
261 logger.debug("# {} # ...schedule started, now sending highPriority command...", deviceWhere);
262 if (percent < positionEstimation) {
263 sendHighPriority(Automation.requestMoveUp(w));
265 sendHighPriority(Automation.requestMoveDown(w));
267 logger.debug("# {} # ...gateway.sendHighPriority() returned", deviceWhere);
269 logger.debug("# {} # moveTime <= MIN_STEP_TIME_MSEC ---> do nothing", deviceWhere);
273 "Command {} cannot be executed: unknown position or shutterRun configuration params not/wrongly set (thing={})",
274 command, thing.getUID());
277 } catch (OWNException e) {
278 logger.debug("Exception while sending request for command {}: {}", command, e.getMessage(), e);
283 protected String ownIdPrefix() {
284 return Who.AUTOMATION.value().toString();
288 protected void handleMessage(BaseOpenMessage msg) {
289 logger.debug("handleMessage({}) for thing: {}", msg, thing.getUID());
290 updateAutomationState((Automation) msg);
291 // REMINDER: update automation state, and only after update thing status using the super method, to avoid delays
292 super.handleMessage(msg);
296 * Updates automation device state based on the Automation message received from OWN network
298 * @param msg the Automation message
300 private void updateAutomationState(Automation msg) {
301 logger.debug("updateAutomationState() - msg={} what={}", msg, msg.getWhat());
303 if (msg.isCommandTranslation()) {
304 logger.debug("msg is command translation, ignoring it");
307 } catch (FrameException fe) {
308 logger.warn("Exception while checking WHERE command translation for frame {}: {}, ignoring it", msg,
312 updateMovingState(MOVING_STATE_MOVING_UP);
313 if (calibrating == CALIBRATION_ACTIVATED) {
314 calibrating = CALIBRATION_GOING_UP;
315 logger.debug("& {} & CALIBRATION - started going ALL UP...", deviceWhere);
317 } else if (msg.isDown()) {
318 updateMovingState(MOVING_STATE_MOVING_DOWN);
319 if (calibrating == CALIBRATION_ACTIVATED) {
320 calibrating = CALIBRATION_GOING_DOWN;
321 logger.debug("& {} & CALIBRATION - started going ALL DOWN...", deviceWhere);
323 } else if (msg.isStop()) {
324 long stoppedAt = System.currentTimeMillis();
325 if (calibrating == CALIBRATION_GOING_DOWN && shutterRun == SHUTTER_RUN_UNDEFINED) {
326 shutterRun = (int) (stoppedAt - startedMovingAt);
327 logger.debug("& {} & CALIBRATION - reached DOWN ---> shutterRun={}", deviceWhere, shutterRun);
328 updateMovingState(MOVING_STATE_STOPPED);
329 logger.debug("& {} & CALIBRATION - COMPLETED, now going to {}%", deviceWhere, positionRequested);
330 handleShutterCommand(new PercentType(positionRequested));
331 Configuration configuration = editConfiguration();
332 configuration.put(OpenWebNetBindingConstants.CONFIG_PROPERTY_SHUTTER_RUN, Integer.toString(shutterRun));
333 updateConfiguration(configuration);
334 logger.debug("& {} & CALIBRATION - configuration updated: shutterRun = {}ms", deviceWhere, shutterRun);
335 } else if (calibrating == CALIBRATION_GOING_UP) {
336 updateMovingState(MOVING_STATE_STOPPED);
337 logger.debug("& {} & CALIBRATION - reached UP, now sending DOWN command...", deviceWhere);
338 calibrating = CALIBRATION_ACTIVATED;
339 Where dw = deviceWhere;
341 String w = dw.value();
343 send(Automation.requestMoveDown(w));
344 } catch (OWNException e) {
345 logger.debug("Exception while sending DOWN command during calibration: {}", e.getMessage(), e);
346 calibrating = CALIBRATION_INACTIVE;
350 updateMovingState(MOVING_STATE_STOPPED);
351 // do deferred command, if present
352 Command cmd = commandRequestedWhileMoving;
354 handleShutterCommand(cmd);
358 logger.debug("Frame {} not supported for thing {}, ignoring it.", msg, thing.getUID());
363 * Updates movingState to newState
365 private void updateMovingState(int newState) {
366 if (movingState == MOVING_STATE_STOPPED) {
367 if (newState != MOVING_STATE_STOPPED) { // moving after stop
368 startedMovingAt = System.currentTimeMillis();
369 synchronized (DATE_FORMATTER) {
370 logger.debug("# {} # MOVING {} - startedMovingAt={} - {}", deviceWhere, newState, startedMovingAt,
371 DATE_FORMATTER.format(new Date(startedMovingAt)));
374 } else { // we were moving
376 if (newState != MOVING_STATE_STOPPED) { // moving after moving, take new timestamp
377 startedMovingAt = System.currentTimeMillis();
378 synchronized (DATE_FORMATTER) {
379 logger.debug("# {} # MOVING {} - startedMovingAt={} - {}", deviceWhere, newState, startedMovingAt,
380 DATE_FORMATTER.format(new Date(startedMovingAt)));
383 // cancel the schedule
384 ScheduledFuture<?> mSc = moveSchedule;
385 if (mSc != null && !mSc.isDone()) {
389 movingState = newState;
390 logger.debug("# {} # movingState={} positionEstimation={} - calibrating={} shutterRun={}", deviceWhere,
391 movingState, positionEstimation, calibrating, shutterRun);
395 * Updates positionEstimation and then channel state based on movedTime and current movingState
397 private void updatePosition() {
398 int newPos = POSITION_UNKNOWN;
399 if (shutterRun > 0) {// we have shutterRun defined, let's calculate new positionEstimation
400 long movedTime = System.currentTimeMillis() - startedMovingAt;
401 logger.debug("# {} # current positionEstimation={} movedTime={}", deviceWhere, positionEstimation,
403 int movedSteps = Math.round((float) movedTime / shutterRun * POSITION_MAX_STEPS);
404 logger.debug("# {} # movedSteps: {} {}", deviceWhere, movedSteps,
405 (movingState == MOVING_STATE_MOVING_DOWN) ? "DOWN(+)" : "UP(-)");
406 if (positionEstimation == POSITION_UNKNOWN && movedSteps >= POSITION_MAX_STEPS) { // we did a full run
407 newPos = (movingState == MOVING_STATE_MOVING_DOWN) ? POSITION_DOWN : POSITION_UP;
408 } else if (positionEstimation != POSITION_UNKNOWN) {
409 newPos = positionEstimation
410 + ((movingState == MOVING_STATE_MOVING_DOWN) ? movedSteps : -1 * movedSteps);
411 logger.debug("# {} # {} {} {} = {}", deviceWhere, positionEstimation,
412 (movingState == MOVING_STATE_MOVING_DOWN) ? "+" : "-", movedSteps, newPos);
413 if (newPos > POSITION_DOWN) {
414 newPos = POSITION_DOWN;
415 } else if (newPos < POSITION_UP) {
416 newPos = POSITION_UP;
420 if (newPos != POSITION_UNKNOWN) {
421 if (newPos != positionEstimation) {
422 updateState(CHANNEL_SHUTTER, new PercentType(newPos));
425 updateState(CHANNEL_SHUTTER, UnDefType.UNDEF);
427 positionEstimation = newPos;
431 public void dispose() {
432 ScheduledFuture<?> mSc = moveSchedule;