简体   繁体   中英

How to run RFID tag listener method in javafx project?

I am working on a project involves RFID technology, what i am trying to do at the moment, once the RFIDTagListener method runs, I basically pass a tag to a reader which its serial number will be sent to a server to get some relevant data back and pop them on a GUI screen. what I have done so far is getting the data upon sending reader's data manually without passing a tag becasue I don't know how to do it otherwise and here is the problem. where I am working on a javafx project and when I tried to put the RFIDTagListener method within the MainController class and on compiling, the taglistner method wouldn't be triggered and just ignored, it's only the GUI screen will be opened.However, I also tried to have RFIDTagListener within the main class but on compiling, the taglistner method would be run first and when it's finished in 5 seconds, my GUI window will be opened next. SO I Don't know where this method should be located exactly. Essentially What I want is to have them both running at the same time, the taglistener running in the background with the GUI window opened simulitniously.

Any recommendation guys would be much appricated.

MainController class :

public class MainController {
    RFID rfid = new RFID();

    String ReaderNo = null;
    String walletJson = new String();
    Gson gson = new Gson();

    public static String sensorServerURL = "http://localhost:8080/PhidgetServer2019/SensorServerRFIDdata";

    walletDAO dao = new walletDAO();
    ArrayList<wallet> allwallets = new ArrayList<wallet>();

    @FXML VBox ConsultHR;
    @FXML private Label message;
    @FXML private Label WalletName;
    @FXML private ListView<ArrayList<wallet>> list;
    @FXML private ListView<ArrayList<wallet>> RoomAList;
    @FXML private TableView<wallet> tableViewData;
    @FXML private TableColumn<wallet, String> NameColumn;
    @FXML private TableColumn<wallet, String> LocationColumn;
    @FXML private TableColumn<wallet, String> TagColumn;


    public void getTags(ActionEvent event) throws SQLException {
        allwallets = dao.getWalletTag();
        try {
            allwallets = dao.getWalletTag();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(allwallets);
        list.getItems().add(allwallets);
    }

    public MainController() throws PhidgetException {
        // Make the RFID Phidget able to detect loss or gain of an rfid card
        rfid.addTagListener(new RFIDTagListener() {
            // What to do when a tag is found
            public void onTag(RFIDTagEvent e) {
                try {
                    ReaderNo = String.valueOf(rfid.getDeviceSerialNumber());
                } catch (PhidgetException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                System.out.println("Reader serial number is " + ' '+ReaderNo);
                wallet walletData = new wallet("385055");   
                walletJson = gson.toJson(walletData);       

                String resultfromserver = sendToServer(walletJson); 
                System.out.println("DEBUG: data in json : " +resultfromserver);
                wallet  walletObject = gson.fromJson(resultfromserver, wallet.class);                       

                System.out.println("DEBUG: The wallet's Data: "+' '+ walletObject);

                WalletName.setText(walletObject.getWalletName());
            }
        });

        rfid.addTagLostListener(new RFIDTagLostListener() {
            // What to do when a tag is lost
            public void onTagLost(RFIDTagLostEvent e) {
                // optional print, used as debug here
                System.out.println("DEBUG: Tag lost: " + e.getTag());
            }
       });
    }
}

Main class :

public class Main extends Application {
   //RFID rfid = new RFID();

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Wallet locator !");
    try {
        Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
 public static void main(String[] args) throws PhidgetException {
    new MainController();           
    launch(args);
 }
}

Main.FXML :

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TableColumn?>  
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="269.0" prefWidth="403.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <Button layoutX="149.0" layoutY="251.0" mnemonicParsing="false" onAction="#getTags" prefHeight="46.0" prefWidth="82.0" text="tags" />
   <Label fx:id="message" layoutX="139.0" layoutY="209.0" prefHeight="35.0" prefWidth="101.0" />
   <ListView id="studentObservableList" fx:id="list" layoutY="209.0" prefHeight="131.0" prefWidth="139.0" />

   <TableView fx:id="tableViewData" prefHeight="200.0" prefWidth="231.0" style="-fx-border-color: red;">
      <columns>
         <TableColumn fx:id="NameColumn" prefWidth="75.0" text="Name"/>
         <TableColumn fx:id="LocationColumn" prefWidth="75.0" text="Location" />
         <TableColumn fx:id="TagColumn" prefWidth="75.0" text="Tag" />
      </columns>
   </TableView>

   <Button layoutX="251.0" layoutY="14.0" mnemonicParsing="false" onAction="#getWallets" prefHeight="56.0" prefWidth="82.0" text="wallets" />
</AnchorPane>

1st point

The main method is inside the MainController class and it's instantiating itself, which is possible but not conventional.

2nd point

Never put your main method into a controller! Put it into your Main class instead.

3rd point

You must define the application launch inside your main method, by calling Application.launch() . This method will call your overriden start method (among other things) and display the GUI:

public static void main(String[] args) throws PhidgetException {
    launch(args);
    new MainController();           
}

4th point

You don't need to instantiate directly a controller in JavaFX. With your current code you're creating a MainController instance which is unlinked to your GUI; that's why you can see your GUI without interacting with it. You need to remove the new MainController(); line into your main method, then check if the fx:controller attribute is defined in the root of your FXML file.

You can call this controller instance using root.getController() in your start method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM