2 * Copyright (c) 2010-2022 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.paradoxalarm.internal.handlers;
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
18 import org.openhab.binding.paradoxalarm.internal.model.ParadoxPanel;
19 import org.openhab.core.thing.ChannelUID;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link EntityBaseHandler} abstract handler class that contains common logic for entities.
32 * @author Konstantin Polihronov - Initial contribution
34 public abstract class EntityBaseHandler extends BaseThingHandler {
36 private static final long INITIAL_DELAY_SECONDS = 15;
37 private static final int MAX_WAIT_TIME_MILLIS = 60000;
38 private long timeStamp;
40 private final Logger logger = LoggerFactory.getLogger(EntityBaseHandler.class);
42 protected EntityConfiguration config;
43 private ScheduledFuture<?> delayedSchedule;
45 public EntityBaseHandler(Thing thing) {
50 public void initialize() {
51 logger.trace("Start initializing. {}", thing.getUID());
52 updateStatus(ThingStatus.UNKNOWN);
54 config = getConfigAs(EntityConfiguration.class);
56 timeStamp = System.currentTimeMillis();
57 delayedSchedule = scheduler.schedule(this::initializeDelayed, INITIAL_DELAY_SECONDS, TimeUnit.SECONDS);
60 private void initializeDelayed() {
61 logger.debug("Start initializeDelayed() in {}", getThing().getUID());
62 ParadoxPanel panel = ParadoxPanel.getInstance();
63 // Asynchronous update not yet done
64 if (panel.getPanelInformation() == null) {
65 // Retry until reach MAX_WAIT_TIME
66 if (System.currentTimeMillis() - timeStamp <= MAX_WAIT_TIME_MILLIS) {
67 logger.debug("Panel information is null. Scheduling initializeDelayed() to be executed again in {} sec",
68 INITIAL_DELAY_SECONDS);
69 delayedSchedule = scheduler.schedule(this::initializeDelayed, INITIAL_DELAY_SECONDS, TimeUnit.SECONDS);
71 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
72 "Panel is not updating the information in " + MAX_WAIT_TIME_MILLIS
73 + " ms. Giving up. Cannot update entity=" + this + ".");
76 // Asynchronous update done but panel is not supported
77 } else if (!panel.isPanelSupported()) {
78 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
79 "Panel is not supported. Cannot update entity=" + this + ".");
82 updateStatus(ThingStatus.ONLINE);
87 public void handleCommand(ChannelUID channelUID, Command command) {
88 if (ThingStatus.OFFLINE == getThing().getStatus()) {
89 logger.debug("Received {} command but {} is OFFLINE with the following detailed status {}", command,
90 getThing().getUID(), getThing().getStatusInfo());
94 if (command instanceof RefreshType) {
100 public void dispose() {
101 if (delayedSchedule != null) {
102 boolean cancelingResult = delayedSchedule.cancel(true);
103 String cancelingSuccessful = cancelingResult ? "successful" : "failed";
104 logger.debug("Canceling schedule of {} is {}", delayedSchedule, cancelingSuccessful);
108 protected abstract void updateEntity();
110 protected int calculateEntityIndex() {
111 return Math.max(0, config.getId() - 1);