From 287cced96176cc4cf61b5cab3703df105ebb0048 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sun, 22 Feb 2015 15:04:11 -0600 Subject: [PATCH] auto-sync --- .../autotest/src/com/rusefi/IoUtil.java | 12 +++++-- .../io/src/com/rusefi/io/LinkManager.java | 7 +++- .../src/com/rusefi/io/serial/PortHolder.java | 12 ++++--- .../com/rusefi/io/serial/SerialConnector.java | 2 ++ .../src/com/rusefi/core/EngineState.java | 15 +++++--- java_console/ui/src/com/rusefi/CmdLine.java | 35 +++++++++++++++++++ java_console/ui/src/com/rusefi/Launcher.java | 4 +-- java_console/ui/ui.iml | 4 +-- 8 files changed, 75 insertions(+), 16 deletions(-) diff --git a/java_console/autotest/src/com/rusefi/IoUtil.java b/java_console/autotest/src/com/rusefi/IoUtil.java index 583d821804..e7efa9e4e9 100644 --- a/java_console/autotest/src/com/rusefi/IoUtil.java +++ b/java_console/autotest/src/com/rusefi/IoUtil.java @@ -22,6 +22,13 @@ import static com.rusefi.waves.WaveReport.isCloseEnough; * 3/19/14. */ public class IoUtil { + private static final int CMD_TIMEOUT = 20; + + /** + * Send a command and wait for the confirmation + * + * @throws IllegalStateException if command was not confirmed + */ static void sendCommand(String command) { final CountDownLatch responseLatch = new CountDownLatch(1); long time = System.currentTimeMillis(); @@ -34,7 +41,7 @@ public class IoUtil { responseLatch.countDown(); } }); - wait(responseLatch, 20); + wait(responseLatch, CMD_TIMEOUT); if (LinkManager.hasError()) throw new IllegalStateException("IO error"); FileLog.MAIN.logLine("Command [" + command + "] executed in " + (System.currentTimeMillis() - time)); @@ -163,10 +170,11 @@ public class IoUtil { * TCP connector is blocking */ LinkManager.open(); - + LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); waitForFirstResponse(); } + @SuppressWarnings("UnusedDeclaration") static void sleep(int seconds) { try { Thread.sleep(seconds * 1000L); diff --git a/java_console/io/src/com/rusefi/io/LinkManager.java b/java_console/io/src/com/rusefi/io/LinkManager.java index 9efe5b5497..5a10a9c0fd 100644 --- a/java_console/io/src/com/rusefi/io/LinkManager.java +++ b/java_console/io/src/com/rusefi/io/LinkManager.java @@ -4,6 +4,7 @@ import com.rusefi.FileLog; import com.rusefi.core.EngineState; import com.rusefi.io.serial.SerialConnector; import com.rusefi.io.tcp.TcpConnector; +import org.jetbrains.annotations.NotNull; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -16,7 +17,7 @@ import java.util.concurrent.ThreadFactory; public class LinkManager { public final static Executor IO_EXECUTOR = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override - public Thread newThread(Runnable r) { + public Thread newThread(@NotNull Runnable r) { Thread t = new Thread(r); t.setName("IO executor thread"); t.setDaemon(true); // need daemon thread so that COM thread is also daemon @@ -57,6 +58,7 @@ public class LinkManager { public static boolean isStimulationMode; public static void start(String port) { + FileLog.MAIN.logLine("Starting " + port); if (isLogViewerMode(port)) { connector = LinkManager.VOID; } else if (TcpConnector.isTcpPort(port)) { @@ -75,6 +77,9 @@ public class LinkManager { return connector == LinkManager.VOID; } + /** + * todo: should this be merged into {@link #start(String)} ? + */ public static void open() { if (connector == null) throw new NullPointerException("connector"); diff --git a/java_console/io/src/com/rusefi/io/serial/PortHolder.java b/java_console/io/src/com/rusefi/io/serial/PortHolder.java index b38c5f3356..b79f2aa443 100644 --- a/java_console/io/src/com/rusefi/io/serial/PortHolder.java +++ b/java_console/io/src/com/rusefi/io/serial/PortHolder.java @@ -5,6 +5,7 @@ import com.rusefi.core.EngineState; import com.rusefi.io.DataListener; import jssc.SerialPort; import jssc.SerialPortException; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -22,8 +23,6 @@ public class PortHolder { private static PortHolder instance = new PortHolder(); private final Object portLock = new Object(); - public static long startedAt = System.currentTimeMillis(); - public PortHolderListener listener = PortHolderListener.VOID; private PortHolder() { @@ -45,7 +44,7 @@ public class PortHolder { FileLog.MAIN.logLine("Opening " + port + " @ " + BAUD_RATE); boolean opened = serialPort.openPort();//Open serial port if (!opened) - FileLog.MAIN.logLine("opened: " + opened); + FileLog.MAIN.logLine("not opened!"); serialPort.setParams(BAUD_RATE, 8, 1, 0);//Set params. int mask = SerialPort.MASK_RXCHAR; //Set the prepared mask @@ -55,7 +54,7 @@ public class PortHolder { FileLog.rlog("ERROR " + e.getMessage()); return false; } - + FileLog.rlog("PortHolder: Sleeping a bit"); try { // todo: why is this delay here? add a comment Thread.sleep(200); @@ -69,6 +68,7 @@ public class PortHolder { } try { + FileLog.rlog("PortHolder: test command"); /** * Let's make sure we have not connected to Tuner Studio port? * @see EngineState#TS_PROTOCOL_TAG @@ -119,7 +119,9 @@ public class PortHolder { } } - private void doWriteCommand(String command) throws SerialPortException { + private void doWriteCommand(@NotNull String command) throws SerialPortException { + if (serialPort == null) + throw new NullPointerException("serialPort"); serialPort.writeString(command + "\r\n"); } diff --git a/java_console/io/src/com/rusefi/io/serial/SerialConnector.java b/java_console/io/src/com/rusefi/io/serial/SerialConnector.java index 3f65d192df..731789e09e 100644 --- a/java_console/io/src/com/rusefi/io/serial/SerialConnector.java +++ b/java_console/io/src/com/rusefi/io/serial/SerialConnector.java @@ -1,5 +1,6 @@ package com.rusefi.io.serial; +import com.rusefi.FileLog; import com.rusefi.io.LinkConnector; /** @@ -13,6 +14,7 @@ public class SerialConnector implements LinkConnector { @Override public void connect() { + FileLog.MAIN.logLine("SerialConnector: connecting"); SerialManager.scheduleOpening(); } diff --git a/java_console/models/src/com/rusefi/core/EngineState.java b/java_console/models/src/com/rusefi/core/EngineState.java index baee086a5f..7ee5f45529 100644 --- a/java_console/models/src/com/rusefi/core/EngineState.java +++ b/java_console/models/src/com/rusefi/core/EngineState.java @@ -16,13 +16,13 @@ import java.util.concurrent.CopyOnWriteArrayList; */ public class EngineState { public static final String SEPARATOR = ","; - public static final int SNIFFED_ADC_COUNT = 16; public static final ValueCallback NOTHING = new ValueCallback() { @Override public void onUpdate(String value) { } }; public static final String PACKING_DELIMITER = ":"; + public static final String RUS_EFI_VERSION_TAG = "rusEfiVersion"; /** * If we get this tag we have probably connected to the wrong port */ @@ -43,11 +43,11 @@ public class EngineState { } } - public final List timeListeners = new CopyOnWriteArrayList(); + public final List timeListeners = new CopyOnWriteArrayList<>(); private final ResponseBuffer buffer; - private final List actions = new ArrayList(); - private final Set keys = new TreeSet(String.CASE_INSENSITIVE_ORDER); + private final List actions = new ArrayList<>(); + private final Set keys = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); public EngineState(@NotNull final EngineStateListener listener) { buffer = new ResponseBuffer(new ResponseBuffer.ResponseListener() { @@ -285,6 +285,13 @@ public class EngineState { } public interface ValueCallback { + static final ValueCallback VOID = new ValueCallback() { + @Override + public void onUpdate(Object value) { + + } + }; + void onUpdate(V value); } diff --git a/java_console/ui/src/com/rusefi/CmdLine.java b/java_console/ui/src/com/rusefi/CmdLine.java index af80ba863f..c5b0e76de6 100644 --- a/java_console/ui/src/com/rusefi/CmdLine.java +++ b/java_console/ui/src/com/rusefi/CmdLine.java @@ -1,8 +1,43 @@ package com.rusefi; +import com.rusefi.core.EngineState; +import com.rusefi.io.LinkManager; +import jssc.SerialPortList; + /** * (c) Andrey Belomutskiy 2013-2015 * 2/22/2015 */ public class CmdLine { + public static void main(String[] args) { + if (args.length == 0 || args.length > 2) { + System.out.println("CmdLine COMMAND [PORT]"); + return; + } + 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"); + 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]); + } + } + + 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); + + IoUtil.sendCommand(command); + System.out.println("Done!"); + System.exit(-1); + } } diff --git a/java_console/ui/src/com/rusefi/Launcher.java b/java_console/ui/src/com/rusefi/Launcher.java index 0b05a442c1..efd74914f2 100644 --- a/java_console/ui/src/com/rusefi/Launcher.java +++ b/java_console/ui/src/com/rusefi/Launcher.java @@ -28,7 +28,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; * @see com.rusefi.ui.engine.EngineSnifferPanel */ public class Launcher extends FrameHelper { - public static final int CONSOLE_VERSION = 20150216; + public static final int CONSOLE_VERSION = 20150222; public static final boolean SHOW_STIMULATOR = true; public static final String TAB_INDEX = "main_tab"; private final String port; @@ -84,7 +84,7 @@ public class Launcher extends FrameHelper { LinkManager.open(); - LinkManager.engineState.registerStringValueAction("rusEfiVersion", new EngineState.ValueCallback() { + LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, new EngineState.ValueCallback() { @Override public void onUpdate(String firmwareVersion) { setTitle(firmwareVersion); diff --git a/java_console/ui/ui.iml b/java_console/ui/ui.iml index 8706fc21f6..0a72c5c228 100644 --- a/java_console/ui/ui.iml +++ b/java_console/ui/ui.iml @@ -17,6 +17,6 @@ + - - + \ No newline at end of file