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.digitalstrom.internal.lib.event;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Iterator;
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
24 import org.openhab.binding.digitalstrom.internal.lib.config.Config;
25 import org.openhab.binding.digitalstrom.internal.lib.event.types.Event;
26 import org.openhab.binding.digitalstrom.internal.lib.event.types.EventItem;
27 import org.openhab.binding.digitalstrom.internal.lib.event.types.JSONEventImpl;
28 import org.openhab.binding.digitalstrom.internal.lib.manager.ConnectionManager;
29 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
30 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.impl.JSONResponseHandler;
31 import org.openhab.core.common.ThreadPoolManager;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.JsonArray;
36 import com.google.gson.JsonObject;
39 * The {@link EventListener} listens for events which will be thrown by the digitalSTROM-Server and it notifies the
40 * added {@link EventHandler} about the detected events, if it supports the event-type.<br>
41 * You can add {@link EventHandler}'s through the constructors or the methods {@link #addEventHandler(EventHandler)} and
42 * {@link #addEventHandlers(List)}.<br>
43 * You can also delete a {@link EventHandler} though the method {@link #removeEventHandler(EventHandler)}.<br>
44 * If the {@link EventListener} is started, both methods subscribe respectively unsubscribe the event-types of the
45 * {@link EventHandler}/s automatically.<br>
46 * If you want to dynamically subscribe event-types, e.g. because a configuration has changed and a
47 * {@link EventHandler} needs to be informed of another event-type, you can use the methods
48 * {@link #addSubscribe(String)} or {@link #addSubscribeEvents(List)} to add more than one event-type. To remove a
49 * subscribed event you can use the method {@link #removeSubscribe(String, String)}, you also have to change the return
50 * of the {@link EventHandler} methods {@link EventHandler#getSupportetEvents()} and
51 * {@link EventHandler#supportsEvent(String)}.
53 * To start and stop the listening you have to call the methods {@link #start()} and {@link #stop()}.
55 * @author Michael Ochel - Initial contribution
56 * @author Matthias Siegele - Initial contribution
58 public class EventListener {
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;
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;
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
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;
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>
85 * To get notified by events you have to call {@link #start()}.
87 * @param connectionManager must not be null
88 * @param eventHandler must not be null
90 public EventListener(ConnectionManager connectionManager, EventHandler eventHandler) {
91 this.connManager = connectionManager;
92 this.config = connectionManager.getConfig();
93 addEventHandler(eventHandler);
97 * This constructor can add more than one {@link EventHandler} as a list of {@link EventHandler}'s.
99 * @param connectionManager must not be null
100 * @param eventHandlers list of {@link EventHandler}'s must not be null
101 * @see #EventListener(ConnectionManager, EventHandler)
103 public EventListener(ConnectionManager connectionManager, List<EventHandler> eventHandlers) {
104 this.connManager = connectionManager;
105 this.config = connectionManager.getConfig();
106 addEventHandlers(eventHandlers);
110 * Creates a new {@link EventListener} without a {@link EventHandler}<br>
112 * To get notified by events you have to call {@link #start()} and {@link #addEventHandler(EventHandler)} or
113 * {@link #addEventHandlers(List)}.
115 * @param connectionManager must not be null
117 public EventListener(ConnectionManager connectionManager) {
118 this.connManager = connectionManager;
119 this.config = connectionManager.getConfig();
123 * Stops this {@link EventListener} and unsubscribe events.
125 public synchronized void stop() {
126 logger.debug("Stop EventListener");
132 * Returns true, if the {@link EventListener} is started.
134 * @return true, if is started
136 public boolean isStarted() {
140 private void internalStop() {
141 if (subscriptionScheduler != null && !subscriptionScheduler.isCancelled()) {
142 subscriptionScheduler.cancel(true);
143 subscriptionScheduler = null;
145 if (pollingScheduler != null && !pollingScheduler.isCancelled()) {
146 pollingScheduler.cancel(true);
147 pollingScheduler = null;
149 logger.debug("internal stop EventListener");
154 * Starts this {@link EventListener} and subscribe events.
156 public synchronized void start() {
157 logger.debug("Start EventListener");
162 private void internalStart() {
163 if (!eventHandlers.isEmpty() && (pollingScheduler == null || pollingScheduler.isCancelled())) {
164 pollingScheduler = scheduler.scheduleWithFixedDelay(runableListener, 0,
165 config.getEventListenerRefreshinterval(), TimeUnit.MILLISECONDS);
166 logger.debug("internal start EventListener");
171 * Adds a {@link List} of {@link EventHandler}'s and subscribe the supported event-types, if the
172 * {@link EventListener} is started and the event-types are not already subscribed.
174 * @param eventHandlers to add
176 public void addEventHandlers(List<EventHandler> eventHandlers) {
177 if (eventHandlers != null) {
178 for (EventHandler eventHandler : eventHandlers) {
179 addEventHandler(eventHandler);
185 * Adds a {@link EventHandler}'s and subscribe the supported event-types, if the
186 * {@link EventListener} is started and the event-types are not already subscribed.<br>
189 * If {@link #start()} was called before the {@link EventListener} will start now, otherwise you have to call
190 * {@link #start()} to get notified by events.
192 * @param eventHandler to add
194 public void addEventHandler(EventHandler eventHandler) {
195 if (eventHandler != null) {
196 boolean handlerExist = false;
197 for (EventHandler handler : eventHandlers) {
198 if (handler.getUID().equals(eventHandler.getUID())) {
203 eventHandlers.add(eventHandler);
204 addSubscribeEvents(eventHandler.getSupportedEvents());
205 logger.debug("eventHandler: {} added", eventHandler.getUID());
214 * Remove a {@link EventHandler} and unsubscribes the supported event-types, if the
215 * {@link EventListener} is started and no other {@link EventHandler} needed the event-types.
217 * @param eventHandler to remove
219 public void removeEventHandler(EventHandler eventHandler) {
220 if (eventHandler != null && eventHandlers.contains(eventHandler)) {
221 List<String> tempSubsList = new ArrayList<>();
223 EventHandler intEventHandler = null;
224 boolean subscribedEventsChanged = false;
225 for (int i = 0; i < eventHandlers.size(); i++) {
226 intEventHandler = eventHandlers.get(i);
227 if (intEventHandler.getUID().equals(eventHandler.getUID())) {
230 tempSubsList.addAll(intEventHandler.getSupportedEvents());
234 intEventHandler = eventHandlers.remove(index);
235 for (String eventName : intEventHandler.getSupportedEvents()) {
236 if (!tempSubsList.contains(eventName)) {
237 subscribedEvents.remove(eventName);
238 subscribedEventsChanged = true;
242 if (subscribedEventsChanged) {
243 // Because of the json-call unsubscribe?eventName=XY&subscriptionID=Z doesn't work like it is explained
244 // in the dS-JSON-API, the whole EventListener will be restarted. The problem is, that not only the
245 // given eventName, rather all events of the subscitionID will be deleted.
252 * Removes a subscribed event and unsubscibe it, if it is not needed by other {@link EventHandler}'s.
254 * @param unsubscribeEvent event name to unsubscibe
255 * @param eventHandlerID EventHandler-ID of the EventHandler that unsubscibe a event
257 public void removeSubscribe(String unsubscribeEvent, String eventHandlerID) {
258 if (subscribedEvents != null && !subscribedEvents.contains(unsubscribeEvent)) {
259 boolean eventNeededByAnotherHandler = false;
260 for (EventHandler handler : eventHandlers) {
261 if (!handler.getUID().equals(eventHandlerID)) {
262 eventNeededByAnotherHandler = handler.getSupportedEvents().contains(unsubscribeEvent);
265 if (!eventNeededByAnotherHandler) {
266 logger.debug("unsubscribeEvent: {} is not needed by other EventHandler's... unsubscribe it",
268 subscribedEvents.remove(unsubscribeEvent);
271 logger.debug("unsubscribeEvent: {} is needed by other EventHandler's... dosen't unsubscribe it",
277 private void restartListener() {
279 if (!eventHandlers.isEmpty() && isStarted) {
280 logger.debug("Min one subscribed events was deleted, EventListener will be restarted");
286 * Adds a event and subscribed it, if it is not subscribed already.
288 * @param subscribeEvent event name to subscribe
290 public void addSubscribe(String subscribeEvent) {
291 if (!subscribedEvents.contains(subscribeEvent)) {
292 subscribedEvents.add(subscribeEvent);
293 logger.debug("subscibeEvent: {} added", subscribeEvent);
295 subscribe(subscribeEvent);
301 * Adds the events of the {@link List} and subscribe them, if a event is not subscribed already.
303 * @param subscribeEvents event name to subscribe
305 public void addSubscribeEvents(List<String> subscribeEvents) {
306 for (String eventName : subscribeEvents) {
307 subscribe(eventName);
311 private void getSubscriptionID() {
312 boolean subscriptionIDavailable = false;
313 while (!subscriptionIDavailable) {
314 String response = connManager.getDigitalSTROMAPI().getEvent(connManager.getSessionToken(), subscriptionID,
317 JsonObject responseObj = JSONResponseHandler.toJsonObject(response);
319 if (JSONResponseHandler.checkResponse(responseObj)) {
322 String errorStr = null;
323 if (responseObj != null && responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()) != null) {
324 errorStr = responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()).getAsString();
326 if (errorStr != null && errorStr.contains(TOKEN_NOT_FOUND)) {
327 subscriptionIDavailable = true;
333 private boolean subscribe(String eventName) {
337 subscribed = connManager.getDigitalSTROMAPI().subscribeEvent(connManager.getSessionToken(), eventName,
338 subscriptionID, config.getConnectionTimeout(), config.getReadTimeout());
341 logger.debug("subscribed event: {} to subscriptionID: {}", eventName, subscriptionID);
344 "Couldn't subscribe event {} ... maybe timeout because system is too busy ... event will be tried to subscribe later again ... ",
350 private void subscribe(final List<String> evetNames) {
351 final Iterator<String> eventNameIter = evetNames.iterator();
352 subscriptionScheduler = scheduler.scheduleWithFixedDelay(new Runnable() {
356 while (eventNameIter.hasNext()) {
357 subscribe(eventNameIter.next());
359 subscriptionScheduler.cancel(true);
361 }, 0, SUBSCRIBE_DELAY, TimeUnit.MILLISECONDS);
364 private final Runnable runableListener = new Runnable() {
369 String response = connManager.getDigitalSTROMAPI().getEvent(connManager.getSessionToken(),
370 subscriptionID, timeout);
371 JsonObject responseObj = JSONResponseHandler.toJsonObject(response);
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();
380 String errorStr = null;
381 if (responseObj != null && responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()) != null) {
382 errorStr = responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()).getAsString();
384 if (errorStr != null && (errorStr.equals(INVALID_SESSION) || errorStr.contains(TOKEN_NOT_FOUND))) {
386 subscribe(subscribedEvents);
387 } else if (errorStr != null) {
388 pollingScheduler.cancel(true);
389 logger.error("Unknown error message at event response: {}", errorStr);
393 subscribe(subscribedEvents);
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());
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);