auto-sync

This commit is contained in:
rusEfi 2015-02-22 15:04:11 -06:00
parent 45d4ed1b9f
commit ee5957ddbe
8 changed files with 75 additions and 16 deletions

View File

@ -22,6 +22,13 @@ import static com.rusefi.waves.WaveReport.isCloseEnough;
* 3/19/14. * 3/19/14.
*/ */
public class IoUtil { 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) { static void sendCommand(String command) {
final CountDownLatch responseLatch = new CountDownLatch(1); final CountDownLatch responseLatch = new CountDownLatch(1);
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
@ -34,7 +41,7 @@ public class IoUtil {
responseLatch.countDown(); responseLatch.countDown();
} }
}); });
wait(responseLatch, 20); wait(responseLatch, CMD_TIMEOUT);
if (LinkManager.hasError()) if (LinkManager.hasError())
throw new IllegalStateException("IO error"); throw new IllegalStateException("IO error");
FileLog.MAIN.logLine("Command [" + command + "] executed in " + (System.currentTimeMillis() - time)); FileLog.MAIN.logLine("Command [" + command + "] executed in " + (System.currentTimeMillis() - time));
@ -163,10 +170,11 @@ public class IoUtil {
* TCP connector is blocking * TCP connector is blocking
*/ */
LinkManager.open(); LinkManager.open();
LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, (EngineState.ValueCallback<String>) EngineState.ValueCallback.VOID);
waitForFirstResponse(); waitForFirstResponse();
} }
@SuppressWarnings("UnusedDeclaration")
static void sleep(int seconds) { static void sleep(int seconds) {
try { try {
Thread.sleep(seconds * 1000L); Thread.sleep(seconds * 1000L);

View File

@ -4,6 +4,7 @@ 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 org.jetbrains.annotations.NotNull;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -16,7 +17,7 @@ import java.util.concurrent.ThreadFactory;
public class LinkManager { public class LinkManager {
public final static Executor IO_EXECUTOR = Executors.newSingleThreadExecutor(new ThreadFactory() { public final static Executor IO_EXECUTOR = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override @Override
public Thread newThread(Runnable r) { public Thread newThread(@NotNull Runnable r) {
Thread t = new Thread(r); Thread t = new Thread(r);
t.setName("IO executor thread"); t.setName("IO executor thread");
t.setDaemon(true); // need daemon thread so that COM thread is also daemon 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 boolean isStimulationMode;
public static void start(String port) { public static void start(String port) {
FileLog.MAIN.logLine("Starting " + port);
if (isLogViewerMode(port)) { if (isLogViewerMode(port)) {
connector = LinkManager.VOID; connector = LinkManager.VOID;
} else if (TcpConnector.isTcpPort(port)) { } else if (TcpConnector.isTcpPort(port)) {
@ -75,6 +77,9 @@ public class LinkManager {
return connector == LinkManager.VOID; return connector == LinkManager.VOID;
} }
/**
* todo: should this be merged into {@link #start(String)} ?
*/
public static void open() { public static void open() {
if (connector == null) if (connector == null)
throw new NullPointerException("connector"); throw new NullPointerException("connector");

View File

@ -5,6 +5,7 @@ import com.rusefi.core.EngineState;
import com.rusefi.io.DataListener; import com.rusefi.io.DataListener;
import jssc.SerialPort; import jssc.SerialPort;
import jssc.SerialPortException; import jssc.SerialPortException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
/** /**
@ -22,8 +23,6 @@ public class PortHolder {
private static PortHolder instance = new PortHolder(); private static PortHolder instance = new PortHolder();
private final Object portLock = new Object(); private final Object portLock = new Object();
public static long startedAt = System.currentTimeMillis();
public PortHolderListener listener = PortHolderListener.VOID; public PortHolderListener listener = PortHolderListener.VOID;
private PortHolder() { private PortHolder() {
@ -45,7 +44,7 @@ public class PortHolder {
FileLog.MAIN.logLine("Opening " + port + " @ " + BAUD_RATE); FileLog.MAIN.logLine("Opening " + port + " @ " + BAUD_RATE);
boolean opened = serialPort.openPort();//Open serial port boolean opened = serialPort.openPort();//Open serial port
if (!opened) if (!opened)
FileLog.MAIN.logLine("opened: " + opened); FileLog.MAIN.logLine("not opened!");
serialPort.setParams(BAUD_RATE, 8, 1, 0);//Set params. serialPort.setParams(BAUD_RATE, 8, 1, 0);//Set params.
int mask = SerialPort.MASK_RXCHAR; int mask = SerialPort.MASK_RXCHAR;
//Set the prepared mask //Set the prepared mask
@ -55,7 +54,7 @@ public class PortHolder {
FileLog.rlog("ERROR " + e.getMessage()); FileLog.rlog("ERROR " + e.getMessage());
return false; return false;
} }
FileLog.rlog("PortHolder: Sleeping a bit");
try { try {
// todo: why is this delay here? add a comment // todo: why is this delay here? add a comment
Thread.sleep(200); Thread.sleep(200);
@ -69,6 +68,7 @@ public class PortHolder {
} }
try { try {
FileLog.rlog("PortHolder: test command");
/** /**
* Let's make sure we have not connected to Tuner Studio port? * Let's make sure we have not connected to Tuner Studio port?
* @see EngineState#TS_PROTOCOL_TAG * @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"); serialPort.writeString(command + "\r\n");
} }

View File

@ -1,5 +1,6 @@
package com.rusefi.io.serial; package com.rusefi.io.serial;
import com.rusefi.FileLog;
import com.rusefi.io.LinkConnector; import com.rusefi.io.LinkConnector;
/** /**
@ -13,6 +14,7 @@ public class SerialConnector implements LinkConnector {
@Override @Override
public void connect() { public void connect() {
FileLog.MAIN.logLine("SerialConnector: connecting");
SerialManager.scheduleOpening(); SerialManager.scheduleOpening();
} }

View File

@ -16,13 +16,13 @@ import java.util.concurrent.CopyOnWriteArrayList;
*/ */
public class EngineState { public class EngineState {
public static final String SEPARATOR = ","; public static final String SEPARATOR = ",";
public static final int SNIFFED_ADC_COUNT = 16;
public static final ValueCallback<String> NOTHING = new ValueCallback<String>() { public static final ValueCallback<String> NOTHING = new ValueCallback<String>() {
@Override @Override
public void onUpdate(String value) { public void onUpdate(String value) {
} }
}; };
public static final String PACKING_DELIMITER = ":"; 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 * If we get this tag we have probably connected to the wrong port
*/ */
@ -43,11 +43,11 @@ public class EngineState {
} }
} }
public final List<EngineTimeListener> timeListeners = new CopyOnWriteArrayList<EngineTimeListener>(); public final List<EngineTimeListener> timeListeners = new CopyOnWriteArrayList<>();
private final ResponseBuffer buffer; private final ResponseBuffer buffer;
private final List<StringActionPair> actions = new ArrayList<StringActionPair>(); private final List<StringActionPair> actions = new ArrayList<>();
private final Set<String> keys = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); private final Set<String> keys = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
public EngineState(@NotNull final EngineStateListener listener) { public EngineState(@NotNull final EngineStateListener listener) {
buffer = new ResponseBuffer(new ResponseBuffer.ResponseListener() { buffer = new ResponseBuffer(new ResponseBuffer.ResponseListener() {
@ -285,6 +285,13 @@ public class EngineState {
} }
public interface ValueCallback<V> { public interface ValueCallback<V> {
static final ValueCallback<?> VOID = new ValueCallback() {
@Override
public void onUpdate(Object value) {
}
};
void onUpdate(V value); void onUpdate(V value);
} }

View File

@ -1,8 +1,43 @@
package com.rusefi; package com.rusefi;
import com.rusefi.core.EngineState;
import com.rusefi.io.LinkManager;
import jssc.SerialPortList;
/** /**
* (c) Andrey Belomutskiy 2013-2015 * (c) Andrey Belomutskiy 2013-2015
* 2/22/2015 * 2/22/2015
*/ */
public class CmdLine { 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<String>) EngineState.ValueCallback.VOID);
IoUtil.sendCommand(command);
System.out.println("Done!");
System.exit(-1);
}
} }

View File

@ -28,7 +28,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
* @see com.rusefi.ui.engine.EngineSnifferPanel * @see com.rusefi.ui.engine.EngineSnifferPanel
*/ */
public class Launcher extends FrameHelper { 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 boolean SHOW_STIMULATOR = true;
public static final String TAB_INDEX = "main_tab"; public static final String TAB_INDEX = "main_tab";
private final String port; private final String port;
@ -84,7 +84,7 @@ public class Launcher extends FrameHelper {
LinkManager.open(); LinkManager.open();
LinkManager.engineState.registerStringValueAction("rusEfiVersion", new EngineState.ValueCallback<String>() { LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, new EngineState.ValueCallback<String>() {
@Override @Override
public void onUpdate(String firmwareVersion) { public void onUpdate(String firmwareVersion) {
setTitle(firmwareVersion); setTitle(firmwareVersion);

View File

@ -17,6 +17,6 @@
<orderEntry type="library" name="batik" level="project" /> <orderEntry type="library" name="batik" level="project" />
<orderEntry type="module" module-name="models" /> <orderEntry type="module" module-name="models" />
<orderEntry type="library" name="SteelSeries" level="project" /> <orderEntry type="library" name="SteelSeries" level="project" />
<orderEntry type="module" module-name="autotest" />
</component> </component>
</module> </module>