logger.log(Level.SEVERE, "Device ID is not valid: " + deviceId);
sendErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Device ID is not valid: " + deviceId, baseRequest, response);
}
else
{
DeviceConnection deviceConnection = DeviceConnectionService.getConnection(device);
DeviceResponse deviceResponse = deviceConnection.sendCommand(new PlayCommand(URLDecoder.decode(streamURL, "utf-8"), 0.0));
if (deviceResponse.getResponseCode() == 200)
{
sendOKResponse(baseRequest, response);
}
else
{
sendErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, deviceResponse.getResponseMessage(), baseRequest, response);
}
}
}
break;
case 3: // Stop stream
{
String deviceId = request.getParameter("d");
Device device = deviceMap.get(deviceId);
DeviceConnection deviceConnection = DeviceConnectionService.getConnection(device);
DeviceResponse deviceResponse = deviceConnection.sendCommand(new StopCommand());
if (deviceResponse.getResponseCode() == 200)
{
sendOKResponse(baseRequest, response);
}
else
{
sendErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, deviceResponse.getResponseMessage(), baseRequest, response);
}
}
break;
case 4: // Get current content play state
{
String deviceId = request.getParameter("d");
Device device = deviceMap.get(deviceId);
DeviceConnection deviceConnection = DeviceConnectionService.getConnection(device);
DeviceResponse deviceResponse = deviceConnection.sendCommand(new ScrubCommand());
if (deviceResponse.getResponseCode() == 200)
{
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
Map<String, Object> responseMap = new HashMap<String, Object>();
responseMap.put("responseCode", "OK");
responseMap.put("duration", deviceResponse.getContentParameterMap().get("duration"));
responseMap.put("position", deviceResponse.getContentParameterMap().get("position"));
response.getWriter().println(JSON.toString(responseMap));
}
else
{
logger.log(Level.INFO, "Error response: code=" + deviceResponse.getResponseCode() + " message=" + deviceResponse.getResponseMessage());
sendErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, deviceResponse.getResponseMessage(), baseRequest, response);
}
}
break;
case 5: // Change the rate of playback
{
String deviceId = request.getParameter("d");
double rate = 0.0;
boolean rateParsed = false;
try
{
rate = Double.parseDouble(request.getParameter("r"));
rateParsed = true;
}
catch (NumberFormatException e)
{
logger.log(Level.INFO, "Could not parse rate parameter: " + request.getParameter("r"));
sendErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Rate is not valid.", baseRequest, response);
}
if(rateParsed)
{
Device device = deviceMap.get(deviceId);
DeviceConnection deviceConnection = DeviceConnectionService.getConnection(device);
DeviceResponse deviceResponse = deviceConnection.sendCommand(new RateCommand(rate));
if (deviceResponse.getResponseCode() == 200)
{
sendOKResponse(baseRequest, response);
}
else
{
sendErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, deviceResponse.getResponseMessage(), baseRequest, response);
}
}
}
break;
case 6: // Scrub to the given location in the stream
{
String deviceId = request.getParameter("d");
boolean scrubToParsed = false;
double scrubTo = 0.0;
try
{
scrubTo = Double.parseDouble(request.getParameter("p"));
scrubToParsed = true;
}
catch (NumberFormatException e)
{
logger.log(Level.INFO, "Could not parse position parameter: " + request.getParameter("p"));
sendErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Position is not valid.", baseRequest, response);
}
if(scrubToParsed)
{
Device device = deviceMap.get(deviceId);
DeviceConnection deviceConnection = DeviceConnectionService.getConnection(device);
DeviceResponse deviceResponse = deviceConnection.sendCommand(new ScrubCommand(scrubTo));
if (deviceResponse.getResponseCode() == 200)
{
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);