better method location

This commit is contained in:
rusefi 2017-09-18 08:00:36 -04:00
parent f54da4c3d3
commit 20195f5551
4 changed files with 55 additions and 40 deletions

View File

@ -4,13 +4,10 @@ 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;
import jssc.SerialPortList;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -151,45 +148,14 @@ public class IoUtil {
} }
} }
/**
* @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)");
System.out.println("All ports: " + Arrays.toString(ports));
return port;
}
static void realHardwareConnect(String port) { static void realHardwareConnect(String port) {
LinkManager.start(port);
final CountDownLatch connected = new CountDownLatch(1);
LinkManager.open(new ConnectionStateListener() {
@Override
public void onConnectionFailed() {
System.out.println("CONNECTION FAILED, did you specify the right port name?");
System.exit(-1);
}
@Override
public void onConnectionEstablished() {
connected.countDown();
}
});
LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID); LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID);
LinkManager.engineState.registerStringValueAction(EngineState.OUTPIN_TAG, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID); LinkManager.engineState.registerStringValueAction(EngineState.OUTPIN_TAG, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID);
LinkManager.engineState.registerStringValueAction(AverageAnglesUtil.KEY, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID); LinkManager.engineState.registerStringValueAction(AverageAnglesUtil.KEY, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID);
try {
connected.await(60, TimeUnit.SECONDS); final CountDownLatch connected = LinkManager.connect(port);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
if (connected.getCount() > 0) if (connected.getCount() > 0)
throw new IllegalStateException("Not connected in time"); throw new IllegalStateException("Not connected in time");
} }
} }

View File

@ -1,5 +1,7 @@
package com.rusefi; package com.rusefi;
import com.rusefi.io.LinkManager;
import static com.rusefi.AutoTest.*; import static com.rusefi.AutoTest.*;
/** /**
@ -46,7 +48,7 @@ public class RealHwTest {
if (args.length == 1 || args.length == 2) { if (args.length == 1 || args.length == 2) {
port = args[0]; port = args[0];
} else if (args.length == 0) { } else if (args.length == 0) {
port = IoUtil.getDefaultPort(); port = LinkManager.getDefaultPort();
} else { } else {
System.out.println("Only one optional argument expected: port number"); System.out.println("Only one optional argument expected: port number");
port = null; port = null;

View File

@ -4,8 +4,10 @@ import com.rusefi.FileLog;
import com.rusefi.core.EngineState; import com.rusefi.core.EngineState;
import com.rusefi.io.serial.SerialConnector; import com.rusefi.io.serial.SerialConnector;
import com.rusefi.io.tcp.TcpConnector; import com.rusefi.io.tcp.TcpConnector;
import jssc.SerialPortList;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.concurrent.*; import java.util.concurrent.*;
/** /**
@ -13,6 +15,30 @@ import java.util.concurrent.*;
* 3/3/14 * 3/3/14
*/ */
public class LinkManager { public class LinkManager {
@NotNull
public static CountDownLatch connect(String port) {
start(port);
final CountDownLatch connected = new CountDownLatch(1);
open(new ConnectionStateListener() {
@Override
public void onConnectionFailed() {
System.out.println("CONNECTION FAILED, did you specify the right port name?");
System.exit(-1);
}
@Override
public void onConnectionEstablished() {
connected.countDown();
}
});
try {
connected.await(60, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return connected;
}
public enum LogLevel { public enum LogLevel {
INFO, INFO,
DEBUG, DEBUG,
@ -44,6 +70,9 @@ public class LinkManager {
}); });
public static final String LOG_VIEWER = "log viewer"; public static final String LOG_VIEWER = "log viewer";
public static final LinkedBlockingQueue<Runnable> COMMUNICATION_QUEUE = new LinkedBlockingQueue<>(); public static final LinkedBlockingQueue<Runnable> COMMUNICATION_QUEUE = new LinkedBlockingQueue<>();
/**
* All request/responses to underlying controller are happening on this single-threaded executor in a FIFO manner
*/
public static final ExecutorService COMMUNICATION_EXECUTOR = new ThreadPoolExecutor(1, 1, public static final ExecutorService COMMUNICATION_EXECUTOR = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS, 0L, TimeUnit.MILLISECONDS,
COMMUNICATION_QUEUE, COMMUNICATION_QUEUE,
@ -51,7 +80,8 @@ public class LinkManager {
@Override @Override
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r); Thread t = Executors.defaultThreadFactory().newThread(r);
t.setName("communication executor");; t.setName("communication executor");
;
return t; return t;
} }
}); });
@ -126,4 +156,19 @@ public class LinkManager {
return message.substring(CommandQueue.CONFIRMATION_PREFIX.length()); return message.substring(CommandQueue.CONFIRMATION_PREFIX.length());
return null; return null;
} }
/**
* @return null if no port located
*/
public 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)");
System.out.println("All ports: " + Arrays.toString(ports));
return port;
}
} }

View File

@ -1,5 +1,7 @@
package com.rusefi; package com.rusefi;
import com.rusefi.io.LinkManager;
/** /**
* (c) Andrey Belomutskiy 2013-2017 * (c) Andrey Belomutskiy 2013-2017
* 2/22/2015 * 2/22/2015
@ -12,7 +14,7 @@ public class CmdLine {
} }
String command = args[0]; String command = args[0];
if (args.length == 1) { if (args.length == 1) {
String port = IoUtil.getDefaultPort(); String port = LinkManager.getDefaultPort();
if (port == null) if (port == null)
return; return;
executeCommand(command, port); executeCommand(command, port);