]> git.basschouten.com Git - openhab-addons.git/blob
0bcdb2b6eca046f10aa75a1d667ba39818ebd4df
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.ojelectronics.internal;
14
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
17
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;
35
36 /**
37  * Handles all traffic with OJ Electronics cloud
38  *
39  * @author Christian Kittel - Initial Contribution
40  */
41 @NonNullByDefault
42 public class OJCloudHandler extends BaseBridgeHandler implements BridgeHandler {
43
44     private final Logger logger = LoggerFactory.getLogger(OJCloudHandler.class);
45     private final HttpClient httpClient;
46
47     private @Nullable RefreshService refreshService;
48     private @Nullable SignInService signInService;
49     private OJElectronicsBridgeConfiguration configuration;
50     private @Nullable ScheduledFuture<?> signTask;
51
52     public OJCloudHandler(Bridge bridge, HttpClient httpClient) {
53         super(bridge);
54         this.httpClient = httpClient;
55         this.configuration = new OJElectronicsBridgeConfiguration();
56     }
57
58     /**
59      * Initializes the binding.
60      */
61     @Override
62     public void initialize() {
63         configuration = getConfigAs(OJElectronicsBridgeConfiguration.class);
64         ensureSignIn();
65     }
66
67     /**
68      * Disposes the binding.
69      */
70     @Override
71     public void dispose() {
72         final RefreshService refreshService = this.refreshService;
73         if (refreshService != null) {
74             refreshService.stop();
75         }
76         final ScheduledFuture<?> signTask = this.signTask;
77         if (signTask != null) {
78             signTask.cancel(true);
79         }
80         this.refreshService = null;
81         signInService = null;
82         super.dispose();
83     }
84
85     @Override
86     public void handleCommand(ChannelUID channelUID, Command command) {
87     }
88
89     private void ensureSignIn() {
90         if (signInService == null) {
91             signInService = new SignInService(configuration, httpClient);
92         }
93         final SignInService signInService = this.signInService;
94         if (signInService != null) {
95             signInService.signIn(this::handleSignInDone, this::handleConnectionLost,
96                     this::handleUnauthorizedWhileSignIn);
97         }
98     }
99
100     private void handleRefreshDone(@Nullable GroupContentResponseModel groupContentResponse,
101             @Nullable String errorMessage) {
102         logger.trace("OJElectronicsCloudHandler.handleRefreshDone({})", groupContentResponse);
103
104         if (groupContentResponse != null && groupContentResponse.errorCode == 0) {
105             new RefreshGroupContentService(groupContentResponse.groupContents, getThing().getThings()).handle();
106         } else {
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();
112             }
113         }
114     }
115
116     private void handleSignInDone(String sessionId) {
117         logger.trace("OJElectronicsCloudHandler.handleSignInDone({})", sessionId);
118         if (refreshService == null) {
119             refreshService = new RefreshService(configuration, httpClient, scheduler);
120         }
121         final RefreshService refreshService = this.refreshService;
122         if (refreshService != null) {
123             refreshService.start(sessionId, this::handleRefreshDone, this::handleConnectionLost,
124                     this::handleUnauthorized);
125
126             updateStatus(ThingStatus.ONLINE);
127         }
128     }
129
130     private void handleUnauthorized() {
131         final RefreshService refreshService = this.refreshService;
132         if (refreshService != null) {
133             refreshService.stop();
134         }
135         restartRefreshServiceAsync(1);
136     }
137
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();
144         }
145     }
146
147     private void handleConnectionLost() {
148         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
149         final RefreshService refreshService = this.refreshService;
150         if (refreshService != null) {
151             refreshService.stop();
152         }
153         restartRefreshServiceAsync(configuration.refreshDelayInSeconds);
154     }
155
156     private void restartRefreshServiceAsync(long delayInSeconds) {
157         signTask = scheduler.schedule(this::ensureSignIn, delayInSeconds, TimeUnit.SECONDS);
158     }
159 }