2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.groheondus.internal.handler;
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;
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;
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;
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;
55 * @author Florian Schmidt - Initial contribution
58 public class GroheOndusSenseHandler<T, M> extends GroheOndusBaseHandler<Appliance, Measurement> {
60 private static final int DEFAULT_POLLING_INTERVAL = 900;
62 private final Logger logger = LoggerFactory.getLogger(GroheOndusSenseHandler.class);
64 public GroheOndusSenseHandler(Thing thing, int thingCounter) {
65 super(thing, Appliance.TYPE, thingCounter);
69 protected int getPollingInterval(Appliance appliance) {
70 if (config.pollingInterval > 0) {
71 return config.pollingInterval;
73 return DEFAULT_POLLING_INTERVAL;
77 protected void updateChannel(ChannelUID channelUID, Appliance appliance, Measurement measurement) {
78 String channelId = channelUID.getIdWithoutGroup();
79 State newState = UnDefType.UNDEF;
82 newState = new StringType(appliance.getName());
84 case CHANNEL_TEMPERATURE:
85 if (measurement.getTemperature() != null) {
86 newState = new QuantityType<>(measurement.getTemperature(), SIUnits.CELSIUS);
89 case CHANNEL_HUMIDITY:
90 if (measurement.getHumidity() != null) {
91 newState = new QuantityType<>(measurement.getHumidity(), Units.PERCENT);
95 Integer batteryStatus = getBatteryStatus(appliance);
96 if (batteryStatus != null) {
97 newState = new DecimalType(batteryStatus);
101 throw new IllegalArgumentException("Channel " + channelUID + " not supported.");
103 updateState(channelUID, newState);
107 protected Measurement getLastDataPoint(Appliance appliance) {
108 if (getOndusService() == null) {
109 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "@text/error.noservice");
110 return new Measurement();
113 ApplianceData applianceData = getApplianceData(appliance);
114 if (applianceData == null) {
115 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/error.empty.response");
116 return new Measurement();
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);
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");
130 Optional<ApplianceStatus> applianceStatusOptional;
132 applianceStatusOptional = ondusService.applianceStatus(appliance);
133 if (!applianceStatusOptional.isPresent()) {
134 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/error.empty.response");
138 return applianceStatusOptional.get().getBatteryStatus();
139 } catch (IOException e) {
140 logger.debug("Could not load appliance status", e);
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) {
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");
162 return (ApplianceData) applianceData;
164 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
165 "@text/error.failedtoloaddata");
167 } catch (IOException e) {
168 logger.debug("Could not load appliance data for {}", thing.getUID(), e);
174 public void handleCommand(ChannelUID channelUID, Command command) {
175 if (command instanceof RefreshType) {