/**
   * Methode checkBufferData() allows you to verify all the data saved in the buffer
   *
   * @throws java.lang.InterruptedException
   */
  public void checkBufferData() throws InterruptedException {
    System.out.println("<--- BEGIN OF CALLING checkBufferData() methode --->");

    // Get the data saved in the buffer
    System.out.println("Contents of the buffer : " + super.getBufferReception());
    String[] data_in = SerialPortGPIO.extractBufferData(super.getBufferReception());

    // reset the reception buffer
    super.resetBufferReception();

    // Extract the first and last data to check the kind of request
    String first_data = data_in[0];
    String last_data = data_in[data_in.length - 1];

    System.out.println("First = " + first_data);
    System.out.println("Last  = " + last_data);

    // Extract only the data about the request
    int size_data = data_in.length - 2;
    String[] data_temp = new String[size_data];
    System.arraycopy(data_in, 1, data_temp, 0, size_data);
    for (int i = 0; i < data_temp.length; i++) {
      // data_temp[i] = data_in[i+1];
      System.out.println("Classe fille : valide data[" + i + "] = " + data_temp[i]);
    }

    // Call the checkSavedData() methode to identify the kind of request
    boolean flag = checkSavedData(first_data, last_data, data_temp);

    System.out.println("<--- END OF CALLING checkBufferData() methode --->");
  }
  public SerialPortSynchronizationInstruction(
      int baudrate, String identifiant, DeviceLinkingData device) {
    super(baudrate);
    id_sesame_sharer = identifiant;
    this.device = device;

    try {
      Thread.sleep(1000);
      super.sendData(BONJOUR);
    } catch (InterruptedException ex) {
    }
  }
  /**
   * Methode : analyzeDataReceived => Traitement des données recu
   *
   * @param received_data
   * @throws java.lang.InterruptedException
   */
  @Override
  public void analyzeDataReceived(String received_data) throws InterruptedException {
    super.setLastReceivedData(received_data);
    switch (received_data) {
      case BONJOUR:
        Thread.sleep(100);
        System.out.println("|BONJOUR| recu dans la classe herité");
        super.sendData(BONJOUR);
        break;

      case DEMANDE_SYNCHRONIZATION_APRES_PARTAGE_ACCES:
        Thread.sleep(100);
        System.out.println(
            "'DEMANDE_SYNCHRONIZATION_APRES_PARTAGE_ACCES' recu dans la classe herité");
        super.sendData(DEMANDE_SYNCHRONIZATION_APRES_PARTAGE_ACCES_AUTORISEE);
        break;

      case PREPARATION_ENREGISTREMENT_CLE_ACCES_ACCREDITEE:
        Thread.sleep(1000);
        System.out.println(
            "'PREPARATION_ENREGISTREMENT_CLE_ACCES_ACCREDITEE' recu dans la classe herité");
        super.sendData(SESAME_PRET_ENREGISTRE_CLE_ACCES);
        break;

      case BEGIN:
        System.out.println("|BEGIN| dans la classe hérité");
        Thread.sleep(100);
        // reset the buffer
        super.resetBufferReception();
        super.setSavingFlag(true);
        break;

      case END:
        Thread.sleep(100);
        System.out.println("|END| dans la classe hérité");
        super.setSavingFlag(false);
        this.checkBufferData();
        break;

      default:
        break;
    }
  }
  /**
   * Methode : checkSavedData : Traitement des données recu et sauvegarder dans le buffer de
   * reception
   *
   * @param first_data
   * @param last_data
   * @param data
   * @return flag : if the data is saved correctly
   * @throws java.lang.InterruptedException
   */
  private boolean checkSavedData(String first_data, String last_data, String[] data)
      throws InterruptedException {
    System.out.println("<--- BEGIN OF THE checkSavedData() methode --->");
    boolean flag = false;

    if ((first_data != null && first_data.equals(DEBUT_ENVOIE_INFORMATION_ACCREDITEE))
        && (last_data != null && last_data.equals(FIN_ENVOIE_INFORMATION_ACCREDITEE))) {
      System.out.println("First if");
      if (data != null) {

        System.out.println("Before calling saveAccreditedInformation()");
        flag = saveAccreditedInformation(data);
        System.out.println("After calling saveAccreditedInformation()");

        if (flag) {
          System.out.println("The data is saved correctly");
          Thread.sleep(500);
          super.sendData(INFORMATION_ACCREDITEE_ENREGISTRE_CORRECTEMENT);
        } else {
          System.out.println("The data is not saved because it contains some invalid data");
          Thread.sleep(500);
          super.sendData(INFORMATION_ACCREDITEE_DONNEES_ERONEES);
        }
      } else {
        System.out.println("The data is not saved because it contains some invalid data");
        Thread.sleep(500);
        super.sendData(INFORMATION_ACCREDITEE_DONNEES_ERONEES);
        System.out.println("Le buffer est invalide");
        flag = false;
      }
    }
    if ((first_data != null && first_data.equals(DEBUT_ENVOIE_CLE_ACCES_ACCREDITEE))
        && (last_data != null && last_data.equals(FIN_ENVOIE_CLE_ACCES_ACCREDITEE))) {
      System.out.println("First if");
      if (data != null) {

        System.out.println("Before calling checkSharingConfirmation()");
        flag = addDeviceLink(data);
        System.out.println("After calling checkSharingConfirmation()");

        if (flag) {
          System.out.println("The data is saved correctly");
          Thread.sleep(500);
          super.sendData(CLE_ACCES_ACCREDITEE_ENREGISTREE_CORRECTEMENT);
          this.getSerial().shutdown();
          super.openUartPort();
        } else {
          System.out.println("The data is not saved because it contains some invalid data");
          Thread.sleep(500);
          super.sendData(CLE_ACCES_ACCREDITEE_DONNEES_ERONNEES);
        }
      } else {
        System.out.println("The data is not saved because it contains some invalid data");
        Thread.sleep(500);
        super.sendData(CLE_ACCES_ACCREDITEE_DONNEES_ERONNEES);
        System.out.println("Le buffer est invalide");
        flag = false;
      }
    }
    // ===>
    else {
      flag = false;
      System.out.println("First Else ");
    }

    System.out.println("<--- END OF THE checkSavedData() methode -->");
    return flag;
  }