use of org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDCurrentStatus in project malp by gateship-one.
the class MPDResponseParser method parseMPDCurrentStatus.
static MPDCurrentStatus parseMPDCurrentStatus(final MPDConnection connection) throws MPDException {
MPDCurrentStatus status = new MPDCurrentStatus();
if (!connection.isConnected()) {
return status;
}
/* Response line from MPD */
String response = connection.readLine();
while (response != null && !response.startsWith("OK")) {
if (response.startsWith(MPDResponses.MPD_RESPONSE_VOLUME)) {
try {
status.setVolume(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_VOLUME.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_REPEAT)) {
try {
status.setRepeat(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_REPEAT.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_RANDOM)) {
try {
status.setRandom(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_RANDOM.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_SINGLE)) {
try {
status.setSinglePlayback(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_SINGLE.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_CONSUME)) {
try {
status.setConsume(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_CONSUME.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_PLAYLIST_VERSION)) {
try {
status.setPlaylistVersion(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_PLAYLIST_VERSION.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_PLAYLIST_LENGTH)) {
try {
status.setPlaylistLength(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_PLAYLIST_LENGTH.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_PLAYBACK_STATE)) {
String state = response.substring(MPDResponses.MPD_RESPONSE_PLAYBACK_STATE.length());
switch(state) {
case MPDResponses.MPD_PLAYBACK_STATE_RESPONSE_PLAY:
status.setPlaybackState(MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PLAYING);
break;
case MPDResponses.MPD_PLAYBACK_STATE_RESPONSE_PAUSE:
status.setPlaybackState(MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PAUSING);
break;
case MPDResponses.MPD_PLAYBACK_STATE_RESPONSE_STOP:
status.setPlaybackState(MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_STOPPED);
break;
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_CURRENT_SONG_INDEX)) {
try {
status.setCurrentSongIndex(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_CURRENT_SONG_INDEX.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_NEXT_SONG_INDEX)) {
try {
status.setNextSongIndex(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_NEXT_SONG_INDEX.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_TIME_INFORMATION_OLD)) {
String timeInfo = response.substring(MPDResponses.MPD_RESPONSE_TIME_INFORMATION_OLD.length());
String[] timeInfoSep = timeInfo.split(":");
if (timeInfoSep.length == 2) {
try {
status.setElapsedTime(Integer.valueOf(timeInfoSep[0]));
status.setTrackLength(Integer.valueOf(timeInfoSep[1]));
} catch (NumberFormatException ignored) {
}
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_ELAPSED_TIME)) {
try {
status.setElapsedTime(Math.round(Float.valueOf(response.substring(MPDResponses.MPD_RESPONSE_ELAPSED_TIME.length()))));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_DURATION)) {
try {
status.setTrackLength(Math.round(Float.valueOf(response.substring(MPDResponses.MPD_RESPONSE_DURATION.length()))));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_BITRATE)) {
try {
status.setBitrate(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_BITRATE.length())));
} catch (NumberFormatException ignored) {
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_AUDIO_INFORMATION)) {
String audioInfo = response.substring(MPDResponses.MPD_RESPONSE_AUDIO_INFORMATION.length());
String[] audioInfoSep = audioInfo.split(":");
if (audioInfoSep.length == 3) {
/* Extract the separate pieces */
try {
/* First is sampleRate */
status.setSamplerate(Integer.valueOf(audioInfoSep[0]));
/* Second is bitresolution */
status.setBitDepth(audioInfoSep[1]);
/* Third is channel count */
status.setChannelCount(Integer.valueOf(audioInfoSep[2]));
} catch (NumberFormatException ignored) {
}
}
} else if (response.startsWith(MPDResponses.MPD_RESPONSE_UPDATING_DB)) {
try {
status.setUpdateDBJob(Integer.valueOf(response.substring(MPDResponses.MPD_RESPONSE_UPDATING_DB.length())));
} catch (NumberFormatException ignored) {
}
}
response = connection.readLine();
}
return status;
}
Aggregations