Developers guide

35
Provide initialised references to the Masterswitch Object to be used by Commands to
interact with the hardware
4.4.1 Commands
Two commands were added to the APT Command set using the procedure defined in Appendix
B.4. Given the simplicity of these commands, they only need to override the execute() method.
// Switch Outlet Command
public class MasterswitchSwitchOutletCommand() extends APTCommand{
public MasterswitchSwitchOutletCommand(int outlet, String newState ) {...}
...
public Object execute(Process parent) throws Exception {
String returnVal = null;
try {
Masterswitch ms = JASMasterswitch.getMasterswitch();
returnVal = ms.setOutletState(outlet, newState );
} catch (MasterswitchException e) {
logger.log( Level.INFO, "Masterswitch Command Failed" + e );
e.printStackTrace();
}
return returnVal;
}
}
// Rename Outlet Command
public class MasterswitchRenameOutletCommand() extends APTCommand {
public MasterswitchRenameOutletCommand(int outlet, String newName ) {...}
...
public Object execute(Process parent) throws Exception {
String returnVal = null;
try {
Masterswitch ms = JASMasterswitch.getMasterswitch();
returnVal = ms.setOutletName(outlet, newName );
} catch (MasterswitchException e) {
logger.log( Level.INFO, "Masterswitch Command Failed" + e );
e.printStackTrace();
}
return returnVal;
}
}
Figure 4-4 Masterswitch commands
The new commands are installed as privileged, as shown in Appendix B.4.2. This ensures that the
Masterswitch commands may only be executed by the user who currently has control, i.e. the
privileged user.
// APTServer.java
// Include the Masterswitch commands as privileged Commands
static final Class[] privCmds = {
...
MasterswitchSwitchOutletCommand.class,
MasterswitchRenameOutletCommand.class };
Figure 4-5 Masterswitch privileged commands