refactoring: better dependency control

This commit is contained in:
rusefi 2020-06-25 22:48:22 -04:00
parent 6a4cc8b266
commit 625b02f964
6 changed files with 31 additions and 32 deletions

View File

@ -214,8 +214,8 @@ public class AutoTest {
assertWave(msg, chart, EngineChart.SPARK_1, 0.1944, x, x + 180, x + 360, x + 540); assertWave(msg, chart, EngineChart.SPARK_1, 0.1944, x, x + 180, x + 360, x + 540);
} }
static EngineChart nextChart() { private EngineChart nextChart() {
return TestingUtils.nextChart(); return TestingUtils.nextChart(linkManager.getCommandQueue());
} }
private void test2003DodgeNeon() { private void test2003DodgeNeon() {

View File

@ -13,7 +13,7 @@ public class EnduranceTest {
public static void main(String[] args) { public static void main(String[] args) {
LinkManager linkManager = new LinkManager(); LinkManager linkManager = new LinkManager();
CommandQueue commandQueue = CommandQueue.getInstance(); CommandQueue commandQueue = linkManager.getCommandQueue();
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
int count = parseCount(args); int count = parseCount(args);
try { try {
@ -31,7 +31,7 @@ public class EnduranceTest {
AutoTest.currentEngineType = 3; AutoTest.currentEngineType = 3;
sendCommand("set " + Fields.CMD_ENGINE_TYPE + " " + 3, AutoTest.COMPLEX_COMMAND_RETRY, Timeouts.SET_ENGINE_TIMEOUT, commandQueue); sendCommand("set " + Fields.CMD_ENGINE_TYPE + " " + 3, AutoTest.COMPLEX_COMMAND_RETRY, Timeouts.SET_ENGINE_TIMEOUT, commandQueue);
sleepSeconds(2); sleepSeconds(2);
sendCommand(getEnableCommand("self_stimulation"), CommandQueue.getInstance()); sendCommand(getEnableCommand("self_stimulation"), commandQueue);
// IoUtil.changeRpm(1200); // IoUtil.changeRpm(1200);
AutoTest.currentEngineType = 28; AutoTest.currentEngineType = 28;
sendCommand("set " + Fields.CMD_ENGINE_TYPE + " " + 28, AutoTest.COMPLEX_COMMAND_RETRY, Timeouts.SET_ENGINE_TIMEOUT, commandQueue); sendCommand("set " + Fields.CMD_ENGINE_TYPE + " " + 28, AutoTest.COMPLEX_COMMAND_RETRY, Timeouts.SET_ENGINE_TIMEOUT, commandQueue);

View File

@ -105,9 +105,9 @@ public class TestingUtils {
assertTrue(msg, value == null); assertTrue(msg, value == null);
} }
static EngineChart nextChart() { static EngineChart nextChart(CommandQueue commandQueue) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
EngineChart chart = EngineChartParser.unpackToMap(getNextWaveChart(CommandQueue.getInstance())); EngineChart chart = EngineChartParser.unpackToMap(getNextWaveChart(commandQueue));
FileLog.MAIN.logLine("AUTOTEST nextChart() in " + (System.currentTimeMillis() - start)); FileLog.MAIN.logLine("AUTOTEST nextChart() in " + (System.currentTimeMillis() - start));
return chart; return chart;
} }

View File

@ -24,6 +24,7 @@ public class CommandQueue {
public static final int SLOW_CONFIRMATION_TIMEOUT = 5000; public static final int SLOW_CONFIRMATION_TIMEOUT = 5000;
public static final Class<CommandQueue> COMMAND_QUEUE_CLASS = CommandQueue.class; public static final Class<CommandQueue> COMMAND_QUEUE_CLASS = CommandQueue.class;
private final Object lock = new Object(); private final Object lock = new Object();
private final LinkManager linkManager;
/** /**
* One complex use-case is when we send out a bunch of commands and then we need to handle all the configurations * One complex use-case is when we send out a bunch of commands and then we need to handle all the configurations
* correctly * correctly
@ -31,7 +32,6 @@ public class CommandQueue {
*/ */
private Set<String> pendingConfirmations = Collections.synchronizedSet(new HashSet<String>()); private Set<String> pendingConfirmations = Collections.synchronizedSet(new HashSet<String>());
public static final CommandQueue instance = new CommandQueue();
private final BlockingQueue<IMethodInvocation> pendingCommands = new LinkedBlockingQueue<>(); private final BlockingQueue<IMethodInvocation> pendingCommands = new LinkedBlockingQueue<>();
private final List<CommandQueueListener> commandListeners = new ArrayList<>(); private final List<CommandQueueListener> commandListeners = new ArrayList<>();
@ -89,7 +89,7 @@ public class CommandQueue {
while (!pendingConfirmations.contains(command)) { while (!pendingConfirmations.contains(command)) {
counter++; counter++;
// FileLog.MAIN.logLine("templog sending " + command + " " + System.currentTimeMillis() + " " + new Date()); // FileLog.MAIN.logLine("templog sending " + command + " " + System.currentTimeMillis() + " " + new Date());
LinkManager.send(command, commandRequest.isFireEvent()); linkManager.send(command, commandRequest.isFireEvent());
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
synchronized (lock) { synchronized (lock) {
lock.wait(commandRequest.getTimeout()); lock.wait(commandRequest.getTimeout());
@ -114,7 +114,8 @@ public class CommandQueue {
MessagesCentral.getInstance().postMessage(CommandQueue.class, "Took " + counter + " attempts"); MessagesCentral.getInstance().postMessage(CommandQueue.class, "Took " + counter + " attempts");
} }
private CommandQueue() { public CommandQueue(LinkManager linkManager) {
this.linkManager = linkManager;
Thread thread = new Thread(runnable, "Commands Queue"); Thread thread = new Thread(runnable, "Commands Queue");
thread.setDaemon(true); thread.setDaemon(true);
thread.start(); thread.start();
@ -140,10 +141,6 @@ public class CommandQueue {
} }
} }
public static CommandQueue getInstance() {
return instance;
}
public void write(String command) { public void write(String command) {
write(command, DEFAULT_TIMEOUT); write(command, DEFAULT_TIMEOUT);
} }

View File

@ -18,6 +18,21 @@ import java.util.concurrent.*;
* 3/3/14 * 3/3/14
*/ */
public class LinkManager { public class LinkManager {
@NotNull
public static LogLevel LOG_LEVEL = LogLevel.INFO;
public static LinkDecoder ENCODER = new LinkDecoder() {
@Override
public String unpack(String packedLine) {
return packedLine;
}
};
public static final String LOG_VIEWER = "log viewer";
private final CommandQueue commandQueue = new CommandQueue(this);
private LinkConnector connector;
@NotNull @NotNull
public CountDownLatch connect(String port) { public CountDownLatch connect(String port) {
final CountDownLatch connected = new CountDownLatch(1); final CountDownLatch connected = new CountDownLatch(1);
@ -63,7 +78,7 @@ public class LinkManager {
} }
public CommandQueue getCommandQueue() { public CommandQueue getCommandQueue() {
return CommandQueue.getInstance(); return commandQueue;
} }
public enum LogLevel { public enum LogLevel {
@ -76,16 +91,6 @@ public class LinkManager {
} }
} }
@NotNull
public static LogLevel LOG_LEVEL = LogLevel.INFO;
public static LinkDecoder ENCODER = new LinkDecoder() {
@Override
public String unpack(String packedLine) {
return packedLine;
}
};
/** /**
* Threading of the whole input/output does not look healthy at all! * Threading of the whole input/output does not look healthy at all!
* *
@ -100,7 +105,6 @@ public class LinkManager {
return t; return t;
} }
}); });
public static final String LOG_VIEWER = "log viewer";
public final LinkedBlockingQueue<Runnable> COMMUNICATION_QUEUE = new LinkedBlockingQueue<>(); public final LinkedBlockingQueue<Runnable> COMMUNICATION_QUEUE = new LinkedBlockingQueue<>();
/** /**
* All request/responses to underlying controller are happening on this single-threaded executor in a FIFO manner * All request/responses to underlying controller are happening on this single-threaded executor in a FIFO manner
@ -144,8 +148,6 @@ public class LinkManager {
} }
}); });
private static LinkConnector connector;
/** /**
* This flag controls if mock controls are needed * This flag controls if mock controls are needed
*/ */
@ -171,7 +173,7 @@ public class LinkManager {
} }
public void setConnector(LinkConnector connector) { public void setConnector(LinkConnector connector) {
LinkManager.connector = connector; this.connector = connector;
} }
public static boolean isLogViewerMode(String port) { public static boolean isLogViewerMode(String port) {
@ -183,10 +185,10 @@ public class LinkManager {
return connector == LinkConnector.VOID; return connector == LinkConnector.VOID;
} }
public static void send(String command, boolean fireEvent) throws InterruptedException { public void send(String command, boolean fireEvent) throws InterruptedException {
if (connector == null) if (this.connector == null)
throw new NullPointerException("connector"); throw new NullPointerException("connector");
connector.send(command, fireEvent); this.connector.send(command, fireEvent);
} }
public void restart() { public void restart() {

View File

@ -15,6 +15,6 @@ public class UIContext {
} }
public CommandQueue getCommandQueue() { public CommandQueue getCommandQueue() {
return CommandQueue.instance; return linkManager.getCommandQueue();
} }
} }