From 5c73e35474439065e67e1722b93482cace01c95c Mon Sep 17 00:00:00 2001 From: rusefi Date: Mon, 1 Jul 2019 00:22:47 -0400 Subject: [PATCH] getting ready for driver install --- .../autotest/src/com/rusefi/AutoTest.java | 2 +- .../autotest/src/com/rusefi/IoUtil.java | 2 +- ...ecHelper.java => SimulatorExecHelper.java} | 8 +- .../logging/src/com/rusefi/FileLog.java | 10 ++- java_console/ui/src/com/rusefi/Launcher.java | 2 +- .../ui/src/com/rusefi/SimulatorHelper.java | 2 +- .../ui/src/com/rusefi/StartupFrame.java | 2 +- .../src/com/rusefi/maintenance/EraseChip.java | 2 +- .../com/rusefi/maintenance/ExecHelper.java | 77 +++++++++++++++++++ .../rusefi/maintenance/FirmwareFlasher.java | 4 +- .../maintenance/ProcessStatusWindow.java | 71 +---------------- .../ui/src/com/rusefi/ui/StatusConsumer.java | 5 ++ .../ui/src/com/rusefi/ui/StatusWindow.java | 3 +- 13 files changed, 106 insertions(+), 84 deletions(-) rename java_console/autotest/src/com/rusefi/{ExecHelper.java => SimulatorExecHelper.java} (91%) create mode 100644 java_console/ui/src/com/rusefi/maintenance/ExecHelper.java create mode 100644 java_console/ui/src/com/rusefi/ui/StatusConsumer.java diff --git a/java_console/autotest/src/com/rusefi/AutoTest.java b/java_console/autotest/src/com/rusefi/AutoTest.java index 1c54cd28ef..94410e7ddb 100644 --- a/java_console/autotest/src/com/rusefi/AutoTest.java +++ b/java_console/autotest/src/com/rusefi/AutoTest.java @@ -486,7 +486,7 @@ public class AutoTest { e.printStackTrace(); failed = true; } finally { - ExecHelper.destroy(); + SimulatorExecHelper.destroy(); } if (failed) System.exit(-1); diff --git a/java_console/autotest/src/com/rusefi/IoUtil.java b/java_console/autotest/src/com/rusefi/IoUtil.java index 481e505ad9..c4ab99c901 100644 --- a/java_console/autotest/src/com/rusefi/IoUtil.java +++ b/java_console/autotest/src/com/rusefi/IoUtil.java @@ -109,7 +109,7 @@ public class IoUtil { if (startProcess) { if (!TcpConnector.getAvailablePorts().isEmpty()) throw new IllegalStateException("Port already binded on startup?"); - ExecHelper.startSimulator(); + SimulatorExecHelper.startSimulator(); } diff --git a/java_console/autotest/src/com/rusefi/ExecHelper.java b/java_console/autotest/src/com/rusefi/SimulatorExecHelper.java similarity index 91% rename from java_console/autotest/src/com/rusefi/ExecHelper.java rename to java_console/autotest/src/com/rusefi/SimulatorExecHelper.java index a550825020..e2914adbb6 100644 --- a/java_console/autotest/src/com/rusefi/ExecHelper.java +++ b/java_console/autotest/src/com/rusefi/SimulatorExecHelper.java @@ -10,7 +10,7 @@ import java.util.function.Consumer; * 3/18/14 * (c) Andrey Belomutskiy */ -public class ExecHelper { +public class SimulatorExecHelper { // see also SimulatorHelper private static final String SIMULATOR_BINARY = "../simulator/build/rusefi_simulator.exe"; static Process simulatorProcess; @@ -26,10 +26,10 @@ public class ExecHelper { FileLog.MAIN.logLine("Binary size: " + new File(SIMULATOR_BINARY).length()); FileLog.MAIN.logLine("Executing " + SIMULATOR_BINARY); - ExecHelper.simulatorProcess = Runtime.getRuntime().exec(SIMULATOR_BINARY); - FileLog.MAIN.logLine("simulatorProcess: " + ExecHelper.simulatorProcess); + SimulatorExecHelper.simulatorProcess = Runtime.getRuntime().exec(SIMULATOR_BINARY); + FileLog.MAIN.logLine("simulatorProcess: " + SimulatorExecHelper.simulatorProcess); - dumpProcessOutput(ExecHelper.simulatorProcess); + dumpProcessOutput(SimulatorExecHelper.simulatorProcess); FileLog.MAIN.logLine("exitValue: " + simulatorProcess.exitValue()); diff --git a/java_console/logging/src/com/rusefi/FileLog.java b/java_console/logging/src/com/rusefi/FileLog.java index 8702cf2d6c..a1f4371933 100644 --- a/java_console/logging/src/com/rusefi/FileLog.java +++ b/java_console/logging/src/com/rusefi/FileLog.java @@ -56,10 +56,18 @@ public enum FileLog { } private static void printOsInfo() { - MAIN.logLine("OS name: " + System.getProperty("os.name")); + MAIN.logLine("OS name: " + getOsName()); MAIN.logLine("OS version: " + System.getProperty(OS_VERSION)); } + private static String getOsName() { + return System.getProperty("os.name"); + } + + public static boolean isWindows() { + return getOsName().contains("Windows"); + } + private FileOutputStream openLog() throws FileNotFoundException { String date = getDate(); createFolderIfNeeded(); diff --git a/java_console/ui/src/com/rusefi/Launcher.java b/java_console/ui/src/com/rusefi/Launcher.java index 423576f160..a2f65feaf4 100644 --- a/java_console/ui/src/com/rusefi/Launcher.java +++ b/java_console/ui/src/com/rusefi/Launcher.java @@ -46,7 +46,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; * @see EngineSnifferPanel */ public class Launcher { - public static final int CONSOLE_VERSION = 20190628; + public static final int CONSOLE_VERSION = 20190630; public static final boolean SHOW_STIMULATOR = false; public static final String INPUT_FILES_PATH = ".."; private static final String TAB_INDEX = "main_tab"; diff --git a/java_console/ui/src/com/rusefi/SimulatorHelper.java b/java_console/ui/src/com/rusefi/SimulatorHelper.java index 9575036505..8a34e0ed68 100644 --- a/java_console/ui/src/com/rusefi/SimulatorHelper.java +++ b/java_console/ui/src/com/rusefi/SimulatorHelper.java @@ -34,7 +34,7 @@ public class SimulatorHelper { FileLog.SIMULATOR_CONSOLE.start(); process = Runtime.getRuntime().exec(BINARY); FileLog.MAIN.logLine("Executing " + BINARY + "=" + process); - ExecHelper.dumpProcessOutput(process); + SimulatorExecHelper.dumpProcessOutput(process); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/java_console/ui/src/com/rusefi/StartupFrame.java b/java_console/ui/src/com/rusefi/StartupFrame.java index 3b5ba7d579..0b161df51e 100644 --- a/java_console/ui/src/com/rusefi/StartupFrame.java +++ b/java_console/ui/src/com/rusefi/StartupFrame.java @@ -127,7 +127,7 @@ public class StartupFrame { installMessage(noPortsMessage, "Check you cables. Check your drivers. Do you want to start simulator maybe?"); realHardwarePanel.add(new URLLabel(VCP_DRIVER_TEXT, VCP_DRIVER_URI)); - if (ProcessStatusWindow.isWindows()) { + if (FileLog.isWindows()) { realHardwarePanel.add(new HorizontalLine()); // for F7 builds we just build one file at the moment realHardwarePanel.add(new FirmwareFlasher(FirmwareFlasher.IMAGE_FILE, "Program Firmware").getButton()); diff --git a/java_console/ui/src/com/rusefi/maintenance/EraseChip.java b/java_console/ui/src/com/rusefi/maintenance/EraseChip.java index 5debf9ba91..b7224e30c0 100644 --- a/java_console/ui/src/com/rusefi/maintenance/EraseChip.java +++ b/java_console/ui/src/com/rusefi/maintenance/EraseChip.java @@ -23,7 +23,7 @@ public class EraseChip extends ProcessStatusWindow { if (dialogResult != JOptionPane.YES_OPTION) return; wnd.showFrame("rusEfi Firmware Flasher"); - submitAction(() -> executeCommand(getEraseCommand())); + ExecHelper.submitAction(() -> executeCommand(getEraseCommand()), EraseChip.this.getClass() + " extProcessThread"); } }); } diff --git a/java_console/ui/src/com/rusefi/maintenance/ExecHelper.java b/java_console/ui/src/com/rusefi/maintenance/ExecHelper.java new file mode 100644 index 0000000000..c07b2c0c72 --- /dev/null +++ b/java_console/ui/src/com/rusefi/maintenance/ExecHelper.java @@ -0,0 +1,77 @@ +package com.rusefi.maintenance; + +import com.rusefi.SimulatorExecHelper; +import com.rusefi.ui.StatusConsumer; +import org.jetbrains.annotations.NotNull; + +import java.io.*; + +/** + * @see SimulatorExecHelper + */ +public class ExecHelper { + private static boolean isRunning(Process p) { + try { + p.exitValue(); + return false; + } catch (IllegalThreadStateException e) { + return true; + } + } + + /** + * This method listens to a data stream from the process, appends messages to UI + * and accumulates output in a buffer + */ + private static void startStreamThread(final Process p, final InputStream stream, final StringBuffer buffer, final StatusConsumer wnd) { + final Thread t = new Thread(() -> { + try { + BufferedReader bis = new BufferedReader(new InputStreamReader(stream)); + while (isRunning(p)) { + String line = bis.readLine(); + if (line == null) + break; + wnd.appendMsg(line); + buffer.append(line); + } + } catch (IOException e) { + wnd.appendMsg("Stream " + e); + } + }); + t.setDaemon(true); + t.start(); + } + + @NotNull + public static StringBuffer executeCommand(String workingDirPath, String command, String binaryRelativeName, StatusConsumer wnd) { + StringBuffer error = new StringBuffer(); + String binaryFullName = workingDirPath + File.separator + binaryRelativeName; + if (!new File(binaryFullName).exists()) { + wnd.appendMsg(binaryFullName + " not found :("); + return error; + } + + command = workingDirPath + File.separator + command; + wnd.appendMsg("Executing " + command); + StringBuffer output = new StringBuffer(); + try { + File workingDir = new File(workingDirPath); + Process p = Runtime.getRuntime().exec(command, null, workingDir); + startStreamThread(p, p.getInputStream(), output, wnd); + startStreamThread(p, p.getErrorStream(), error, wnd); + p.waitFor(); + } catch (IOException e) { + wnd.appendMsg("IOError: " + e); + } catch (InterruptedException e) { + wnd.appendMsg("WaitError: " + e); + } + wnd.appendMsg("Done!"); + return error; + } + + protected static void submitAction(Runnable runnable, String threadName) { + Thread thread = new Thread(runnable, threadName); + thread.setDaemon(true); + thread.start(); + } +} diff --git a/java_console/ui/src/com/rusefi/maintenance/FirmwareFlasher.java b/java_console/ui/src/com/rusefi/maintenance/FirmwareFlasher.java index cb89ad7580..229d88d1c5 100644 --- a/java_console/ui/src/com/rusefi/maintenance/FirmwareFlasher.java +++ b/java_console/ui/src/com/rusefi/maintenance/FirmwareFlasher.java @@ -38,14 +38,14 @@ public class FirmwareFlasher extends ProcessStatusWindow { wnd.showFrame("rusEfi Firmware Flasher"); - submitAction(() -> doFlashFirmware()); + ExecHelper.submitAction(() -> doFlashFirmware(), getClass() + " extProcessThread"); } }); } public static String getOpenocdCommad() { String cfg = "stm32f4discovery.cfg"; - return BINARY_LOCATION + File.separator + OPENOCD_EXE + " -f openocd/" + cfg; + return OPENOCD_EXE + " -f openocd/" + cfg; } private void doFlashFirmware() { diff --git a/java_console/ui/src/com/rusefi/maintenance/ProcessStatusWindow.java b/java_console/ui/src/com/rusefi/maintenance/ProcessStatusWindow.java index b6de692268..87b6add7b0 100644 --- a/java_console/ui/src/com/rusefi/maintenance/ProcessStatusWindow.java +++ b/java_console/ui/src/com/rusefi/maintenance/ProcessStatusWindow.java @@ -2,82 +2,13 @@ package com.rusefi.maintenance; import com.rusefi.ui.StatusWindow; -import java.io.*; - /** * (c) Andrey Belomutskiy 2013-2018 */ public class ProcessStatusWindow { - - public static boolean isWindows() { - return System.getProperty("os.name").toLowerCase().contains("win"); - } - protected final StatusWindow wnd = new StatusWindow(); - protected static boolean isRunning(Process p) { - try { - p.exitValue(); - return false; - } catch (IllegalThreadStateException e) { - return true; - } - } - - /** - * This method listens to a data stream from the process, appends messages to UI - * and accumulates output in a buffer - */ - protected void startStreamThread(final Process p, final InputStream stream, final StringBuffer buffer) { - final Thread t = new Thread(new Runnable() { - @Override - public void run() { - try { - BufferedReader bis = new BufferedReader(new InputStreamReader(stream)); - while (isRunning(p)) { - String line = bis.readLine(); - if (line == null) - break; - wnd.appendMsg(line); - buffer.append(line); - } - } catch (IOException e) { - wnd.appendMsg("Stream " + e); - } - } - }); - t.setDaemon(true); - t.start(); - } - protected StringBuffer executeCommand(String command) { - StringBuffer error = new StringBuffer(); - String binaryFullName = FirmwareFlasher.BINARY_LOCATION + File.separator + FirmwareFlasher.OPENOCD_EXE; - if (!new File(binaryFullName).exists()) { - wnd.appendMsg(binaryFullName + " not found :("); - return error; - } - - wnd.appendMsg("Executing " + command); - StringBuffer output = new StringBuffer(); - try { - File workingDir = new File(FirmwareFlasher.BINARY_LOCATION); - Process p = Runtime.getRuntime().exec(command, null, workingDir); - startStreamThread(p, p.getInputStream(), output); - startStreamThread(p, p.getErrorStream(), error); - p.waitFor(); - } catch (IOException e) { - wnd.appendMsg("IOError: " + e); - } catch (InterruptedException e) { - wnd.appendMsg("WaitError: " + e); - } - wnd.appendMsg("Done!"); - return error; - } - - protected void submitAction(Runnable runnable) { - Thread thread = new Thread(runnable, "console extProcessThread"); - thread.setDaemon(true); - thread.start(); + return ExecHelper.executeCommand(FirmwareFlasher.BINARY_LOCATION, command, FirmwareFlasher.OPENOCD_EXE, wnd); } } \ No newline at end of file diff --git a/java_console/ui/src/com/rusefi/ui/StatusConsumer.java b/java_console/ui/src/com/rusefi/ui/StatusConsumer.java new file mode 100644 index 0000000000..a911fff07d --- /dev/null +++ b/java_console/ui/src/com/rusefi/ui/StatusConsumer.java @@ -0,0 +1,5 @@ +package com.rusefi.ui; + +public interface StatusConsumer { + void appendMsg(String s); +} diff --git a/java_console/ui/src/com/rusefi/ui/StatusWindow.java b/java_console/ui/src/com/rusefi/ui/StatusWindow.java index bf689076e6..05721c9858 100644 --- a/java_console/ui/src/com/rusefi/ui/StatusWindow.java +++ b/java_console/ui/src/com/rusefi/ui/StatusWindow.java @@ -12,7 +12,7 @@ import java.awt.*; * (c) Andrey Belomutskiy 2013-2018 * 3/7/2015 */ -public class StatusWindow { +public class StatusWindow implements StatusConsumer { // todo: extract driver from console bundle? find a separate driver bundle? private static final String CONSOLE_DRIVER_URI = "http://www.st.com/st-web-ui/static/active/en/st_prod_software_internet/resource/technical/software/utility/stsw-link004.zip"; private final JTextArea log = new JTextArea(); @@ -53,6 +53,7 @@ public class StatusWindow { log.setText(""); // let's remove stuff from previous invocation } + @Override public void appendMsg(final String s) { SwingUtilities.invokeLater(new Runnable() { @Override