2 * Copyright (c) 2010-2022 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 java.io.IOException;
16 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.grohe.ondus.api.OndusService;
21 import org.grohe.ondus.api.model.BaseAppliance;
22 import org.grohe.ondus.api.model.Location;
23 import org.grohe.ondus.api.model.Room;
24 import org.openhab.binding.groheondus.internal.GroheOndusApplianceConfiguration;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseThingHandler;
31 import org.openhab.core.thing.binding.BridgeHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * @author Florian Schmidt - Initial contribution
39 public abstract class GroheOndusBaseHandler<T extends BaseAppliance, M> extends BaseThingHandler {
40 private final Logger logger = LoggerFactory.getLogger(GroheOndusBaseHandler.class);
42 protected @Nullable GroheOndusApplianceConfiguration config;
44 private final int applianceType;
46 public GroheOndusBaseHandler(Thing thing, int applianceType) {
48 this.applianceType = applianceType;
52 public void initialize() {
53 config = getConfigAs(GroheOndusApplianceConfiguration.class);
55 OndusService ondusService = getOndusService();
56 if (ondusService == null) {
57 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
58 "No initialized OndusService available from bridge.");
63 T appliance = getAppliance(ondusService);
64 if (appliance == null) {
65 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.COMMUNICATION_ERROR, "Could not load appliance");
68 int pollingInterval = getPollingInterval(appliance);
69 scheduler.scheduleWithFixedDelay(this::updateChannels, 0, pollingInterval, TimeUnit.SECONDS);
71 updateStatus(ThingStatus.UNKNOWN);
75 public void channelLinked(ChannelUID channelUID) {
76 super.channelLinked(channelUID);
78 OndusService ondusService = getOndusService();
79 if (ondusService == null) {
80 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
81 "No initialized OndusService available from bridge.");
86 T appliance = getAppliance(ondusService);
87 if (appliance == null) {
90 updateChannel(channelUID, appliance, getLastDataPoint(appliance));
93 public void updateChannels() {
94 OndusService ondusService = getOndusService();
95 if (ondusService == null) {
96 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
97 "No initialized OndusService available from bridge.");
102 T appliance = getAppliance(ondusService);
103 if (appliance == null) {
107 M measurement = getLastDataPoint(appliance);
108 getThing().getChannels().forEach(channel -> updateChannel(channel.getUID(), appliance, measurement));
110 updateStatus(ThingStatus.ONLINE);
113 protected abstract M getLastDataPoint(T appliance);
115 protected abstract void updateChannel(ChannelUID channelUID, T appliance, M measurement);
117 public @Nullable OndusService getOndusService() {
118 Bridge bridge = getBridge();
119 if (bridge == null) {
122 BridgeHandler handler = bridge.getHandler();
123 if (!(handler instanceof GroheOndusAccountHandler)) {
127 return ((GroheOndusAccountHandler) handler).getService();
128 } catch (IllegalStateException e) {
129 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
134 protected Room getRoom() {
135 return new Room(config.roomId, getLocation());
138 protected Location getLocation() {
139 return new Location(config.locationId);
142 protected @Nullable T getAppliance(OndusService ondusService) {
144 BaseAppliance appliance = ondusService.getAppliance(getRoom(), config.applianceId).orElse(null);
145 if (appliance.getType() != getType()) {
146 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
147 "Thing is not a GROHE SENSE Guard device.");
150 return (T) appliance;
151 } catch (IOException e) {
152 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
153 logger.debug("Could not load appliance", e);
158 protected abstract int getPollingInterval(T appliance);
160 private int getType() {
161 return this.applianceType;