]> git.basschouten.com Git - openhab-addons.git/blob
186d142bdb142f468bedff8c424ca557052f874c
[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.api;
14
15 import static org.openhab.binding.heos.internal.resources.HeosConstants.*;
16
17 import java.io.IOException;
18
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;
30
31 /**
32  * The {@link HeosEventController} is responsible for handling event, which are
33  * received by the HEOS system.
34  *
35  * @author Johannes Einig - Initial contribution
36  */
37 @NonNullByDefault
38 public class HeosEventController extends HeosSystemEventListener {
39     private final Logger logger = LoggerFactory.getLogger(HeosEventController.class);
40
41     private final HeosSystem system;
42
43     private long lastEventTime;
44
45     public HeosEventController(HeosSystem system) {
46         this.system = system;
47         lastEventTime = System.currentTimeMillis();
48     }
49
50     public void handleEvent(HeosEventObject eventObject) {
51         HeosEvent command = eventObject.command;
52         lastEventTime = System.currentTimeMillis();
53
54         logger.debug("Handling event: {}", eventObject);
55
56         if (command == null) {
57             return;
58         }
59
60         switch (command) {
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:
69             case SOURCES_CHANGED:
70                 fireStateEvent(eventObject);
71                 break;
72
73             case USER_CHANGED:
74                 fireBridgeEvent(EVENT_TYPE_SYSTEM, true, command);
75                 break;
76
77             case PLAYER_NOW_PLAYING_CHANGED:
78                 String pid = eventObject.getAttribute(HeosCommunicationAttribute.PLAYER_ID);
79
80                 if (pid == null) {
81                     logger.debug("HEOS did not mention which player changed, unlikely but ignore");
82                     break;
83                 }
84
85                 try {
86                     HeosResponseObject<Media> mediaResponse = system.send(HeosCommands.getNowPlayingMedia(pid),
87                             Media.class);
88                     Media responseMedia = mediaResponse.payload;
89                     if (responseMedia != null) {
90                         fireMediaEvent(pid, responseMedia);
91                     }
92                 } catch (IOException | Telnet.ReadException e) {
93                     logger.debug("Failed to retrieve current playing media, will try again next time.", e);
94                 }
95                 break;
96
97             case GROUPS_CHANGED:
98             case PLAYERS_CHANGED:
99                 fireBridgeEvent(EVENT_TYPE_EVENT, true, command);
100                 break;
101         }
102     }
103
104     public void connectionToSystemLost() {
105         fireBridgeEvent(EVENT_TYPE_EVENT, false, CONNECTION_LOST);
106     }
107
108     public void eventStreamTimeout() {
109         fireBridgeEvent(EVENT_TYPE_EVENT, false, EVENT_STREAM_TIMEOUT);
110     }
111
112     public void systemReachable() {
113         fireBridgeEvent(EVENT_TYPE_EVENT, true, CONNECTION_RESTORED);
114     }
115
116     long getLastEventTime() {
117         return lastEventTime;
118     }
119 }