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