2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.heos.internal.resources;
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLEncoder;
17 import java.nio.charset.StandardCharsets;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
23 * The {@link HeosCommands} provides the available commands for the HEOS network.
25 * @author Johannes Einig - Initial contribution
28 public class HeosCommands {
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";
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=";
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=";
68 // Player Commands get Information
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=";
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=";
87 public static String registerChangeEventOn() {
88 return REGISTER_CHANGE_EVENT_ON;
91 public static String registerChangeEventOff() {
92 return REGISTER_CHANGE_EVENT_OFF;
95 public static String heosAccountCheck() {
96 return HEOS_ACCOUNT_CHECK;
99 public static String setPlayStatePlay(String pid) {
100 return setPlayStatePlay + pid + "&state=play";
103 public static String setPlayStatePause(String pid) {
104 return setPlayStatePause + pid + "&state=pause";
107 public static String setPlayStateStop(String pid) {
108 return setPlayStateStop + pid + "&state=stop";
111 public static String volumeUp(String pid) {
112 return volumeUp + pid + "&step=1";
115 public static String volumeDown(String pid) {
116 return volumeDown + pid + "&step=1";
119 public static String setMuteOn(String pid) {
120 return setMute + pid + "&state=on";
123 public static String setMuteOff(String pid) {
124 return setMute + pid + "&state=off";
127 public static String setMuteToggle(String pid) {
128 return setMuteToggle + pid + "&state=off";
131 public static String setShuffleMode(String pid, String shuffle) {
132 return setPlayMode + pid + "&shuffle=" + shuffle;
135 public static String setRepeatMode(String pid, String repeat) {
136 return setPlayMode + pid + "&repeat=" + repeat;
139 public static String getPlayMode(String pid) {
140 return getPlayMode + pid;
143 public static String playNext(String pid) {
144 return playNext + pid;
147 public static String playPrevious(String pid) {
148 return playPrevious + pid;
151 public static String setVolume(String vol, String pid) {
152 return setVolume + pid + "&level=" + vol;
155 public static String getPlayers() {
159 public static String getPlayerInfo(String pid) {
160 return getPlayerInfo + pid;
163 public static String getPlayState(String pid) {
164 return getPlayState + pid;
167 public static String getNowPlayingMedia(String pid) {
168 return getNowPlayingMedia + pid;
171 public static String getVolume(String pid) {
172 return playerGetVolume + pid;
175 public static String getMusicSources() {
176 return getMusicSources;
179 public static String prettifyJSONon() {
180 return prettifyJSONon;
183 public static String prettifyJSONoff() {
184 return prettifyJSONoff;
187 public static String getMute(String pid) {
188 return playerGetMute + pid;
191 public static String getQueue(String pid) {
192 return getQueue + pid;
195 public static String getQueue(String pid, int start, int end) {
196 return getQueue(pid) + "&range=" + start + "," + end;
199 public static String playQueueItem(String pid, String qid) {
200 return playQueueItem + pid + "&qid=" + qid;
203 public static String deleteQueueItem(String pid, String qid) {
204 return deleteQueueItem + pid + "&qid=" + qid;
207 public static String browseSource(String sid) {
208 return browseSource + sid;
211 public static String playStream(String pid) {
212 return playStream + pid;
215 public static String addToQueue(String pid) {
216 return addToQueue + pid;
219 public static String addContainerToQueuePlayNow(String pid, String sid, String cid) {
220 return addToQueue + pid + "&sid=" + sid + "&cid=" + cid + "&aid=1";
223 public static String clearQueue(String pid) {
224 return clearQueue + pid;
227 public static String rebootSystem() {
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;
235 newCommand = newCommand + pid;
238 newCommand = newCommand + "&sid=" + sid;
241 newCommand = newCommand + "&cid=" + cid;
244 newCommand = newCommand + "&mid=" + mid;
247 newCommand = newCommand + "&name=" + name;
252 public static String playStream(String pid, String sid, String mid) {
253 return playStream + pid + "&sid=" + sid + "&mid=" + mid;
256 public static String playInputSource(String des_pid, String source_pid, String input) {
257 return playInputSource + des_pid + "&spid=" + source_pid + "&input=inputs/" + input;
260 public static String playURL(String pid, String url) {
261 return playURL + pid + "&url=" + url;
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;
270 public static String signOut() {
274 public static String heartbeat() {
278 public static String getGroups() {
282 public static String getGroupInfo(String gid) {
283 return getGroupsInfo + gid;
286 public static String setGroup(String[] gid) {
287 String players = String.join(",", gid);
289 return setGroup + players;
292 public static String getGroupVolume(String gid) {
293 return getGroupVolume + gid;
296 public static String setGroupVolume(String volume, String gid) {
297 return setGroupVolume + gid + "&level=" + volume;
300 public static String setGroupVolumeUp(String gid) {
301 return groupVolumeUp + gid + "&step=1";
304 public static String setGroupVolumeDown(String gid) {
305 return groupVolumeDown + gid + "&step=1";
308 public static String getGroupMute(String gid) {
309 return getGroupMute + gid;
312 public static String setGroupMuteOn(String gid) {
313 return setGroupMute + gid + "&state=on";
316 public static String setGroupMuteOff(String gid) {
317 return setGroupMute + gid + "&state=off";
320 public static String getToggleGroupMute(String gid) {
321 return toggleGroupMute + gid;
324 private static String urlEncode(String username) {
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");