2 * Copyright (c) 2010-2023 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.LinkedList;
18 import java.util.List;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
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;
34 import com.google.gson.JsonArray;
35 import com.google.gson.JsonObject;
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 EventHandler} methods {@link EventHandler#getSupportetEvents()} and
50 * {@link EventHandler#supportsEvent(String)}.
52 * To start and stop the listening you have to call the methods {@link #start()} and {@link #stop()}.
54 * @author Michael Ochel - Initial contribution
55 * @author Matthias Siegele - Initial contribution
57 public class EventListener {
59 private final Logger logger = LoggerFactory.getLogger(EventListener.class);
60 private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(Config.THREADPOOL_NAME);
61 public static final int SUBSCRIBE_DELAY = 500;
62 private ScheduledFuture<?> pollingScheduler;
63 private ScheduledFuture<?> subscriptionScheduler;
65 private int subscriptionID = 15;
66 private final int timeout = 500;
67 private final List<String> subscribedEvents = Collections.synchronizedList(new LinkedList<>());
68 private boolean subscribed = false;
71 public static final String INVALID_SESSION = "Invalid session!";
72 public static final String TOKEN_NOT_FOUND = "token not found."; // Complete text: "Event subscription token not
75 private final ConnectionManager connManager;
76 private final List<EventHandler> eventHandlers = Collections.synchronizedList(new LinkedList<>());
77 private final Config config;
78 private boolean isStarted = false;
81 * Creates a new {@link EventListener} to listen to the supported event-types of the given eventHandler and notify
82 * about a detected event.<br>
84 * To get notified by events you have to call {@link #start()}.
86 * @param connectionManager must not be null
87 * @param eventHandler must not be null
89 public EventListener(ConnectionManager connectionManager, EventHandler eventHandler) {
90 this.connManager = connectionManager;
91 this.config = connectionManager.getConfig();
92 addEventHandler(eventHandler);
96 * This constructor can add more than one {@link EventHandler} as a list of {@link EventHandler}'s.
98 * @param connectionManager must not be null
99 * @param eventHandlers list of {@link EventHandler}'s must not be null
100 * @see #EventListener(ConnectionManager, EventHandler)
102 public EventListener(ConnectionManager connectionManager, List<EventHandler> eventHandlers) {
103 this.connManager = connectionManager;
104 this.config = connectionManager.getConfig();
105 addEventHandlers(eventHandlers);
109 * Creates a new {@link EventListener} without an {@link EventHandler}<br>
111 * To get notified by events you have to call {@link #start()} and {@link #addEventHandler(EventHandler)} or
112 * {@link #addEventHandlers(List)}.
114 * @param connectionManager must not be null
116 public EventListener(ConnectionManager connectionManager) {
117 this.connManager = connectionManager;
118 this.config = connectionManager.getConfig();
122 * Stops this {@link EventListener} and unsubscribe events.
124 public synchronized void stop() {
125 logger.debug("Stop EventListener");
131 * Returns true, if the {@link EventListener} is started.
133 * @return true, if is started
135 public boolean isStarted() {
139 private void stopSubscriptionScheduler() {
140 final ScheduledFuture<?> subscriptionScheduler = this.subscriptionScheduler;
141 if (subscriptionScheduler != null) {
142 subscriptionScheduler.cancel(true);
143 this.subscriptionScheduler = null;
147 private void internalStop() {
148 stopSubscriptionScheduler();
150 ScheduledFuture<?> pollingScheduler = this.pollingScheduler;
151 if (pollingScheduler != null) {
152 pollingScheduler.cancel(true);
153 this.pollingScheduler = null;
155 logger.debug("internal stop EventListener");
160 * Starts this {@link EventListener} and subscribe events.
162 public synchronized void start() {
163 logger.debug("Start EventListener");
168 private void internalStart() {
169 if (!eventHandlers.isEmpty() && (pollingScheduler == null || pollingScheduler.isCancelled())) {
170 pollingScheduler = scheduler.scheduleWithFixedDelay(runableListener, 0,
171 config.getEventListenerRefreshinterval(), TimeUnit.MILLISECONDS);
172 logger.debug("internal start EventListener");
177 * Adds a {@link List} of {@link EventHandler}'s and subscribe the supported event-types, if the
178 * {@link EventListener} is started and the event-types are not already subscribed.
180 * @param eventHandlers to add
182 public void addEventHandlers(List<EventHandler> eventHandlers) {
183 if (eventHandlers != null) {
184 for (EventHandler eventHandler : eventHandlers) {
185 addEventHandler(eventHandler);
191 * Adds an {@link EventHandler}'s and subscribe the supported event-types, if the
192 * {@link EventListener} is started and the event-types are not already subscribed.<br>
195 * If {@link #start()} was called before the {@link EventListener} will start now, otherwise you have to call
196 * {@link #start()} to get notified by events.
198 * @param eventHandler to add
200 public void addEventHandler(EventHandler eventHandler) {
201 if (eventHandler != null) {
202 boolean handlerExist = false;
203 for (EventHandler handler : eventHandlers) {
204 if (handler.getUID().equals(eventHandler.getUID())) {
209 eventHandlers.add(eventHandler);
210 addSubscribeEvents(eventHandler.getSupportedEvents());
211 logger.debug("eventHandler: {} added", eventHandler.getUID());
220 * Remove an {@link EventHandler} and unsubscribes the supported event-types, if the
221 * {@link EventListener} is started and no other {@link EventHandler} needed the event-types.
223 * @param eventHandler to remove
225 public void removeEventHandler(EventHandler eventHandler) {
226 if (eventHandler != null && eventHandlers.contains(eventHandler)) {
227 List<String> tempSubsList = new ArrayList<>();
229 EventHandler intEventHandler = null;
230 boolean subscribedEventsChanged = false;
231 for (int i = 0; i < eventHandlers.size(); i++) {
232 intEventHandler = eventHandlers.get(i);
233 if (intEventHandler.getUID().equals(eventHandler.getUID())) {
236 tempSubsList.addAll(intEventHandler.getSupportedEvents());
240 intEventHandler = eventHandlers.remove(index);
241 for (String eventName : intEventHandler.getSupportedEvents()) {
242 if (!tempSubsList.contains(eventName)) {
243 subscribedEvents.remove(eventName);
244 subscribedEventsChanged = true;
248 if (subscribedEventsChanged) {
249 // Because of the json-call unsubscribe?eventName=XY&subscriptionID=Z doesn't work like it is explained
250 // in the dS-JSON-API, the whole EventListener will be restarted. The problem is, that not only the
251 // given eventName, rather all events of the subscitionID will be deleted.
258 * Removes a subscribed event and unsubscibe it, if it is not needed by other {@link EventHandler}'s.
260 * @param unsubscribeEvent event name to unsubscibe
261 * @param eventHandlerID EventHandler-ID of the EventHandler that unsubscibe an event
263 public void removeSubscribe(String unsubscribeEvent, String eventHandlerID) {
264 if (subscribedEvents != null && !subscribedEvents.contains(unsubscribeEvent)) {
265 boolean eventNeededByAnotherHandler = false;
266 for (EventHandler handler : eventHandlers) {
267 if (!handler.getUID().equals(eventHandlerID)) {
268 eventNeededByAnotherHandler = handler.getSupportedEvents().contains(unsubscribeEvent);
271 if (!eventNeededByAnotherHandler) {
272 logger.debug("unsubscribeEvent: {} is not needed by other EventHandler's... unsubscribe it",
274 subscribedEvents.remove(unsubscribeEvent);
277 logger.debug("unsubscribeEvent: {} is needed by other EventHandler's... dosen't unsubscribe it",
283 private void restartListener() {
285 if (!eventHandlers.isEmpty() && isStarted) {
286 logger.debug("Min one subscribed events was deleted, EventListener will be restarted");
292 * Adds an event and subscribes it, if it is not subscribed already.
294 * @param subscribeEvent event name to subscribe
296 public void addSubscribe(String subscribeEvent) {
297 if (!subscribedEvents.contains(subscribeEvent)) {
298 subscribedEvents.add(subscribeEvent);
299 logger.debug("subscibeEvent: {} added", subscribeEvent);
301 subscribe(subscribeEvent);
307 * Adds the events of the {@link List} and subscribe them, if an event is not subscribed already.
309 * @param subscribeEvents event name to subscribe
311 public void addSubscribeEvents(List<String> subscribeEvents) {
312 for (String eventName : subscribeEvents) {
313 subscribe(eventName);
317 private void getSubscriptionID() {
318 boolean subscriptionIDavailable = false;
319 while (!subscriptionIDavailable) {
320 String response = connManager.getDigitalSTROMAPI().getEvent(connManager.getSessionToken(), subscriptionID,
323 JsonObject responseObj = JSONResponseHandler.toJsonObject(response);
325 if (JSONResponseHandler.checkResponse(responseObj)) {
328 String errorStr = null;
329 if (responseObj != null && responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()) != null) {
330 errorStr = responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()).getAsString();
332 if (errorStr != null && errorStr.contains(TOKEN_NOT_FOUND)) {
333 subscriptionIDavailable = true;
339 private boolean subscribe(String eventName) {
343 subscribed = connManager.getDigitalSTROMAPI().subscribeEvent(connManager.getSessionToken(), eventName,
344 subscriptionID, config.getConnectionTimeout(), config.getReadTimeout());
347 logger.debug("subscribed event: {} to subscriptionID: {}", eventName, subscriptionID);
350 "Couldn't subscribe event {} ... maybe timeout because system is too busy ... event will be tried to subscribe later again ... ",
356 private void subscribe(final List<String> eventNames) {
357 subscriptionScheduler = scheduler.scheduleWithFixedDelay(() -> {
358 eventNames.forEach(this::subscribe);
359 stopSubscriptionScheduler();
360 }, 0, SUBSCRIBE_DELAY, TimeUnit.MILLISECONDS);
363 private final Runnable runableListener = new Runnable() {
368 String response = connManager.getDigitalSTROMAPI().getEvent(connManager.getSessionToken(),
369 subscriptionID, timeout);
370 JsonObject responseObj = JSONResponseHandler.toJsonObject(response);
372 if (JSONResponseHandler.checkResponse(responseObj)) {
373 JsonObject obj = JSONResponseHandler.getResultJsonObject(responseObj);
374 if (obj != null && obj.get(JSONApiResponseKeysEnum.EVENTS.getKey()).isJsonArray()) {
375 JsonArray array = obj.get(JSONApiResponseKeysEnum.EVENTS.getKey()).getAsJsonArray();
379 String errorStr = null;
380 if (responseObj != null && responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()) != null) {
381 errorStr = responseObj.get(JSONApiResponseKeysEnum.MESSAGE.getKey()).getAsString();
383 if (errorStr != null && (errorStr.equals(INVALID_SESSION) || errorStr.contains(TOKEN_NOT_FOUND))) {
385 subscribe(subscribedEvents);
386 } else if (errorStr != null) {
387 pollingScheduler.cancel(true);
388 logger.error("Unknown error message at event response: {}", errorStr);
392 subscribe(subscribedEvents);
397 private void unsubscribe() {
398 for (String eventName : this.subscribedEvents) {
399 connManager.getDigitalSTROMAPI().unsubscribeEvent(this.connManager.getSessionToken(), eventName,
400 this.subscriptionID, config.getConnectionTimeout(), config.getReadTimeout());
404 private void handleEvent(JsonArray array) {
405 if (array.size() > 0) {
406 Event event = new JSONEventImpl(array);
407 for (EventItem item : event.getEventItems()) {
408 logger.debug("detect event {}", item.toString());
409 for (EventHandler handler : eventHandlers) {
410 if (handler.supportsEvent(item.getName())) {
411 logger.debug("inform handler with id {} about event {}", handler.getUID(), item.toString());
412 handler.handleEvent(item);