当RFID卡在阅读器上(Arduino)时,切换Javafx场景
我现在正在为学校做一个项目,其任务是为假医院开发桌面应用程序。
该应用程序用于存储和显示患者的病历。因此,必须具有登录系统。对于登录系统,我们需要使用Arduino和随附的MFRC-522 RFID模块。基本上,当扫描卡时,应登录用户,只要卡在读取器上,他就会登录他的帐户。一旦他脱下卡,他就会从自己的帐户中登记。
我已经设法在Java中对Arduinosketch和Bytestream-Reader进行了编码。 (为此,我将JSerialComm库用于Java)。
我现在的问题是: 我为应用程序制作了一个UI,但是当Arduino读取卡片时,我找不到交换场景的方法。因此,基本上,我希望UI一旦触摸卡片读取器,就可以将UI从登录区切换到大型霉素,并且一旦将卡从读取器带走后,应再次显示loginmenu。
package application;
import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;
public class CardReader {
SerialPort activePort;
public static boolean LoggedOut = true;
public static String ID = "";
public CardReader() {
activePort = SerialPort.getCommPort("COM3");
activePort.openPort();
activePort.addDataListener(new SerialPortDataListener() {
@Override
public void serialEvent(SerialPortEvent event) {
int size = event.getSerialPort().bytesAvailable();
byte[] buffer = new byte[size];
event.getSerialPort().readBytes(buffer, size);
for (byte b : buffer) {
if (b == 10) {
CardReader.ID = CardReader.ID.trim();
if (CardReader.ID.equals("XXXXX")) {
if (LoggedOut == false) {
mainClass.LoginManager.logout(mainClass.currentUser);
//Show loginmenu
System.out.println("logged out");
LoggedOut = true;
}
} else {
mainClass.currentUser.setUserID(CardReader.ID);
if(mainClass.LoginManager.loginWithCard(mainClass.currentUser, mainClass.listOfAllUsers) == true) {
//Here I want to show the mainmenu
//I tried to call methods from the LoginController here
//directly, but that doesn't work
System.out.println("Logged in");
System.out.println("|" + CardReader.ID + "|");
LoggedOut = false;
}else {
//Ausgabe Karte gesperrt
System.out.println("Karte gesperrt");
}
}
//System.out.println("|" + CardReader.ID.trim() + "|");
CardReader.ID = "";
} else {
CardReader.ID += (char) b;
}
}
}
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
}
});
}
}
我用来处理我尝试从logincontroller类调用方法的UI来处理UI的控制器的代码
package application;
import java.io.IOException;
import application.User.permissions;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
public class LoginController {
private Stage stage;
private Scene scene;
private Parent root;
Alert alert = new Alert(AlertType.WARNING);
@FXML
private TextField TextFieldUsername;
@FXML
private TextField TextFieldPassword;
public void setUIKarte(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("LoginmenuCard.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void setUIUser(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("LoginmenuUser.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
//I use this code for mutiple Scenes, that's why there are also functions
//for the option to login with a Username and password
public void loginbuttonclick(ActionEvent event) throws IOException {
mainClass.currentUser.setUsername(TextField.getText());
mainClass.aktuellerBenutzer.setPassword(TextFieldPassword.getText());
if (mainClass.LoginManager.L(mainClass.currentUser,
mainClass.listOfAllUsers)) {
setHauptmenu(event, mainClass.currentUser.getUserPerm());
} else {
mainClass.LoginManager.lo(mainClass.currentUser);
alert.setContentText("Username and Password are incorrect.");
alert.show();
}
}
public void setMainmenu(ActionEvent event, Permissions inPermissions) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("CardMenu.fxml"));;
switch (inPermissions) {
case ADMIN:
root = FXMLLoader.load(getClass().getResource("MainmenuAdmin.fxml"));
break;
case DOCTOR:
root = FXMLLoader.load(getClass().getResource("Mainmenu.fxml"));
break;
case NURSE:
root = FXMLLoader.load(getClass().getResource("Mainmenu.fxml"));
break;
case STAFF:
root = FXMLLoader.load(getClass().getResource("Mainmenu.fxml"));
break;
case NONE:
root = FXMLLoader.load(getClass().getResource("Mainmenu.fxml"));
break;
}
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void InvisButtonClick(ActionEvent event) {
}
}
,但这无效,因为SerialPortListener在另一个线程上(由库自动)。
I am working on a project for school right now and the task is to develope a Desktop-application for a fake hospital.
The application is used to store and display medical records of patients. Therefore it is required to have a login system. For the Login system, we are required to use an Arduino and the included MFRC-522 RFID module. Basically when a card is scanned, the user should be logged in and he stays logged into his account for as long as the card is on the reader. As soon as he takes off the card, he loggs out of his account.
I already managed to code the Arduinosketch and the bytestream-reader in Java. (I used the JSerialComm library for Java for that).
My problem right now is:
I made a UI for the application, but I can't find a way to swap the scene when the Arduino reads a card. So basically, I want the UI to switch from the Loginscene to the mainmenuscene as soon as the card touches the card reader and as soon as the card is taken away from the reader, the loginmenu should be displayed again.
package application;
import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;
public class CardReader {
SerialPort activePort;
public static boolean LoggedOut = true;
public static String ID = "";
public CardReader() {
activePort = SerialPort.getCommPort("COM3");
activePort.openPort();
activePort.addDataListener(new SerialPortDataListener() {
@Override
public void serialEvent(SerialPortEvent event) {
int size = event.getSerialPort().bytesAvailable();
byte[] buffer = new byte[size];
event.getSerialPort().readBytes(buffer, size);
for (byte b : buffer) {
if (b == 10) {
CardReader.ID = CardReader.ID.trim();
if (CardReader.ID.equals("XXXXX")) {
if (LoggedOut == false) {
mainClass.LoginManager.logout(mainClass.currentUser);
//Show loginmenu
System.out.println("logged out");
LoggedOut = true;
}
} else {
mainClass.currentUser.setUserID(CardReader.ID);
if(mainClass.LoginManager.loginWithCard(mainClass.currentUser, mainClass.listOfAllUsers) == true) {
//Here I want to show the mainmenu
//I tried to call methods from the LoginController here
//directly, but that doesn't work
System.out.println("Logged in");
System.out.println("|" + CardReader.ID + "|");
LoggedOut = false;
}else {
//Ausgabe Karte gesperrt
System.out.println("Karte gesperrt");
}
}
//System.out.println("|" + CardReader.ID.trim() + "|");
CardReader.ID = "";
} else {
CardReader.ID += (char) b;
}
}
}
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
}
});
}
}
Code for the Controller of the loginmenu I use to handle the UI
package application;
import java.io.IOException;
import application.User.permissions;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
public class LoginController {
private Stage stage;
private Scene scene;
private Parent root;
Alert alert = new Alert(AlertType.WARNING);
@FXML
private TextField TextFieldUsername;
@FXML
private TextField TextFieldPassword;
public void setUIKarte(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("LoginmenuCard.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void setUIUser(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("LoginmenuUser.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
//I use this code for mutiple Scenes, that's why there are also functions
//for the option to login with a Username and password
public void loginbuttonclick(ActionEvent event) throws IOException {
mainClass.currentUser.setUsername(TextField.getText());
mainClass.aktuellerBenutzer.setPassword(TextFieldPassword.getText());
if (mainClass.LoginManager.L(mainClass.currentUser,
mainClass.listOfAllUsers)) {
setHauptmenu(event, mainClass.currentUser.getUserPerm());
} else {
mainClass.LoginManager.lo(mainClass.currentUser);
alert.setContentText("Username and Password are incorrect.");
alert.show();
}
}
public void setMainmenu(ActionEvent event, Permissions inPermissions) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("CardMenu.fxml"));;
switch (inPermissions) {
case ADMIN:
root = FXMLLoader.load(getClass().getResource("MainmenuAdmin.fxml"));
break;
case DOCTOR:
root = FXMLLoader.load(getClass().getResource("Mainmenu.fxml"));
break;
case NURSE:
root = FXMLLoader.load(getClass().getResource("Mainmenu.fxml"));
break;
case STAFF:
root = FXMLLoader.load(getClass().getResource("Mainmenu.fxml"));
break;
case NONE:
root = FXMLLoader.load(getClass().getResource("Mainmenu.fxml"));
break;
}
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void InvisButtonClick(ActionEvent event) {
}
}
I tried to call methods from the LoginController class directly, but that doesn't work because the SerialportListener is on another Thread (automatically by the library).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论