digidotcom / xbee-java

Set of Java APIs to interact with Digi International's XBee radio frequency modules.
Mozilla Public License 2.0
81 stars 56 forks source link

IOSampleListener duplicating readings #52

Open igoralves98 opened 8 years ago

igoralves98 commented 8 years ago

This issue has been banging my head...

I'm building a java program that will be able to open/close my garage door and monitor the respective status.

For this i wrote a initialization class that register an IOSampleListener using this code:

localDevice.addIOSampleListener(new PortaoGaragemListener());

The class PortaoGaragemListener.java is above:

public class PortaoGaragemListener implements IIOSampleReceiveListener{

  private static final Logger logger = LoggerFactory.getLogger(PortaoGaragemListener.class.getName());

  @Override
  public void ioSampleReceived(RemoteXBeeDevice remoteDevice, IOSample ioSample) {
    byte[] rssi = ByteUtils.intToByteArray(0);
    RegistroNotificacaoDao registroNotificacaoDao = new RegistroNotificacaoDao();
    String msgAberto = "Portão aberto";
    String msgFechado = "Portão fechado";
    try{
      rssi = remoteDevice.getParameter("DB");
      logger.info("Remote RSSI: " + ByteUtils.byteArrayToInt(rssi) + " dBm");
      if (ioSample.getDigitalValue(IOLine.DIO4_AD4).toString() == "High"){
        logger.info(msgAberto);
        registroNotificacaoDao.registraNotificacao(msgAberto, 3, 1);
      }else{
         logger.info(msgFechado);
         registroNotificacaoDao.registraNotificacao(msgFechado, 3, 1);
       }
    }catch(Exception e){
       System.out.println(e);
     }
  }
}

The program works almost fine.

I say almost because sometimes, when I press and release the button, the listener is suddenly called two and sometimes three times.

I'm using log4j2 and this implementation is giving me several debug information of the communication between the two xbees and, I was able to see that the tx/rx is running smoothly when I do not register a listener class but, when I instantiate the listener I got this issue.

My xbee connection is above:

img_9758-640x376

rubenmoral commented 7 years ago

Hi @igoralves98,

Could you please specify the XBee modules and firmware version you are using?

Remember that when you press and release the button, you are going to receive two samples: one for the press action and another one for the release action. What it's weird is that you sometimes receive three... (maybe mechanical issues?)

Anyway, you should change the way you compare the received IO sample, it's wrong to compare two strings with ==. In addition we have an enumeration class for the IO values:

if (ioSample.getDigitalValue(IOLine.DIO4_AD4) == IOValue.HIGH) {
    ...
} else {
    ...
}

Thanks.