2 * Copyright (c) 2010-2021 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.gardena.internal.handler;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.concurrent.TimeUnit;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.gardena.internal.GardenaSmart;
22 import org.openhab.binding.gardena.internal.GardenaSmartEventListener;
23 import org.openhab.binding.gardena.internal.GardenaSmartImpl;
24 import org.openhab.binding.gardena.internal.config.GardenaConfig;
25 import org.openhab.binding.gardena.internal.discovery.GardenaDeviceDiscoveryService;
26 import org.openhab.binding.gardena.internal.exception.GardenaException;
27 import org.openhab.binding.gardena.internal.model.dto.Device;
28 import org.openhab.binding.gardena.internal.util.UidUtils;
29 import org.openhab.core.io.net.http.HttpClientFactory;
30 import org.openhab.core.io.net.http.WebSocketFactory;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Channel;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingStatusDetail;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.BaseBridgeHandler;
39 import org.openhab.core.thing.binding.ThingHandlerService;
40 import org.openhab.core.types.Command;
41 import org.openhab.core.types.RefreshType;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link GardenaAccountHandler} is the handler for a Gardena smart system access and connects it to the framework.
48 * @author Gerhard Riegler - Initial contribution
51 public class GardenaAccountHandler extends BaseBridgeHandler implements GardenaSmartEventListener {
52 private final Logger logger = LoggerFactory.getLogger(GardenaAccountHandler.class);
53 private final long REINITIALIZE_DELAY_SECONDS = 10;
55 private @Nullable GardenaDeviceDiscoveryService discoveryService;
57 private @Nullable GardenaSmart gardenaSmart;
58 private HttpClientFactory httpClientFactory;
59 private WebSocketFactory webSocketFactory;
61 public GardenaAccountHandler(Bridge bridge, HttpClientFactory httpClientFactory,
62 WebSocketFactory webSocketFactory) {
64 this.httpClientFactory = httpClientFactory;
65 this.webSocketFactory = webSocketFactory;
69 public void initialize() {
70 logger.debug("Initializing Gardena account '{}'", getThing().getUID().getId());
74 public void setDiscoveryService(GardenaDeviceDiscoveryService discoveryService) {
75 this.discoveryService = discoveryService;
79 * Initializes the GardenaSmart account.
81 private void initializeGardena() {
82 final GardenaAccountHandler instance = this;
83 scheduler.execute(() -> {
85 GardenaConfig gardenaConfig = getThing().getConfiguration().as(GardenaConfig.class);
86 logger.debug("{}", gardenaConfig);
88 String id = getThing().getUID().getId();
89 gardenaSmart = new GardenaSmartImpl(id, gardenaConfig, instance, scheduler, httpClientFactory,
91 final GardenaDeviceDiscoveryService discoveryService = this.discoveryService;
92 if (discoveryService != null) {
93 discoveryService.startScan(null);
94 discoveryService.waitForScanFinishing();
96 updateStatus(ThingStatus.ONLINE);
97 } catch (GardenaException ex) {
98 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, ex.getMessage());
100 scheduleReinitialize();
101 logger.warn("{}", ex.getMessage());
107 * Schedules a reinitialization, if Gardena smart system account is not reachable.
109 private void scheduleReinitialize() {
110 scheduler.schedule(() -> {
111 if (getThing().getStatus() != ThingStatus.UNINITIALIZED) {
114 }, REINITIALIZE_DELAY_SECONDS, TimeUnit.SECONDS);
118 public void dispose() {
124 * Disposes the GardenaSmart account.
126 private void disposeGardena() {
127 logger.debug("Disposing Gardena account '{}'", getThing().getUID().getId());
128 final GardenaDeviceDiscoveryService discoveryService = this.discoveryService;
129 if (discoveryService != null) {
130 discoveryService.stopScan();
132 final GardenaSmart gardenaSmart = this.gardenaSmart;
133 if (gardenaSmart != null) {
134 gardenaSmart.dispose();
139 * Returns the Gardena smart system implementation.
141 public @Nullable GardenaSmart getGardenaSmart() {
146 public Collection<Class<? extends ThingHandlerService>> getServices() {
147 return Collections.singleton(GardenaDeviceDiscoveryService.class);
151 public void handleCommand(ChannelUID channelUID, Command command) {
152 if (RefreshType.REFRESH == command) {
153 logger.debug("Refreshing Gardena account '{}'", getThing().getUID().getId());
160 public void onDeviceUpdated(Device device) {
161 for (ThingUID thingUID : UidUtils.getThingUIDs(device, getThing())) {
162 final Thing gardenaThing;
163 final GardenaThingHandler gardenaThingHandler;
164 if ((gardenaThing = getThing().getThing(thingUID)) != null
165 && (gardenaThingHandler = (GardenaThingHandler) gardenaThing.getHandler()) != null) {
167 gardenaThingHandler.updateProperties(device);
168 for (Channel channel : gardenaThing.getChannels()) {
169 gardenaThingHandler.updateChannel(channel.getUID());
171 gardenaThingHandler.updateStatus(device);
172 } catch (GardenaException ex) {
173 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, ex.getMessage());
174 } catch (AccountHandlerNotAvailableException ignore) {
181 public void onNewDevice(Device device) {
182 final GardenaDeviceDiscoveryService discoveryService = this.discoveryService;
183 if (discoveryService != null) {
184 discoveryService.deviceDiscovered(device);
186 onDeviceUpdated(device);
190 public void onError() {
191 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Connection lost");
193 scheduleReinitialize();