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.heos.internal.api;
15 import static org.openhab.binding.heos.internal.resources.HeosConstants.*;
17 import java.io.IOException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.heos.internal.json.dto.HeosCommunicationAttribute;
21 import org.openhab.binding.heos.internal.json.dto.HeosEvent;
22 import org.openhab.binding.heos.internal.json.dto.HeosEventObject;
23 import org.openhab.binding.heos.internal.json.dto.HeosResponseObject;
24 import org.openhab.binding.heos.internal.json.payload.Media;
25 import org.openhab.binding.heos.internal.resources.HeosCommands;
26 import org.openhab.binding.heos.internal.resources.HeosSystemEventListener;
27 import org.openhab.binding.heos.internal.resources.Telnet;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link HeosEventController} is responsible for handling event, which are
33 * received by the HEOS system.
35 * @author Johannes Einig - Initial contribution
38 public class HeosEventController extends HeosSystemEventListener {
39 private final Logger logger = LoggerFactory.getLogger(HeosEventController.class);
41 private final HeosSystem system;
43 private long lastEventTime;
45 public HeosEventController(HeosSystem system) {
47 lastEventTime = System.currentTimeMillis();
50 public void handleEvent(HeosEventObject eventObject) {
51 HeosEvent command = eventObject.command;
52 lastEventTime = System.currentTimeMillis();
54 logger.debug("Handling event: {}", eventObject);
56 if (command == null) {
61 case PLAYER_NOW_PLAYING_PROGRESS:
62 case PLAYER_STATE_CHANGED:
63 case PLAYER_VOLUME_CHANGED:
64 case SHUFFLE_MODE_CHANGED:
65 case REPEAT_MODE_CHANGED:
66 case PLAYER_PLAYBACK_ERROR:
67 case GROUP_VOLUME_CHANGED:
68 case PLAYER_QUEUE_CHANGED:
70 fireStateEvent(eventObject);
74 fireBridgeEvent(EVENT_TYPE_SYSTEM, true, command);
77 case PLAYER_NOW_PLAYING_CHANGED:
78 String pid = eventObject.getAttribute(HeosCommunicationAttribute.PLAYER_ID);
81 logger.debug("HEOS did not mention which player changed, unlikely but ignore");
86 HeosResponseObject<Media> mediaResponse = system.send(HeosCommands.getNowPlayingMedia(pid),
88 Media responseMedia = mediaResponse.payload;
89 if (responseMedia != null) {
90 fireMediaEvent(pid, responseMedia);
92 } catch (IOException | Telnet.ReadException e) {
93 logger.debug("Failed to retrieve current playing media, will try again next time.", e);
99 fireBridgeEvent(EVENT_TYPE_EVENT, true, command);
104 public void connectionToSystemLost() {
105 fireBridgeEvent(EVENT_TYPE_EVENT, false, CONNECTION_LOST);
108 public void eventStreamTimeout() {
109 fireBridgeEvent(EVENT_TYPE_EVENT, false, EVENT_STREAM_TIMEOUT);
112 public void systemReachable() {
113 fireBridgeEvent(EVENT_TYPE_EVENT, true, CONNECTION_RESTORED);
116 long getLastEventTime() {
117 return lastEventTime;