]> git.basschouten.com Git - openhab-addons.git/blob
4d51a9b4166f33c0807c2df05ca6027208ce6c00
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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         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);
70             } else {
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 + ".");
74             }
75
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 + ".");
80             // All OK
81         } else {
82             updateStatus(ThingStatus.ONLINE);
83         }
84     }
85
86     @Override
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());
91             return;
92         }
93
94         if (command instanceof RefreshType) {
95             updateEntity();
96         }
97     }
98
99     @Override
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);
105         }
106     }
107
108     protected abstract void updateEntity();
109
110     protected int calculateEntityIndex() {
111         return Math.max(0, config.getId() - 1);
112     }
113 }