only: refactoring: now LinkManager.getCommPorts method returns set of strings instead of array

This commit is contained in:
kifir 2024-08-09 19:25:13 +03:00 committed by rusefillc
parent 576629de2f
commit a98285252f
4 changed files with 22 additions and 29 deletions

View File

@ -9,8 +9,8 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@ -41,19 +41,19 @@ public class PortDetector {
public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<SerialAutoChecker.CallbackContext, Void> callback, PortDetector.DetectorMode mode) {
String rusEfiAddress = System.getProperty("rusefi.address");
if (rusEfiAddress != null) {
return getSignatureFromPorts(mode, callback, new String[] {rusEfiAddress});
return getSignatureFromPorts(mode, callback, Set.of(rusEfiAddress));
}
String[] serialPorts = getPortNames();
if (serialPorts.length == 0) {
final Set<String> serialPorts = LinkManager.getCommPorts();
if (serialPorts.isEmpty()) {
log.error("No serial ports detected");
return new SerialAutoChecker.AutoDetectResult(null, null);
}
log.info("Trying " + Arrays.toString(serialPorts));
log.info("Trying [" + String.join(", ", serialPorts) + "]");
return getSignatureFromPorts(mode, callback, serialPorts);
}
@NotNull
private static SerialAutoChecker.AutoDetectResult getSignatureFromPorts(DetectorMode mode, Function<SerialAutoChecker.CallbackContext, Void> callback, String[] serialPorts) {
private static SerialAutoChecker.AutoDetectResult getSignatureFromPorts(DetectorMode mode, Function<SerialAutoChecker.CallbackContext, Void> callback, Set<String> serialPorts) {
List<Thread> serialFinder = new ArrayList<>();
CountDownLatch portFound = new CountDownLatch(1);
AtomicReference<SerialAutoChecker.AutoDetectResult> result = new AtomicReference<>();
@ -103,13 +103,6 @@ public class PortDetector {
return autoDetectSerial(callback, PortDetector.DetectorMode.DETECT_TS);
}
private static String[] getPortNames() {
// long now = System.currentTimeMillis();
String[] portNames = LinkManager.getCommPorts();
// log.info("Took " + (System.currentTimeMillis() - now));
return portNames;
}
@Nullable
public static SerialAutoChecker.AutoDetectResult autoDetectPort(JFrame parent) {
SerialAutoChecker.AutoDetectResult autoDetectedPort = autoDetectSerial(null);

View File

@ -19,8 +19,10 @@ import org.jetbrains.annotations.NotNull;
import java.io.Closeable;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import static com.devexperts.logging.Logging.getLogging;
@ -109,13 +111,10 @@ public class LinkManager implements Closeable {
return COMMUNICATION_EXECUTOR.submit(runnable);
}
public static String[] getCommPorts() {
public static Set<String> getCommPorts() {
SerialPort[] ports = SerialPort.getCommPorts();
// wow sometimes driver returns same port name more than once?!
TreeSet<String> names = new TreeSet<>();
for (SerialPort port : ports)
names.add(port.getSystemPortName());
return names.toArray(new String[0]);
return Arrays.stream(ports).map(SerialPort::getSystemPortName).collect(Collectors.toCollection(TreeSet::new));
}
public BinaryProtocol getBinaryProtocol() {
@ -306,8 +305,8 @@ public class LinkManager implements Closeable {
return;
close(); // Explicitly kill the connection (call connectors destructor??????)
String[] ports = getCommPorts();
boolean isPortAvailableAgain = Arrays.asList(ports).contains(lastTriedPort);
final Set<String> ports = getCommPorts();
final boolean isPortAvailableAgain = ports.contains(lastTriedPort);
log.info("restart isPortAvailableAgain=" + isPortAvailableAgain);
if (isPortAvailableAgain) {
connect(lastTriedPort);

View File

@ -212,7 +212,7 @@ public enum SerialPortScanner {
boolean stLinkConnected;
boolean PCANConnected;
String[] serialPorts = LinkManager.getCommPorts();
final Set<String> serialPorts = LinkManager.getCommPorts();
List<String> portsToInspect = new ArrayList<>();
@ -241,7 +241,7 @@ public enum SerialPortScanner {
// In any other scenario, auto could have unexpected behavior for the user
List<String> toRemove = new ArrayList<>();
for (String x : portCache.keySet()) {
if (Arrays.stream(serialPorts).noneMatch(x::equals)) {
if (!serialPorts.contains(x)) {
toRemove.add(x);
}
}

View File

@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@ -201,9 +202,9 @@ public class ProgramSelector {
if (PortDetector.AUTO.equals(ecuPort)) {
return true;
} else {
final String[] currentPorts = LinkManager.getCommPorts();
final Set<String> currentPorts = LinkManager.getCommPorts();
log.info("currentPorts: [" + String.join(",", currentPorts) + "]");
return !Arrays.stream(LinkManager.getCommPorts()).anyMatch(ecuPort::equals);
return !LinkManager.getCommPorts().contains(ecuPort);
}
},
callbacks
@ -211,17 +212,17 @@ public class ProgramSelector {
}
private static List<String> waitForNewPortAppeared(
final String[] portsBefore,
final Set<String> portsBefore,
final UpdateOperationCallbacks callbacks
) {
final List<String> newPorts = new ArrayList<>();
waitForPredicate(
"Waiting for new port to appear...",
() -> {
final String[] portsAfter = LinkManager.getCommPorts();
final Set<String> portsAfter = LinkManager.getCommPorts();
log.info("portsAfter: [" + String.join(",", portsAfter) + "]");
for (String s : portsAfter) {
if (!Arrays.stream(portsBefore).anyMatch(s::equals)) {
if (!portsBefore.contains(s)) {
// This item is in the after list but not before list
newPorts.add(s);
}
@ -235,7 +236,7 @@ public class ProgramSelector {
private static void flashOpenbltSerialAutomatic(JComponent parent, String ecuPort, UpdateOperationCallbacks callbacks) {
AutoupdateUtil.assertNotAwtThread();
final String[] portsBefore = LinkManager.getCommPorts();
final Set<String> portsBefore = LinkManager.getCommPorts();
rebootToOpenblt(parent, ecuPort, callbacks);
// invoking blocking method
@ -261,7 +262,7 @@ public class ProgramSelector {
if (newItems.size() > 1) {
// More than one port appeared? whattt?
callbacks.logLine("Unable to find ECU after reboot as multiple serial ports appeared. Before: " + portsBefore.length);
callbacks.logLine("Unable to find ECU after reboot as multiple serial ports appeared. Before: " + portsBefore.size());
callbacks.error();
return;
}