]> git.basschouten.com Git - openhab-addons.git/blob
5e39491a406f40c6a246da6d863e0be5258a2fee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.action;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.heos.internal.api.HeosFacade;
20 import org.openhab.binding.heos.internal.exception.HeosNotConnectedException;
21 import org.openhab.binding.heos.internal.handler.HeosBridgeHandler;
22 import org.openhab.binding.heos.internal.resources.Telnet;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The class is responsible to call corresponding action on HEOS Handler
33  *
34  * @author Martin van Wingerden - Initial contribution
35  */
36 @ThingActionsScope(name = "heos")
37 @NonNullByDefault
38 public class HeosActions implements ThingActions {
39
40     private static final Logger logger = LoggerFactory.getLogger(HeosActions.class);
41
42     private @Nullable HeosBridgeHandler handler;
43
44     @Override
45     public void setThingHandler(@Nullable ThingHandler handler) {
46         if (handler instanceof HeosBridgeHandler bridgeHandler) {
47             this.handler = bridgeHandler;
48         }
49     }
50
51     @Override
52     public @Nullable ThingHandler getThingHandler() {
53         return handler;
54     }
55
56     private @Nullable HeosFacade getConnection() throws HeosNotConnectedException {
57         HeosBridgeHandler handler = this.handler;
58         if (handler == null) {
59             return null;
60         }
61
62         return handler.getApiConnection();
63     }
64
65     @RuleAction(label = "play an input", description = "Play an input from another device.")
66     public void playInputFromPlayer(
67             @ActionInput(name = "source", label = "Source Player", description = "Player used for input") @Nullable Integer sourcePlayer,
68             @ActionInput(name = "input", label = "Source Input", description = "Input source used") @Nullable String input,
69             @ActionInput(name = "destination", label = "Destination Player", description = "Device for audio output") @Nullable Integer destinationPlayer) {
70         if (sourcePlayer == null || input == null || destinationPlayer == null) {
71             logger.debug(
72                     "Skipping HEOS playInputFromPlayer due to null value: sourcePlayer: {}, input: {}, destination: {}",
73                     sourcePlayer, input, destinationPlayer);
74             return;
75         }
76
77         try {
78             HeosFacade connection = getConnection();
79
80             if (connection == null) {
81                 logger.debug("Skipping HEOS playInputFromPlayer because no connection was available");
82                 return;
83             }
84
85             connection.playInputSource(destinationPlayer.toString(), sourcePlayer.toString(), input);
86         } catch (IOException | Telnet.ReadException e) {
87             logger.warn("Failed to play input source!", e);
88         }
89     }
90
91     public static void playInputFromPlayer(ThingActions actions, @Nullable Integer sourcePlayer, @Nullable String input,
92             @Nullable Integer destinationPlayer) {
93         ((HeosActions) actions).playInputFromPlayer(sourcePlayer, input, destinationPlayer);
94     }
95 }