Example Code

The following example is bundled with the application. It will connect to the PLC, turn on a light, then disconnect from the PLC. There are other examples that are included in the test directory that show how to ping the PLC, listen for insteon messages and communicate with an insteon device.

package jcox.jplc.example;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import jcox.jplc.ApplicationModule;
import jcox.jplc.device.DeviceAddress;
import jcox.jplc.device.InsteonNetworkBridge;
import jcox.jplc.device.SendInsteonResponse;
import jcox.jplc.message.Hops;
import jcox.jplc.message.InsteonMessageFlags;
import jcox.jplc.message.InsteonMessageType;
import jcox.jplc.message.StandardInsteonMessage;
import jcox.jplc.message.command.CommonCommand;
import jcox.jplc.message.command.CommonCommandType;

import org.apache.log4j.Logger;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class SendInsteonMessage {

  final static Logger log = Logger.getLogger(SendInsteonMessage.class);
  
  private static final long TIMEOUT_IN_SECONDS = 10;
  
  /**
   @param args
   */
  public static void main(String[] args) {
    
  //set
  ApplicationModule module = new ApplicationModule();
    
    Injector injector = Guice.createInjector(module);
    
    try {
      log.info("Getting PLC Instance.");
      InsteonNetworkBridge plc = injector.getInstance(InsteonNetworkBridge.class);
      
      log.info("Connecting...");
      plc.connect();
      
      log.info("PLC connected?: " + plc.isConnected() );
      
      //construct the request
      DeviceAddress dummyPLCAddress = new DeviceAddress((byte)0x00(byte)0x00(byte)0x00);
      DeviceAddress myLampAddress = new DeviceAddress((byte)0x0E(byte)0xA1(byte)0xA6);    
      InsteonMessageFlags insteonMessageFlags = new InsteonMessageFlags(InsteonMessageType.DIRECT_MESSAGE,false,Hops.THREE, Hops.THREE);
      CommonCommand commonCommand = new CommonCommand(CommonCommandType.ON, (byte)0xFF);
      StandardInsteonMessage turnItOnPlease = new StandardInsteonMessage(dummyPLCAddress, myLampAddress, insteonMessageFlags,commonCommand);
      
      log.info("Sending message: " + turnItOnPlease);
      
      //send it
      Future<SendInsteonResponse> asynchResponse = plc.sendInsteon(turnItOnPlease);
      log.info("Sent.  Waiting for Response.");
      
      //see what happened, cancel the Future if timed out
      try {
        SendInsteonResponse eventualResponse = asynchResponse.get(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
        log.info("The asynchronous response from the plc to the request was: " + eventualResponse.toString());
      catch (TimeoutException te) {
        log.error("There was a timeout error while waiting for the asnch response.  Cancelling the request");
        asynchResponse.cancel(true);
      }
      
      log.info("Disconnecting...");
      plc.disconnect();

      log.info("PLC connected?: " + plc.isConnected() );
    catch (Exception ex) {
      log.error("There was an error.", ex);
    finally {
     module.shutdownHook();
    }
  }

}
Java2html
Get JPLC at SourceForge.net. Fast, secure and Free Open Source software downloads