]> git.basschouten.com Git - openhab-addons.git/blob
0bb4b139928f1a1c7dc50d4718c8f7f78b64a520
[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.paradoxalarm.internal.handlers;
14
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
17
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;
28
29 /**
30  * The {@link EntityBaseHandler} abstract handler class that contains common logic for entities.
31  *
32  * @author Konstantin Polihronov - Initial contribution
33  */
34 public abstract class EntityBaseHandler extends BaseThingHandler {
35
36     private static final long INITIAL_DELAY_SECONDS = 15;
37     private static final int MAX_WAIT_TIME_MILLIS = 60000;
38     private long timeStamp;
39
40     private final Logger logger = LoggerFactory.getLogger(EntityBaseHandler.class);
41
42     protected EntityConfiguration config;
43     private ScheduledFuture<?> delayedSchedule;
44
45     public EntityBaseHandler(Thing thing) {
46         super(thing);
47     }
48
49     @Override
50     public void initialize() {
51         logger.trace("Start initializing. {}", thing.getUID());
52         updateStatus(ThingStatus.UNKNOWN);
53
54         config = getConfigAs(EntityConfiguration.class);
55
56         timeStamp = System.currentTimeMillis();
57         delayedSchedule = scheduler.schedule(this::initializeDelayed, INITIAL_DELAY_SECONDS, TimeUnit.SECONDS);
58     }
59
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);
71             } else {
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 + ".");
75             }
76
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 + ".");
81             // All OK
82         } else {
83             updateStatus(ThingStatus.ONLINE);
84         }
85     }
86
87     @Override
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());
92             return;
93         }
94
95         if (command instanceof RefreshType) {
96             updateEntity();
97         }
98     }
99
100     @Override
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);
106         }
107     }
108
109     protected abstract void updateEntity();
110
111     protected int calculateEntityIndex() {
112         return Math.max(0, config.getId() - 1);
113     }
114 }