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.netatmo.internal.handler.capability;
15 import static java.time.temporal.ChronoUnit.*;
17 import java.time.Duration;
18 import java.time.Instant;
19 import java.util.Optional;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
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;
32 * {@link RefreshCapability} is the class used to embed the refreshing needs calculation for devices
34 * @author Gaƫl L'hopital - Initial contribution
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);
43 private final Logger logger = LoggerFactory.getLogger(RefreshCapability.class);
44 private final ScheduledExecutorService scheduler;
46 private Duration dataValidity;
47 private Instant dataTimeStamp = Instant.now();
48 private Instant dataTimeStamp0 = Instant.MIN;
49 private Optional<ScheduledFuture<?>> refreshJob = Optional.empty();
50 private final boolean refreshConfigured;
52 public RefreshCapability(CommonInterface handler, ScheduledExecutorService scheduler, int refreshInterval) {
54 this.scheduler = scheduler;
55 this.dataValidity = Duration.ofSeconds(Math.max(0, refreshInterval));
56 this.refreshConfigured = !probing();
57 freeJobAndReschedule(2);
61 public void dispose() {
62 freeJobAndReschedule(0);
67 public void expireData() {
68 dataTimeStamp = Instant.now().minus(dataValidity);
69 freeJobAndReschedule(1);
72 private Duration dataAge() {
73 return Duration.between(dataTimeStamp, Instant.now());
76 private boolean probing() {
77 return dataValidity.getSeconds() <= 0;
80 private void proceedWithUpdate() {
81 handler.proceedWithUpdate();
83 if (!ThingStatus.ONLINE.equals(handler.getThing().getStatus())) {
84 logger.debug("Module is not ONLINE; special refresh interval is used");
85 delay = OFFLINE_INTERVAL.toSeconds();
87 dataTimeStamp0 = Instant.MIN;
89 } else if (refreshConfigured) {
90 delay = dataValidity.getSeconds();
92 delay = (probing() ? PROBING_INTERVAL : dataValidity.minus(dataAge()).plus(DEFAULT_DELAY)).toSeconds();
94 delay = delay < 2 ? PROBING_INTERVAL.toSeconds() : delay;
95 logger.debug("Module refreshed, next one in {} s", delay);
96 freeJobAndReschedule(delay);
100 protected void updateNAThing(NAThing newData) {
101 super.updateNAThing(newData);
102 newData.getLastSeen().ifPresent(timestamp -> {
103 Instant tsInstant = timestamp.toInstant();
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 logger.debug("Data validity period identified to be {}", dataValidity);
112 logger.debug("Data validity period not yet found - data timestamp unchanged");
115 dataTimeStamp = tsInstant;
119 private void freeJobAndReschedule(long delay) {
120 refreshJob.ifPresent(job -> job.cancel(false));
121 refreshJob = delay == 0 ? Optional.empty()
122 : Optional.of(scheduler.schedule(() -> proceedWithUpdate(), delay, TimeUnit.SECONDS));