From 203ccc89c00730e07e39423e8fdfc0803fdeb10c Mon Sep 17 00:00:00 2001 From: kifir Date: Thu, 15 Aug 2024 13:46:23 +0300 Subject: [PATCH] only: refactoring: extract `AvailableHardware` from `SerialPortScanner` class --- .../java/com/rusefi/AvailableHardware.java | 49 +++++++++++++++++++ .../java/com/rusefi/SerialPortScanner.java | 41 ---------------- .../main/java/com/rusefi/StartupFrame.java | 2 +- .../rusefi/maintenance/ProgramSelector.java | 3 +- .../rusefi/ui/basic/BasicStartupFrame.java | 3 +- 5 files changed, 54 insertions(+), 44 deletions(-) create mode 100644 java_console/ui/src/main/java/com/rusefi/AvailableHardware.java diff --git a/java_console/ui/src/main/java/com/rusefi/AvailableHardware.java b/java_console/ui/src/main/java/com/rusefi/AvailableHardware.java new file mode 100644 index 0000000000..66f5427cf3 --- /dev/null +++ b/java_console/ui/src/main/java/com/rusefi/AvailableHardware.java @@ -0,0 +1,49 @@ +package com.rusefi; + +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class AvailableHardware { + + private final List ports; + private final boolean dfuFound; + private final boolean stLinkConnected; + private final boolean PCANConnected; + + public AvailableHardware(List ports, boolean dfuFound, boolean stLinkConnected, boolean PCANConnected) { + this.ports = ports; + this.dfuFound = dfuFound; + this.stLinkConnected = stLinkConnected; + this.PCANConnected = PCANConnected; + } + + @NotNull + public List getKnownPorts() {return new ArrayList<>(ports);} + + public List getKnownPorts(final SerialPortScanner.SerialPortType type) { + return ports.stream().filter(port -> port.type == type).collect(Collectors.toList()); + } + + public boolean isDfuFound() { + return dfuFound; + } + + public boolean isStLinkConnected() {return stLinkConnected;} + public boolean isPCANConnected(){return PCANConnected;} + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + AvailableHardware that = (AvailableHardware) o; + return dfuFound == that.dfuFound && stLinkConnected == that.stLinkConnected && PCANConnected == that.PCANConnected && ports.equals(that.ports); + } + + public boolean isEmpty() { + return !dfuFound && !stLinkConnected && !PCANConnected && ports.isEmpty(); + } +} + diff --git a/java_console/ui/src/main/java/com/rusefi/SerialPortScanner.java b/java_console/ui/src/main/java/com/rusefi/SerialPortScanner.java index ec0a10e07f..d324f0f687 100644 --- a/java_console/ui/src/main/java/com/rusefi/SerialPortScanner.java +++ b/java_console/ui/src/main/java/com/rusefi/SerialPortScanner.java @@ -289,47 +289,6 @@ public enum SerialPortScanner { void onChange(AvailableHardware currentHardware); } - public static class AvailableHardware { - - private final List ports; - private final boolean dfuFound; - private final boolean stLinkConnected; - private final boolean PCANConnected; - - public AvailableHardware(List ports, boolean dfuFound, boolean stLinkConnected, boolean PCANConnected) { - this.ports = ports; - this.dfuFound = dfuFound; - this.stLinkConnected = stLinkConnected; - this.PCANConnected = PCANConnected; - } - - @NotNull - public List getKnownPorts() {return new ArrayList<>(ports);} - - public List getKnownPorts(final SerialPortType type) { - return ports.stream().filter(port -> port.type == type).collect(Collectors.toList()); - } - - public boolean isDfuFound() { - return dfuFound; - } - - public boolean isStLinkConnected() {return stLinkConnected;} - public boolean isPCANConnected(){return PCANConnected;} - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - AvailableHardware that = (AvailableHardware) o; - return dfuFound == that.dfuFound && stLinkConnected == that.stLinkConnected && PCANConnected == that.PCANConnected && ports.equals(that.ports); - } - - public boolean isEmpty() { - return !dfuFound && !stLinkConnected && !PCANConnected && ports.isEmpty(); - } - } - public static String getEcuSignature(String port) { try (IoStream stream = BufferedSerialIoStream.openPort(port)) { return SerialAutoChecker.checkResponse(stream, callbackContext -> null); diff --git a/java_console/ui/src/main/java/com/rusefi/StartupFrame.java b/java_console/ui/src/main/java/com/rusefi/StartupFrame.java index 36ea1bafa3..85f777407c 100644 --- a/java_console/ui/src/main/java/com/rusefi/StartupFrame.java +++ b/java_console/ui/src/main/java/com/rusefi/StartupFrame.java @@ -258,7 +258,7 @@ public class StartupFrame { return jLabel; } - private void applyKnownPorts(SerialPortScanner.AvailableHardware currentHardware) { + private void applyKnownPorts(AvailableHardware currentHardware) { List ports = currentHardware.getKnownPorts(); log.info("Rendering available ports: " + ports); connectPanel.setVisible(!ports.isEmpty()); diff --git a/java_console/ui/src/main/java/com/rusefi/maintenance/ProgramSelector.java b/java_console/ui/src/main/java/com/rusefi/maintenance/ProgramSelector.java index 32fc8858b2..fe3430146c 100644 --- a/java_console/ui/src/main/java/com/rusefi/maintenance/ProgramSelector.java +++ b/java_console/ui/src/main/java/com/rusefi/maintenance/ProgramSelector.java @@ -1,6 +1,7 @@ package com.rusefi.maintenance; import com.devexperts.logging.Logging; +import com.rusefi.AvailableHardware; import com.rusefi.UiProperties; import com.rusefi.config.generated.Integration; import com.rusefi.core.FindFileHelper; @@ -339,7 +340,7 @@ public class ProgramSelector { return content; } - public void apply(SerialPortScanner.AvailableHardware currentHardware) { + public void apply(AvailableHardware currentHardware) { noHardware.setVisible(currentHardware.isEmpty()); updateModeAndButton.setVisible(!currentHardware.isEmpty()); diff --git a/java_console/ui/src/main/java/com/rusefi/ui/basic/BasicStartupFrame.java b/java_console/ui/src/main/java/com/rusefi/ui/basic/BasicStartupFrame.java index df0b832e0d..e142de9d75 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/basic/BasicStartupFrame.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/basic/BasicStartupFrame.java @@ -1,6 +1,7 @@ package com.rusefi.ui.basic; import com.devexperts.logging.Logging; +import com.rusefi.AvailableHardware; import com.rusefi.Launcher; import com.rusefi.SerialPortScanner; import com.rusefi.StartupFrame; @@ -93,7 +94,7 @@ public class BasicStartupFrame { UiUtils.centerWindow(frame.getFrame()); } - private void onHardwareUpdated(final SerialPortScanner.AvailableHardware currentHardware) { + private void onHardwareUpdated(final AvailableHardware currentHardware) { status.stop(); frame.getFrame().pack();