]> git.basschouten.com Git - openhab-addons.git/blob
fa89c9d765aa0556252f31e399ce3e33a7d94bcc
[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 actions Spotify Actions object.
41      * @param contextUri context uri (track or other)
42      * @param deviceId Id of the device to play on, or current device if given null
43      * @param offset Offset in the list, default 0.
44      * @param positionMs position in the track in milliseconds, default 0,
45      */
46     @RuleAction(label = "@text/actions.play.label", description = "@text/actions.play.description")
47     public void play(
48             @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,
49             @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,
50             @ActionInput(name = "offset", label = "@text/actions.play.offset.label", description = "@text/actions.play.offset.description", type = "java.lang.Integer", defaultValue = "0") final int offset,
51             @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) {
52         ((SpotifyBridgeHandler) getThingHandler()).getSpotifyApi().playTrack(deviceId == null ? "" : deviceId,
53                 contextUri, offset, positionMs);
54     }
55
56     /**
57      * Play a context uri (track or other) on the current active device.
58      *
59      * @param actions Spotify Actions object.
60      * @param contextUri context uri (track or other)
61      */
62     public static void play(ThingActions actions, String contextUri) {
63         ((SpotifyActions) actions).play(contextUri, null, 0, 0);
64     }
65
66     /**
67      * Play a context uri (track or other) on the current active device at the given offset and/or position in
68      * milliseconds.
69      *
70      * @param actions Spotify Actions object.
71      * @param contextUri context uri (track or other)
72      * @param offset Offset in the list, default 0.
73      * @param positionMs position in the track in milliseconds, default 0,
74      */
75     public static void play(ThingActions actions, String contextUri, final int offset, final int positionMs) {
76         ((SpotifyActions) actions).play(contextUri, null, positionMs, positionMs);
77     }
78
79     /**
80      * Play a context uri (track or other) on the given device.
81      *
82      * @param actions Spotify Actions object.
83      * @param contextUri context uri (track or other)
84      * @param deviceId Id of the device to play on, or current device if given null
85      */
86     public static void play(ThingActions actions, String contextUri, @Nullable String deviceId) {
87         ((SpotifyActions) actions).play(contextUri, deviceId, 0, 0);
88     }
89
90     /**
91      * Play a context uri (track or other) on the current active device (if null is passed for deviceID) or the given
92      * device at the given offset and/or position in milliseconds.
93      *
94      * @param actions Spotify Actions object.
95      * @param contextUri context uri (track or other)
96      * @param deviceId Id of the device to play on, or current device if given null
97      * @param offset Offset in the list, default 0.
98      * @param positionMs position in the track in milliseconds, default 0,
99      */
100     public static void play(ThingActions actions, String contextUri, @Nullable String deviceId, final int offset,
101             final int positionMs) {
102         ((SpotifyActions) actions).play(contextUri, deviceId, positionMs, positionMs);
103     }
104
105     @Override
106     public void setThingHandler(ThingHandler handler) {
107         this.handler = handler;
108     }
109
110     @Override
111     public @Nullable ThingHandler getThingHandler() {
112         return handler;
113     }
114 }