/*
   * (non-Javadoc)
   *
   * @see
   * org.rifidi.edge.sensors.AbstractGPIOService#testGPI(java.lang.String,
   * int)
   */
  @Override
  public boolean testGPI(String readerID, int port) throws CannotExecuteException {
    LLRPReaderSession session = super.getSession(readerID);

    GET_READER_CONFIG grc = new GET_READER_CONFIG();
    grc.setGPOPortNum(new UnsignedShort(0));
    grc.setAntennaID(new UnsignedShort(0));
    GetReaderConfigRequestedData requestedData = new GetReaderConfigRequestedData();
    grc.setRequestedData(requestedData);
    grc.setGPIPortNum(new UnsignedShort(0));
    GET_READER_CONFIG_RESPONSE grcr = null;
    try {
      grcr = (GET_READER_CONFIG_RESPONSE) session.transact(grc);
    } catch (Exception e) {
      throw new CannotExecuteException(
          "Exception during transact while trying to " + "obtain GPI information");
    }
    List<GPIPortCurrentState> gpilist = grcr.getGPIPortCurrentStateList();

    for (GPIPortCurrentState gpi : gpilist) {
      if (gpi.getGPIPortNum().intValue() == port) {
        if (gpi.getState().intValue() == 1) {
          return true;
        } else {
          return false;
        }
      }
    }

    throw new CannotExecuteException("No such port found while searching for GPI value: " + port);
  }
  /*
   * (non-Javadoc)
   *
   * @see org.rifidi.edge.sensors.AbstractGPIOService#setGPO(java.lang.String,
   * java.util.Collection)
   */
  @Override
  public void setGPO(String readerID, Collection<Integer> ports) throws CannotExecuteException {
    LLRPReaderSession session = super.getSession(readerID);

    GET_READER_CONFIG grc = new GET_READER_CONFIG();
    grc.setGPOPortNum(new UnsignedShort(0));
    grc.setAntennaID(new UnsignedShort(0));
    GetReaderConfigRequestedData requestedData = new GetReaderConfigRequestedData();
    grc.setRequestedData(requestedData);
    grc.setGPIPortNum(new UnsignedShort(0));
    GET_READER_CONFIG_RESPONSE grcr = null;
    try {
      grcr = (GET_READER_CONFIG_RESPONSE) session.transact(grc);
    } catch (Exception e) {
      throw new CannotExecuteException(
          "Exception during transact while trying to " + "obtain GPO information");
    }
    int num_ports = grcr.getGPOWriteDataList().size();

    List<GPOWriteData> writedata = new ArrayList<GPOWriteData>();
    for (int i = 1; i <= num_ports; i++) {
      Bit setbit;
      if (ports.contains(new Integer(i))) {
        setbit = new Bit(1);
      } else {
        setbit = new Bit(0);
      }
      GPOWriteData data = new GPOWriteData();
      data.setGPOData(setbit);
      data.setGPOPortNumber(new UnsignedShort(i));
      writedata.add(data);
    }

    SET_READER_CONFIG src = new SET_READER_CONFIG();
    src.setGPOWriteDataList(writedata);
    src.setResetToFactoryDefault(new Bit(0));

    session.send(src);
  }