]> git.basschouten.com Git - openhab-addons.git/blob
ab0faf124d5a9c2b5051d9bc418780c9437b8511
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.homeconnect.internal.client;
14
15 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.TimeUnit;
23 import java.util.stream.Collectors;
24
25 import javax.ws.rs.client.Client;
26 import javax.ws.rs.client.ClientBuilder;
27 import javax.ws.rs.sse.SseEventSource;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException;
32 import org.openhab.binding.homeconnect.internal.client.exception.CommunicationException;
33 import org.openhab.binding.homeconnect.internal.client.listener.HomeConnectEventListener;
34 import org.openhab.binding.homeconnect.internal.client.model.Event;
35 import org.openhab.core.auth.client.oauth2.OAuthClientService;
36 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Server-Sent-Events client for Home Connect API.
42  *
43  * @author Jonas BrĂ¼stel - Initial contribution
44  * @author Laurent Garnier - Replace okhttp SSE by JAX-RS SSE
45  *
46  */
47 @NonNullByDefault
48 public class HomeConnectEventSourceClient {
49
50     private static final int SSE_REQUEST_READ_TIMEOUT = 90;
51     private static final int EVENT_QUEUE_SIZE = 300;
52
53     private final String apiUrl;
54     private final ClientBuilder clientBuilder;
55     private final SseEventSourceFactory eventSourceFactory;
56     private final OAuthClientService oAuthClientService;
57     private final Map<HomeConnectEventListener, SseEventSource> eventSourceConnections;
58     private final Map<SseEventSource, HomeConnectEventSourceListener> eventSourceListeners;
59     private final ScheduledExecutorService scheduler;
60     private final CircularQueue<Event> eventQueue;
61
62     private final Logger logger = LoggerFactory.getLogger(HomeConnectEventSourceClient.class);
63
64     public HomeConnectEventSourceClient(ClientBuilder clientBuilder, SseEventSourceFactory eventSourceFactory,
65             OAuthClientService oAuthClientService, boolean simulated, ScheduledExecutorService scheduler,
66             @Nullable List<Event> eventHistory) {
67         this.scheduler = scheduler;
68         this.clientBuilder = clientBuilder;
69         this.eventSourceFactory = eventSourceFactory;
70         this.oAuthClientService = oAuthClientService;
71
72         apiUrl = simulated ? API_SIMULATOR_BASE_URL : API_BASE_URL;
73         eventSourceConnections = new HashMap<>();
74         eventSourceListeners = new HashMap<>();
75         eventQueue = new CircularQueue<>(EVENT_QUEUE_SIZE);
76         if (eventHistory != null) {
77             eventQueue.addAll(eventHistory);
78         }
79     }
80
81     /**
82      * Register {@link HomeConnectEventListener} to receive events by Home Connect API. This helps to reduce the
83      * amount of request you would usually need to update all channels.
84      *
85      * Checkout rate limits of the API at. https://developer.home-connect.com/docs/general/ratelimiting
86      *
87      * @param haId appliance id
88      * @param eventListener appliance event listener
89      * @throws CommunicationException API communication exception
90      * @throws AuthorizationException oAuth authorization exception
91      */
92     public synchronized void registerEventListener(final String haId, final HomeConnectEventListener eventListener)
93             throws CommunicationException, AuthorizationException {
94         logger.debug("Register event listener for '{}': {}", haId, eventListener);
95
96         if (!eventSourceConnections.containsKey(eventListener)) {
97             logger.debug("Create new event source listener for '{}'.", haId);
98             Client client = clientBuilder.readTimeout(SSE_REQUEST_READ_TIMEOUT, TimeUnit.SECONDS).register(
99                     new HomeConnectStreamingRequestFilter(HttpHelper.getAuthorizationHeader(oAuthClientService)))
100                     .build();
101             SseEventSource eventSource = eventSourceFactory
102                     .newSource(client.target(apiUrl + "/api/homeappliances/" + haId + "/events"));
103             HomeConnectEventSourceListener eventSourceListener = new HomeConnectEventSourceListener(haId, eventListener,
104                     this, scheduler, eventQueue);
105             eventSource.register(eventSourceListener::onEvent, eventSourceListener::onError,
106                     eventSourceListener::onComplete);
107             eventSourceListeners.put(eventSource, eventSourceListener);
108             eventSourceConnections.put(eventListener, eventSource);
109             eventSource.open();
110         }
111     }
112
113     /**
114      * Unregister {@link HomeConnectEventListener}.
115      *
116      * @param eventListener appliance event listener
117      */
118     public synchronized void unregisterEventListener(HomeConnectEventListener eventListener) {
119         unregisterEventListener(eventListener, false, false);
120     }
121
122     /**
123      * Unregister {@link HomeConnectEventListener}.
124      *
125      * @param eventListener appliance event listener
126      * @param completed true when the event source is known as already completed by the server
127      */
128     public synchronized void unregisterEventListener(HomeConnectEventListener eventListener, boolean completed) {
129         unregisterEventListener(eventListener, false, completed);
130     }
131
132     /**
133      * Unregister {@link HomeConnectEventListener}.
134      *
135      * @param eventListener appliance event listener
136      * @param immediate true when the unregistering of the event source has to be fast
137      * @param completed true when the event source is known as already completed by the server
138      */
139     public synchronized void unregisterEventListener(HomeConnectEventListener eventListener, boolean immediate,
140             boolean completed) {
141         if (eventSourceConnections.containsKey(eventListener)) {
142             SseEventSource eventSource = eventSourceConnections.get(eventListener);
143             if (eventSource != null) {
144                 closeEventSource(eventSource, immediate, completed);
145                 eventSourceListeners.remove(eventSource);
146             }
147             eventSourceConnections.remove(eventListener);
148         }
149     }
150
151     private void closeEventSource(SseEventSource eventSource, boolean immediate, boolean completed) {
152         if (eventSource.isOpen() && !completed) {
153             logger.debug("Close event source (immediate = {})", immediate);
154             eventSource.close(immediate ? 0 : 10, TimeUnit.SECONDS);
155         }
156         HomeConnectEventSourceListener eventSourceListener = eventSourceListeners.get(eventSource);
157         if (eventSourceListener != null) {
158             eventSourceListener.stopMonitor();
159         }
160     }
161
162     /**
163      * Connection count.
164      *
165      * @return connection count
166      */
167     public synchronized int connectionSize() {
168         return eventSourceConnections.size();
169     }
170
171     /**
172      * Dispose event source client
173      *
174      * @param immediate true to request a fast execution
175      */
176     public synchronized void dispose(boolean immediate) {
177         eventSourceConnections.forEach((key, eventSource) -> closeEventSource(eventSource, immediate, false));
178         eventSourceListeners.clear();
179         eventSourceConnections.clear();
180     }
181
182     /**
183      * Get latest events
184      *
185      * @return event queue
186      */
187     public Collection<Event> getLatestEvents() {
188         return eventQueue.getAll();
189     }
190
191     /**
192      * Get latest events by haId
193      *
194      * @param haId appliance id
195      * @return event queue
196      */
197     public List<Event> getLatestEvents(String haId) {
198         return eventQueue.getAll().stream().filter(event -> haId.equals(event.getHaId())).collect(Collectors.toList());
199     }
200 }