diff options
| author | Jesse Morgan <jesse@jesterpm.net> | 2012-10-16 08:16:30 -0700 | 
|---|---|---|
| committer | Jesse Morgan <jesse@jesterpm.net> | 2012-10-16 08:16:30 -0700 | 
| commit | 446a26f85474596be21b88dea42778ab25022d67 (patch) | |
| tree | 217d75ee79ebb70c6171ebfc2cde3e8736a9b6e3 /src | |
| parent | c2947cb21ee2170731c34c17a470bddb52c800e9 (diff) | |
Changed UI interface and added command line argument parsing.
Also added an argument to enumerate the serial ports on the system.
Diffstat (limited to 'src')
| -rw-r--r-- | src/net/tuschhcm/routercontrol/SwitcherApp.java | 167 | ||||
| -rw-r--r-- | src/net/tuschhcm/routercontrol/ui/ConsoleUI.java | 21 | ||||
| -rw-r--r-- | src/net/tuschhcm/routercontrol/ui/UserInterface.java | 14 | 
3 files changed, 133 insertions, 69 deletions
| diff --git a/src/net/tuschhcm/routercontrol/SwitcherApp.java b/src/net/tuschhcm/routercontrol/SwitcherApp.java index 648d0b5..99d1907 100644 --- a/src/net/tuschhcm/routercontrol/SwitcherApp.java +++ b/src/net/tuschhcm/routercontrol/SwitcherApp.java @@ -1,8 +1,11 @@  package net.tuschhcm.routercontrol;
 +import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.Map;
 +import gnu.io.CommPortIdentifier;
 +
  import net.tuschhcm.routercontrol.router.Router;
  import net.tuschhcm.routercontrol.router.ShinyBow5544Router;
  import net.tuschhcm.routercontrol.ui.ConsoleUI;
 @@ -10,66 +13,106 @@ import net.tuschhcm.routercontrol.ui.UserInterface;  import net.tuschhcm.routercontrol.ui.UserInterface.Action;
  public class SwitcherApp {
 -	/**
 -	 * The router I control.
 -	 */
 -	private final Router mRouter;
 -	
 -	/**
 -	 * My user interface.
 -	 */
 -	private final UserInterface mUI;
 -	
 -	/**
 -	 * My list of presets.
 -	 */
 -	private final Map<Integer, Preset> mPresets;
 -	
 -	/**
 -	 * Create a switcher app.
 -	 * @param portName the port name for the router.
 -	 */
 -	public SwitcherApp(final String portName) {
 -		mUI = new ConsoleUI();
 -		mRouter = new ShinyBow5544Router(portName);
 -		mPresets = new HashMap<Integer, Preset>();
 -		
 -		// TODO: Load the presets
 -		
 -		// TODO: Setup UI hooks
 -		mUI.setPresetSelectionAction(new Action() {
 -			public void onAction() {
 -				handlePresetSelected();
 -			}
 -		});
 -		
 -		// TODO: Send the presets to the UI
 -	}
 -	
 -	/**
 -	 * Start the application.
 -	 */
 -	public void run() {
 -		mUI.run();
 -	}
 -	
 -	/**
 -	 * Handles a preset selection.
 -	 */
 -	private void handlePresetSelected() {
 -		int selectedPreset = mUI.getSelectedPreset();
 -		// TODO: What to do when the preset is selected
 -	}
 -	
 -	/**
 -	 * Entry-point for the application.
 -	 * @param args Command line arguments
 -	 */
 -	public static void main(String[] args) {
 -		// TODO: Parse command line arguments
 -		String comPort = "COM1";
 -		
 -		SwitcherApp app = new SwitcherApp(comPort);
 -		app.run();
 -	}
 +    /**
 +     * The router I control.
 +     */
 +    private final Router mRouter;
 +    
 +    /**
 +     * My user interface.
 +     */
 +    private final UserInterface mUI;
 +    
 +    /**
 +     * My list of presets.
 +     */
 +    private final Map<Integer, Preset> mPresets;
 +    
 +    /**
 +     * Create a switcher app.
 +     * @param portName the port name for the router.
 +     */
 +    public SwitcherApp(final String portName) {
 +        mUI = new ConsoleUI();
 +        mRouter = new ShinyBow5544Router(portName);
 +        mPresets = new HashMap<Integer, Preset>();
 +        
 +        // Setup UI actions
 +        mUI.setPresetSelectionAction(new Action() {
 +            public void onAction() {
 +                handlePresetSelected();
 +            }
 +        });
 +        
 +        mUI.setToggleControlLockAction(new Action() {
 +            public void onAction() {
 +                handleLockToggled();
 +            }
 +        });
 +    }
 +    
 +    /**
 +     * Start the application.
 +     */
 +    public void run() {
 +        // TODO: Load the presets
 +        // TODO: Send the presets to the UI
 +        mUI.run();
 +    }
 +    
 +    /**
 +     * Handles a preset selection.
 +     */
 +    private void handlePresetSelected() {
 +        int selectedPreset = mUI.getSelectedPreset();
 +        // TODO: What to do when the preset is selected
 +    }
 +
 +    /**
 +     * Locks or unlocks the physical router controls.
 +     */
 +    private void handleLockToggled() {
 +        mRouter.setLockControls(mUI.getControlLockStatus());
 +    }
 +    
 +    /**
 +     * Entry-point for the application.
 +     * @param args Command line arguments
 +     */
 +    public static void main(String[] args) {
 +        // Parse command line arguments
 +        if (args.length != 1) {
 +            System.err.println("You must pass one these arguments:");
 +            System.err.println("\t[commport]      The name of the serial port to use.");
 +            System.err.println("\t--list-ports    List the available serial ports.");
 +            return;
 +        }
 +
 +        if (args[0].equals("--list-ports")) {
 +            listCommPorts();
 +            return;
 +        }
 +
 +        String comPort = args[0];
 +        
 +        SwitcherApp app = new SwitcherApp(comPort);
 +        app.run();
 +    }
 +
 +    private static void listCommPorts() {
 +        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
 +    
 +        if (ports.hasMoreElements()) {
 +            System.out.println("These ports are available on your system: ");
 +            while (ports.hasMoreElements()) {
 +                CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
 +                if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {   
 +                    System.out.println(port.getName());
 +                }
 +            } 
 +
 +        } else {
 +            System.out.println("There are no serial ports on your system.");
 +        }
 +    }
  }
 diff --git a/src/net/tuschhcm/routercontrol/ui/ConsoleUI.java b/src/net/tuschhcm/routercontrol/ui/ConsoleUI.java index 52baa65..263b83e 100644 --- a/src/net/tuschhcm/routercontrol/ui/ConsoleUI.java +++ b/src/net/tuschhcm/routercontrol/ui/ConsoleUI.java @@ -3,13 +3,13 @@ package net.tuschhcm.routercontrol.ui;  public class ConsoleUI implements UserInterface {
  	@Override
 -	public void addPreset(int number, String name) {
 +	public void addPreset(int number, final String name) {
  		// TODO Auto-generated method stub
  	}
  	@Override
 -	public void setPresetSelectionAction(Action action) {
 +	public void setPresetSelectionAction(final Action action) {
  		// TODO Auto-generated method stub
  	}
 @@ -26,10 +26,19 @@ public class ConsoleUI implements UserInterface {  	}
 -	@Override
 -	public void setControlsLockAction(Action action) {
 +    @Override
 +	public void setToggleControlLockAction(final Action action) {
  		// TODO Auto-generated method stub
 -		
 -	}
 +    }
 +    @Override
 +    public boolean getControlLockStatus() {
 +		// TODO Auto-generated method stub
 +        return false;
 +    }
 +
 +    @Override
 +    public void setControlLockStatus(boolean enabled) {
 +		// TODO Auto-generated method stub
 +    }
  }
 diff --git a/src/net/tuschhcm/routercontrol/ui/UserInterface.java b/src/net/tuschhcm/routercontrol/ui/UserInterface.java index 7a441c5..33b62b2 100644 --- a/src/net/tuschhcm/routercontrol/ui/UserInterface.java +++ b/src/net/tuschhcm/routercontrol/ui/UserInterface.java @@ -26,7 +26,19 @@ public interface UserInterface {  	/**
  	 * Handle toggling the control lock
  	 */
 -	public void setControlsLockAction(final Action action);
 +	public void setToggleControlLockAction(final Action action);
 +
 +    /**
 +     * Get the status of the control lock.
 +     * @return true if the lock is enabled.
 +     */
 +    public boolean getControlLockStatus();
 +
 +    /**
 +     * Set the control lock status on the UI.
 +     * @param enabled true to set the controls lock.
 +     */
 +    public void setControlLockStatus(boolean enabled);
  	/**
  	 * Start the user interface.
 | 
