/* ============================================================= Power.java Author: Martin Olsson, molsson@csee.usf.edu and Jakob Klamra jklamra@csee.usf.edu This file is part of the power management service. This is a UPnP service that will allow a device to enter power sleep mode by communicating with a proxy. The service has two modes: POWER and SLEEP. The service and proxy communicates by using GENA events. To run the program use: java -classpath [path to Atinav library] Power To power up the device type on, to power down type off, to exit the program type exit. This file contains the main function for the program. It will build the service by providing all strings and names needed for the XML scheme. The it registers the service and lets the Atinav SDK send events, notifications answer discoveries and handle all other UPnP communication. A known bug in the program is that several exceptions is received when the device that runs the program enters power sleep mode. These exceptions are due to the fact that notifications cannot be sent. The program is unable to recover from this error state and must be restarted to be able to send notifications again. The program was developed using the Atinav Java SDK for UPnP Devices. A free trial of the SDK can be downloaded from: http://www.avelink.com/upnp/index.htm You need to install the SDK to be able to run the program. This program was downloaded from http://www.csee.usf.edu/~jklamra/upnp/ and is part of a master thesis about power management in Universal Plug and Play. Please refer to the homepage for known bugs and comments. We do not guarantee the functionality of this program. Use this program at your own risk. ============================================================= */ import java.net.*; import java.util.*; import java.io.*; import com.atinav.upnp.device.*; public class Power{ private static final String SERVICE_ID = "upnp:schemas-power:Powermgmnt:1"; public static void main(String args[])throws Exception{ UPnPService service = null; BufferedReader keyboardInput; keyboardInput = new BufferedReader(new InputStreamReader(System.in)); String newLine; UPnPDeviceFramework instance = null; try{ instance= UPnPDeviceFramework.getInstance(6523); UPnPDevice power = new UPnPDevice("USF","test","0.1","Power management test","USF","Power 0.1"); //power.includePresentation(false); //doesn't work for some reason, gives null pointer exc initialize(power); addService(power); instance.setThreadPool(10); instance.registerRootDeviceToKit(power,1800,4,0,true,true); instance.startFramework(); power.createDescription(System.out); service = power.getServiceById(SERVICE_ID); service.createServiceDescription(System.out); }catch(FrameworkInitializationException ex) { System.out.println(ex.getMessage()); System.out.println("\nPress to quit...\n"); keyboardInput.read(); System.exit(0); } System.out.println("\nType on to power up the device, off to power down and exit to disconnect."); while(true){ newLine = keyboardInput.readLine(); if(newLine.equals("on")){ System.out.println("Device powering up"); if(service.getStateVariableByName("power").getStringValue().equals("true")) System.out.println("Device is already ON"); else service.changeStateVariableValue("power","true",true); } else if(newLine.equals("off")){ System.out.println("Device powering down"); if(service.getStateVariableByName("power").getStringValue().equals("false")) System.out.println("Device is already OFF"); else service.changeStateVariableValue("power","false",true); } else if(newLine.equals("exit")){ System.out.println("Disconnecting"); instance.shutDownFramework(); System.exit(0); } else System.out.println("Wrong command, type on, off or exit."); } } public static void initialize(UPnPDevice pow ){//Does the necessary initialization of the device pow.setModelDescription("Power Management Test"); pow.setManufacturerURL("http://www.csee.usf.edu/~jklamra/upnp"); pow.setModelNumber("1"); pow.setModelURL("http://www.csee.usf.edu/~jklamra/upnp"); pow.setSerialNumber("123"); pow.setUPC(123456789012L); pow.setUDN("7894ddde-65ef-96df-aba4-14565fe85ddd"); } public static void addService(UPnPDevice pow) throws Exception{ UPnPStateVariable sv[] = new UPnPStateVariable[1]; sv[0] = new UPnPStateVariable("power","boolean"); //The state variable holding the expression being evaluated sv[0].setDefaultValue("true"); UPnPOutputArgument outarg = new UPnPOutputArgument("OutputPower",sv[0]); GetPowerStatus getPow = new GetPowerStatus(); getPow.addOutputArgument(outarg); //PowerUp on = new PowerUp(); //PowerDown off = new PowerDown(); UPnPService service = new UPnPService("schemas-power","Powermgmt","1");// The only service of this UPnPDevice service.setServiceId(SERVICE_ID); service.addAction(getPow); //service.addAction(on); //service.addAction(off); service.addStateVariable(sv); pow.addService(service); } }