/** Set the parameters object to the settings in the properties object. */
  private void loadParams() throws SerialConnectionException {

    try {
      PropertyResourceBundle props =
          (PropertyResourceBundle) PropertyResourceBundle.getBundle(CONFIG_BUNDLE_NAME);
      System.out.println("BaudRate=" + props.getString("baudRate"));

      ezlink.info("BaudRate= : " + props.getString("baudRate"));

      parameters.setBaudRate(props.getString("baudRate"));
      parameters.setFlowControlIn(props.getString("flowControlIn"));
      parameters.setFlowControlOut(props.getString("flowControlOut"));
      parameters.setParity(props.getString("parity"));
      parameters.setDatabits(props.getString("databits"));
      parameters.setStopbits(props.getString("stopbits"));
      parameters.setPIN(props.getString("pin"));
      parameters.setSMC(props.getString("smc"));

      parameters.setDriver(props.getString("driver"));
      parameters.setURL(props.getString("url"));
      parameters.setUserName(props.getString("username"));
      parameters.setPassword(props.getString("password"));
      parameters.setPortName(props.getString("portName"));

    } catch (Exception exp) {
      ezlink.info("+++Error While setting parameters : +++");
      ezlink.error(new Object(), exp);
      throw new SerialConnectionException("Error While setting parameters=" + exp.getMessage());
    }
  }
  /**
   * Opening Connection to the serial port.first check whether port is open if not create new
   * connection to the port.
   */
  private void openConnection() {
    try {
      if (connection.isOpen()) {
        System.out.println("Port Open!,Can't open a new connection while a port is open.");
        ezlink.info("Port Open!,Can't open a new connection while a port is open : ");
      }

      connection.openConnection();
    } catch (SerialConnectionException e2) {
      System.out.println(e2);
      ezlink.error(new Object(), e2);
    }
  }
  // public String getDebitCmd(String stCardRndNo, String stTerRndNo, String stAmt, String stPurse)
  // throws Exception {
  public ETerminalDataDto getDebitCmd(
      String stCardRndNo, String stTerRndNo, String stAmt, String stPurse) throws Exception {

    ETerminalDataDto objETerminalDataDto = new ETerminalDataDto();

    String strDebitCmd = "";
    String strResHeader = "";
    String strActDebitCmd = "";
    String strUserData = "";
    String strSignSessionKey = "";
    String strDebitSessionKey = "";
    String strRefno = "";
    String strEzlinkString = "";

    // String strDebitCmd=null;
    try {
      ezlink.info("\n-------SerialManager--------------START--------------------------------");
      ezlink.info("getDebitCmd Request received in " + SerialManager.class.getName());
      // Commet this after testing with main method;
      // serialDemo.initConfig()
      // serialDemo.connection.sendMessage(ISOUtil.hex2byte("020079600037000002007024058000C10004164999770007848180000000000000011111011000100309020037003237303031393630313638313638313035323934202020000334343400063031313030320388"));

      // Debit command
      byte[] debitCommand =
          connection.sendMessage(
              ISOUtil.hex2byte(HeaderUtil.getReqHeader(stCardRndNo, stTerRndNo, stAmt, stPurse)));
      System.out.println("Debit Command=" + ISOUtil.hexString(debitCommand));
      ezlink.info("Debit Command= : " + ISOUtil.hexString(debitCommand));

      /*
      byte[] recieptData = connection.sendMessage(ISOUtil.hex2byte(HeaderUtil.getReqRecieptData("EEF6BAADFDDB93685EC111ACD05D7133E9472DA4416D0C079000")));
      System.out.println("Reciept Data =" + ISOUtil.hexString(recieptData));
      ezlink.info("Reciept Data= : " + ISOUtil.hexString(recieptData));
      */
      // debitCommand =
      // ISOUtil.hex2byte("36303030303030303030313132303030301C343100165F332D39907B07D4C9C72441CF93C3461C3432000854495430303031201C3433001647058A8594B61B0707BD7794B3F7078D1C34340016134DBF682CD22C68A60D1F0E71F929D01C34350012DC7934737E93BE4FDC7934731C30303030303031313230303030");
      /*
      String[] strResSplit = ISOUtil.hexString(debitCommand).split("1C");

      for(int i=0;i<strResSplit.length;i++)
      {
      strResSplit[i] = strResSplit[i].substring(8);
      System.out.println(strResSplit[i]);
      ezlink.info("strResSplit [ "+i+" ]" + strResSplit[i]);
      }
      strDebitCmd = "250315021403"+stTerRndNo+strResSplit[1]+strResSplit[2];
      System.out.println("strDebitCmd= "+strDebitCmd +"  Len  "+strDebitCmd.length());
      ezlink.info("strDebitCmd= "+strDebitCmd+" Len : " + strDebitCmd.length());

      */
      // String strResponse = ISOUtil.hexString(recieptData);
      String strResponse = ISOUtil.hexString(debitCommand);
      ezlink.info("+++strResponse Length : " + strResponse.length());
      ezlink.info("+++strResponse  : " + strResponse);
      //          if (strResponse.length() == 222) {
      // ezlink.info("+++Inside IF  :
      // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
      /*
      //Old terminal
      strResHeader = strResponse.substring(0, 34);
      strActDebitCmd = strResponse.substring(44, 76);
      strUserData = strResponse.substring(86, 102);
      strSignSessionKey = strResponse.substring(112, 144);
      strDebitSessionKey = strResponse.substring(154, 186);
      strRefno = strResponse.substring(196, 220);
      */
      // New Terminal
      strResHeader = strResponse.substring(0, 34);
      strRefno = strResponse.substring(44, 68); // 24
      strActDebitCmd = strResponse.substring(78, 110); // 32
      strUserData = strResponse.substring(120, 136); // 16
      strSignSessionKey = strResponse.substring(146, 178); // 32
      strDebitSessionKey = strResponse.substring(188, 220); // 32

      strEzlinkString = strResponse.substring(230, strResponse.length() - 2);

      System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
      System.out.println("Last two digits: " + strResponse.substring(strResponse.length() - 2));
      System.out.println("Response length : " + strResponse.length());
      System.out.println("Response from terminal DC : " + strResponse);
      System.out.println("Ezlink String : " + strEzlinkString);
      System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");

      ezlink.info("\n--------SERIAL MANAGER-------RESPONSE--------------------");
      ezlink.info("strResHeader : " + strResHeader);
      ezlink.info("strActDebitCmd : " + strActDebitCmd);
      ezlink.info("strUserData : " + strUserData);
      ezlink.info("strSignSessionKey : " + strSignSessionKey);
      ezlink.info("strDebitSessionKey : " + strDebitSessionKey);
      ezlink.info("strRefno : " + strRefno);
      ezlink.info("strEzlinkString : " + strEzlinkString);
      ezlink.info("\n--------SERIAL MANAGER-------RESPONSE--------------------");

      strDebitCmd = "250315021403" + stTerRndNo + strActDebitCmd + strUserData;
      System.out.println("strDebitCmd= " + strDebitCmd + "  Len  " + strDebitCmd.length());
      ezlink.info("strDebitCmd= " + strDebitCmd + " Len : " + strDebitCmd.length());

      objETerminalDataDto.setDebitCmd(strDebitCmd);
      objETerminalDataDto.setTerminalSessionKey(strSignSessionKey);
      objETerminalDataDto.setDebitSessionKey(strDebitSessionKey);
      objETerminalDataDto.setEzLinkString(strEzlinkString);
      /*
                  } else {
                      //ezlink.info("+++Inside IF  : ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
                      System.out.println("Response from the Terminal not received fully...");
                      ezlink.info("Response from the Terminal not received fully...!!!!!!!");
                  }
      */
    } catch (Exception exp) {
      System.out.println(exp);
      ezlink.error(new Object(), exp);
      throw exp;
    }
    // return strDebitCmd;
    return objETerminalDataDto;
  }
  // --------------------------getDecryptedRecieptData-------------------------------------------------------------
  // public String getDebitCmd(String stCardRndNo, String stTerRndNo, String stAmt, String stPurse)
  // throws Exception {
  public String getDecryptedRecieptData(ETerminalDataDto objETerminalDataDto) throws Exception {

    // ETerminalDataDto objETerminalDataDto=new ETerminalDataDto();

    String strDecryptedRecieptDataRefNo = "";
    String strDecryptedRecieptData = "";
    String strResHeader = "";

    // String strDebitCmd=null;
    try {
      ezlink.info(
          "\n-------SerialManager--------------START----4 decrypting RecieptData-------------------");
      ezlink.info("getDecryptedRecieptData Request received in " + SerialManager.class.getName());
      // Commet this after testing with main method;
      // serialDemo.initConfig()
      // serialDemo.connection.sendMessage(ISOUtil.hex2byte("020079600037000002007024058000C10004164999770007848180000000000000011111011000100309020037003237303031393630313638313638313035323934202020000334343400063031313030320388"));

      // Debit command
      byte[] decRecieptData = null;
      try {
        decRecieptData =
            connection.sendMessage(
                ISOUtil.hex2byte(
                    HeaderUtil.getReqRecieptData(
                        objETerminalDataDto.getRecieptData(),
                        objETerminalDataDto.getTerminalSessionKey(),
                        objETerminalDataDto.getDebitSessionKey(),
                        objETerminalDataDto.getCan(),
                        objETerminalDataDto.getEzLinkString(),
                        "")));
      } catch (Exception e) {
        e.printStackTrace();
      }
      System.out.println("Decrypted Reciept Data=" + ISOUtil.hexString(decRecieptData));
      ezlink.info("Decrypted Reciept Data= : " + ISOUtil.hexString(decRecieptData));
      String strResponse = ISOUtil.hexString(decRecieptData);
      ezlink.info("+++Decrypted Reciept Data strResponse Length : " + strResponse.length());
      ezlink.info("+++Decrypted Reciept Data strResponse  : " + strResponse);
      //          if (strResponse.length() == 222) {
      // ezlink.info("+++Inside IF  :
      // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );

      strResHeader = strResponse.substring(0, 34);
      strDecryptedRecieptDataRefNo = strResponse.substring(44, 68); // 24

      strDecryptedRecieptData = strResponse.substring(78, strResponse.length() - 2);

      System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
      System.out.println("Last two digits: " + strResponse.substring(strResponse.length() - 2));
      System.out.println("Response length : " + strResponse.length());
      System.out.println("Response from terminal DC : " + strResponse);
      System.out.println("Decrypted Ezlink String : " + strDecryptedRecieptData);
      System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");

      ezlink.info(
          "\n--------SERIAL MANAGER-------RESPONSE----4DecryptedRecieptData----------------");
      ezlink.info("strResHeader : " + strResHeader);

      ezlink.info("strDecryptedRecieptDataRefNo : " + strDecryptedRecieptDataRefNo);
      ezlink.info("strDecryptedRecieptData : " + strDecryptedRecieptData);
      ezlink.info("\n--------SERIAL MANAGER-------RESPONSE--------------------");

      // strDebitCmd = "250315021403" + stTerRndNo + strActDebitCmd + strUserData;
      // System.out.println("strDebitCmd= " + strDebitCmd + "  Len  " + strDebitCmd.length());
      // ezlink.info("strDebitCmd= " + strDebitCmd + " Len : " + strDebitCmd.length());

      /*
                  } else {
                      //ezlink.info("+++Inside IF  : ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
                      System.out.println("Response from the Terminal not received fully...");
                      ezlink.info("Response from the Terminal not received fully...!!!!!!!");
                  }
      */
    } catch (Exception exp) {
      System.out.println(exp);
      ezlink.error(new Object(), exp);
      throw exp;
    }

    return strDecryptedRecieptData;
  }