]> git.basschouten.com Git - openhab-addons.git/blob
6d7623d89d3e0bd71a88548699b68c67c1d9ba45
[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.spotify.internal.actions;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.spotify.internal.handler.SpotifyBridgeHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.thing.binding.ThingActions;
21 import org.openhab.core.thing.binding.ThingActionsScope;
22 import org.openhab.core.thing.binding.ThingHandler;
23 import org.openhab.core.thing.binding.ThingHandlerService;
24
25 /**
26  * Spotify Rule Actions.
27  *
28  * @author Hilbrand Bouwkamp - Initial contribution
29  */
30 @ThingActionsScope(name = "spotify")
31 @NonNullByDefault
32 public class SpotifyActions implements ThingActions, ThingHandlerService {
33
34     private @Nullable ThingHandler handler;
35
36     /**
37      * Play a context uri (track or other) on the current active device (if null is passed for deviceID) or the given
38      * device at the given offset and/or position in milliseconds.
39      *
40      * @param contextUri context uri (track or other)
41      * @param deviceId Id of the device to play on, or current device if given null
42      * @param offset Offset in the list, default 0.
43      * @param positionMs position in the track in milliseconds, default 0,
44      */
45     @RuleAction(label = "@text/actions.play.label", description = "@text/actions.play.description")
46     public void play(
47             @ActionInput(name = "contextUri", label = "@text/actions.play.context_uri.label", description = "@text/actions.play.context_uri.description", type = "java.lang.String", required = true) String contextUri,
48             @ActionInput(name = "deviceId", label = "@text/actions.play.device_id.label", description = "@text/actions.play.device_id.description", type = "java.lang.String", defaultValue = "") @Nullable String deviceId,
49             @ActionInput(name = "offset", label = "@text/actions.play.offset.label", description = "@text/actions.play.offset.description", type = "java.lang.Integer", defaultValue = "0") final int offset,
50             @ActionInput(name = "positionMs", label = "@text/actions.play.positions_ms.label", description = "@text/actions.play.positions_ms.description", type = "java.lang.Integer", defaultValue = "0") final int positionMs) {
51         ((SpotifyBridgeHandler) getThingHandler()).getSpotifyApi().playTrack(deviceId == null ? "" : deviceId,
52                 contextUri, offset, positionMs);
53     }
54
55     /**
56      * Play a context uri (track or other) on the current active device.
57      *
58      * @param actions Spotify Actions object.
59      * @param contextUri context uri (track or other)
60      */
61     public static void play(ThingActions actions, String contextUri) {
62         ((SpotifyActions) actions).play(contextUri, null, 0, 0);
63     }
64
65     /**
66      * Play a context uri (track or other) on the current active device at the given offset and/or position in
67      * milliseconds.
68      *
69      * @param actions Spotify Actions object.
70      * @param contextUri context uri (track or other)
71      * @param offset Offset in the list, default 0.
72      * @param positionMs position in the track in milliseconds, default 0,
73      */
74     public static void play(ThingActions actions, String contextUri, final int offset, final int positionMs) {
75         ((SpotifyActions) actions).play(contextUri, null, positionMs, positionMs);
76     }
77
78     /**
79      * Play a context uri (track or other) on the given device.
80      *
81      * @param actions Spotify Actions object.
82      * @param contextUri context uri (track or other)
83      * @param deviceId Id of the device to play on, or current device if given null
84      */
85     public static void play(ThingActions actions, String contextUri, @Nullable String deviceId) {
86         ((SpotifyActions) actions).play(contextUri, deviceId, 0, 0);
87     }
88
89     /**
90      * Play a context uri (track or other) on the current active device (if null is passed for deviceID) or the given
91      * device at the given offset and/or position in milliseconds.
92      *
93      * @param actions Spotify Actions object.
94      * @param contextUri context uri (track or other)
95      * @param deviceId Id of the device to play on, or current device if given null
96      * @param offset Offset in the list, default 0.
97      * @param positionMs position in the track in milliseconds, default 0,
98      */
99     public static void play(ThingActions actions, String contextUri, @Nullable String deviceId, final int offset,
100             final int positionMs) {
101         ((SpotifyActions) actions).play(contextUri, deviceId, positionMs, positionMs);
102     }
103
104     @Override
105     public void setThingHandler(ThingHandler handler) {
106         this.handler = handler;
107     }
108
109     @Override
110     public @Nullable ThingHandler getThingHandler() {
111         return handler;
112     }
113 }