]> git.basschouten.com Git - openhab-addons.git/blob
9e36f1aba12c5f14c8d26d41844c17863fef0ed6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 java.util.Optional;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * {@link ParentUpdateCapability} is the class used to request data update upon initialization of a module
26  *
27  * @author GaĆ«l L'hopital - Initial contribution
28  *
29  */
30 @NonNullByDefault
31 public class ParentUpdateCapability extends Capability {
32     private static final int DEFAULT_DELAY_S = 2;
33
34     private final Logger logger = LoggerFactory.getLogger(ParentUpdateCapability.class);
35     private Optional<ScheduledFuture<?>> job = Optional.empty();
36
37     public ParentUpdateCapability(CommonInterface handler) {
38         super(handler);
39     }
40
41     @Override
42     public void initialize() {
43         job = Optional.of(handler.getScheduler().schedule(() -> {
44             logger.debug("Requesting parents data update for Thing {}", handler.getId());
45             CommonInterface bridgeHandler = handler.getBridgeHandler();
46             if (bridgeHandler != null) {
47                 bridgeHandler.expireData();
48             }
49         }, DEFAULT_DELAY_S, TimeUnit.SECONDS));
50     }
51
52     @Override
53     public void dispose() {
54         job.ifPresent(j -> j.cancel(true));
55         job = Optional.empty();
56         super.dispose();
57     }
58 }