]> git.basschouten.com Git - openhab-addons.git/blob
af28cdd6fa112f932b7c849e001e6e0d1b5b2cc3
[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.handler;
14
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;
19
20 import java.io.IOException;
21 import java.util.Map;
22 import java.util.concurrent.Future;
23 import java.util.concurrent.TimeUnit;
24
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;
43
44 /**
45  * The {@link HeosPlayerHandler} handles the actions for a HEOS player.
46  * Channel commands are received and send to the dedicated channels
47  *
48  * @author Johannes Einig - Initial contribution
49  */
50 @NonNullByDefault
51 public class HeosPlayerHandler extends HeosThingBaseHandler {
52     private final Logger logger = LoggerFactory.getLogger(HeosPlayerHandler.class);
53
54     private @NonNullByDefault({}) String pid;
55     private @Nullable Future<?> scheduledFuture;
56
57     public HeosPlayerHandler(Thing thing, HeosDynamicStateDescriptionProvider heosDynamicStateDescriptionProvider) {
58         super(thing, heosDynamicStateDescriptionProvider);
59     }
60
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         @Nullable
64         HeosChannelHandler channelHandler = getHeosChannelHandler(channelUID);
65         if (channelHandler != null) {
66             try {
67                 channelHandler.handlePlayerCommand(command, getId(), thing.getUID());
68                 handleSuccess();
69             } catch (IOException | ReadException e) {
70                 handleError(e);
71             }
72         }
73     }
74
75     @Override
76     public void initialize() {
77         super.initialize();
78
79         PlayerConfiguration configuration = thing.getConfiguration().as(PlayerConfiguration.class);
80         pid = configuration.pid;
81
82         cancel(scheduledFuture);
83         scheduledFuture = scheduler.submit(this::delayedInitialize);
84     }
85
86     private synchronized void delayedInitialize() {
87         try {
88             refreshPlayState(pid);
89
90             handleThingStateUpdate(getApiConnection().getPlayerInfo(pid));
91
92             updateStatus(ThingStatus.ONLINE);
93         } catch (HeosFunctionalException e) {
94             if (e.getCode() == HeosErrorCode.INVALID_ID) {
95                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, e.getCode().toString());
96             } else {
97                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getCode().toString());
98             }
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);
103         }
104     }
105
106     @Override
107     void refreshPlayState(String id) throws IOException, ReadException {
108         super.refreshPlayState(id);
109
110         handleThingStateUpdate(getApiConnection().getPlayerMuteState(id));
111         handleThingStateUpdate(getApiConnection().getPlayerVolume(id));
112     }
113
114     @Override
115     public void dispose() {
116         cancel(scheduledFuture);
117         super.dispose();
118     }
119
120     @Override
121     public String getId() {
122         return pid;
123     }
124
125     @Override
126     public void setNotificationSoundVolume(PercentType volume) {
127     }
128
129     @Override
130     public void playerStateChangeEvent(HeosEventObject eventObject) {
131         if (!pid.equals(eventObject.getAttribute(PLAYER_ID))) {
132             return;
133         }
134
135         if (GROUP_VOLUME_CHANGED == eventObject.command) {
136             logger.debug("Ignoring group-volume changes for players");
137             return;
138         }
139
140         handleThingStateUpdate(eventObject);
141     }
142
143     @Override
144     public void playerStateChangeEvent(HeosResponseObject<?> responseObject) throws HeosFunctionalException {
145         if (!pid.equals(responseObject.getAttribute(PLAYER_ID))) {
146             return;
147         }
148
149         handleThingStateUpdate(responseObject);
150     }
151
152     @Override
153     public void playerMediaChangeEvent(String eventPid, Media media) {
154         if (!pid.equals(eventPid)) {
155             return;
156         }
157
158         handleThingMediaUpdate(media);
159     }
160
161     @Override
162     public void setStatusOffline() {
163         updateStatus(ThingStatus.OFFLINE);
164     }
165
166     @Override
167     public void setStatusOnline() {
168         updateStatus(ThingStatus.ONLINE);
169     }
170
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);
178         @Nullable
179         String serialNumber = player.serial;
180         if (serialNumber != null) {
181             prop.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
182         } else {
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
186         }
187     }
188 }