import java.io.IOException;

import jcontrol.comm.FT1_2;
import jcontrol.comm.FT1_2EventListener;
import jcontrol.comm.RS232Selector;
import jcontrol.io.Buzzer;

/**
 * <p>Basic code for power-line communication using the <code>class
 * FT1_2</code>.</p>
 * 
 * <p>(C) DOMOLOGIC Home Automation GmbH 2003</p>
 */
public class PowerLineExample_Base implements FT1_2EventListener {
  
  /** FT1.2 protocol */
  static FT1_2 ft12;

  /** System buzzer */   
  Buzzer buzzer = new Buzzer();
  
  /** 
   * Init FT1.2 protocol 
   */
  public PowerLineExample_Base() {
    // switch RS232 to power-line modem
    RS232Selector.selectPort(RS232Selector.INTERNAL); 
    
    try {
      // instantiate FT1_2 protocol
      ft12 = new FT1_2();
      // set ourself as the FT1_2EventListener      
      ft12.setListener(this);
    } catch (java.io.IOException e) {
      // 2 second error beep
      buzzer.on((short)300, (short)2000);
      // read errorcode to reset error flag
      ft12.errorCode();
    }    
  }
  
  
  /**
   * FT1.2 event listener. This method is called any time
   * a power-line datagram drops in.
   * 
   * @see jcontrol.comm.FT1_2EventListener#onIndication(byte[], int)
   */
  public void onIndication(byte[] udat, int control) {
    try {
       switch(control){
         case FT1_2.CF_PRM_SEND_UDAT:
           ft12.sendACK(udat);
           break;
         case FT1_2.CF_PRM_REQ_STATUS:
           ft12.sendResponse(null);
           break;
         case FT1_2.CF_PRM_REQ_CLASS1:
           byte[] buf=new byte[10];
           ft12.sendResponse(buf);
           break;
       }
     } catch(IOException e){}
  }
  

  /**
   * Main method. Program execution starts here.
   */
  public static void main(String[] args) {
    new PowerLineExample_Base();
  }
}
