refactor some simplicity in to port detection logic

This commit is contained in:
Matthew Kennedy 2023-10-28 21:59:06 -07:00
parent 5a00508053
commit 8b9e9517a4
3 changed files with 20 additions and 38 deletions

View File

@ -25,20 +25,16 @@ public class PortDetector {
private static final NamedThreadFactory AUTO_DETECT_PORT = new NamedThreadFactory("ECU AutoDetectPort", true);
public static final String AUTO = "auto";
public enum DetectorMode {
DETECT_TS,
}
/**
* Connect to all serial ports and find out which one respond first
* @param callback
* @return port name on which rusEFI was detected or null if none
*/
@NotNull
public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<SerialAutoChecker.CallbackContext, Void> callback, PortDetector.DetectorMode mode) {
public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<SerialAutoChecker.CallbackContext, Void> callback) {
String rusEfiAddress = System.getProperty("rusefi.address");
if (rusEfiAddress != null) {
return getSignatureFromPorts(mode, callback, new String[] {rusEfiAddress});
return getSignatureFromPorts(callback, new String[] {rusEfiAddress});
}
String[] serialPorts = getPortNames();
if (serialPorts.length == 0) {
@ -46,11 +42,11 @@ public class PortDetector {
return new SerialAutoChecker.AutoDetectResult(null, null);
}
log.info("Trying " + Arrays.toString(serialPorts));
return getSignatureFromPorts(mode, callback, serialPorts);
return getSignatureFromPorts(callback, serialPorts);
}
@NotNull
private static SerialAutoChecker.AutoDetectResult getSignatureFromPorts(DetectorMode mode, Function<SerialAutoChecker.CallbackContext, Void> callback, String[] serialPorts) {
private static SerialAutoChecker.AutoDetectResult getSignatureFromPorts(Function<SerialAutoChecker.CallbackContext, Void> callback, String[] serialPorts) {
List<Thread> serialFinder = new ArrayList<>();
CountDownLatch portFound = new CountDownLatch(1);
AtomicReference<SerialAutoChecker.AutoDetectResult> result = new AtomicReference<>();
@ -58,7 +54,12 @@ public class PortDetector {
Thread thread = AUTO_DETECT_PORT.newThread(new Runnable() {
@Override
public void run() {
new SerialAutoChecker(mode, serialPort, portFound).openAndCheckResponse(mode, result, callback);
SerialAutoChecker.AutoDetectResult checkResult = SerialAutoChecker.openAndCheckResponse(serialPort, callback);
if (checkResult != null) {
result.set(checkResult);
portFound.countDown();
}
}
@Override
@ -93,10 +94,6 @@ public class PortDetector {
return autoDetectResult;
}
public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<SerialAutoChecker.CallbackContext, Void> callback) {
return autoDetectSerial(callback, PortDetector.DetectorMode.DETECT_TS);
}
private static String[] getPortNames() {
// long now = System.currentTimeMillis();
String[] portNames = LinkManager.getCommPorts();

View File

@ -17,17 +17,11 @@ import java.util.function.Function;
import static com.rusefi.binaryprotocol.IoHelper.checkResponseCode;
public class SerialAutoChecker {
public final class SerialAutoChecker {
private final static Logging log = Logging.getLogging(SerialAutoChecker.class);
private final PortDetector.DetectorMode mode;
private final String serialPort;
private final CountDownLatch portFound;
public SerialAutoChecker(PortDetector.DetectorMode mode, String serialPort, CountDownLatch portFound) {
this.mode = mode;
this.serialPort = serialPort;
this.portFound = portFound;
}
// static class - no instances
private SerialAutoChecker() { }
public static String checkResponse(IoStream stream) {
return checkResponse(stream, null);
@ -59,27 +53,19 @@ public class SerialAutoChecker {
}
}
public void openAndCheckResponse(PortDetector.DetectorMode mode, AtomicReference<AutoDetectResult> result, Function<CallbackContext, Void> callback) {
public static AutoDetectResult openAndCheckResponse(String serialPort, Function<CallbackContext, Void> callback) {
String signature;
// java 101: just a reminder that try-with syntax would take care of closing stream and that's important here!
try (IoStream stream = getStreamByMode(mode)) {
try (IoStream stream = BufferedSerialIoStream.openPort(serialPort)) {
signature = checkResponse(stream, callback);
log.info("Got signature=" + signature + " from " + serialPort);
}
if (signature != null) {
/**
* propagating result after closing the port so that it could be used right away
*/
AutoDetectResult value = new AutoDetectResult(serialPort, signature);
log.info("Propagating " + value);
result.set(value);
portFound.countDown();
}
return new AutoDetectResult(serialPort, signature);
}
@Nullable
private IoStream getStreamByMode(PortDetector.DetectorMode mode) {
return BufferedSerialIoStream.openPort(serialPort);
return null;
}
public static class CallbackContext {

View File

@ -95,7 +95,6 @@ public class DfuFlasher {
callbacks.log("Auto-detecting port...\n");
// instead of opening the just-detected port we execute the command using the same stream we used to discover port
// it's more reliable this way
// ISSUE: that's blocking stuff on UI thread at the moment, TODO smarter threading!
port = PortDetector.autoDetectSerial(callbackContext -> {
boolean isSignatureValidatedLocal = DfuHelper.sendDfuRebootCommand(parent, callbackContext.getSignature(), callbackContext.getStream(), callbacks, command);
isSignatureValidated.set(isSignatureValidatedLocal);