]> git.basschouten.com Git - openhab-addons.git/blob
801b73657934143dae432cb836fe4d13a13ef1ba
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.digitalstrom.internal.lib.event;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22
23 import org.openhab.binding.digitalstrom.internal.lib.config.Config;
24 import org.openhab.binding.digitalstrom.internal.lib.event.types.Event;
25 import org.openhab.binding.digitalstrom.internal.lib.event.types.EventItem;
26 import org.openhab.binding.digitalstrom.internal.lib.event.types.JSONEventImpl;
27 import org.openhab.binding.digitalstrom.internal.lib.manager.ConnectionManager;
28 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
29 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.impl.JSONResponseHandler;
30 import org.openhab.core.common.ThreadPoolManager;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.gson.JsonArray;
35 import com.google.gson.JsonObject;
36
37 /**
38  * The {@link EventListener} listens for events which will be thrown by the digitalSTROM-Server and it notifies the
39  * added {@link EventHandler} about the detected events, if it supports the event-type.<br>
40  * You can add {@link EventHandler}'s through the constructors or the methods {@link #addEventHandler(EventHandler)} and
41  * {@link #addEventHandlers(List)}.<br>
42  * You can also delete an {@link EventHandler} though the method {@link #removeEventHandler(EventHandler)}.<br>
43  * If the {@link EventListener} is started, both methods subscribe respectively unsubscribe the event-types of the
44  * {@link EventHandler}/s automatically.<br>
45  * If you want to dynamically subscribe event-types, e.g. because a configuration has changed and a
46  * {@link EventHandler} needs to be informed of another event-type, you can use the methods
47  * {@link #addSubscribe(String)} or {@link #addSubscribeEvents(List)} to add more than one event-type. To remove a
48  * subscribed event you can use the method {@link #removeSubscribe(String, String)}, you also have to change the return
49  * of the {@link org.openhab.binding.digitalstrom.internal.lib.event.EventHandler} methods
50  * {@link EventHandler#getSupportedEvents()} and
51  * {@link EventHandler#supportsEvent(String)}.
52  * <br>
53  * To start and stop the listening you have to call the methods {@link #start()} and {@link #stop()}.
54  *
55  * @author Michael Ochel - Initial contribution
56  * @author Matthias Siegele - Initial contribution
57  */
58 public class EventListener {
59
60     private final Logger logger = LoggerFactory.getLogger(EventListener.class);
61     private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(Config.THREADPOOL_NAME);
62     public static final int SUBSCRIBE_DELAY = 500;
63     private ScheduledFuture<?> pollingScheduler;
64     private ScheduledFuture<?> subscriptionScheduler;
65
66     private int subscriptionID = 15;
67     private final int timeout = 500;
68     private final List<String> subscribedEvents = Collections.synchronizedList(new LinkedList<>());
69     private boolean subscribed = false;
70
71     // error message
72     public static final String INVALID_SESSION = "Invalid session!";
73     public static final String TOKEN_NOT_FOUND = "token not found."; // Complete text: "Event subscription token not
74                                                                      // found."
75
76     private final ConnectionManager connManager;
77     private final List<EventHandler> eventHandlers = Collections.synchronizedList(new LinkedList<>());
78     private final Config config;
79     private boolean isStarted = false;
80
81     /**
82      * Creates a new {@link EventListener} to listen to the supported event-types of the given eventHandler and notify
83      * about a detected event.<br>
84      * <br>
85      * To get notified by events you have to call {@link #start()}.
86      *
87      * @param connectionManager must not be null
88      * @param eventHandler must not be null
89      */
90     public EventListener(ConnectionManager connectionManager, EventHandler eventHandler) {
91         this.connManager = connectionManager;
92         this.config = connectionManager.getConfig();
93         addEventHandler(eventHandler);
94     }
95
96     /**
97      * This constructor can add more than one {@link EventHandler} as a list of {@link EventHandler}'s.
98      *
99      * @param connectionManager must not be null
100      * @param eventHandlers list of {@link EventHandler}'s must not be null
101      * @see #EventListener(ConnectionManager, EventHandler)
102      */
103     public EventListener(ConnectionManager connectionManager, List<EventHandler> eventHandlers) {
104         this.connManager = connectionManager;
105         this.config = connectionManager.getConfig();
106         addEventHandlers(eventHandlers);
107     }
108
109     /**
110      * Creates a new {@link EventListener} without an {@link EventHandler}<br>
111      * <br>
112      * To get notified by events you have to call {@link #start()} and {@link #addEventHandler(EventHandler)} or
113      * {@link #addEventHandlers(List)}.
114      *
115      * @param connectionManager must not be null
116      */
117     public EventListener(ConnectionManager connectionManager) {
118         this.connManager = connectionManager;
119         this.config = connectionManager.getConfig();
120     }
121
122     /**
123      * Stops this {@link EventListener} and unsubscribe events.
124      */
125     public synchronized void stop() {
126         logger.debug("Stop EventListener");
127         isStarted = false;
128         internalStop();
129     }
130
131     /**
132      * Returns true, if the {@link EventListener} is started.
133      *
134      * @return true, if is started
135      */
136     public boolean isStarted() {
137         return isStarted;
138     }
139
140     private void stopSubscriptionScheduler() {
141         final ScheduledFuture<?> subscriptionScheduler = this.subscriptionScheduler;
142         if (subscriptionScheduler != null) {
143             subscriptionScheduler.cancel(true);
144             this.subscriptionScheduler = null;
145         }
146     }
147
148     private void internalStop() {
149         stopSubscriptionScheduler();
150
151         ScheduledFuture<?> pollingScheduler = this.pollingScheduler;
152         if (pollingScheduler != null) {
153             pollingScheduler.cancel(true);
154             this.pollingScheduler = null;
155             unsubscribe();
156             logger.debug("internal stop EventListener");
157         }
158     }
159
160     /**
161      * Starts this {@link EventListener} and subscribe events.
162      */
163     public synchronized void start() {
164         logger.debug("Start EventListener");
165         isStarted = true;
166         internalStart();
167     }
168
169     private void internalStart() {
170         if (!eventHandlers.isEmpty() && (pollingScheduler == null || pollingScheduler.isCancelled())) {
171             pollingScheduler = scheduler.scheduleWithFixedDelay(runableListener, 0,
172                     config.getEventListenerRefreshinterval(), TimeUnit.MILLISECONDS);
173             logger.debug("internal start EventListener");
174         }
175     }
176
177     /**
178      * Adds a {@link List} of {@link EventHandler}'s and subscribe the supported event-types, if the
179      * {@link EventListener} is started and the event-types are not already subscribed.
180      *
181      * @param eventHandlers to add
182      */
183     public void addEventHandlers(List<EventHandler> eventHandlers) {
184         if (eventHandlers != null) {
185             for (EventHandler eventHandler : eventHandlers) {
186                 addEventHandler(eventHandler);
187             }
188         }
189     }
190
191     /**
192      * Adds an {@link EventHandler}'s and subscribe the supported event-types, if the
193      * {@link EventListener} is started and the event-types are not already subscribed.<br>
194      * <br>
195      * <b>Note:</b><br>
196      * If {@link #start()} was called before the {@link EventListener} will start now, otherwise you have to call
197      * {@link #start()} to get notified by events.
198      *
199      * @param eventHandler to add
200      */
201     public void addEventHandler(EventHandler eventHandler) {
202         if (eventHandler != null) {
203             boolean handlerExist = false;
204             for (EventHandler handler : eventHandlers) {
205                 if (handler.getUID().equals(eventHandler.getUID())) {
206                     handlerExist = true;
207                 }
208             }
209             if (!handlerExist) {
210                 eventHandlers.add(eventHandler);
211                 addSubscribeEvents(eventHandler.getSupportedEvents());
212                 logger.debug("eventHandler: {} added", eventHandler.getUID());
213                 if (isStarted) {
214                     internalStart();
215                 }
216             }
217         }
218     }
219
220     /**
221      * Remove an {@link EventHandler} and unsubscribes the supported event-types, if the
222      * {@link EventListener} is started and no other {@link EventHandler} needed the event-types.
223      *
224      * @param eventHandler to remove
225      */
226     public void removeEventHandler(EventHandler eventHandler) {
227         if (eventHandler != null && eventHandlers.contains(eventHandler)) {
228             List<String> tempSubsList = new ArrayList<>();
229             int index = -1;
230             EventHandler intEventHandler = null;
231             boolean subscribedEventsChanged = false;
232             for (int i = 0; i < eventHandlers.size(); i++) {
233                 intEventHandler = eventHandlers.get(i);
234                 if (intEventHandler.getUID().equals(eventHandler.getUID())) {
235                     index = i;
236                 } else {
237                     tempSubsList.addAll(intEventHandler.getSupportedEvents());
238                 }
239             }
240             if (index != -1) {
241                 intEventHandler = eventHandlers.remove(index);
242                 for (String eventName : intEventHandler.getSupportedEvents()) {
243                     if (!tempSubsList.contains(eventName)) {
244                         subscribedEvents.remove(eventName);
245                         subscribedEventsChanged = true;
246                     }
247                 }
248             }
249             if (subscribedEventsChanged) {
250                 // Because of the json-call unsubscribe?eventName=XY&subscriptionID=Z doesn't work like it is explained
251                 // in the dS-JSON-API, the whole EventListener will be restarted. The problem is, that not only the
252                 // given eventName, rather all events of the subscitionID will be deleted.
253                 restartListener();
254             }
255         }
256     }
257
258     /**
259      * Removes a subscribed event and unsubscibe it, if it is not needed by other {@link EventHandler}'s.
260      *
261      * @param unsubscribeEvent event name to unsubscibe
262      * @param eventHandlerID EventHandler-ID of the EventHandler that unsubscibe an event
263      */
264     public void removeSubscribe(String unsubscribeEvent, String eventHandlerID) {
265         if (subscribedEvents != null && !subscribedEvents.contains(unsubscribeEvent)) {
266             boolean eventNeededByAnotherHandler = false;
267             for (EventHandler handler : eventHandlers) {
268                 if (!handler.getUID().equals(eventHandlerID)) {
269                     eventNeededByAnotherHandler = handler.getSupportedEvents().contains(unsubscribeEvent);
270                 }
271             }
272             if (!eventNeededByAnotherHandler) {
273                 logger.debug("unsubscribeEvent: {} is not needed by other EventHandler's... unsubscribe it",
274                         unsubscribeEvent);
275                 subscribedEvents.remove(unsubscribeEvent);
276                 restartListener();
277             } else {
278                 logger.debug("unsubscribeEvent: {} is needed by other EventHandler's... dosen't unsubscribe it",
279                         unsubscribeEvent);
280             }
281         }
282     }
283
284     private void restartListener() {
285         internalStop();
286         if (!eventHandlers.isEmpty() && isStarted) {
287             logger.debug("Min one subscribed events was deleted, EventListener will be restarted");
288             internalStart();
289         }
290     }
291
292     /**
293      * Adds an event and subscribes it, if it is not subscribed already.
294      *
295      * @param subscribeEvent event name to subscribe
296      */
297     public void addSubscribe(String subscribeEvent) {
298         if (!subscribedEvents.contains(subscribeEvent)) {
299             subscribedEvents.add(subscribeEvent);
300             logger.debug("subscibeEvent: {} added", subscribeEvent);
301             if (subscribed) {
302                 subscribe(subscribeEvent);
303             }
304         }
305     }
306
307     /**
308      * Adds the events of the {@link List} and subscribe them, if an event is not subscribed already.
309      *
310      * @param subscribeEvents event name to subscribe
311      */
312     public void addSubscribeEvents(List<String> subscribeEvents) {
313         for (String eventName : subscribeEvents) {
314             subscribe(eventName);
315         }
316     }
317
318     private void getSubscriptionID() {
319         boolean subscriptionIDavailable = false;
320         while (!subscriptionIDavailable) {
321             String response = connManager.getDigitalSTROMAPI().getEvent(connManager.getSessionToken(), subscriptionID,
322                     timeout);
323
324             JsonObject responseObj = JSONResponseHandler.toJsonObject(response);
325
326             if (JSONResponseHandler.checkResponse(responseObj)) {
327                 subscriptionID++;
328             } else {
329                 String errorStr = null;
330                 if (responseObj != null && responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()) != null) {
331                     errorStr = responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()).getAsString();
332                 }
333                 if (errorStr != null && errorStr.contains(TOKEN_NOT_FOUND)) {
334                     subscriptionIDavailable = true;
335                 }
336             }
337         }
338     }
339
340     private boolean subscribe(String eventName) {
341         if (!subscribed) {
342             getSubscriptionID();
343         }
344         subscribed = connManager.getDigitalSTROMAPI().subscribeEvent(connManager.getSessionToken(), eventName,
345                 subscriptionID, config.getConnectionTimeout(), config.getReadTimeout());
346
347         if (subscribed) {
348             logger.debug("subscribed event: {} to subscriptionID: {}", eventName, subscriptionID);
349         } else {
350             logger.error(
351                     "Couldn't subscribe event {} ... maybe timeout because system is too busy ... event will be tried to subscribe later again ... ",
352                     eventName);
353         }
354         return subscribed;
355     }
356
357     private void subscribe(final List<String> eventNames) {
358         subscriptionScheduler = scheduler.scheduleWithFixedDelay(() -> {
359             eventNames.forEach(this::subscribe);
360             stopSubscriptionScheduler();
361         }, 0, SUBSCRIBE_DELAY, TimeUnit.MILLISECONDS);
362     }
363
364     private final Runnable runableListener = new Runnable() {
365
366         @Override
367         public void run() {
368             if (subscribed) {
369                 String response = connManager.getDigitalSTROMAPI().getEvent(connManager.getSessionToken(),
370                         subscriptionID, timeout);
371                 JsonObject responseObj = JSONResponseHandler.toJsonObject(response);
372
373                 if (JSONResponseHandler.checkResponse(responseObj)) {
374                     JsonObject obj = JSONResponseHandler.getResultJsonObject(responseObj);
375                     if (obj != null && obj.get(JSONApiResponseKeysEnum.EVENTS.getKey()).isJsonArray()) {
376                         JsonArray array = obj.get(JSONApiResponseKeysEnum.EVENTS.getKey()).getAsJsonArray();
377                         handleEvent(array);
378                     }
379                 } else {
380                     String errorStr = null;
381                     if (responseObj != null && responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()) != null) {
382                         errorStr = responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()).getAsString();
383                     }
384                     if (errorStr != null && (errorStr.equals(INVALID_SESSION) || errorStr.contains(TOKEN_NOT_FOUND))) {
385                         unsubscribe();
386                         subscribe(subscribedEvents);
387                     } else if (errorStr != null) {
388                         pollingScheduler.cancel(true);
389                         logger.error("Unknown error message at event response: {}", errorStr);
390                     }
391                 }
392             } else {
393                 subscribe(subscribedEvents);
394             }
395         }
396     };
397
398     private void unsubscribe() {
399         for (String eventName : this.subscribedEvents) {
400             connManager.getDigitalSTROMAPI().unsubscribeEvent(this.connManager.getSessionToken(), eventName,
401                     this.subscriptionID, config.getConnectionTimeout(), config.getReadTimeout());
402         }
403     }
404
405     private void handleEvent(JsonArray array) {
406         if (array.size() > 0) {
407             Event event = new JSONEventImpl(array);
408             for (EventItem item : event.getEventItems()) {
409                 logger.debug("detect event {}", item.toString());
410                 for (EventHandler handler : eventHandlers) {
411                     if (handler.supportsEvent(item.getName())) {
412                         logger.debug("inform handler with id {} about event {}", handler.getUID(), item.toString());
413                         handler.handleEvent(item);
414                     }
415                 }
416             }
417         }
418     }
419 }