]> git.basschouten.com Git - openhab-addons.git/blob
149eb23f41307db26070b49836670e21ec862a40
[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.heos.internal.resources;
14
15 import java.util.Set;
16 import java.util.concurrent.CopyOnWriteArraySet;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.heos.internal.json.dto.HeosEventObject;
20 import org.openhab.binding.heos.internal.json.payload.Media;
21
22 /**
23  * The {@link HeosSystemEventListener } is used for classes which
24  * wants to inform players or groups about change events
25  * from the HEOS system. Classes which wants to be informed
26  * has to implement the {@link HeosEventListener} and register at
27  * the class which extends this {@link HeosSystemEventListener}
28  *
29  * @author Johannes Einig - Initial contribution
30  * @author Martin van Wingerden - change handling of stop/pause depending on playing item type
31  */
32 @NonNullByDefault
33 public class HeosSystemEventListener {
34     private final Set<HeosEventListener> listenerList = new CopyOnWriteArraySet<>();
35
36     /**
37      * Register a listener from type {@link HeosEventListener} to be notified by
38      * a change event
39      *
40      * @param listener the lister from type {@link HeosEventListener} for change events
41      */
42     public void addListener(HeosEventListener listener) {
43         listenerList.add(listener);
44     }
45
46     /**
47      * Removes the listener from the notification list
48      *
49      * @param listener the listener from type {@link HeosEventListener} to be removed
50      */
51     public void removeListener(HeosEventListener listener) {
52         listenerList.remove(listener);
53     }
54
55     /**
56      * Notifies the registered listener of a changed state type event
57      *
58      * @param eventObject the command of the event
59      */
60     public void fireStateEvent(HeosEventObject eventObject) {
61         listenerList.forEach(element -> element.playerStateChangeEvent(eventObject));
62     }
63
64     /**
65      * Notifies the registered listener of a changed media type event
66      *
67      * @param pid the ID of the player or group which has changed
68      * @param media the media information
69      */
70     public void fireMediaEvent(String pid, Media media) {
71         listenerList.forEach(element -> element.playerMediaChangeEvent(pid, media));
72     }
73
74     /**
75      * Notifies the registered listener if a change of the bridge state
76      *
77      * @param event the event type
78      * @param success the result (success or fail)
79      * @param command the command of the event
80      */
81     public void fireBridgeEvent(String event, boolean success, Object command) {
82         for (HeosEventListener heosEventListener : listenerList) {
83             heosEventListener.bridgeChangeEvent(event, success, command);
84         }
85     }
86 }