2 * Copyright (c) 2010-2023 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.net.URLEncoder;
16 import java.nio.charset.StandardCharsets;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * The {@link HeosCommands} provides the available commands for the HEOS network.
24 * @author Johannes Einig - Initial contribution
27 public class HeosCommands {
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";
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=";
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=";
67 // Player Commands get Information
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=";
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=";
86 public static String registerChangeEventOn() {
87 return REGISTER_CHANGE_EVENT_ON;
90 public static String registerChangeEventOff() {
91 return REGISTER_CHANGE_EVENT_OFF;
94 public static String heosAccountCheck() {
95 return HEOS_ACCOUNT_CHECK;
98 public static String setPlayStatePlay(String pid) {
99 return setPlayStatePlay + pid + "&state=play";
102 public static String setPlayStatePause(String pid) {
103 return setPlayStatePause + pid + "&state=pause";
106 public static String setPlayStateStop(String pid) {
107 return setPlayStateStop + pid + "&state=stop";
110 public static String volumeUp(String pid) {
111 return volumeUp + pid + "&step=1";
114 public static String volumeDown(String pid) {
115 return volumeDown + pid + "&step=1";
118 public static String setMuteOn(String pid) {
119 return setMute + pid + "&state=on";
122 public static String setMuteOff(String pid) {
123 return setMute + pid + "&state=off";
126 public static String setMuteToggle(String pid) {
127 return setMuteToggle + pid + "&state=off";
130 public static String setShuffleMode(String pid, String shuffle) {
131 return setPlayMode + pid + "&shuffle=" + shuffle;
134 public static String setRepeatMode(String pid, String repeat) {
135 return setPlayMode + pid + "&repeat=" + repeat;
138 public static String getPlayMode(String pid) {
139 return getPlayMode + pid;
142 public static String playNext(String pid) {
143 return playNext + pid;
146 public static String playPrevious(String pid) {
147 return playPrevious + pid;
150 public static String setVolume(String vol, String pid) {
151 return setVolume + pid + "&level=" + vol;
154 public static String getPlayers() {
158 public static String getPlayerInfo(String pid) {
159 return getPlayerInfo + pid;
162 public static String getPlayState(String pid) {
163 return getPlayState + pid;
166 public static String getNowPlayingMedia(String pid) {
167 return getNowPlayingMedia + pid;
170 public static String getVolume(String pid) {
171 return playerGetVolume + pid;
174 public static String getMusicSources() {
175 return getMusicSources;
178 public static String prettifyJSONon() {
179 return prettifyJSONon;
182 public static String prettifyJSONoff() {
183 return prettifyJSONoff;
186 public static String getMute(String pid) {
187 return playerGetMute + pid;
190 public static String getQueue(String pid) {
191 return getQueue + pid;
194 public static String getQueue(String pid, int start, int end) {
195 return getQueue(pid) + "&range=" + start + "," + end;
198 public static String playQueueItem(String pid, String qid) {
199 return playQueueItem + pid + "&qid=" + qid;
202 public static String deleteQueueItem(String pid, String qid) {
203 return deleteQueueItem + pid + "&qid=" + qid;
206 public static String browseSource(String sid) {
207 return browseSource + sid;
210 public static String playStream(String pid) {
211 return playStream + pid;
214 public static String addToQueue(String pid) {
215 return addToQueue + pid;
218 public static String addContainerToQueuePlayNow(String pid, String sid, String cid) {
219 return addToQueue + pid + "&sid=" + sid + "&cid=" + cid + "&aid=1";
222 public static String clearQueue(String pid) {
223 return clearQueue + pid;
226 public static String rebootSystem() {
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;
234 newCommand = newCommand + pid;
237 newCommand = newCommand + "&sid=" + sid;
240 newCommand = newCommand + "&cid=" + cid;
243 newCommand = newCommand + "&mid=" + mid;
246 newCommand = newCommand + "&name=" + name;
251 public static String playStream(String pid, String sid, String mid) {
252 return playStream + pid + "&sid=" + sid + "&mid=" + mid;
255 public static String playInputSource(String des_pid, String source_pid, String input) {
256 return playInputSource + des_pid + "&spid=" + source_pid + "&input=inputs/" + input;
259 public static String playURL(String pid, String url) {
260 return playURL + pid + "&url=" + url;
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;
269 public static String signOut() {
273 public static String heartbeat() {
277 public static String getGroups() {
281 public static String getGroupInfo(String gid) {
282 return getGroupsInfo + gid;
285 public static String setGroup(String[] gid) {
286 String players = String.join(",", gid);
288 return setGroup + players;
291 public static String getGroupVolume(String gid) {
292 return getGroupVolume + gid;
295 public static String setGroupVolume(String volume, String gid) {
296 return setGroupVolume + gid + "&level=" + volume;
299 public static String setGroupVolumeUp(String gid) {
300 return groupVolumeUp + gid + "&step=1";
303 public static String setGroupVolumeDown(String gid) {
304 return groupVolumeDown + gid + "&step=1";
307 public static String getGroupMute(String gid) {
308 return getGroupMute + gid;
311 public static String setGroupMuteOn(String gid) {
312 return setGroupMute + gid + "&state=on";
315 public static String setGroupMuteOff(String gid) {
316 return setGroupMute + gid + "&state=off";
319 public static String getToggleGroupMute(String gid) {
320 return toggleGroupMute + gid;
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", "@");