]> git.basschouten.com Git - openhab-addons.git/blob
1989090dd9f9bae153cd8eca968f53d378639363
[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.groheondus.internal.handler;
14
15 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.*;
16
17 import java.io.IOException;
18 import java.time.Instant;
19 import java.time.temporal.ChronoUnit;
20 import java.util.List;
21 import java.util.Optional;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.grohe.ondus.api.OndusService;
26 import org.grohe.ondus.api.model.ApplianceStatus;
27 import org.grohe.ondus.api.model.BaseApplianceData;
28 import org.grohe.ondus.api.model.sense.Appliance;
29 import org.grohe.ondus.api.model.sense.ApplianceData;
30 import org.grohe.ondus.api.model.sense.ApplianceData.Measurement;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.QuantityType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.library.unit.SIUnits;
35 import org.openhab.core.library.unit.Units;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusDetail;
40 import org.openhab.core.types.Command;
41 import org.openhab.core.types.State;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * @author Florian Schmidt - Initial contribution
47  */
48 @NonNullByDefault
49 public class GroheOndusSenseHandler<T, M> extends GroheOndusBaseHandler<Appliance, Measurement> {
50
51     private static final int DEFAULT_POLLING_INTERVAL = 900;
52
53     private final Logger logger = LoggerFactory.getLogger(GroheOndusSenseHandler.class);
54
55     public GroheOndusSenseHandler(Thing thing) {
56         super(thing, Appliance.TYPE);
57     }
58
59     @Override
60     protected int getPollingInterval(Appliance appliance) {
61         if (config.pollingInterval > 0) {
62             return config.pollingInterval;
63         }
64         return DEFAULT_POLLING_INTERVAL;
65     }
66
67     @Override
68     protected void updateChannel(ChannelUID channelUID, Appliance appliance, Measurement measurement) {
69         String channelId = channelUID.getIdWithoutGroup();
70         State newState;
71         switch (channelId) {
72             case CHANNEL_NAME:
73                 newState = new StringType(appliance.getName());
74                 break;
75             case CHANNEL_TEMPERATURE:
76                 newState = new QuantityType<>(measurement.getTemperature(), SIUnits.CELSIUS);
77                 break;
78             case CHANNEL_HUMIDITY:
79                 newState = new QuantityType<>(measurement.getHumidity(), Units.PERCENT);
80                 break;
81             case CHANNEL_BATTERY:
82                 newState = new DecimalType(getBatteryStatus(appliance));
83                 break;
84             default:
85                 throw new IllegalArgumentException("Channel " + channelUID + " not supported.");
86         }
87         if (newState != null) {
88             updateState(channelUID, newState);
89         }
90     }
91
92     @Override
93     protected Measurement getLastDataPoint(Appliance appliance) {
94         if (getOndusService() == null) {
95             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
96                     "No initialized OndusService available from bridge.");
97             return new Measurement();
98         }
99
100         ApplianceData applianceData = getApplianceData(appliance);
101         if (applianceData == null) {
102             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Could not load data from API.");
103             return new Measurement();
104         }
105         List<Measurement> measurementList = applianceData.getData().getMeasurement();
106
107         return measurementList.isEmpty() ? new Measurement() : measurementList.get(measurementList.size() - 1);
108     }
109
110     private int getBatteryStatus(Appliance appliance) {
111         OndusService ondusService = getOndusService();
112         if (ondusService == null) {
113             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
114                     "No initialized OndusService available from bridge.");
115             return -1;
116         }
117
118         Optional<ApplianceStatus> applianceStatusOptional;
119         try {
120             applianceStatusOptional = ondusService.applianceStatus(appliance);
121             if (!applianceStatusOptional.isPresent()) {
122                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
123                         "Could not load data from API.");
124                 return -1;
125             }
126
127             return applianceStatusOptional.get().getBatteryStatus();
128         } catch (IOException e) {
129             logger.debug("Could not load appliance status", e);
130         }
131         return -1;
132     }
133
134     private @Nullable ApplianceData getApplianceData(Appliance appliance) {
135         Instant yesterday = Instant.now().minus(1, ChronoUnit.DAYS);
136         Instant today = Instant.now();
137         OndusService service = getOndusService();
138         if (service == null) {
139             return null;
140         }
141         try {
142             BaseApplianceData applianceData = service.applianceData(appliance, yesterday, today).orElse(null);
143             if (applianceData.getType() != Appliance.TYPE) {
144                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
145                         "Thing is not a GROHE SENSE device.");
146                 return null;
147             }
148             return (ApplianceData) applianceData;
149         } catch (IOException e) {
150             logger.debug("Could not load appliance data", e);
151         }
152         return null;
153     }
154
155     @Override
156     public void handleCommand(ChannelUID channelUID, Command command) {
157     }
158 }