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