001package ca.bc.webarts.tools;
002
003import ca.bc.webarts.widgets.Util;
004
005//import gnu.io.NoSuchPortException;
006//import gnu.io.PortInUseException;
007import javax.comm.NoSuchPortException;
008import javax.comm.PortInUseException;
009
010import x10.FireCracker;
011import x10.X10Command;
012
013public class X10FireCracker
014{
015  /** The port the firecracker is attached to. */
016  private String portDescriptor_ = "/dev/ttyS0";
017
018  /**  Constructor * */
019  X10FireCracker()
020  {
021  }
022
023
024  /**
025   *  The main program for the X10FireCracker class.
026   *
027   * @param  arg  The command line arguments
028   */
029  public static void main( String[] arg )
030  {
031    if (arg.length > 2)
032    {
033      X10FireCracker instance = new X10FireCracker();
034      instance.sendX10Command(arg[0], arg[1], arg[2]);
035    }
036    else
037    {
038      System.out.println( "X10FireCracker " );
039      System.out.println( "Call with the houseCode device command" );
040    }
041  }
042
043
044  /**
045   *  Abstracts the actual sending of the X10 Command.
046   *
047   * @param  x10Command       This is either 'on' or 'off'
048   * @param  x10HouseCode     send an uppercase house code
049   * @param  x10DeviceNumber  send the device nuymber as a string... ie. 'String.valueOf( 1 );'
050   */
051  public void sendX10Command( String x10HouseCode,
052                              String x10DeviceNumber,
053                              String x10Command)
054  {
055    FireCracker fc = new FireCracker();
056    try
057    {
058      System.out.println( "Connecting to FireCracker on port " + portDescriptor_ );
059
060      fc.openPort( portDescriptor_ );
061    }
062    catch ( PortInUseException piux )
063    {
064      System.out.println( "Specified port in use by application: " +
065          piux.currentOwner );
066      return;
067    }
068    catch ( NoSuchPortException nspx )
069    {
070      System.out.println( "Specified port not recognized: " + portDescriptor_ + "\n" );
071      //usage();
072    }
073
074    // Give the device time to power up?
075    // It's not very reliable without this delay.
076    Util.sleep( 400 );
077
078    X10Command xcmd = null;
079    //System.out.println("Preparing:"+ x10HouseCode.charAt(0)+Integer.parseInt(x10DeviceNumber)+" "+x10Command);
080    if ( x10Command.equals( "on" ) )
081    {
082      xcmd = X10Command.makeOnCommand( x10HouseCode.charAt( 0 ),
083          Integer.parseInt( x10DeviceNumber ) );
084    }
085    else if ( x10Command.equals( "off" ) )
086    {
087      xcmd = X10Command.makeOffCommand( x10HouseCode.charAt( 0 ),
088          Integer.parseInt( x10DeviceNumber ) );
089    }
090    else if ( x10Command.equals( "dim" ) )
091    {
092      xcmd = X10Command.makeDimCommand( x10HouseCode.charAt( 0 ) );
093    }
094    else if ( x10Command.equals( "bright" ) )
095    {
096      xcmd = X10Command.makeBrightCommand( x10HouseCode.charAt( 0 ) );
097    }
098    System.out.println( "Sending:" + x10HouseCode.charAt( 0 ) + x10DeviceNumber + " " + x10Command );
099    fc.sendCommand( xcmd );
100
101    System.out.println( "\nDisconnecting from FireCracker" );
102    fc.closePort();
103  }
104
105}