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.handler;
15 import static org.openhab.binding.heos.internal.HeosBindingConstants.*;
16 import static org.openhab.binding.heos.internal.handler.FutureUtil.cancel;
17 import static org.openhab.binding.heos.internal.json.dto.HeosCommunicationAttribute.PLAYER_ID;
18 import static org.openhab.binding.heos.internal.json.dto.HeosEvent.GROUP_VOLUME_CHANGED;
20 import java.io.IOException;
22 import java.util.concurrent.Future;
23 import java.util.concurrent.TimeUnit;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.heos.internal.configuration.PlayerConfiguration;
28 import org.openhab.binding.heos.internal.exception.HeosFunctionalException;
29 import org.openhab.binding.heos.internal.json.dto.HeosErrorCode;
30 import org.openhab.binding.heos.internal.json.dto.HeosEventObject;
31 import org.openhab.binding.heos.internal.json.dto.HeosResponseObject;
32 import org.openhab.binding.heos.internal.json.payload.Media;
33 import org.openhab.binding.heos.internal.json.payload.Player;
34 import org.openhab.binding.heos.internal.resources.Telnet.ReadException;
35 import org.openhab.core.library.types.PercentType;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusDetail;
40 import org.openhab.core.types.Command;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link HeosPlayerHandler} handles the actions for a HEOS player.
46 * Channel commands are received and send to the dedicated channels
48 * @author Johannes Einig - Initial contribution
51 public class HeosPlayerHandler extends HeosThingBaseHandler {
52 private final Logger logger = LoggerFactory.getLogger(HeosPlayerHandler.class);
54 private @NonNullByDefault({}) String pid;
55 private @Nullable Future<?> scheduledFuture;
57 public HeosPlayerHandler(Thing thing, HeosDynamicStateDescriptionProvider heosDynamicStateDescriptionProvider) {
58 super(thing, heosDynamicStateDescriptionProvider);
62 public void handleCommand(ChannelUID channelUID, Command command) {
64 HeosChannelHandler channelHandler = getHeosChannelHandler(channelUID);
65 if (channelHandler != null) {
67 channelHandler.handlePlayerCommand(command, getId(), thing.getUID());
69 } catch (IOException | ReadException e) {
76 public void initialize() {
79 PlayerConfiguration configuration = thing.getConfiguration().as(PlayerConfiguration.class);
80 pid = configuration.pid;
82 cancel(scheduledFuture);
83 scheduledFuture = scheduler.submit(this::delayedInitialize);
86 private synchronized void delayedInitialize() {
88 refreshPlayState(pid);
90 handleThingStateUpdate(getApiConnection().getPlayerInfo(pid));
92 updateStatus(ThingStatus.ONLINE);
93 } catch (HeosFunctionalException e) {
94 if (e.getCode() == HeosErrorCode.INVALID_ID) {
95 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, e.getCode().toString());
97 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getCode().toString());
99 } catch (IOException | ReadException e) {
100 logger.debug("Failed to initialize, will try again", e);
101 cancel(scheduledFuture, false);
102 scheduledFuture = scheduler.schedule(this::delayedInitialize, 3, TimeUnit.SECONDS);
107 void refreshPlayState(String id) throws IOException, ReadException {
108 super.refreshPlayState(id);
110 handleThingStateUpdate(getApiConnection().getPlayerMuteState(id));
111 handleThingStateUpdate(getApiConnection().getPlayerVolume(id));
115 public void dispose() {
116 cancel(scheduledFuture);
121 public String getId() {
126 public void setNotificationSoundVolume(PercentType volume) {
130 public void playerStateChangeEvent(HeosEventObject eventObject) {
131 if (!pid.equals(eventObject.getAttribute(PLAYER_ID))) {
135 if (GROUP_VOLUME_CHANGED == eventObject.command) {
136 logger.debug("Ignoring group-volume changes for players");
140 handleThingStateUpdate(eventObject);
144 public void playerStateChangeEvent(HeosResponseObject<?> responseObject) throws HeosFunctionalException {
145 if (!pid.equals(responseObject.getAttribute(PLAYER_ID))) {
149 handleThingStateUpdate(responseObject);
153 public void playerMediaChangeEvent(String eventPid, Media media) {
154 if (!pid.equals(eventPid)) {
158 handleThingMediaUpdate(media);
162 public void setStatusOffline() {
163 updateStatus(ThingStatus.OFFLINE);
167 public void setStatusOnline() {
168 updateStatus(ThingStatus.ONLINE);
171 public static void propertiesFromPlayer(Map<String, ? super String> prop, Player player) {
172 prop.put(PROP_NAME, player.name);
173 prop.put(PROP_PID, String.valueOf(player.playerId));
174 prop.put(Thing.PROPERTY_MODEL_ID, player.model);
175 prop.put(Thing.PROPERTY_FIRMWARE_VERSION, player.version);
176 prop.put(PROP_NETWORK, player.network);
177 prop.put(PROP_IP, player.ip);
179 String serialNumber = player.serial;
180 if (serialNumber != null) {
181 prop.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
183 prop.put(Thing.PROPERTY_SERIAL_NUMBER, String.valueOf(player.playerId)); // If no serial number is provided,
184 // write an empty string to
185 // prevent error during runtime