only: refactoring: extract part of functionality is extracted into SerialPortCache class

This commit is contained in:
kifir 2024-08-15 11:57:54 +03:00 committed by rusefillc
parent 568c06cacc
commit 859ddd23b5
2 changed files with 47 additions and 27 deletions

View File

@ -0,0 +1,42 @@
package com.rusefi;
import com.devexperts.logging.Logging;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class SerialPortCache {
private final static Logging log = Logging.getLogging(SerialPortCache.class);
private final Map<String, SerialPortScanner.PortResult> cachedPorts = new HashMap<>();
Optional<SerialPortScanner.PortResult> get(final String serialPort) {
return Optional.ofNullable(cachedPorts.get(serialPort));
}
void put(final SerialPortScanner.PortResult port) {
cachedPorts.put(port.port, port);
}
void retainAll(final Set<String> serialPortsToRetain) {
// Clean the port cache of any entries that no longer exist
// If the same port appears later, we want to re-probe it at that time
// In any other scenario, auto could have unexpected behavior for the user
List<String> toRemove = new ArrayList<>();
for (String x : cachedPorts.keySet()) {
if (!serialPortsToRetain.contains(x)) {
toRemove.add(x);
}
}
// two steps to avoid ConcurrentModificationException
toRemove.forEach(p -> {
cachedPorts.remove(p);
log.info("Removing port " + p);
});
}
}

View File

@ -201,7 +201,7 @@ public enum SerialPortScanner {
return new ArrayList<>(results.values());
}
private final static Map<String, PortResult> portCache = new HashMap<>();
private final static SerialPortCache portCache = new SerialPortCache();
/**
* Find all available serial ports and checks if simulator local TCP port is available
@ -218,40 +218,18 @@ public enum SerialPortScanner {
for (String serialPort : serialPorts) {
// First, check the port cache
if (portCache.containsKey(serialPort)) {
// We've already probed this port - don't re-probe it again
PortResult cached = portCache.get(serialPort);
ports.add(cached);
} else {
portsToInspect.add(serialPort);
}
final Optional<PortResult> cachedPort = portCache.get(serialPort);
cachedPort.ifPresentOrElse(ports::add, () -> portsToInspect.add(serialPort));
}
for (PortResult p : inspectPorts(portsToInspect)) {
log.info("Port " + p.port + " detected as: " + p.type.friendlyString);
ports.add(p);
portCache.put(p.port, p);
portCache.put(p);
}
{
// Clean the port cache of any entries that no longer exist
// If the same port appears later, we want to re-probe it at that time
// In any other scenario, auto could have unexpected behavior for the user
List<String> toRemove = new ArrayList<>();
for (String x : portCache.keySet()) {
if (!serialPorts.contains(x)) {
toRemove.add(x);
}
}
// two steps to avoid ConcurrentModificationException
toRemove.forEach(p -> {
portCache.remove(p);
log.info("Removing port " + p);
});
}
portCache.retainAll(serialPorts);
// Sort ports by their type to put your ECU at the top
ports.sort(Comparator.comparingInt(a -> a.type.sortOrder));