I am writing on a nfcreader programm in JavaFX. Using ACR122U nfcreader and the nfcreader class found on github. It works as expected, for now I am holding the card to the reader and after the card uid is read it loads a webpage from our server and shows information.
Problem:
- the NFC reader is running in a background task and always fires if a card is hold on the reader. I need to prevent this - while the webpage is loading.
What I did:
- if the nfcread service succeeds it calls a method "loadZeitdatenToWebview(nfcuid)"
- in this method a variable is set to true to tell Java that the webview still loads data
- I am setting this variable to false if the Workerstate of the webview succeeds otherwise its true... for a amount of 15 seconds checked by a pausetransition - if I get no webview "success" within 15 seconds it cancels the whole nfcread and tells the user there is no connection to the server - so far
- it works as it should if I have no connection to the server it stops the nfcread and everything is fine
- but if I get a webview success - it works only once ... I have to wait for 15 seconds (from the pause transition) till it reads again and shows me the webpage. So pause transition isn't the best way to achieve whar I need :( - but what else can be used ? in short: readnfc -> get uid from card -> webview calls a page dep. on the uid -> no other nfcreader should be possible till webpage is fully loaded and shwon to the user -> if there is no connection to the server after 15 seconds stop everything ...
Code for the nfcread succeeded... :
nfcread.setOnSucceeded(new EventHandler<WorkerStateEvent>() { //if service succeeded
@Override
public void handle(WorkerStateEvent event) {
labelReaderStatus.setText("NFC Readerstatus: warte auf NFC");
labelReaderStatusInfo.setStyle("-fx-background-color: WHITE; -fx-text-fill: black;");
labelReaderStatusInfo.setText("warte...");
System.out.println("NFC UID Card: " + nfcread.getValue()); //return value from service nfcread
if (nfcread.getValue() != "" && nfcread.getValue() != "Error" && nfcread.getValue() != null && nfcread.getValue() != "Error no Card") {
uidRead = nfcread.getValue();
labelReaderStatusInfo.setText("NFC Chip wurde gelesen - beginne mit Auswertung ");
labelReaderStatusInfo.setStyle("-fx-background-color: GREEN; -fx-text-fill: white;");
//chekc if webengine still loads data if not loadmethod
//loadData to webview
if (!waitForData) {
loadZeitdatenToWebview(uidRead);
} else {
System.out.println("Still loading data for "+uidRead);
nfcread.reset();
}
} //wenn uidRead keinen Fehler und nicht leer ist
else if (nfcread.getValue() == "Error") {...
and here the code for wait for webview or stop ...
//start webengine
private void loadZeitdatenToWebview(String uid) {
//wait till page loaded:
engine = webviewer.getEngine();
engine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
if (newState == Worker.State.SUCCEEDED) {
System.out.println("Workerstate Succeeded");
System.out.println("waitforData: " +waitForData );
// new page has loaded, process:
this.waitForData = false;
zeiterfassungErfolgreich();
System.out.println("waitforData done reset: " +waitForData );
}
if (engine.getLoadWorker().getState() != Worker.State.RUNNING) {
System.out.println("Workerstate Running ");
System.out.println("load state while data request:" + engine.getLoadWorker().getState()); //SCHEDULED
this.waitForData = true;
zeiterfassungRunning();
}
});
engine.load(httpHostforZeiterfassung + uid);
//engine.load("http://10.0.0.11/nfcread.php?uid=" + uid);
System.out.println("here: " + engine.getLoadWorker().getState());
}
private void zeiterfassungErfolgreich() {
System.out.println("zeiterfassungErfolgreich");
System.out.println("Fertig:" +waitForData);
labelReaderStatusInfo.setText("Zeiterfassung erfolgreich...");
//nfcread.reset();
}
private void zeiterfassungRunning() {
System.out.println("zeiterfassungRunning check - for data within 15 seconds");
//engine.getLoadWorker().cancel();
PauseTransition pause = new PauseTransition(Duration.seconds(15));
if (engine.getLoadWorker().getState() != Worker.State.SUCCEEDED) pause.setOnFinished(event -> {
if (engine.getLoadWorker().getState() == Worker.State.SUCCEEDED) {pause.stop(); waitForData=false; }
else {
engine.executeScript("window.stop()");
waitForData = false;
labelReaderStatusInfo.setStyle("-fx-background-color:red; -fx-text-fill: white;");
labelReaderStatusInfo.setText("Error keine Verbindung zum Server!");
nfcread.cancel();
}
//System.out.println("Running:" +waitForData);
});
pause.play();
System.out.println("here2: " + engine.getLoadWorker().getState());
}
//end webengine
If I can use the pausetransition this way how can I prevent the waiting time if webview succeeded or better what should I use to make this work ? Thanks to @all here !!
Additional Info: my actual code gives me this output if I try to nfcread a card within those 15 seconds:
- Waiting for a card..
- Failed OFF
- Event: 0BDF8D69
- load state while data request:SCHEDULED
- zeiterfassungRunning check - for data within 15 seconds
- here2: SCHEDULED
- here: RUNNING
- Waiting for a card..
- zeiterfassungErfolgreich
- Fertig:true
- load state while data request:SUCCEEDED
- zeiterfassungRunning check - for data within 15 seconds
- here2: SUCCEEDED
- Failed OFF
- Event: 0BDF8D69
- Still loading dataWaiting for a card. << last line shows me that it is not allowed to load new content ...