still trying to simplify things

This commit is contained in:
rusefi 2020-05-16 12:54:15 -04:00
parent aef391b2bd
commit 7f92b3b40d
6 changed files with 38 additions and 38 deletions

View File

@ -5,6 +5,7 @@ import com.rusefi.core.EngineState;
import com.rusefi.core.Sensor; import com.rusefi.core.Sensor;
import com.rusefi.core.SensorCentral; import com.rusefi.core.SensorCentral;
import com.rusefi.io.CommandQueue; import com.rusefi.io.CommandQueue;
import com.rusefi.io.ConnectionStateListener;
import com.rusefi.io.InvocationConfirmationListener; import com.rusefi.io.InvocationConfirmationListener;
import com.rusefi.io.LinkManager; import com.rusefi.io.LinkManager;
import com.rusefi.io.tcp.TcpConnector; import com.rusefi.io.tcp.TcpConnector;
@ -131,11 +132,11 @@ public class IoUtil {
// Thread.sleep(3000); // Thread.sleep(3000);
// //
// FileLog.rlog("Got a TCP port! Connecting..."); // FileLog.rlog("Got a TCP port! Connecting...");
LinkManager.start("" + TcpConnector.DEFAULT_PORT);
/** /**
* TCP connector is blocking * TCP connector is blocking
*/ */
LinkManager.open(); LinkManager.startAndConnect("" + TcpConnector.DEFAULT_PORT, ConnectionStateListener.VOID);
LinkManager.engineState.registerStringValueAction(Fields.PROTOCOL_VERSION_TAG, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID); LinkManager.engineState.registerStringValueAction(Fields.PROTOCOL_VERSION_TAG, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID);
waitForFirstResponse(); waitForFirstResponse();
} }

View File

@ -18,9 +18,8 @@ import java.util.concurrent.*;
public class LinkManager { public class LinkManager {
@NotNull @NotNull
public static CountDownLatch connect(String port) { public static CountDownLatch connect(String port) {
start(port);
final CountDownLatch connected = new CountDownLatch(1); final CountDownLatch connected = new CountDownLatch(1);
open(new ConnectionStateListener() { startAndConnect(port, new ConnectionStateListener() {
@Override @Override
public void onConnectionFailed() { public void onConnectionFailed() {
System.out.println("CONNECTION FAILED, did you specify the right port name?"); System.out.println("CONNECTION FAILED, did you specify the right port name?");
@ -134,7 +133,7 @@ public class LinkManager {
*/ */
public static boolean isSimulationMode; public static boolean isSimulationMode;
public static void start(String port) { public static void startAndConnect(String port, ConnectionStateListener stateListener) {
FileLog.MAIN.logLine("LinkManager: Starting " + port); FileLog.MAIN.logLine("LinkManager: Starting " + port);
if (isLogViewerMode(port)) { if (isLogViewerMode(port)) {
connector = LinkConnector.VOID; connector = LinkConnector.VOID;
@ -144,6 +143,7 @@ public class LinkManager {
} else { } else {
connector = new SerialConnector(port); connector = new SerialConnector(port);
} }
connect(stateListener);
} }
public static boolean isLogViewerMode(String port) { public static boolean isLogViewerMode(String port) {
@ -154,17 +154,14 @@ public class LinkManager {
return connector == LinkConnector.VOID; return connector == LinkConnector.VOID;
} }
/** public static void connect(ConnectionStateListener listener) {
* todo: should this be merged into {@link #start(String)} ?
*/
public static void open(ConnectionStateListener listener) {
if (connector == null) if (connector == null)
throw new NullPointerException("connector"); throw new NullPointerException("connector");
connector.connect(listener); connector.connect(listener);
} }
public static void open() { public static void connect() {
open(ConnectionStateListener.VOID); connect(ConnectionStateListener.VOID);
} }
public static void send(String command, boolean fireEvent) throws InterruptedException { public static void send(String command, boolean fireEvent) throws InterruptedException {

View File

@ -3,11 +3,9 @@ package com.rusefi.io.serial;
import com.fazecast.jSerialComm.SerialPort; import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener; import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent; import com.fazecast.jSerialComm.SerialPortEvent;
import com.opensr5.Logger;
import com.opensr5.io.DataListener; import com.opensr5.io.DataListener;
import com.rusefi.FileLog; import com.rusefi.FileLog;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import org.jetbrains.annotations.NotNull;
/** /**
* https://github.com/Fazecast/jSerialComm looks to be alive as of 2019 * https://github.com/Fazecast/jSerialComm looks to be alive as of 2019
@ -74,16 +72,15 @@ public class SerialIoStreamJSerialComm implements IoStream {
sp.writeBytes(bytes, bytes.length); sp.writeBytes(bytes, bytes.length);
} }
/**
* Just open physical serial and not much more
* @see PortHolder#connectAndReadConfiguration()
*/
public static IoStream openPort(String port) { public static IoStream openPort(String port) {
return openPort(port, BaudRateHolder.INSTANCE.baudRate, FileLog.LOGGER); FileLog.LOGGER.info("[SerialIoStreamJSerialComm] " + port);
} SerialPort serialPort = SerialPort.getCommPort(port);
serialPort.setBaudRate(BaudRateHolder.INSTANCE.baudRate);
@NotNull serialPort.openPort();
private static IoStream openPort(String port, int baudRate, Logger logger) { return new SerialIoStreamJSerialComm(serialPort, port);
logger.info("[SerialIoStreamJSerialComm] " + port);
SerialPort sp = SerialPort.getCommPort(port);
sp.setBaudRate(baudRate);
sp.openPort();
return new SerialIoStreamJSerialComm(sp, port);
} }
} }

View File

@ -10,6 +10,7 @@ import com.rusefi.io.IoStream;
import com.rusefi.io.LinkManager; import com.rusefi.io.LinkManager;
import com.rusefi.io.serial.SerialIoStreamJSerialComm; import com.rusefi.io.serial.SerialIoStreamJSerialComm;
import com.rusefi.maintenance.ExecHelper; import com.rusefi.maintenance.ExecHelper;
import org.jetbrains.annotations.Nullable;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -24,6 +25,7 @@ public class ConsoleTools {
TOOLS.put("headless", ConsoleTools::runHeadless); TOOLS.put("headless", ConsoleTools::runHeadless);
TOOLS.put("compile", ConsoleTools::invokeCompileExpressionTool); TOOLS.put("compile", ConsoleTools::invokeCompileExpressionTool);
TOOLS.put("ptrace_enums", ConsoleTools::runPerfTraceTool); TOOLS.put("ptrace_enums", ConsoleTools::runPerfTraceTool);
TOOLS.put("save_binary_configuration", ConsoleTools::saveBinaryConfig);
TOOLS.put("functional_test", ConsoleTools::runFunctionalTest); TOOLS.put("functional_test", ConsoleTools::runFunctionalTest);
TOOLS.put("compile_fsio_file", ConsoleTools::runCompileTool); TOOLS.put("compile_fsio_file", ConsoleTools::runCompileTool);
TOOLS.put("firing_order", ConsoleTools::runFiringOrderTool); TOOLS.put("firing_order", ConsoleTools::runFiringOrderTool);
@ -37,8 +39,12 @@ public class ConsoleTools {
} }
} }
private static void saveBinaryConfig(String[] args) {
}
private static void sendCommand(String command) throws IOException { private static void sendCommand(String command) throws IOException {
String autoDetectedPort = Launcher.autoDetectPort(); String autoDetectedPort = autoDetectPort();
if (autoDetectedPort == null) if (autoDetectedPort == null)
return; return;
IoStream stream = SerialIoStreamJSerialComm.openPort(autoDetectedPort); IoStream stream = SerialIoStreamJSerialComm.openPort(autoDetectedPort);
@ -86,8 +92,7 @@ public class ConsoleTools {
System.err.println("rusEFI not detected"); System.err.println("rusEFI not detected");
return; return;
} }
LinkManager.start(autoDetectedPort); LinkManager.startAndConnect(autoDetectedPort, new ConnectionStateListener() {
LinkManager.connector.connect(new ConnectionStateListener() {
@Override @Override
public void onConnectionEstablished() { public void onConnectionEstablished() {
SensorLogger.init(); SensorLogger.init();
@ -143,6 +148,16 @@ public class ConsoleTools {
} }
return false; } return false; }
@Nullable
private static String autoDetectPort() {
String autoDetectedPort = PortDetector.autoDetectPort(null);
if (autoDetectedPort == null) {
System.err.println("rusEFI not detected");
return null;
}
return autoDetectedPort;
}
interface ConsoleTool { interface ConsoleTool {
void runTool(String args[]) throws Exception; void runTool(String args[]) throws Exception;
} }

View File

@ -65,7 +65,7 @@ public class Launcher {
private static Frame staticFrame; private static Frame staticFrame;
MainFrame mainFrame = new MainFrame(tabbedPane); private MainFrame mainFrame = new MainFrame(tabbedPane);
/** /**
* We can listen to tab activation event if we so desire * We can listen to tab activation event if we so desire
@ -203,16 +203,6 @@ public class Launcher {
SwingUtilities.invokeAndWait(() -> awtCode(args)); SwingUtilities.invokeAndWait(() -> awtCode(args));
} }
@Nullable
static String autoDetectPort() {
String autoDetectedPort = PortDetector.autoDetectPort(null);
if (autoDetectedPort == null) {
System.err.println("rusEFI not detected");
return null;
}
return autoDetectedPort;
}
private static void awtCode(String[] args) { private static void awtCode(String[] args) {
if (JustOneInstance.isAlreadyRunning()) { if (JustOneInstance.isAlreadyRunning()) {
int result = JOptionPane.showConfirmDialog(null, "Looks like another instance is already running. Do you really want to start another instance?", int result = JOptionPane.showConfirmDialog(null, "Looks like another instance is already running. Do you really want to start another instance?",

View File

@ -65,7 +65,7 @@ public class MainFrame {
} }
}); });
LinkManager.open(new ConnectionStateListener() { LinkManager.connect(new ConnectionStateListener() {
@Override @Override
public void onConnectionFailed() { public void onConnectionFailed() {
} }