Skip to content

Commit

Permalink
Switched to ToggleSwitch instead of button in the connect toggles.
Browse files Browse the repository at this point in the history
Fixed bug where the serial port doesn't close properly.
  • Loading branch information
abdellah2288 committed Sep 10, 2024
1 parent 53eae8d commit 2477967
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 84 deletions.
73 changes: 45 additions & 28 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 36 additions & 48 deletions src/main/java/com/ranshinban/ranshinban/BLE/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -59,7 +57,6 @@ public class Scanner
{
while(true)
{
Platform.runLater(() -> scannerIcon.setVisible(SerialHandler.portOpen() || httpClient.isActivated()));
try
{
while (SerialHandler.portOpen())
Expand All @@ -79,7 +76,7 @@ public class Scanner
}
updateScannedBeaconTable();
}
while(httpClient.isActivated())
while(httpClient.getActivatedProperty().getValue())
{
httpClient.requestBeaconList(urlField.getText());
scannedBeacons = stringToBeacon(httpClient.getResponseProperty().get());
Expand All @@ -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());
});

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/ranshinban/ranshinban/Wireless/httpClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 ->
{
Expand All @@ -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;
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/ranshinban/ranshinban/utils/SerialHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
Expand Down

0 comments on commit 2477967

Please sign in to comment.