]> git.basschouten.com Git - openhab-addons.git/blob
d2edfad22723a584a2074cd1621aef3a63f3ed21
[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.tado.internal.handler;
14
15 import java.io.IOException;
16 import java.util.Calendar;
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Objects;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.tado.internal.api.ApiException;
24 import org.openhab.binding.tado.internal.api.model.ControlDevice;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link TadoBatteryChecker} checks the battery state of Tado control
33  * devices.
34  *
35  * @author Andrew Fiddian-Green - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class TadoBatteryChecker {
40     private final Logger logger = LoggerFactory.getLogger(TadoBatteryChecker.class);
41
42     private final Map<Long, State> zoneList = new HashMap<>();
43     private final TadoHomeHandler homeHandler;
44
45     private Date refreshTime = new Date();
46
47     public TadoBatteryChecker(TadoHomeHandler homeHandler) {
48         this.homeHandler = homeHandler;
49     }
50
51     private synchronized void refreshZoneList() {
52         Date now = new Date();
53         if (now.after(refreshTime) || zoneList.isEmpty()) {
54             // be frugal, we only need to refresh the battery state hourly
55             Calendar calendar = Calendar.getInstance();
56             calendar.setTime(now);
57             calendar.add(Calendar.HOUR, 1);
58             refreshTime = calendar.getTime();
59
60             Long homeId = homeHandler.getHomeId();
61             if (homeId != null) {
62                 logger.debug("Fetching (battery state) zone list for HomeId {}", homeId);
63                 zoneList.clear();
64                 try {
65                     homeHandler.getApi().listZones(homeId).forEach(zone -> {
66                         boolean batteryLow = !zone.getDevices().stream().map(ControlDevice::getBatteryState)
67                                 .filter(Objects::nonNull).allMatch(s -> s.equals("NORMAL"));
68                         zoneList.put(Long.valueOf(zone.getId()), OnOffType.from(batteryLow));
69                     });
70                 } catch (IOException | ApiException e) {
71                     logger.debug("Fetch (battery state) zone list exception");
72                 }
73             }
74         }
75     }
76
77     public State getBatteryLowAlarm(long zoneId) {
78         refreshZoneList();
79         return zoneList.getOrDefault(zoneId, UnDefType.UNDEF);
80     }
81 }