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.squeezebox.internal.handler;
15 import java.util.List;
16 import java.util.concurrent.atomic.AtomicBoolean;
17 import java.util.concurrent.atomic.AtomicInteger;
19 import org.openhab.binding.squeezebox.internal.model.Favorite;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * This {@link SqueezeBoxNotificationListener}- is a type of PlayerEventListener
25 * that's used to monitor certain events related to the notification functionality.
27 * @author Mark Hilbush - Initial contribution
28 * @author Mark Hilbush - Added event to update favorites list
30 public final class SqueezeBoxNotificationListener implements SqueezeBoxPlayerEventListener {
31 private final Logger logger = LoggerFactory.getLogger(SqueezeBoxNotificationListener.class);
33 private final String playerMAC;
35 // Used to monitor when the player stops
36 private final AtomicBoolean started = new AtomicBoolean(false);
37 private final AtomicBoolean stopped = new AtomicBoolean(false);
39 // Used to monitor when the player pauses
40 private final AtomicBoolean paused = new AtomicBoolean(false);
42 // Used to monitor for updates to the playlist
43 private final AtomicBoolean playlistUpdated = new AtomicBoolean(false);
45 // Used to monitor when the player volume changes to a specific target value
46 private final AtomicInteger volume = new AtomicInteger(-1);
48 SqueezeBoxNotificationListener(String playerMAC) {
49 this.playerMAC = playerMAC;
53 public void resetStopped() {
54 this.started.set(false);
55 this.stopped.set(false);
58 public boolean isStopped() {
59 return this.stopped.get();
63 public void resetPaused() {
64 this.paused.set(false);
67 public boolean isPaused() {
68 return this.paused.get();
72 public void resetPlaylistUpdated() {
73 this.playlistUpdated.set(false);
76 public boolean isPlaylistUpdated() {
77 return this.playlistUpdated.get();
81 public void resetVolumeUpdated() {
85 public boolean isVolumeUpdated(int volume) {
86 return this.volume.get() == volume;
89 // Implementation of listener interfaces
91 public void playerAdded(SqueezeBoxPlayer player) {
95 public void powerChangeEvent(String mac, boolean power) {
99 * Monitor for player mode changing to stop.
102 public void modeChangeEvent(String mac, String mode) {
103 if (!this.playerMAC.equals(mac)) {
106 logger.trace("Mode is {} for player {}", mode, mac);
108 if (mode.equals("play")) {
109 this.started.set(true);
110 } else if (this.started.get() && mode.equals("stop")) {
111 this.stopped.set(true);
113 if (mode.equals("pause")) {
114 this.paused.set(true);
119 * Monitor for when the volume is updated to a specific target value
122 public void absoluteVolumeChangeEvent(String mac, int volume) {
123 if (!this.playerMAC.equals(mac)) {
126 this.volume.set(volume);
127 logger.trace("Volume is {} for player {}", volume, mac);
131 public void relativeVolumeChangeEvent(String mac, int volumeChange) {
132 if (!this.playerMAC.equals(mac)) {
136 int newVolume = this.volume.get() + volumeChange;
137 newVolume = Math.min(newVolume, 100);
138 newVolume = Math.max(newVolume, 0);
140 this.volume.set(newVolume);
141 logger.trace("Volume changed [{}] for player {}. New volume: {}", volumeChange, mac, volume);
145 public void muteChangeEvent(String mac, boolean mute) {
149 public void currentPlaylistIndexEvent(String mac, int index) {
153 public void currentPlayingTimeEvent(String mac, int time) {
157 public void durationEvent(String mac, int duration) {
161 * Monitor for when the playlist is updated
164 public void numberPlaylistTracksEvent(String mac, int track) {
165 if (!this.playerMAC.equals(mac)) {
168 logger.trace("Number of playlist tracks is {} for player {}", track, mac);
169 playlistUpdated.set(true);
173 public void currentPlaylistShuffleEvent(String mac, int shuffle) {
177 public void currentPlaylistRepeatEvent(String mac, int repeat) {
181 public void titleChangeEvent(String mac, String title) {
185 public void albumChangeEvent(String mac, String album) {
189 public void artistChangeEvent(String mac, String artist) {
193 public void coverArtChangeEvent(String mac, String coverArtUrl) {
197 public void yearChangeEvent(String mac, String year) {
201 public void genreChangeEvent(String mac, String genre) {
205 public void remoteTitleChangeEvent(String mac, String title) {
209 public void irCodeChangeEvent(String mac, String ircode) {
213 public void updateFavoritesListEvent(List<Favorite> favorites) {
217 public void sourceChangeEvent(String mac, String source) {
221 public void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand) {
225 public void connectedStateChangeEvent(String mac, boolean connected) {