]> git.basschouten.com Git - openhab-addons.git/blob
a5470c74b8c77f2f134d4029b37f463a90e0b9ce
[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.surepetcare.internal.handler;
14
15 import static org.openhab.core.thing.ThingStatus.ONLINE;
16
17 import java.time.Duration;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.surepetcare.internal.SurePetcareAPIHelper;
23 import org.openhab.core.cache.ExpiringCache;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link SurePetcareBaseObjectHandler} is responsible for handling the things created to represent Sure Petcare
34  * objects.
35  *
36  * @author Rene Scherer - Initial Contribution
37  */
38 @NonNullByDefault
39 public abstract class SurePetcareBaseObjectHandler extends BaseThingHandler {
40
41     private static final int CACHE_TIMEOUT_SECOND = 3;
42
43     private final Logger logger = LoggerFactory.getLogger(SurePetcareBaseObjectHandler.class);
44
45     protected final SurePetcareAPIHelper petcareAPI;
46
47     protected ExpiringCache<Integer> updateThingCache = new ExpiringCache<>(Duration.ofSeconds(CACHE_TIMEOUT_SECOND),
48             this::refreshCache);
49
50     public SurePetcareBaseObjectHandler(Thing thing, final SurePetcareAPIHelper petcareAPI) {
51         super(thing);
52         this.petcareAPI = petcareAPI;
53     }
54
55     @Override
56     public void initialize() {
57         updateStatus(ONLINE);
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         if (command instanceof RefreshType) {
63             updateThingCache.getValue();
64         }
65     }
66
67     @Override
68     public void updateProperties(@Nullable Map<String, String> properties) {
69         super.updateProperties(properties);
70     }
71
72     private @Nullable Integer refreshCache() {
73         logger.debug("cache has timed out, we refresh the values in the thing");
74         updateThing();
75         // we don't care about the cache content, we just return an empty object
76         return Integer.MIN_VALUE;
77     }
78
79     /**
80      * Updates all channels of a thing.
81      */
82     protected abstract void updateThing();
83 }