/** * Send command to Plex * * @param config The binding configuration for the item * @param command Command to send * @throws IOException When it's not possible to send HTTP GET command */ public void sendCommand(PlexBindingConfig config, Command command) throws IOException { String cmd = null; String property = config.getProperty(); if (property.equals(PlexProperty.VOLUME.getName())) { cmd = getVolumeCommand(config, command); } else if (property.equals(PlexProperty.PROGRESS.getName())) { cmd = getProgressCommand(config, command); } else if (property.equals(PlexProperty.PLAYPAUSE.getName())) { cmd = getPlayPauseCommand(config); } else { cmd = config.getProperty(); } if (cmd != null) { Server host = getHost(config.getMachineIdentifier()); if (host != null && !isBlank(host.getHost())) { String uri = String.format("http://%s:%s/player/%s", host.getHost(), host.getPort(), cmd); uri = appendParametersForCommand(uri, config.getMachineIdentifier()); internalSendCommand(config.getMachineIdentifier(), uri); } else { logger.debug( "Cannot send command, host is unknown for machine ID {}", config.getMachineIdentifier()); } } }
private String getProgressCommand(PlexBindingConfig config, Command command) { PlexSession session = getSessionByMachineId(config.getMachineIdentifier()); String url = null; if (session != null) { int offset = 0; if (command.getClass().equals(PercentType.class)) { PercentType percent = (PercentType) command; offset = new BigDecimal(session.getDuration()) .multiply( percent .toBigDecimal() .divide(new BigDecimal("100"), new MathContext(5, RoundingMode.HALF_UP))) .intValue(); offset = Math.max(0, offset); offset = Math.min(session.getDuration(), offset); url = String.format("playback/seekTo?offset=%d", offset); } else if (command.getClass().equals(IncreaseDecreaseType.class)) { if (command.equals(IncreaseDecreaseType.DECREASE)) { url = PlexProperty.STEP_BACK.getName(); } else { url = PlexProperty.STEP_FORWARD.getName(); } } } return url; }
private String getVolumeCommand(PlexBindingConfig config, Command command) { int newVolume = 100; PlexSession session = getSessionByMachineId(config.getMachineIdentifier()); if (session != null) { newVolume = session.getVolume(); } if (command.getClass().equals(PercentType.class)) { PercentType percentType = (PercentType) command; newVolume = percentType.intValue(); } else if (command.getClass().equals(IncreaseDecreaseType.class)) { if (command.equals(IncreaseDecreaseType.DECREASE)) { newVolume = Math.max(0, newVolume - VOLUME_STEP); } else { newVolume = Math.min(100, newVolume + VOLUME_STEP); } } if (session != null) { session.setVolume(newVolume); callback.updateReceived(session); } String url = String.format("playback/setParameters?volume=%d", newVolume); return url; }
private String getPlayPauseCommand(PlexBindingConfig config) { String command = PlexProperty.PAUSE.getName(); PlexSession session = getSessionByMachineId(config.getMachineIdentifier()); if (session != null) { if (PlexPlayerState.Paused.equals(session.getState())) { command = PlexProperty.PLAY.getName(); } } return command; }