]> git.basschouten.com Git - openhab-addons.git/blob
d06d4e1e75959882e3121d9d3b456300288986ba
[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.chromecast.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.chromecast.internal.handler.ChromecastHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.ActionOutput;
20 import org.openhab.core.automation.annotation.RuleAction;
21 import org.openhab.core.thing.binding.ThingActions;
22 import org.openhab.core.thing.binding.ThingActionsScope;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link ChromecastActions} class defines rule actions for playing URLs
29  *
30  * @author Scott Hanson - Initial contribution
31  */
32 @ThingActionsScope(name = "chromecast")
33 @NonNullByDefault
34 public class ChromecastActions implements ThingActions {
35
36     private final Logger logger = LoggerFactory.getLogger(ChromecastActions.class);
37
38     private @Nullable ChromecastHandler handler;
39
40     @RuleAction(label = "@text/playURLActionLabel", description = "@text/playURLActionDescription")
41     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean playURL(
42             @ActionInput(name = "url") @Nullable String url) {
43         if (url == null) {
44             logger.warn("Cannot Play as URL is missing.");
45             return false;
46         }
47
48         final ChromecastHandler handler = this.handler;
49         if (handler == null) {
50             logger.warn("Handler is null, cannot play.");
51             return false;
52         } else {
53             return handler.playURL(null, url, null);
54         }
55     }
56
57     @RuleAction(label = "@text/playURLTypeActionLabel", description = "@text/playURLTypeActionDescription")
58     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean playURL(
59             @ActionInput(name = "url") @Nullable String url,
60             @ActionInput(name = "mediaType") @Nullable String mediaType) {
61         if (url == null) {
62             logger.warn("Cannot Play as URL is missing.");
63             return false;
64         }
65
66         final ChromecastHandler handler = this.handler;
67         if (handler == null) {
68             logger.warn("Handler is null, cannot tweet.");
69             return false;
70         } else {
71             return handler.playURL(null, url, mediaType);
72         }
73     }
74
75     public static boolean playURL(ThingActions actions, @Nullable String url) {
76         return ((ChromecastActions) actions).playURL(url);
77     }
78
79     public static boolean playURL(ThingActions actions, @Nullable String url, @Nullable String mediaType) {
80         return ((ChromecastActions) actions).playURL(url, mediaType);
81     }
82
83     @Override
84     public void setThingHandler(@Nullable ThingHandler handler) {
85         if (handler instanceof ChromecastHandler) {
86             this.handler = (ChromecastHandler) handler;
87         }
88     }
89
90     @Override
91     public @Nullable ThingHandler getThingHandler() {
92         return handler;
93     }
94 }