Ex2: WoTServer.java

1   /*
2    * WoTServer.java
3    *
4    * Created on Apr 23, 2010 4:43:14 PM;
5    */
6   
7   package org.sunspotworld.demo;
8   
9   import com.sun.spot.wot.NanoAppServer;
10  import com.sun.spot.wot.TCP6Handler;
11  import com.sun.spot.wot.UDP6Handler;
12  import com.sun.spot.peripheral.ISleepManager;
13  import com.sun.spot.peripheral.Spot;
14  import com.sun.spot.resources.Resources;
15  import com.sun.spot.resources.transducers.ISwitch;
16  import java.io.IOException;
17  import javax.microedition.midlet.MIDlet;
18  import javax.microedition.midlet.MIDletStateChangeException;
19  
20  /**
21   * The startApp method of this class is called by the VM to start the
22   * application.
23   * 
24   * The manifest specifies this class as MIDlet-1, which means it will
25   * be selected for execution.
26   */
27  public class WoTServer extends MIDlet {
28      protected void startApp() throws MIDletStateChangeException {
29  
30          try {
31              // Deep sleep is disabled for this exercise so the SPOTs are
32              // always reachable from the Gateway
33              System.out.println("***** Disabling deep sleep *****");
34              ISleepManager sleepManager = Spot.getInstance().getSleepManager();
35              sleepManager.disableDeepSleep();
36              // This will make sure the SPOT is configured as an ENDNODE the next time it is rebooted.
37              Spot.getInstance().setPersistentProperty("spot.mesh.routing.enable", "ENDNODE");
38  
39              // We create an instance of the NanoAppServer and register
40              // web applications mapped to specific parts of the server's
41              // URL space
42              NanoAppServer nas = new NanoAppServer();
43  
44              // Pre-existing services include the following ...
45              // ... this one returns status information (e.g. battery, uptime)
46              nas.registerApp("/status", new Status("n=Status\nsh=s\nd=http://bit.ly/8YtWQE"));
47              // ... this one can be used to read/create/modify SPOT properties
48              // We'll use this to set the spot.name property which assigns
49              // a name to the SPOT. Note that one can associate meta information
50              // with a Web Application by passing a string of Java properties
51              // Here, the name is "Properties manager", the short URL is p
52              // i.e. the application can be accessed at /props as well as /p,
53              // and the URL for the description is http://bit.ly/aa8iOZ
54              nas.registerApp("/props", new PropertiesManager("n=Properties\nsh=p\nd=http://bit.ly/aa8iOZ"));
55              // ... this one can be used to blink LEDs (useful to physically
56              // locate a SPOT with known Id)
57              nas.registerApp("/blink", new Blinker("n=Blink LEDs\nsh=b\nd=http://bit.ly/bPAzq1"));
58  
59              // For this hands-on-lab, we will create two additional web
60              // applications. The first will allow us to access the light sensor 
61              // reading using an HTTP GET. The second will allow us to retrieve 
62              // and monitor the LED settings via HTTP GETs and PUTs.
63              /*
64               * XXX Uncomment these lines after you've fully implemented the 
65               * application. Replace xxxx in the realm specification with the
66               * last 4 hex digits of your SPOT.
67               */
68  //            nas.registerApp("/light", new HOLLightSensor("n=Light sensor\nd=http://bit.ly/aLqyNA"));
69  //            nas.registerApp("/leds", new HOLLEDController("n=LED control\nd=http://bit.ly/9C3rlP\nrealm=spot-xxxx"));
70  
71              if (WoTConstants.SVC_CONNECTION_TYPE.equals("udp")) {
72                  new UDP6Handler(WoTConstants.TCP_UDP_SVCPORT, nas).start();
73              } else {
74                  new TCP6Handler(WoTConstants.TCP_UDP_SVCPORT, nas).start();
75              }
76  
77              System.out.println("Registering switch listener ...");
78              ISwitch sw = (ISwitch) Resources.lookup(ISwitch.class, "SW2");
79              sw.addISwitchListener(new SwitchListener());
80  
81              if (WoTConstants.AUTO_DISCOVERY) {
82                  DiscoveryHandler dh = new DiscoveryHandler();
83                  dh.start();
84                  System.out.println("Adding deep sleep listener ...");
85                  Spot.getInstance().addDeepSleepListener(dh);
86              } else {
87                  System.out.println("***** Autodiscovery is turned off *****");
88              }
89              //new SPOTRHTTPHandler("http://sensor.network.com:1234/warn", nas).start();
90              
91              // *** Do not use these for the Hands-on-lab at JavaOne 2010 ***
92              // and a few other to access temperature, energy, memory and app
93              // information
94  //            nas.registerApp("/temp", new TempSensor("n=Access temperature readings\nsh=t\nd=http://bit.ly/azWwbW"));
95  //            nas.registerApp("/mem", new MemorySensor("n=Access Memory information\nsh=m\nd=http://bit.ly/boIMfV"));
96  //            nas.registerApp("/energy", new EnergySensor("n=Access energy information\nd=http://bit.ly/9hA5Ai"));
97  //            nas.registerApp("/apps", new AppManager("n=Application manager\nd=http://bit.ly/b7Jdrv"));
98  //            nas.setDefaultApp(new DefaultApp("n=Default app"));
99  //          // This is the original app for LED control (finer grained)  
100 //            nas.registerApp("/leds", new LEDController("n=Monitor/control LEDs\nd=http://bit.ly/9C3rlP\nrealm=LEDLord"));
101 
102         } catch (IOException ex) {
103             ex.printStackTrace();
104         }
105     }
106 
107     protected void pauseApp() {
108     }
109 
110     protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
111     }
112 }
113