From 247796745f79219587c74842ff4820d3930649a8 Mon Sep 17 00:00:00 2001 From: abdellah2288 Date: Tue, 10 Sep 2024 20:27:33 +0100 Subject: [PATCH] Switched to ToggleSwitch instead of button in the connect toggles. Fixed bug where the serial port doesn't close properly. --- .idea/workspace.xml | 73 +++++++++------- .../ranshinban/ranshinban/BLE/Scanner.java | 84 ++++++++----------- .../ranshinban/Localization/BeaconMapper.java | 1 + .../ranshinban/Wireless/httpClient.java | 12 +-- .../ranshinban/utils/SerialHandler.java | 12 ++- 5 files changed, 98 insertions(+), 84 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a8e925d..d9a05eb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -14,7 +14,10 @@ - + + + + - { + "keyToString": { + "Application.Main.executor": "Run", + "Application.start application.executor": "Run", + "Maven.Dotonbori [javafx:jlink].executor": "Run", + "Maven.Dotonbori [javafx:run].executor": "Run", + "Maven.Dotonbori [org.openjfx:javafx-maven-plugin:0.0.8:jlink].executor": "Run", + "Maven.Dotonbori [org.openjfx:javafx-maven-plugin:0.0.8:run].executor": "Run", + "Maven.Dotonbori [package].executor": "Run", + "RunOnceActivity.ShowReadmeOnStart": "true", + "SHARE_PROJECT_CONFIGURATION_FILES": "true", + "git-widget-placeholder": "main", + "kotlin-language-version-configured": "true", + "last_opened_file_path": "/home/abdellah/IdeaProjects/Ranshinban", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "project.structure.last.edited": "Project", + "project.structure.proportion": "0.15", + "project.structure.side.proportion": "0.2", + "run.code.analysis.last.selected.profile": "pProject Default", + "settings.editor.selected.configurable": "preferences.lookFeel", + "vue.rearranger.settings.migration": "true" } -}]]> +} @@ -101,7 +104,7 @@ - + + + + + + file://$PROJECT_DIR$/src/main/java/com/ranshinban/ranshinban/Wireless/httpClient.java + 53 + + + + \ No newline at end of file diff --git a/src/main/java/com/ranshinban/ranshinban/BLE/Scanner.java b/src/main/java/com/ranshinban/ranshinban/BLE/Scanner.java index 9cd2b41..231cfe8 100644 --- a/src/main/java/com/ranshinban/ranshinban/BLE/Scanner.java +++ b/src/main/java/com/ranshinban/ranshinban/BLE/Scanner.java @@ -30,6 +30,7 @@ import javafx.scene.shape.Rectangle; import javafx.scene.shape.SVGPath; import javafx.util.StringConverter; +import org.controlsfx.control.ToggleSwitch; import org.controlsfx.glyphfont.FontAwesome; import java.util.ArrayList; @@ -43,11 +44,8 @@ public class Scanner static private Button registeredBeaconsButton; static private Button rssiCalibrationButton; static private Button generateMapButton; - static private Button serialConnectButton; - static private Button connectButton; - - static private final SVGPath serialSVG = new SVGPath(); - static private final SVGPath internetSVG = new SVGPath(); + static private ToggleSwitch serialConnectButton; + static private ToggleSwitch connectButton; static private final TextField urlField = new TextField(); @@ -59,7 +57,6 @@ public class Scanner { while(true) { - Platform.runLater(() -> scannerIcon.setVisible(SerialHandler.portOpen() || httpClient.isActivated())); try { while (SerialHandler.portOpen()) @@ -79,7 +76,7 @@ public class Scanner } updateScannedBeaconTable(); } - while(httpClient.isActivated()) + while(httpClient.getActivatedProperty().getValue()) { httpClient.requestBeaconList(urlField.getText()); scannedBeacons = stringToBeacon(httpClient.getResponseProperty().get()); @@ -97,12 +94,11 @@ public class Scanner } catch (Exception e) { - httpClient.deactivateClient(); SerialHandler.closePort(); Platform.runLater(()-> { - serialConnectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_OFF,"2em")); - connectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_OFF,"2em")); + connectButton.setSelected(false); + serialConnectButton.setSelected(false); errorWindow.raiseErrorWindow(e.getMessage()); }); @@ -162,16 +158,8 @@ static public VBox scannerPanel() registeredBeaconsButton = new Button("Browse"); rssiCalibrationButton = new Button("Calibrate"); generateMapButton = new Button("Map"); - serialConnectButton = new Button(); - connectButton = new Button(); - - serialConnectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_OFF,"2em")); - serialConnectButton.getStyleClass().clear(); - serialConnectButton.setStyle(null); - - connectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_OFF,"2em")); - connectButton.getStyleClass().clear(); - connectButton.setStyle(null); + serialConnectButton = new ToggleSwitch(); + connectButton = new ToggleSwitch(); registeredBeaconsButton.prefWidthProperty().bind(controlsWidth); rssiCalibrationButton.prefWidthProperty().bind(controlsWidth); @@ -242,42 +230,40 @@ public SerialPort fromString(String string) } ); + httpClient.getActivatedProperty().bind(connectButton.selectedProperty()); + connectButton.selectedProperty().addListener((observable, oldValue, newValue) -> + { + if(newValue.booleanValue()) + { + SerialHandler.closePort(); + serialConnectButton.setSelected(false); + } + }); - connectButton.setOnAction(e -> + serialConnectButton.selectedProperty().addListener((observable, oldValue, newValue) -> { - if(httpClient.isActivated()) + if(newValue.booleanValue()) { - httpClient.deactivateClient(); - connectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_OFF,"2em")); + if(serialBox.getSelectionModel().getSelectedItem() == null) + { + errorWindow.raiseErrorWindow("No Serial port selected"); + serialConnectButton.setSelected(false); + return; + } + SerialHandler.openPort((SerialPort) serialBox.getSelectionModel().getSelectedItem(),115200); + if(!SerialHandler.portOpen()) + { + errorWindow.raiseErrorWindow("Could not open port"); + serialConnectButton.setSelected(false); + return; + } + connectButton.setSelected(false); } else { SerialHandler.closePort(); - serialConnectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_OFF,"2em")); - connectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_ON,"2em")); - httpClient.activateClient(); } }); - serialConnectButton.setOnAction(e -> - { - if(serialBox.getSelectionModel().getSelectedItem() == null) - { - errorWindow.raiseErrorWindow("No Serial port selected"); - return; - } - if(SerialHandler.portOpen()) - { - SerialHandler.closePort(); - serialConnectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_OFF,"2em")); - } - else - { - httpClient.deactivateClient(); - SerialHandler.openPort((SerialPort) serialBox.getSelectionModel().getSelectedItem(),115200); - serialConnectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_ON,"2em")); - connectButton.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.TOGGLE_OFF,"2em")); - } - }); registeredBeaconsButton.setOnAction( e -> @@ -332,6 +318,8 @@ public SerialPort fromString(String string) controlBox.getChildren().addAll(controlGrid,separator,scannerIcon); vbox.getChildren().addAll(controlBox, scannedBeaconsTable); + scannerIcon.visibleProperty().bind(connectButton.selectedProperty().or(serialConnectButton.selectedProperty())); + scannerThread.start(); return vbox; @@ -355,7 +343,7 @@ static public Beacon[] getScannedBeacons() } static public Boolean isActive() { - return SerialHandler.portOpen() || httpClient.isActivated(); + return SerialHandler.portOpen() || httpClient.getActivatedProperty().getValue(); } static public Beacon getBeacon(String address,boolean checkIfNew) { diff --git a/src/main/java/com/ranshinban/ranshinban/Localization/BeaconMapper.java b/src/main/java/com/ranshinban/ranshinban/Localization/BeaconMapper.java index efb72bb..484d25d 100644 --- a/src/main/java/com/ranshinban/ranshinban/Localization/BeaconMapper.java +++ b/src/main/java/com/ranshinban/ranshinban/Localization/BeaconMapper.java @@ -9,6 +9,7 @@ import javafx.geometry.Insets; import javafx.geometry.Point3D; import javafx.scene.*; +import javafx.scene.chart.AreaChart; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; diff --git a/src/main/java/com/ranshinban/ranshinban/Wireless/httpClient.java b/src/main/java/com/ranshinban/ranshinban/Wireless/httpClient.java index 61bae24..b891e7e 100644 --- a/src/main/java/com/ranshinban/ranshinban/Wireless/httpClient.java +++ b/src/main/java/com/ranshinban/ranshinban/Wireless/httpClient.java @@ -1,6 +1,8 @@ package com.ranshinban.ranshinban.Wireless; import com.ranshinban.ranshinban.utils.errorWindow; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleStringProperty; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; @@ -12,12 +14,12 @@ public class httpClient { private static final CloseableHttpClient mainClient = HttpClientBuilder.create().build(); - private static volatile boolean activated = false; + private static volatile BooleanProperty activated = new SimpleBooleanProperty(false); private static final SimpleStringProperty responseProperty = new SimpleStringProperty(); static public int requestBeaconList(String url) throws Exception { - if(activated) + if(activated.getValue()) { HttpResponse httpResponse = mainClient.execute(new HttpGet("http://" + url), response -> { @@ -42,13 +44,13 @@ static public SimpleStringProperty getResponseProperty() } static public void activateClient() { - activated = true; + activated.setValue(true); } static public void deactivateClient() { - activated = false; + activated.setValue(false); } - static public boolean isActivated() + static public BooleanProperty getActivatedProperty() { return activated; } diff --git a/src/main/java/com/ranshinban/ranshinban/utils/SerialHandler.java b/src/main/java/com/ranshinban/ranshinban/utils/SerialHandler.java index c47fc15..de779b9 100644 --- a/src/main/java/com/ranshinban/ranshinban/utils/SerialHandler.java +++ b/src/main/java/com/ranshinban/ranshinban/utils/SerialHandler.java @@ -38,7 +38,8 @@ public void serialEvent(SerialPortEvent event) serialPort.readBytes(recievedData, recievedData.length); scannerBuffer.append(new String(recievedData, StandardCharsets.UTF_8)); debugBuffer.append(new String(recievedData, StandardCharsets.UTF_8)); - if(scannerBuffer.toString().contains(scanCutOff)) + + if(scannerBuffer.indexOf(scanCutOff) != -1) { scanBuffer = scannerBuffer.toString(); scannerBuffer.delete(0, scannerBuffer.length()); @@ -47,12 +48,17 @@ public void serialEvent(SerialPortEvent event) } ); - return serialPort.openPort(); + return serialPort.isOpen(); } static public void closePort() { - if(currentPort != null) currentPort.closePort(); + if(currentPort != null) + { + currentPort.removeDataListener(); + currentPort.flushIOBuffers(); + currentPort.closePort(); + } scannerBuffer.setLength(0); scanBuffer = null; currentPort = null;