2 * Copyright (c) 2010-2023 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 ParadoxIP150BridgeHandler bridge = (ParadoxIP150BridgeHandler) getBridge().getHandler();
63 ParadoxPanel panel = bridge.getPanel();
64 // Asynchronous update not yet done
65 if (panel.getPanelInformation() == null) {
66 // Retry until reach MAX_WAIT_TIME
67 if (System.currentTimeMillis() - timeStamp <= MAX_WAIT_TIME_MILLIS) {
68 logger.debug("Panel information is null. Scheduling initializeDelayed() to be executed again in {} sec",
69 INITIAL_DELAY_SECONDS);
70 delayedSchedule = scheduler.schedule(this::initializeDelayed, INITIAL_DELAY_SECONDS, TimeUnit.SECONDS);
72 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
73 "Panel is not updating the information in " + MAX_WAIT_TIME_MILLIS
74 + " ms. Giving up. Cannot update entity=" + this + ".");
77 // Asynchronous update done but panel is not supported
78 } else if (!panel.isPanelSupported()) {
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80 "Panel is not supported. Cannot update entity=" + this + ".");
83 updateStatus(ThingStatus.ONLINE);
88 public void handleCommand(ChannelUID channelUID, Command command) {
89 if (ThingStatus.OFFLINE == getThing().getStatus()) {
90 logger.debug("Received {} command but {} is OFFLINE with the following detailed status {}", command,
91 getThing().getUID(), getThing().getStatusInfo());
95 if (command instanceof RefreshType) {
101 public void dispose() {
102 if (delayedSchedule != null) {
103 boolean cancelingResult = delayedSchedule.cancel(true);
104 String cancelingSuccessful = cancelingResult ? "successful" : "failed";
105 logger.debug("Canceling schedule of {} is {}", delayedSchedule, cancelingSuccessful);
109 protected abstract void updateEntity();
111 protected int calculateEntityIndex() {
112 return Math.max(0, config.getId() - 1);