]> git.basschouten.com Git - openhab-addons.git/blob
624257ae9d71769e2e1926c2687af42be2649ad7
[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.groheondus.internal.handler;
14
15 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.CHANNEL_BATTERY;
16 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.CHANNEL_HUMIDITY;
17 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.CHANNEL_NAME;
18 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.CHANNEL_TEMPERATURE;
19
20 import java.io.IOException;
21 import java.time.Instant;
22 import java.time.ZonedDateTime;
23 import java.time.temporal.ChronoUnit;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27 import java.util.Optional;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
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.RefreshType;
42 import org.openhab.core.types.State;
43 import org.openhab.core.types.UnDefType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import io.github.floriansw.ondus.api.OndusService;
48 import io.github.floriansw.ondus.api.model.ApplianceStatus;
49 import io.github.floriansw.ondus.api.model.BaseApplianceData;
50 import io.github.floriansw.ondus.api.model.sense.Appliance;
51 import io.github.floriansw.ondus.api.model.sense.ApplianceData;
52 import io.github.floriansw.ondus.api.model.sense.ApplianceData.Measurement;
53
54 /**
55  * @author Florian Schmidt - Initial contribution
56  */
57 @NonNullByDefault
58 public class GroheOndusSenseHandler<T, M> extends GroheOndusBaseHandler<Appliance, Measurement> {
59
60     private static final int DEFAULT_POLLING_INTERVAL = 900;
61
62     private final Logger logger = LoggerFactory.getLogger(GroheOndusSenseHandler.class);
63
64     public GroheOndusSenseHandler(Thing thing, int thingCounter) {
65         super(thing, Appliance.TYPE, thingCounter);
66     }
67
68     @Override
69     protected int getPollingInterval(Appliance appliance) {
70         if (config.pollingInterval > 0) {
71             return config.pollingInterval;
72         }
73         return DEFAULT_POLLING_INTERVAL;
74     }
75
76     @Override
77     protected void updateChannel(ChannelUID channelUID, Appliance appliance, Measurement measurement) {
78         String channelId = channelUID.getIdWithoutGroup();
79         State newState = UnDefType.UNDEF;
80         switch (channelId) {
81             case CHANNEL_NAME:
82                 newState = new StringType(appliance.getName());
83                 break;
84             case CHANNEL_TEMPERATURE:
85                 if (measurement.getTemperature() != null) {
86                     newState = new QuantityType<>(measurement.getTemperature(), SIUnits.CELSIUS);
87                 }
88                 break;
89             case CHANNEL_HUMIDITY:
90                 if (measurement.getHumidity() != null) {
91                     newState = new QuantityType<>(measurement.getHumidity(), Units.PERCENT);
92                 }
93                 break;
94             case CHANNEL_BATTERY:
95                 Integer batteryStatus = getBatteryStatus(appliance);
96                 if (batteryStatus != null) {
97                     newState = new DecimalType(batteryStatus);
98                 }
99                 break;
100             default:
101                 throw new IllegalArgumentException("Channel " + channelUID + " not supported.");
102         }
103         updateState(channelUID, newState);
104     }
105
106     @Override
107     protected Measurement getLastDataPoint(Appliance appliance) {
108         if (getOndusService() == null) {
109             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "@text/error.noservice");
110             return new Measurement();
111         }
112
113         ApplianceData applianceData = getApplianceData(appliance);
114         if (applianceData == null) {
115             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/error.empty.response");
116             return new Measurement();
117         }
118         List<Measurement> measurementList = applianceData.getData().getMeasurement();
119         Collections.sort(measurementList, Comparator.comparing(e -> ZonedDateTime.parse(e.timestamp)));
120         return measurementList.isEmpty() ? new Measurement() : measurementList.get(measurementList.size() - 1);
121     }
122
123     private @Nullable Integer getBatteryStatus(Appliance appliance) {
124         OndusService ondusService = getOndusService();
125         if (ondusService == null) {
126             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "@text/error.noservice");
127             return null;
128         }
129
130         Optional<ApplianceStatus> applianceStatusOptional;
131         try {
132             applianceStatusOptional = ondusService.applianceStatus(appliance);
133             if (!applianceStatusOptional.isPresent()) {
134                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/error.empty.response");
135                 return null;
136             }
137
138             return applianceStatusOptional.get().getBatteryStatus();
139         } catch (IOException e) {
140             logger.debug("Could not load appliance status", e);
141         }
142         return null;
143     }
144
145     private @Nullable ApplianceData getApplianceData(Appliance appliance) {
146         // Dates are stripped of time part inside library
147         Instant now = Instant.now();
148         Instant twoDaysAgo = now.minus(2, ChronoUnit.DAYS); // Devices only report once a day - at best
149         Instant tomorrow = now.plus(1, ChronoUnit.DAYS);
150         OndusService service = getOndusService();
151         if (service == null) {
152             return null;
153         }
154         try {
155             logger.debug("Fetching data for {} from {} to {}", thing.getUID(), twoDaysAgo, tomorrow);
156             BaseApplianceData applianceData = service.applianceData(appliance, twoDaysAgo, tomorrow).orElse(null);
157             if (applianceData != null) {
158                 if (applianceData.getType() != Appliance.TYPE) {
159                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/error.notsense");
160                     return null;
161                 }
162                 return (ApplianceData) applianceData;
163             } else {
164                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
165                         "@text/error.failedtoloaddata");
166             }
167         } catch (IOException e) {
168             logger.debug("Could not load appliance data for {}", thing.getUID(), e);
169         }
170         return null;
171     }
172
173     @Override
174     public void handleCommand(ChannelUID channelUID, Command command) {
175         if (command instanceof RefreshType) {
176             updateChannels();
177         }
178     }
179 }