package sonos;
import java.io.IOException;
import common.Sender;
import common.TrackMetaData;
import net.sbbi.upnp.Discovery;
import net.sbbi.upnp.ServicesEventing;
import net.sbbi.upnp.ServiceEventHandler;
import net.sbbi.upnp.devices.UPNPDevice;
import net.sbbi.upnp.devices.UPNPRootDevice;
import net.sbbi.upnp.services.UPNPService;
public class SonosScraper
implements ServiceEventHandler {
static int counter; //used for determining when to resubscribe. can't do more than 4 events
static int fakecounter; //used for distinguishing song events in debug info
static UPNPDevice mrDevice;
static UPNPService avtService;
static SonosScraper handler;
static String pausexml; //empty if not paused. for handling the pause event send
static String pausestring;
public void handleStateVariableEvent( String varName, String newValue ) {
if (counter % 4 != 0)
{
// basically if there is a pause. don't do an event print. but load it into the pause string
//else if no pause(HANDLED from above) and pausexml is not blank, compare with new xml to see if same song.
//if same song, don't do new event. else print new event. all cases clear pausexml.
//else print song
//System.out.println(pausexml);
if (newValue.contains(pausestring))
{
//System.out.println("BLOCK 1 -- "+counter);
pausexml = newValue;
}
else if (pausexml!="")
{
//System.out.println("BLOCK 2 -- "+counter);
String tempcompare1 = newValue.substring(newValue.indexOf("<CurrentTrackURI"),newValue.indexOf("/><CurrentTrackDuration"));
String tempcompare2 = pausexml.substring(pausexml.indexOf("<CurrentTrackURI"),pausexml.indexOf("/><CurrentTrackDuration"));
//System.out.println("compare\n"+tempcompare1+'\n'+tempcompare2+'\n'+"compare");
if (!tempcompare1.equals(tempcompare2))
{
//System.out.println("BLOCK 4");
System.out.println("New Song Event -- "+fakecounter++);
//System.out.println( "State variable " + varName + " changed to " + newValue );System.out.println("SUCCESSSSSS!!!!!");
sendtosite(newValue);
}
pausexml = "";
}
else
{
//System.out.println("BLOCK 3 -- "+counter);
System.out.println("New Song Event -- "+fakecounter++);
//System.out.println( "State variable " + varName + " changed to " + newValue );System.out.println("SUCCESSSSSS!!!!!");
sendtosite(newValue);
}
counter++;
}
else
{
counter++;
ServicesEventing instance = ServicesEventing.getInstance();
try {
instance.unRegister(avtService, handler);
instance.register( avtService, handler, 86400 );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// takes in AVTransport event xml, and leads to the parsing of this and sending to the site
public static void sendtosite(String sonosxml)
{
String fullsonosxml = sonosxml.replaceAll(""", '"'+"").replaceAll("<", "<").replaceAll(">",">").replaceAll("&", "&").replaceAll("'", "'");
String newsonosxml = fullsonosxml.substring(fullsonosxml.indexOf("<DIDL-Lite"),fullsonosxml.indexOf("DIDL-Lite>")+10);
newsonosxml = newsonosxml.replaceAll(""", '"'+"").replaceAll("<", "<").replaceAll(">",">").replaceAll("&", "&").replaceAll("'", "'");
// System.out.println(newsonosxml);
TrackMetaData tmd = XMLParser.parseTrackMetaData(newsonosxml);
Sender sender = Sender.getXMLSender();
sender.sendDataToSite(tmd);
}
public static void main( String[] args ) {
counter = 1;
fakecounter = 1;
pausexml = "";
pausestring = "<TransportState val=\"PAUSED_PLAYBACK\"/>";
ServicesEventing instance = ServicesEventing.getInstance();
handler = new SonosScraper();
instance.setDaemon( false );
// let's find a device
UPNPRootDevice[] devices;
try {
devices = Discovery.discover(Constants.SONOS_DEVICE_TYPE);
if (devices != null) {
for (UPNPRootDevice device : devices) {
System.out.println("Device found: " + device.getFriendlyName());
}
} else {
System.out.println("No devices found");
}
// System.out.println( devices[0].getChildDevice(Constants.MEDIA_RENDERER_DEVICE_TYPE).getFriendlyName());
if ( devices != null ) {
mrDevice = (UPNPDevice)devices[0].getChildDevice(Constants.MEDIA_RENDERER_DEVICE_TYPE);
avtService = (UPNPService)mrDevice.getService(Constants.SONOS_SERVICE_AV_TRANSPORT);
System.out.println(avtService.getServiceId());
try {
int duration = instance.register( avtService, handler, 86400 );
// ServiceEventSubscription sub= instance.registerEvent(avtService, handler, -1);
if ( duration != -1 ) {
System.out.println( "State variable events registered for " + duration + " seconds" );
}
} catch ( IOException ex ) {
ex.printStackTrace( System.err );
// comm error during registration with device such as timeoutException
}
} else {
System.out.println( "Unable to find devices" );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}