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