From a2421c9f79fefd21f93c985197f4f49c75f8cd04 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Mon, 23 Feb 2015 08:05:15 -0600 Subject: [PATCH] auto-sync --- java_console/autotest/autotest.iml | 4 +-- .../autotest/src/com/rusefi/AutoTest.java | 25 +++++++++----- .../autotest/src/com/rusefi/IoUtil.java | 33 +++++++++++++++++-- .../autotest/src/com/rusefi/RealHwTest.java | 20 +++++++++++ .../io/src/com/rusefi/io/CommandQueue.java | 16 +++++---- .../src/com/rusefi/AverageAnglesUtil.java | 3 +- .../src/com/rusefi/core/EngineState.java | 6 ++++ .../ui/src/com/rusefi/AnalogChartCentral.java | 3 +- java_console/ui/src/com/rusefi/CmdLine.java | 18 +++------- 9 files changed, 90 insertions(+), 38 deletions(-) diff --git a/java_console/autotest/autotest.iml b/java_console/autotest/autotest.iml index d35f6f1e18..b5276310e9 100644 --- a/java_console/autotest/autotest.iml +++ b/java_console/autotest/autotest.iml @@ -9,6 +9,6 @@ + - - + \ No newline at end of file diff --git a/java_console/autotest/src/com/rusefi/AutoTest.java b/java_console/autotest/src/com/rusefi/AutoTest.java index 978f1627ec..9425c37a75 100644 --- a/java_console/autotest/src/com/rusefi/AutoTest.java +++ b/java_console/autotest/src/com/rusefi/AutoTest.java @@ -6,6 +6,7 @@ import com.rusefi.waves.WaveReport; import static com.rusefi.IoUtil.nextChart; import static com.rusefi.IoUtil.sendCommand; +import static com.rusefi.IoUtil.sleep; import static com.rusefi.TestingUtils.*; /** @@ -17,7 +18,7 @@ import static com.rusefi.TestingUtils.*; * 3/5/14 */ public class AutoTest { - private static void mainTestBody() { + static void mainTestBody() { testCitroenBerlingo(); testMazda626(); test2003DodgeNeon(); @@ -29,15 +30,21 @@ public class AutoTest { } private static void testCitroenBerlingo() { - sendCommand("set_engine_type 15"); + setEngineType(15); // time to change engine type nextChart(); String msg = "Citroen"; // todo: add more content } + private static void setEngineType(int type) { + sendCommand("set_engine_type " + type, 10000); + sleep(5); + sendCommand("enable self_stimulation"); + } + private static void testMazda626() { - sendCommand("set_engine_type 28"); + setEngineType(28); WaveChart chart; // time to change engine type nextChart(); @@ -50,7 +57,7 @@ public class AutoTest { } private static void test2003DodgeNeon() { - sendCommand("set_engine_type 23"); + setEngineType(23); WaveChart chart; // time to change engine type nextChart(); @@ -90,7 +97,7 @@ public class AutoTest { } private static void testMazdaProtege() { - sendCommand("set_engine_type 14"); + setEngineType(14); WaveChart chart; nextChart(); // a bit of extra time to change engine type IoUtil.changeRpm(200); @@ -113,7 +120,7 @@ public class AutoTest { } private static void test1995DodgeNeon() { - sendCommand("set_engine_type 2"); + setEngineType(2); WaveChart chart; sendCommand("set_whole_fuel_map 3"); IoUtil.changeRpm(2000); @@ -142,7 +149,7 @@ public class AutoTest { } private static void testFordFiesta() { - sendCommand("set_engine_type 4"); + setEngineType(4); WaveChart chart; IoUtil.changeRpm(2000); chart = nextChart(); @@ -156,7 +163,7 @@ public class AutoTest { } private static void testFord6() { - sendCommand("set_engine_type 7"); + setEngineType(7); WaveChart chart; IoUtil.changeRpm(2000); chart = nextChart(); @@ -173,7 +180,7 @@ public class AutoTest { } private static void testFordAspire() { - sendCommand("set_engine_type 3"); + setEngineType(3); String msg; WaveChart chart; // todo: interesting changeRpm(100); diff --git a/java_console/autotest/src/com/rusefi/IoUtil.java b/java_console/autotest/src/com/rusefi/IoUtil.java index e7efa9e4e9..6337fc240d 100644 --- a/java_console/autotest/src/com/rusefi/IoUtil.java +++ b/java_console/autotest/src/com/rusefi/IoUtil.java @@ -10,6 +10,7 @@ import com.rusefi.io.tcp.TcpConnector; import com.rusefi.waves.WaveChart; import com.rusefi.waves.WaveChartParser; import com.rusefi.waves.WaveReport; +import jssc.SerialPortList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -30,12 +31,16 @@ public class IoUtil { * @throws IllegalStateException if command was not confirmed */ static void sendCommand(String command) { + sendCommand(command, CommandQueue.DEFAULT_TIMEOUT); + } + + static void sendCommand(String command, int timeoutMs) { final CountDownLatch responseLatch = new CountDownLatch(1); long time = System.currentTimeMillis(); if (LinkManager.hasError()) throw new IllegalStateException("IO error"); FileLog.MAIN.logLine("Sending command [" + command + "]"); - CommandQueue.getInstance().write(command, CommandQueue.DEFAULT_TIMEOUT, new InvocationConfirmationListener() { + CommandQueue.getInstance().write(command, timeoutMs, new InvocationConfirmationListener() { @Override public void onCommandConfirmation() { responseLatch.countDown(); @@ -77,7 +82,7 @@ public class IoUtil { final AtomicReference result = new AtomicReference<>(); FileLog.MAIN.logLine("waiting for next chart"); - LinkManager.engineState.registerStringValueAction(WaveReport.WAVE_CHART, new EngineState.ValueCallback() { + LinkManager.engineState.replaceStringValueAction(WaveReport.WAVE_CHART, new EngineState.ValueCallback() { @Override public void onUpdate(String value) { waveChartLatch.countDown(); @@ -88,7 +93,7 @@ public class IoUtil { long waitStartTime = System.currentTimeMillis(); wait(waveChartLatch, timeout); FileLog.MAIN.logLine("got next chart in " + (System.currentTimeMillis() - waitStartTime) + "ms"); - LinkManager.engineState.removeAction(WaveReport.WAVE_CHART); + LinkManager.engineState.replaceStringValueAction(WaveReport.WAVE_CHART, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); if (result.get() == null) throw new IllegalStateException("Chart timeout: " + timeout); return result.get(); @@ -182,4 +187,26 @@ public class IoUtil { throw new IllegalStateException(e); } } + + /** + * @return null if no port located + */ + static String getDefaultPort() { + String[] ports = SerialPortList.getPortNames(); + if (ports.length == 0) { + System.out.println("Port not specified and no ports found"); + return null; + } + String port = ports[ports.length - 1]; + System.out.println("Using last of " + ports.length + " port(s)"); + return port; + } + + static void realHardwareConnect(String port) { + LinkManager.start(port); + LinkManager.open(); + LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); + LinkManager.engineState.registerStringValueAction(EngineState.OUTPIN_TAG, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); + LinkManager.engineState.registerStringValueAction(AverageAnglesUtil.KEY, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); + } } diff --git a/java_console/autotest/src/com/rusefi/RealHwTest.java b/java_console/autotest/src/com/rusefi/RealHwTest.java index 102e46e90b..ba53a44c50 100644 --- a/java_console/autotest/src/com/rusefi/RealHwTest.java +++ b/java_console/autotest/src/com/rusefi/RealHwTest.java @@ -1,5 +1,7 @@ package com.rusefi; +import static com.rusefi.AutoTest.*; + /** * this test connects to real hardware via serial port * (c) Andrey Belomutskiy 2013-2015 @@ -7,6 +9,24 @@ package com.rusefi; */ public class RealHwTest { public static void main(String[] args) { + FileLog.MAIN.start(); + String port; + if (args.length == 1) { + port = args[0]; + } else if (args.length == 0) { + port = IoUtil.getDefaultPort(); + if (port == null) + return; + } else { + System.out.println("Only one optional argument expected: port number"); + return; + } + runRealHardwareTest(port); + } + + private static void runRealHardwareTest(String port) { + IoUtil.realHardwareConnect(port); + mainTestBody(); } } diff --git a/java_console/io/src/com/rusefi/io/CommandQueue.java b/java_console/io/src/com/rusefi/io/CommandQueue.java index 95720c2c63..a461202948 100644 --- a/java_console/io/src/com/rusefi/io/CommandQueue.java +++ b/java_console/io/src/com/rusefi/io/CommandQueue.java @@ -83,7 +83,9 @@ public class CommandQueue { } private CommandQueue() { - new Thread(runnable, "Commands Queue").start(); + Thread thread = new Thread(runnable, "Commands Queue"); + thread.setDaemon(true); + thread.start(); final MessagesCentral mc = MessagesCentral.getInstance(); mc.addListener(new MessagesCentral.MessageListener() { @Override @@ -139,22 +141,22 @@ public class CommandQueue { /** * Non-blocking command request */ - public void write(String command, int timeout, InvocationConfirmationListener listener) { + public void write(String command, int timeoutMs, InvocationConfirmationListener listener) { for (CommandQueueListener cql : commandListeners) cql.onCommand(command); - pendingCommands.add(new MethodInvocation(command, timeout, listener)); + pendingCommands.add(new MethodInvocation(command, timeoutMs, listener)); } static class MethodInvocation { private final String text; - private final int timeout; + private final int timeoutMs; private final InvocationConfirmationListener listener; - MethodInvocation(String text, int timeout, InvocationConfirmationListener listener) { + MethodInvocation(String text, int timeoutMs, InvocationConfirmationListener listener) { this.text = text; - this.timeout = timeout; + this.timeoutMs = timeoutMs; this.listener = listener; } @@ -163,7 +165,7 @@ public class CommandQueue { } public int getTimeout() { - return timeout; + return timeoutMs; } } diff --git a/java_console/models/src/com/rusefi/AverageAnglesUtil.java b/java_console/models/src/com/rusefi/AverageAnglesUtil.java index 70285a791d..75e3d3af2d 100644 --- a/java_console/models/src/com/rusefi/AverageAnglesUtil.java +++ b/java_console/models/src/com/rusefi/AverageAnglesUtil.java @@ -15,7 +15,8 @@ import java.util.*; */ public class AverageAnglesUtil { - public static final String ANALOG_CHART = "analog_chart,"; + public static final String KEY = "analog_chart"; + public static final String ANALOG_CHART = KEY + ","; private static int currentRpm = -1; diff --git a/java_console/models/src/com/rusefi/core/EngineState.java b/java_console/models/src/com/rusefi/core/EngineState.java index 68b319c8ee..a9075cccab 100644 --- a/java_console/models/src/com/rusefi/core/EngineState.java +++ b/java_console/models/src/com/rusefi/core/EngineState.java @@ -2,6 +2,7 @@ package com.rusefi.core; import com.rusefi.FileLog; import com.rusefi.SensorConversion; +import com.rusefi.waves.WaveReport; import org.jetbrains.annotations.NotNull; import javax.swing.*; @@ -30,6 +31,11 @@ public class EngineState { private static final CharSequence TS_PROTOCOL_TAG = "ts_p_al"; private final Object lock = new Object(); + public void replaceStringValueAction(String key, ValueCallback callback) { + removeAction(key); + registerStringValueAction(key, callback); + } + private static class StringActionPair extends Pair> { public final String prefix; diff --git a/java_console/ui/src/com/rusefi/AnalogChartCentral.java b/java_console/ui/src/com/rusefi/AnalogChartCentral.java index 5576227181..9ebf80e5f7 100644 --- a/java_console/ui/src/com/rusefi/AnalogChartCentral.java +++ b/java_console/ui/src/com/rusefi/AnalogChartCentral.java @@ -7,12 +7,11 @@ import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class AnalogChartCentral { - private static final String KEY = "analog_chart"; private static List listeners = new CopyOnWriteArrayList<>(); static { - LinkManager.engineState.registerStringValueAction(KEY, new EngineState.ValueCallback() { + LinkManager.engineState.registerStringValueAction(AverageAnglesUtil.KEY, new EngineState.ValueCallback() { @Override public void onUpdate(String message) { for (AnalogChartListener listener : listeners) diff --git a/java_console/ui/src/com/rusefi/CmdLine.java b/java_console/ui/src/com/rusefi/CmdLine.java index 68acecaef2..a728d12c15 100644 --- a/java_console/ui/src/com/rusefi/CmdLine.java +++ b/java_console/ui/src/com/rusefi/CmdLine.java @@ -1,9 +1,5 @@ package com.rusefi; -import com.rusefi.core.EngineState; -import com.rusefi.io.LinkManager; -import jssc.SerialPortList; - /** * (c) Andrey Belomutskiy 2013-2015 * 2/22/2015 @@ -16,13 +12,9 @@ public class CmdLine { } String command = args[0]; if (args.length == 1) { - String[] ports = SerialPortList.getPortNames(); - if (ports.length == 0) { - System.out.println("Port not specified and no ports found"); + String port = IoUtil.getDefaultPort(); + if (port == null) return; - } - String port = ports[ports.length - 1]; - System.out.println("Using last of " + ports.length + " port(s)"); executeCommand(command, port); } else { executeCommand(command, args[1]); @@ -32,13 +24,11 @@ public class CmdLine { private static void executeCommand(String command, String port) { System.out.println("Sending " + command); System.out.println("Sending to " + port); - LinkManager.start(port); - LinkManager.open(); - LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); - LinkManager.engineState.registerStringValueAction(EngineState.OUTPIN_TAG, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); + IoUtil.realHardwareConnect(port); IoUtil.sendCommand(command); System.out.println("Done!"); System.exit(-1); } + }