]> git.basschouten.com Git - openhab-addons.git/blob
11750ec6c3fc0acaecd69b57446a64f4d1fbff1a
[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.netatmo.internal.handler.capability;
14
15 import static java.time.temporal.ChronoUnit.*;
16
17 import java.time.Duration;
18 import java.time.Instant;
19 import java.time.ZonedDateTime;
20 import java.util.Optional;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.netatmo.internal.api.dto.NAThing;
26 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
27 import org.openhab.core.thing.ThingStatus;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * {@link RefreshCapability} is the class used to embed the refreshing needs calculation for devices
33  *
34  * @author GaĆ«l L'hopital - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 public class RefreshCapability extends Capability {
39     private static final Duration DEFAULT_DELAY = Duration.of(20, SECONDS);
40     private static final Duration PROBING_INTERVAL = Duration.of(120, SECONDS);
41     private static final Duration OFFLINE_INTERVAL = Duration.of(15, MINUTES);
42
43     private final Logger logger = LoggerFactory.getLogger(RefreshCapability.class);
44
45     private Duration dataValidity;
46     private Instant dataTimeStamp = Instant.now();
47     private Instant dataTimeStamp0 = Instant.MIN;
48     private Optional<ScheduledFuture<?>> refreshJob = Optional.empty();
49     private boolean refreshConfigured;
50
51     public RefreshCapability(CommonInterface handler, int refreshInterval) {
52         super(handler);
53         this.dataValidity = Duration.ofSeconds(Math.max(0, refreshInterval));
54     }
55
56     @Override
57     public void initialize() {
58         this.refreshConfigured = !probing();
59         freeJobAndReschedule(2);
60     }
61
62     @Override
63     public void dispose() {
64         freeJobAndReschedule(0);
65         super.dispose();
66     }
67
68     @Override
69     public void expireData() {
70         dataTimeStamp = Instant.now().minus(dataValidity);
71         freeJobAndReschedule(1);
72     }
73
74     private Duration dataAge() {
75         return Duration.between(dataTimeStamp, Instant.now());
76     }
77
78     private boolean probing() {
79         return dataValidity.getSeconds() <= 0;
80     }
81
82     private void proceedWithUpdate() {
83         handler.proceedWithUpdate();
84         long delay;
85         if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) {
86             logger.debug("Module is not ONLINE; special refresh interval is used");
87             delay = OFFLINE_INTERVAL.toSeconds();
88             if (probing()) {
89                 dataTimeStamp0 = Instant.MIN;
90             }
91         } else {
92             delay = refreshConfigured ? dataValidity.getSeconds()
93                     : (probing() ? PROBING_INTERVAL : dataValidity.minus(dataAge()).plus(DEFAULT_DELAY)).toSeconds();
94         }
95         delay = delay < 2 ? PROBING_INTERVAL.toSeconds() : delay;
96         logger.debug("Module refreshed, next one in {}s", delay);
97         freeJobAndReschedule(delay);
98     }
99
100     @Override
101     protected void updateNAThing(NAThing newData) {
102         super.updateNAThing(newData);
103         newData.getLastSeen().map(ZonedDateTime::toInstant).ifPresent(tsInstant -> {
104             if (probing()) {
105                 if (Instant.MIN.equals(dataTimeStamp0)) {
106                     dataTimeStamp0 = tsInstant;
107                     logger.debug("First data timestamp is {}", dataTimeStamp0);
108                 } else if (tsInstant.isAfter(dataTimeStamp0)) {
109                     dataValidity = Duration.between(dataTimeStamp0, tsInstant);
110                     refreshConfigured = true;
111                     logger.debug("Data validity period identified to be {}", dataValidity);
112                 } else {
113                     logger.debug("Data validity period not yet found, data timestamp unchanged");
114                 }
115             }
116             dataTimeStamp = tsInstant;
117         });
118     }
119
120     private void freeJobAndReschedule(long delay) {
121         refreshJob.ifPresent(job -> job.cancel(true));
122         refreshJob = Optional.ofNullable(delay == 0 ? null
123                 : handler.getScheduler().schedule(() -> proceedWithUpdate(), delay, TimeUnit.SECONDS));
124     }
125 }