]> git.basschouten.com Git - openhab-addons.git/blob
3717db00d5e52165fdcc177f480e47cba570da80
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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  */
31 @NonNullByDefault
32 public class HeosSystemEventListener {
33     private Set<HeosEventListener> listenerList = new CopyOnWriteArraySet<>();
34
35     /**
36      * Register a listener from type {@link HeosEventListener} to be notified by
37      * a change event
38      *
39      * @param listener the lister from type {@link HeosEventListener} for change events
40      */
41     public void addListener(HeosEventListener listener) {
42         listenerList.add(listener);
43     }
44
45     /**
46      * Removes the listener from the notification list
47      *
48      * @param listener the listener from type {@link HeosEventListener} to be removed
49      */
50     public void removeListener(HeosEventListener listener) {
51         listenerList.remove(listener);
52     }
53
54     /**
55      * Notifies the registered listener of a changed state type event
56      *
57      * @param eventObject the command of the event
58      */
59     public void fireStateEvent(HeosEventObject eventObject) {
60         listenerList.forEach(element -> element.playerStateChangeEvent(eventObject));
61     }
62
63     /**
64      * Notifies the registered listener of a changed media type event
65      *
66      * @param pid the ID of the player or group which has changed
67      * @param media the media information
68      */
69     public void fireMediaEvent(String pid, Media media) {
70         listenerList.forEach(element -> element.playerMediaChangeEvent(pid, media));
71     }
72
73     /**
74      * Notifies the registered listener if a change of the bridge state
75      *
76      * @param event the event type
77      * @param success the result (success or fail)
78      * @param command the command of the event
79      */
80     public void fireBridgeEvent(String event, boolean success, Object command) {
81         for (HeosEventListener heosEventListener : listenerList) {
82             heosEventListener.bridgeChangeEvent(event, success, command);
83         }
84     }
85 }