]> git.basschouten.com Git - openhab-addons.git/blob
e12d48ab33cf97eb766e68a3108537d47f6bb4a3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.resources;
14
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * The {@link HeosCommands} provides the available commands for the HEOS network.
24  *
25  * @author Johannes Einig - Initial contribution
26  */
27 @NonNullByDefault
28 public class HeosCommands {
29
30     // System Commands
31     private static final String REGISTER_CHANGE_EVENT_ON = "heos://system/register_for_change_events?enable=on";
32     private static final String REGISTER_CHANGE_EVENT_OFF = "heos://system/register_for_change_events?enable=off";
33     private static final String HEOS_ACCOUNT_CHECK = "heos://system/check_account";
34     private static final String prettifyJSONon = "heos://system/prettify_json_response?enable=on";
35     private static final String prettifyJSONoff = "heos://system/prettify_json_response?enable=off";
36     private static final String rebootSystem = "heos://system/reboot";
37     private static final String signOut = "heos://system/sign_out";
38     private static final String heartbeat = "heos://system/heart_beat";
39
40     // Player Commands Control
41     private static final String setPlayStatePlay = "heos://player/set_play_state?pid=";
42     private static final String setPlayStatePause = "heos://player/set_play_state?pid=";
43     private static final String setPlayStateStop = "heos://player/set_play_state?pid=";
44     private static final String setVolume = "heos://player/set_volume?pid=";
45     private static final String volumeUp = "heos://player/volume_up?pid=";
46     private static final String volumeDown = "heos://player/volume_down?pid=";
47     private static final String setMute = "heos://player/set_mute?pid=";
48     private static final String setMuteToggle = "heos://player/toggle_mute?pid=";
49     private static final String playNext = "heos://player/play_next?pid=";
50     private static final String playPrevious = "heos://player/play_previous?pid=";
51     private static final String playQueueItem = "heos://player/play_queue?pid=";
52     private static final String clearQueue = "heos://player/clear_queue?pid=";
53     private static final String deleteQueueItem = "heos://player/remove_from_queue?pid=";
54     private static final String setPlayMode = "heos://player/set_play_mode?pid=";
55
56     // Group Commands Control
57     private static final String getGroups = "heos://group/get_groups";
58     private static final String getGroupsInfo = "heos://group/get_group_info?gid=";
59     private static final String setGroup = "heos://group/set_group?pid=";
60     private static final String getGroupVolume = "heos://group/get_volume?gid=";
61     private static final String setGroupVolume = "heos://group/set_volume?gid=";
62     private static final String getGroupMute = "heos://group/get_mute?gid=";
63     private static final String setGroupMute = "heos://group/set_mute?gid=";
64     private static final String toggleGroupMute = "heos://group/toggle_mute?gid=";
65     private static final String groupVolumeUp = "heos://group/volume_up?gid=";
66     private static final String groupVolumeDown = "heos://group/volume_down?gid=";
67
68     // Player Commands get Information
69
70     private static final String getPlayers = "heos://player/get_players";
71     private static final String getPlayerInfo = "heos://player/get_player_info?pid=";
72     private static final String getPlayState = "heos://player/get_play_state?pid=";
73     private static final String getNowPlayingMedia = "heos://player/get_now_playing_media?pid=";
74     private static final String playerGetVolume = "heos://player/get_volume?pid=";
75     private static final String playerGetMute = "heos://player/get_mute?pid=";
76     private static final String getQueue = "heos://player/get_queue?pid=";
77     private static final String getPlayMode = "heos://player/get_play_mode?pid=";
78
79     // Browse Commands
80     private static final String getMusicSources = "heos://browse/get_music_sources";
81     private static final String browseSource = "heos://browse/browse?sid=";
82     private static final String playStream = "heos://browse/play_stream?pid=";
83     private static final String addToQueue = "heos://browse/add_to_queue?pid=";
84     private static final String playInputSource = "heos://browse/play_input?pid=";
85     private static final String playURL = "heos://browse/play_stream?pid=";
86
87     public static String registerChangeEventOn() {
88         return REGISTER_CHANGE_EVENT_ON;
89     }
90
91     public static String registerChangeEventOff() {
92         return REGISTER_CHANGE_EVENT_OFF;
93     }
94
95     public static String heosAccountCheck() {
96         return HEOS_ACCOUNT_CHECK;
97     }
98
99     public static String setPlayStatePlay(String pid) {
100         return setPlayStatePlay + pid + "&state=play";
101     }
102
103     public static String setPlayStatePause(String pid) {
104         return setPlayStatePause + pid + "&state=pause";
105     }
106
107     public static String setPlayStateStop(String pid) {
108         return setPlayStateStop + pid + "&state=stop";
109     }
110
111     public static String volumeUp(String pid) {
112         return volumeUp + pid + "&step=1";
113     }
114
115     public static String volumeDown(String pid) {
116         return volumeDown + pid + "&step=1";
117     }
118
119     public static String setMuteOn(String pid) {
120         return setMute + pid + "&state=on";
121     }
122
123     public static String setMuteOff(String pid) {
124         return setMute + pid + "&state=off";
125     }
126
127     public static String setMuteToggle(String pid) {
128         return setMuteToggle + pid + "&state=off";
129     }
130
131     public static String setShuffleMode(String pid, String shuffle) {
132         return setPlayMode + pid + "&shuffle=" + shuffle;
133     }
134
135     public static String setRepeatMode(String pid, String repeat) {
136         return setPlayMode + pid + "&repeat=" + repeat;
137     }
138
139     public static String getPlayMode(String pid) {
140         return getPlayMode + pid;
141     }
142
143     public static String playNext(String pid) {
144         return playNext + pid;
145     }
146
147     public static String playPrevious(String pid) {
148         return playPrevious + pid;
149     }
150
151     public static String setVolume(String vol, String pid) {
152         return setVolume + pid + "&level=" + vol;
153     }
154
155     public static String getPlayers() {
156         return getPlayers;
157     }
158
159     public static String getPlayerInfo(String pid) {
160         return getPlayerInfo + pid;
161     }
162
163     public static String getPlayState(String pid) {
164         return getPlayState + pid;
165     }
166
167     public static String getNowPlayingMedia(String pid) {
168         return getNowPlayingMedia + pid;
169     }
170
171     public static String getVolume(String pid) {
172         return playerGetVolume + pid;
173     }
174
175     public static String getMusicSources() {
176         return getMusicSources;
177     }
178
179     public static String prettifyJSONon() {
180         return prettifyJSONon;
181     }
182
183     public static String prettifyJSONoff() {
184         return prettifyJSONoff;
185     }
186
187     public static String getMute(String pid) {
188         return playerGetMute + pid;
189     }
190
191     public static String getQueue(String pid) {
192         return getQueue + pid;
193     }
194
195     public static String getQueue(String pid, int start, int end) {
196         return getQueue(pid) + "&range=" + start + "," + end;
197     }
198
199     public static String playQueueItem(String pid, String qid) {
200         return playQueueItem + pid + "&qid=" + qid;
201     }
202
203     public static String deleteQueueItem(String pid, String qid) {
204         return deleteQueueItem + pid + "&qid=" + qid;
205     }
206
207     public static String browseSource(String sid) {
208         return browseSource + sid;
209     }
210
211     public static String playStream(String pid) {
212         return playStream + pid;
213     }
214
215     public static String addToQueue(String pid) {
216         return addToQueue + pid;
217     }
218
219     public static String addContainerToQueuePlayNow(String pid, String sid, String cid) {
220         return addToQueue + pid + "&sid=" + sid + "&cid=" + cid + "&aid=1";
221     }
222
223     public static String clearQueue(String pid) {
224         return clearQueue + pid;
225     }
226
227     public static String rebootSystem() {
228         return rebootSystem;
229     }
230
231     public static String playStream(@Nullable String pid, @Nullable String sid, @Nullable String cid,
232             @Nullable String mid, @Nullable String name) {
233         String newCommand = playStream;
234         if (pid != null) {
235             newCommand = newCommand + pid;
236         }
237         if (sid != null) {
238             newCommand = newCommand + "&sid=" + sid;
239         }
240         if (cid != null) {
241             newCommand = newCommand + "&cid=" + cid;
242         }
243         if (mid != null) {
244             newCommand = newCommand + "&mid=" + mid;
245         }
246         if (name != null) {
247             newCommand = newCommand + "&name=" + name;
248         }
249         return newCommand;
250     }
251
252     public static String playStream(String pid, String sid, String mid) {
253         return playStream + pid + "&sid=" + sid + "&mid=" + mid;
254     }
255
256     public static String playInputSource(String des_pid, String source_pid, String input) {
257         return playInputSource + des_pid + "&spid=" + source_pid + "&input=inputs/" + input;
258     }
259
260     public static String playURL(String pid, String url) {
261         return playURL + pid + "&url=" + url;
262     }
263
264     public static String signIn(String username, String password) {
265         String encodedUsername = urlEncode(username);
266         String encodedPassword = urlEncode(password);
267         return "heos://system/sign_in?un=" + encodedUsername + "&pw=" + encodedPassword;
268     }
269
270     public static String signOut() {
271         return signOut;
272     }
273
274     public static String heartbeat() {
275         return heartbeat;
276     }
277
278     public static String getGroups() {
279         return getGroups;
280     }
281
282     public static String getGroupInfo(String gid) {
283         return getGroupsInfo + gid;
284     }
285
286     public static String setGroup(String[] gid) {
287         String players = String.join(",", gid);
288
289         return setGroup + players;
290     }
291
292     public static String getGroupVolume(String gid) {
293         return getGroupVolume + gid;
294     }
295
296     public static String setGroupVolume(String volume, String gid) {
297         return setGroupVolume + gid + "&level=" + volume;
298     }
299
300     public static String setGroupVolumeUp(String gid) {
301         return groupVolumeUp + gid + "&step=1";
302     }
303
304     public static String setGroupVolumeDown(String gid) {
305         return groupVolumeDown + gid + "&step=1";
306     }
307
308     public static String getGroupMute(String gid) {
309         return getGroupMute + gid;
310     }
311
312     public static String setGroupMuteOn(String gid) {
313         return setGroupMute + gid + "&state=on";
314     }
315
316     public static String setGroupMuteOff(String gid) {
317         return setGroupMute + gid + "&state=off";
318     }
319
320     public static String getToggleGroupMute(String gid) {
321         return toggleGroupMute + gid;
322     }
323
324     private static String urlEncode(String username) {
325         try {
326             String encoded = URLEncoder.encode(username, StandardCharsets.UTF_8.toString());
327             // however it cannot handle escaped @ signs
328             return encoded.replace("%40", "@");
329         } catch (UnsupportedEncodingException e) {
330             throw new IllegalStateException("UTF-8 is not supported, bailing out");
331         }
332     }
333 }