I modified a few file: Controller Module, Driver, LED Flash, and Sensor Module. Since I just installed a new OS, I can not commit anything yet, but I also did not want to commit this because it will probably break some code. Check it out and if you like it , I will commit it.
package org.apparatus_templi; public abstract class ControllerModule extends Driver { /* * Abstract methods to be implemented in subclass */ public abstract String getControllerListXML(); public abstract String getControllerStatusXML(String controllerName); public abstract void tellController(String controllerName, String command); public void begin_execution() { this.setType("Controller"); setRunning(true);// set the thread state to running this.will_run();//invoke the thread's will_run method in the subclass /* * Check to see if the subclass assigned a name and protocol version to the thread * if not, assign random string as name then default protocol. */ if(deviceName.equals(" ")) { this.setName("Controller " + counter); //just a place holder for random name because a name was not assigned ot it. counter++; //names will be given out as "Senser 0", Sensor 1", and so fourth. } if(protocol_version.equals(" ") { protocol_version("0.0"); //user did not assign protocol version. Assign default version of 0.0 } this.run(); //then invoke the thread's run method inherited from the java api to fire off the thread } }
package org.apparatus_templi; public abstract class Driver extends Runnable { protected String type; //Controller or Sensor type. protected String name = " "; //name of the device Empty String as default or could assign a random name if not assigned one public volatile int counter = 0; //used to assign the threads random names if not explicitly given one protected String protocol_version =" "; //this current operating protocol verison the device is running protected boolean running = false; public abstract String getModuleType(); public abstract String getModuleName(); public abstract void receiveCommand(String command); public abstract String getWidgetXML(); public abstract String getFullPageXML(); public abstract void will_run(); //let the thread know it is about to run /* * Let the subclass know it is about to terminate. * The subclass can do any extra serialization or whatever it needs * to do before terminating. */ public abstract void will_terminate(); /* * This is the make shift constructor for the thread. All of the necessary setup * can be done in this method before invoking the run command. */ public void begin_execution(); public void terminate() { this.will_terminate(); //call the subclass implementation } /* * If the device is not given a name during creation, it will have an empty string as default * The thread's begin_execution() function (make shift constructor) can handle this. It can assign it a random * name. */ public void setName(String device_name) { this.name = device_name; } public String deviceName() { return this.name; } public void isRunning() { return this.running; } public void setRunning(boolean run) { this.running = run; } /* * The devices' current running protocol verison. I.e (0.0 or 0.1) and so fourth */ public void protocol_version(String version) { this.protocol_version = version; } }
package org.apparatus_templi; public abstract class SensorModule extends Driver { /* * Abstract methods to be implemented in subclass */ public abstract String getSensorList(); public abstract String getSensorData(String sensorName); public void begin_execution() { this.setType("sensor"); setRunning(true);// set the thread state to running this.will_run();//invoke the thread's will_run method in the subclass /* * Check to see if the subclass assigned a name to the thread * if not, assign random string as name. */ if(deviceName.equals(" ")) { this.setName("Sensor " + counter); //just a place holder for random name because a name was not assigned ot it. counter++; //names will be given out as "Senser 0", Sensor 1", and so fourth. } if(protocol_version.equals(" ") { protocol_version("0.0"); //user did not assign protocol version. Assign default version of 0.0 } this.run(); //then invoke the thread's run method inherited from the java api to fire off the thread } }
package org.apparatus_templi; /** * LedFlash * Controls a remote array of LED pixels. * * Remote side expects commands in the form of: * "(int)" * where (int) is the single digit value of the pin number to power. * Valid values are 4 - 9 * * Driver does not listen for any responses * @author Jonathan Nelson <ciasaboark@xxxxxxxxx> */ public class LedFlash implements ControllerModule { public LedFlash() { } public LedFlash(String message) { } publlic void will_run() { this.setName("Led Flash"); this.protocol_verison("0.0"); } /* * Since every driver is exited to have intimate knowledge of * the remote module that it corresponds with this can be a hard * coded XML response. */ @Override public String getControllerListXML() { Log.d(moduleName, "getControllerListXML() returning hard coded value for now"); return new String( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<controlerList>" + "<controler>" + "<name>LED 1</name>" + "</controler>" + "<controler>" + "<name>LED 2</name>" + "</controler>" + "<controler>" + "<name>LED 1</name>" + "</controler>" + "</controlerList>"); } /* * Since our simple driver does not keep track of the status of the LEDs * it can only respond with 'Unknown' for the status. If this were a real * driver you would want to check that the controlerName is valid, and * return a response based of of the controller's last known status. */ @Override public String getControllerStatusXML(String controllerName) { Log.d(moduleName, "getControllerStatusXML() returning hard coded value for now"); return new String( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<controller>" + "<name>" + controllerName + "</name>" + "<status>Unknown</status>" + "</controller>"); } /* * This method will be called by the Coordinator on behalf * of the front-ends. It is up to you do validate the incoming * controllerName and command (if desired). In this case we will * switch based off the controllerName. Since the only thing that * this driver does is flash the LED we do not even have to check the * command. * * Note that the commands being send are very simple (a single digit * number corresponding to a pin on the arduino). A more complex * driver might send multiple pin numbers at a time, or even a length * of time in which to flash. The format of the command is free form * text, but steer clear of using the line feed character for now. */ @Override public void tellController(String controllerName, String command) { switch (controllerName) { case "LED 1": Coordinator.sendCommand(moduleName, "4"); break; case "LED 2": Coordinator.sendCommand(moduleName, "5"); break; case "LED 3": Coordinator.sendCommand(moduleName, "6"); break; default: Log.e(moduleName, "tellController() Given invalid LED name"); break; } } /* * Our starting point of execution for this driver. * This simply runs a loop sending commands to flash LEDs attached to * different pins. The driver does not care about responses from the * remote module, so receiveMessage() is left unimplemented. If the * Coordinator needs to shut down this thread it can do so through the * terminate() method, which will make the while loop exit * * The startup procedure is very simple, ask the Coordinator if the * remote module is active. If it isn't, then terminate, else begin sending * commands */ @Override public void run() { Log.d(moduleName, "starting"); if (Coordinator.isModulePresent(moduleName)) { while (running) { /* * This is our main loop. All of the processing will happen here * Our simple driver will repeatedly send three messages to the * remote module, sleeping 5 seconds between each message. */ for (int i = 3; i < 10; i++) { Log.d(moduleName, "flashing LED on pin " + i); Coordinator.sendCommand(moduleName, String.valueOf(i)); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } else { Log.w(moduleName, "remote module not present, shutting down"); } } /* * We don't care about any response messages. */ @Override public void receiveCommand(String command) { Log.d(moduleName, "received command, ignoring"); } /* * The XML format for the widget needs to be standardized */ @Override public String getWidgetXML() { Log.w(moduleName, "getWidgetXML() unimplimented"); return null; } /* * The XML format for the full page control needs to be standardized */ @Override public String getFullPageXML() { Log.w(moduleName, "getFullPageXML() unimplimented"); return null; } }