auto-sync

This commit is contained in:
rusEfi 2015-03-09 21:16:13 -05:00
parent 450e583099
commit 00da31097a
8 changed files with 58 additions and 28 deletions

View File

@ -632,7 +632,7 @@ int tunerStudioHandleCrcCommand(ts_channel_s *tsChannel, char *data, int incomin
} else if (command == TS_GET_TEXT) {
handleGetText(tsChannel);
} else if (command == TS_EXECUTE) {
handleExecuteCommand(tsChannel, data, incomingPacketSize);
handleExecuteCommand(tsChannel, data, incomingPacketSize - 1);
} else if (command == TS_OUTPUT_COMMAND) {
handleOutputChannelsCommand(tsChannel, TS_CRC);
} else if (command == TS_PAGE_COMMAND) {

View File

@ -17,7 +17,7 @@ import java.util.concurrent.LinkedBlockingQueue;
*/
@SuppressWarnings("FieldCanBeLocal")
public class CommandQueue {
private static final String CONFIRMATION_PREFIX = "confirmation_";
public static final String CONFIRMATION_PREFIX = "confirmation_";
public static final int DEFAULT_TIMEOUT = 500;
private static final int COMMAND_CONFIRMATION_TIMEOUT = 1000;
private static final int SLOW_CONFIRMATION_TIMEOUT = 5000;
@ -113,26 +113,11 @@ public class CommandQueue {
* this method handles command confirmations packed as
* TODO: add example, todo: refactor method and add unit test
*/
private void handleConfirmationMessage(String message, MessagesCentral mc) {
String confirmation = message.substring(CONFIRMATION_PREFIX.length());
int index = confirmation.indexOf(":");
if (index < 0) {
mc.postMessage(CommandQueue.class, "Broken confirmation: " + confirmation);
return;
}
String number = confirmation.substring(index + 1);
int length;
try {
length = Integer.parseInt(number);
} catch (NumberFormatException e) {
mc.postMessage(CommandQueue.class, "Broken confirmation length: " + confirmation);
return;
}
if (length != index) {
mc.postMessage(CommandQueue.class, "Broken confirmation length: " + confirmation);
return;
}
latestConfirmation = confirmation.substring(0, length);
private void handleConfirmationMessage(final String message, MessagesCentral mc) {
String confirmation = LinkManager.unpackConfirmation(message);
if (confirmation == null)
mc.postMessage(CommandQueue.class, "Broken confirmation length: " + message);
latestConfirmation = confirmation;
mc.postMessage(CommandQueue.class, "got valid conf! " + latestConfirmation);
synchronized (lock) {
lock.notifyAll();
@ -154,7 +139,8 @@ public class CommandQueue {
/**
* Non-blocking command request
* Command is placed in the queue where it would be until it is confirmed
* @param command dev console command
*
* @param command dev console command
* @param timeoutMs retry timeout
*/
public void write(String command, int timeoutMs, InvocationConfirmationListener listener) {

View File

@ -14,4 +14,6 @@ public interface LinkConnector {
boolean hasError();
String unpack(String packet);
String unpackConfirmation(String message);
}

View File

@ -47,6 +47,11 @@ public class LinkManager {
public String unpack(String packet) {
return EngineState.unpackString(packet);
}
@Override
public String unpackConfirmation(String message) {
return TcpConnector.doUnpackConfirmation(message);
}
};
public static EngineState engineState = new EngineState(new EngineState.EngineStateListenerImpl() {
@Override
@ -98,7 +103,7 @@ public class LinkManager {
public static void send(String command) throws InterruptedException {
if (connector == null)
throw new NullPointerException("connector");
connector.send(encodeCommand(command));
connector.send(command);
}
public static String encodeCommand(String command) {
@ -117,6 +122,10 @@ public class LinkManager {
return connector.hasError();
}
public static String unpackConfirmation(String message) {
return connector.unpackConfirmation(message);
}
public static interface LinkStateListener {
public static final LinkStateListener VOID = new LinkStateListener() {
@Override

View File

@ -130,7 +130,7 @@ public class PortHolder {
private void doWriteCommand(@NotNull String command) throws SerialPortException {
if (serialPort == null)
throw new NullPointerException("serialPort");
serialPort.writeString(command + "\r\n");
serialPort.writeBytes((command + "\n").getBytes());
}
public static PortHolder getInstance() {

View File

@ -4,6 +4,7 @@ import com.rusefi.FileLog;
import com.rusefi.core.EngineState;
import com.rusefi.io.LinkConnector;
import com.rusefi.io.LinkManager;
import com.rusefi.io.tcp.TcpConnector;
/**
* @author Andrey Belomutskiy
@ -36,7 +37,13 @@ public class SerialConnector implements LinkConnector {
}
@Override
public void send(String command) throws InterruptedException {
public void send(String text) throws InterruptedException {
String command = LinkManager.encodeCommand(text);
PortHolder.getInstance().packAndSend(command);
}
@Override
public String unpackConfirmation(String message) {
return TcpConnector.doUnpackConfirmation(message);
}
}

View File

@ -2,6 +2,7 @@ package com.rusefi.io.tcp;
import com.rusefi.FileLog;
import com.rusefi.core.EngineState;
import com.rusefi.io.CommandQueue;
import com.rusefi.io.LinkConnector;
import com.rusefi.io.LinkManager;
@ -38,6 +39,25 @@ public class TcpConnector implements LinkConnector {
}
}
public static String doUnpackConfirmation(String message) {
String confirmation = message.substring(CommandQueue.CONFIRMATION_PREFIX.length());
int index = confirmation.indexOf(":");
if (index < 0) {
return null;
}
String number = confirmation.substring(index + 1);
int length;
try {
length = Integer.parseInt(number);
} catch (NumberFormatException e) {
return null;
}
if (length != index) {
return null;
}
return confirmation.substring(0, length);
}
static class InvalidTcpPort extends Exception {
}
@ -112,7 +132,8 @@ public class TcpConnector implements LinkConnector {
}
@Override
public void send(String command) throws InterruptedException {
public void send(String text) throws InterruptedException {
String command = LinkManager.encodeCommand(text);
FileLog.rlog("Writing " + command);
try {
writer.write(command + "\r\n");
@ -124,6 +145,11 @@ public class TcpConnector implements LinkConnector {
}
}
@Override
public String unpackConfirmation(String message) {
return doUnpackConfirmation(message);
}
public static Collection<String> getAvailablePorts() {
return isTcpPortOpened() ? Collections.singletonList("" + DEFAULT_PORT) : Collections.<String>emptyList();
}

View File

@ -29,7 +29,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
* @see com.rusefi.StartupFrame
*/
public class Launcher extends FrameHelper {
public static final int CONSOLE_VERSION = 20150307;
public static final int CONSOLE_VERSION = 20150308;
public static final boolean SHOW_STIMULATOR = true;
public static final String TAB_INDEX = "main_tab";
private final String port;