only: refactoring: extract `AvailableHardware` from `SerialPortScanner` class

This commit is contained in:
kifir 2024-08-15 13:46:23 +03:00 committed by rusefillc
parent a7688c58d7
commit 203ccc89c0
5 changed files with 54 additions and 44 deletions

View File

@ -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<SerialPortScanner.PortResult> ports;
private final boolean dfuFound;
private final boolean stLinkConnected;
private final boolean PCANConnected;
public AvailableHardware(List<SerialPortScanner.PortResult> ports, boolean dfuFound, boolean stLinkConnected, boolean PCANConnected) {
this.ports = ports;
this.dfuFound = dfuFound;
this.stLinkConnected = stLinkConnected;
this.PCANConnected = PCANConnected;
}
@NotNull
public List<SerialPortScanner.PortResult> getKnownPorts() {return new ArrayList<>(ports);}
public List<SerialPortScanner.PortResult> 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();
}
}

View File

@ -289,47 +289,6 @@ public enum SerialPortScanner {
void onChange(AvailableHardware currentHardware);
}
public static class AvailableHardware {
private final List<PortResult> ports;
private final boolean dfuFound;
private final boolean stLinkConnected;
private final boolean PCANConnected;
public AvailableHardware(List<PortResult> ports, boolean dfuFound, boolean stLinkConnected, boolean PCANConnected) {
this.ports = ports;
this.dfuFound = dfuFound;
this.stLinkConnected = stLinkConnected;
this.PCANConnected = PCANConnected;
}
@NotNull
public List<PortResult> getKnownPorts() {return new ArrayList<>(ports);}
public List<PortResult> 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);

View File

@ -258,7 +258,7 @@ public class StartupFrame {
return jLabel;
}
private void applyKnownPorts(SerialPortScanner.AvailableHardware currentHardware) {
private void applyKnownPorts(AvailableHardware currentHardware) {
List<SerialPortScanner.PortResult> ports = currentHardware.getKnownPorts();
log.info("Rendering available ports: " + ports);
connectPanel.setVisible(!ports.isEmpty());

View File

@ -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());

View File

@ -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();