2 * Copyright (c) 2010-2020 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.ojelectronics.internal;
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.openhab.binding.ojelectronics.internal.config.OJElectronicsBridgeConfiguration;
22 import org.openhab.binding.ojelectronics.internal.models.groups.GroupContentResponseModel;
23 import org.openhab.binding.ojelectronics.internal.services.RefreshGroupContentService;
24 import org.openhab.binding.ojelectronics.internal.services.RefreshService;
25 import org.openhab.binding.ojelectronics.internal.services.SignInService;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseBridgeHandler;
31 import org.openhab.core.thing.binding.BridgeHandler;
32 import org.openhab.core.types.Command;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Handles all traffic with OJ Electronics cloud
39 * @author Christian Kittel - Initial Contribution
42 public class OJCloudHandler extends BaseBridgeHandler implements BridgeHandler {
44 private final Logger logger = LoggerFactory.getLogger(OJCloudHandler.class);
45 private final HttpClient httpClient;
47 private @Nullable RefreshService refreshService;
48 private @Nullable SignInService signInService;
49 private OJElectronicsBridgeConfiguration configuration;
50 private @Nullable ScheduledFuture<?> signTask;
52 public OJCloudHandler(Bridge bridge, HttpClient httpClient) {
54 this.httpClient = httpClient;
55 this.configuration = new OJElectronicsBridgeConfiguration();
59 * Initializes the binding.
62 public void initialize() {
63 configuration = getConfigAs(OJElectronicsBridgeConfiguration.class);
68 * Disposes the binding.
71 public void dispose() {
72 final RefreshService refreshService = this.refreshService;
73 if (refreshService != null) {
74 refreshService.stop();
76 final ScheduledFuture<?> signTask = this.signTask;
77 if (signTask != null) {
78 signTask.cancel(true);
80 this.refreshService = null;
86 public void handleCommand(ChannelUID channelUID, Command command) {
89 private void ensureSignIn() {
90 if (signInService == null) {
91 signInService = new SignInService(configuration, httpClient);
93 final SignInService signInService = this.signInService;
94 if (signInService != null) {
95 signInService.signIn(this::handleSignInDone, this::handleConnectionLost,
96 this::handleUnauthorizedWhileSignIn);
100 private void handleRefreshDone(@Nullable GroupContentResponseModel groupContentResponse,
101 @Nullable String errorMessage) {
102 logger.trace("OJElectronicsCloudHandler.handleRefreshDone({})", groupContentResponse);
104 if (groupContentResponse != null && groupContentResponse.errorCode == 0) {
105 new RefreshGroupContentService(groupContentResponse.groupContents, getThing().getThings()).handle();
107 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
108 (errorMessage == null) ? "Wrong or no result model; Refreshing stoppped" : errorMessage);
109 final RefreshService refreshService = this.refreshService;
110 if (refreshService != null) {
111 refreshService.stop();
116 private void handleSignInDone(String sessionId) {
117 logger.trace("OJElectronicsCloudHandler.handleSignInDone({})", sessionId);
118 if (refreshService == null) {
119 refreshService = new RefreshService(configuration, httpClient, scheduler);
121 final RefreshService refreshService = this.refreshService;
122 if (refreshService != null) {
123 refreshService.start(sessionId, this::handleRefreshDone, this::handleConnectionLost,
124 this::handleUnauthorized);
126 updateStatus(ThingStatus.ONLINE);
130 private void handleUnauthorized() {
131 final RefreshService refreshService = this.refreshService;
132 if (refreshService != null) {
133 refreshService.stop();
135 restartRefreshServiceAsync(1);
138 private void handleUnauthorizedWhileSignIn() {
139 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
140 "Could not sign in. Check user name and password.");
141 final RefreshService refreshService = this.refreshService;
142 if (refreshService != null) {
143 refreshService.stop();
147 private void handleConnectionLost() {
148 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
149 final RefreshService refreshService = this.refreshService;
150 if (refreshService != null) {
151 refreshService.stop();
153 restartRefreshServiceAsync(configuration.refreshDelayInSeconds);
156 private void restartRefreshServiceAsync(long delayInSeconds) {
157 signTask = scheduler.schedule(this::ensureSignIn, delayInSeconds, TimeUnit.SECONDS);