auto-sync

This commit is contained in:
rusEfi 2015-03-09 20:10:07 -05:00
parent 782d396edf
commit cc937ac86a
9 changed files with 78 additions and 30 deletions

View File

@ -25,7 +25,6 @@ public class BinaryProtocol {
private static final int SWITCH_TO_BINARY_RESPONSE = 0xA7E; private static final int SWITCH_TO_BINARY_RESPONSE = 0xA7E;
private static final int TIMEOUT = 30 * 1000; private static final int TIMEOUT = 30 * 1000;
private final Logger logger; private final Logger logger;
private final SerialPort serialPort; private final SerialPort serialPort;
private static final int BUFFER_SIZE = 10000; private static final int BUFFER_SIZE = 10000;
@ -35,7 +34,7 @@ public class BinaryProtocol {
private final Object lock = new Object(); private final Object lock = new Object();
private ConfigurationImage controller; private ConfigurationImage controller;
public BinaryProtocol(final Logger logger, SerialPort serialPort) throws SerialPortException { public BinaryProtocol(final Logger logger, SerialPort serialPort) {
this.logger = logger; this.logger = logger;
this.serialPort = serialPort; this.serialPort = serialPort;
@ -54,15 +53,20 @@ public class BinaryProtocol {
} }
} }
}; };
try {
serialPort.addEventListener(new SerialPortReader(serialPort, listener)); serialPort.addEventListener(new SerialPortReader(serialPort, listener));
} catch (SerialPortException e) {
throw new IllegalStateException(e);
}
} }
void switchToBinaryProtocol() throws SerialPortException, EOFException, InterruptedException { public void switchToBinaryProtocol() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
while (true) { while (true) {
dropPending(); dropPending();
try {
serialPort.writeBytes("~\n".getBytes()); serialPort.writeBytes("~\n".getBytes());
synchronized (cbb) { synchronized (cbb) {
waitForBytes(2, start); waitForBytes(2, start);
@ -72,6 +76,9 @@ public class BinaryProtocol {
continue; continue;
} }
} }
} catch (SerialPortException | EOFException | InterruptedException e) {
throw new IllegalStateException(e);
}
break; break;
} }
} }
@ -193,10 +200,14 @@ public class BinaryProtocol {
setController(image); setController(image);
} }
public byte[] exchange(byte[] packet) throws SerialPortException, InterruptedException, EOFException { public byte[] exchange(byte[] packet) {
dropPending(); dropPending();
try {
sendCrcPacket(packet); sendCrcPacket(packet);
return receivePacket(); return receivePacket();
} catch (SerialPortException | InterruptedException | EOFException e) {
throw new IllegalStateException(e);
}
} }
public void writeData(byte[] content, Integer offset, int size, Logger logger) throws SerialPortException, EOFException, InterruptedException { public void writeData(byte[] content, Integer offset, int size, Logger logger) throws SerialPortException, EOFException, InterruptedException {
@ -226,7 +237,7 @@ public class BinaryProtocol {
} }
} }
public void burn() throws InterruptedException, EOFException, SerialPortException { private void burn() throws InterruptedException, EOFException, SerialPortException {
if (!isBurnPending) if (!isBurnPending)
return; return;
@ -295,7 +306,12 @@ public class BinaryProtocol {
serialPort.writeBytes(packet); serialPort.writeBytes(packet);
} }
public void sendTextCommand(String text) throws SerialPortException, EOFException, InterruptedException { /**
* This method blocks until a confirmation is received
*
* @return true in case of timeout, false if got proper confirmation
*/
public boolean sendTextCommand(String text) {
byte[] asBytes = text.getBytes(); byte[] asBytes = text.getBytes();
byte[] command = new byte[asBytes.length + 1]; byte[] command = new byte[asBytes.length + 1];
command[0] = 'E'; command[0] = 'E';
@ -306,14 +322,20 @@ public class BinaryProtocol {
if (!checkResponseCode(response, RESPONSE_COMMAND_OK) || response.length != 1) { if (!checkResponseCode(response, RESPONSE_COMMAND_OK) || response.length != 1) {
continue; continue;
} }
break; return false;
} }
} }
public void requestText() throws InterruptedException, EOFException, SerialPortException { public String requestText() {
byte[] response = exchange(new byte[]{'G'}); try {
byte[] response = new byte[0];
response = exchange(new byte[]{'G'});
if (response != null && response.length == 1) if (response != null && response.length == 1)
Thread.sleep(100); Thread.sleep(100);
System.out.println(new String(response)); // System.out.println(result);
return new String(response, 1, response.length - 1);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
} }
} }

View File

@ -12,4 +12,6 @@ public interface LinkConnector {
void restart(); void restart();
boolean hasError(); boolean hasError();
String unpack(String packet);
} }

View File

@ -25,7 +25,7 @@ public class LinkManager {
} }
}); });
public static final String LOG_VIEWER = "log viewer"; public static final String LOG_VIEWER = "log viewer";
private static final LinkConnector VOID = new LinkConnector() { public static final LinkConnector VOID = new LinkConnector() {
@Override @Override
public void connect(LinkStateListener listener) { public void connect(LinkStateListener listener) {
} }
@ -42,6 +42,11 @@ public class LinkManager {
public boolean hasError() { public boolean hasError() {
return false; return false;
} }
@Override
public String unpack(String packet) {
return EngineState.unpackString(packet);
}
}; };
public static EngineState engineState = new EngineState(new EngineState.EngineStateListenerImpl() { public static EngineState engineState = new EngineState(new EngineState.EngineStateListenerImpl() {
@Override @Override
@ -50,7 +55,7 @@ public class LinkManager {
} }
}); });
public static boolean onlyUI = false; public static boolean onlyUI = false;
private static LinkConnector connector; public static LinkConnector connector;
/** /**
* This flag controls if mock controls are needed * This flag controls if mock controls are needed
@ -104,6 +109,10 @@ public class LinkManager {
connector.restart(); connector.restart();
} }
public static String unpack(String packet) {
return connector.unpack(packet);
}
public static boolean hasError() { public static boolean hasError() {
return connector.hasError(); return connector.hasError();
} }

View File

@ -24,7 +24,7 @@ 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 PortHolderListener listener = PortHolderListener.VOID; public PortHolderListener portHolderListener = PortHolderListener.VOID;
private PortHolder() { private PortHolder() {
} }
@ -33,7 +33,7 @@ public class PortHolder {
private SerialPort serialPort; private SerialPort serialPort;
boolean openPort(String port, DataListener dataListener, LinkManager.LinkStateListener listener) { boolean openPort(String port, DataListener dataListener, LinkManager.LinkStateListener listener) {
this.listener.onPortHolderMessage(SerialManager.class, "Opening port: " + port); this.portHolderListener.onPortHolderMessage(SerialManager.class, "Opening port: " + port);
if (port == null) if (port == null)
return false; return false;
boolean result = open(port, dataListener); boolean result = open(port, dataListener);
@ -108,14 +108,14 @@ public class PortHolder {
*/ */
public void packAndSend(String command) throws InterruptedException { public void packAndSend(String command) throws InterruptedException {
FileLog.MAIN.logLine("Sending [" + command + "]"); FileLog.MAIN.logLine("Sending [" + command + "]");
listener.onPortHolderMessage(PortHolder.class, "Sending [" + command + "]"); portHolderListener.onPortHolderMessage(PortHolder.class, "Sending [" + command + "]");
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
synchronized (portLock) { synchronized (portLock) {
while (serialPort == null) { while (serialPort == null) {
if (System.currentTimeMillis() - now > 3 * MINUTE) if (System.currentTimeMillis() - now > 3 * MINUTE)
listener.onPortHolderMessage(PortHolder.class, "Looks like connection is gone :("); portHolderListener.onPortHolderMessage(PortHolder.class, "Looks like connection is gone :(");
portLock.wait(MINUTE); portLock.wait(MINUTE);
} }
// we are here only when serialPort!=null, that means we have a connection // we are here only when serialPort!=null, that means we have a connection

View File

@ -1,6 +1,7 @@
package com.rusefi.io.serial; package com.rusefi.io.serial;
import com.rusefi.FileLog; import com.rusefi.FileLog;
import com.rusefi.core.EngineState;
import com.rusefi.io.LinkConnector; import com.rusefi.io.LinkConnector;
import com.rusefi.io.LinkManager; import com.rusefi.io.LinkManager;
@ -29,6 +30,11 @@ public class SerialConnector implements LinkConnector {
return false; return false;
} }
@Override
public String unpack(String packet) {
return EngineState.unpackString(packet);
}
@Override @Override
public void send(String command) throws InterruptedException { public void send(String command) throws InterruptedException {
PortHolder.getInstance().packAndSend(command); PortHolder.getInstance().packAndSend(command);

View File

@ -1,6 +1,7 @@
package com.rusefi.io.tcp; package com.rusefi.io.tcp;
import com.rusefi.FileLog; import com.rusefi.FileLog;
import com.rusefi.core.EngineState;
import com.rusefi.io.LinkConnector; import com.rusefi.io.LinkConnector;
import com.rusefi.io.LinkManager; import com.rusefi.io.LinkManager;
@ -105,6 +106,11 @@ public class TcpConnector implements LinkConnector {
return withError; return withError;
} }
@Override
public String unpack(String packet) {
return EngineState.unpackString(packet);
}
@Override @Override
public void send(String command) throws InterruptedException { public void send(String command) throws InterruptedException {
FileLog.rlog("Writing " + command); FileLog.rlog("Writing " + command);

View File

@ -2,6 +2,7 @@ package com.rusefi.core;
import com.rusefi.FileLog; import com.rusefi.FileLog;
import com.rusefi.SensorConversion; import com.rusefi.SensorConversion;
import com.rusefi.io.LinkManager;
import com.rusefi.waves.WaveReport; import com.rusefi.waves.WaveReport;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -59,7 +60,7 @@ public class EngineState {
public EngineState(@NotNull final EngineStateListener listener) { public EngineState(@NotNull final EngineStateListener listener) {
buffer = new ResponseBuffer(new ResponseBuffer.ResponseListener() { buffer = new ResponseBuffer(new ResponseBuffer.ResponseListener() {
public void onResponse(String message) { public void onResponse(String message) {
String response = unpackString(message); String response = LinkManager.unpack(message);
if (response != null) { if (response != null) {
int i = response.indexOf(FileLog.END_OF_TIMESTAND_TAG); int i = response.indexOf(FileLog.END_OF_TIMESTAND_TAG);
if (i != -1) if (i != -1)

View File

@ -19,7 +19,7 @@ public class MessagesCentral {
private final List<MessageListener> listeners = new CopyOnWriteArrayList<>(); private final List<MessageListener> listeners = new CopyOnWriteArrayList<>();
private MessagesCentral() { private MessagesCentral() {
PortHolder.getInstance().listener = new PortHolderListener() { PortHolder.getInstance().portHolderListener = new PortHolderListener() {
@Override @Override
public void onPortHolderMessage(Class clazz, String message) { public void onPortHolderMessage(Class clazz, String message) {
postMessage(clazz, message); postMessage(clazz, message);

View File

@ -2,6 +2,7 @@ package com.rusefi.core.test;
import com.rusefi.core.SensorCentral; import com.rusefi.core.SensorCentral;
import com.rusefi.core.EngineState; import com.rusefi.core.EngineState;
import com.rusefi.io.LinkManager;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -41,6 +42,7 @@ public class EngineStateTest {
es.processNewData("line:7:"); es.processNewData("line:7:");
es.processNewData(SensorCentral.RPM_KEY + SEPARATOR); es.processNewData(SensorCentral.RPM_KEY + SEPARATOR);
assertEquals(0, rpmResult.get()); assertEquals(0, rpmResult.get());
LinkManager.connector = LinkManager.VOID;
es.processNewData("600\r"); es.processNewData("600\r");
assertEquals(600, rpmResult.get()); assertEquals(600, rpmResult.get());
} }