auto-sync
This commit is contained in:
parent
782d396edf
commit
cc937ac86a
|
@ -25,7 +25,6 @@ public class BinaryProtocol {
|
|||
private static final int SWITCH_TO_BINARY_RESPONSE = 0xA7E;
|
||||
private static final int TIMEOUT = 30 * 1000;
|
||||
|
||||
|
||||
private final Logger logger;
|
||||
private final SerialPort serialPort;
|
||||
private static final int BUFFER_SIZE = 10000;
|
||||
|
@ -35,7 +34,7 @@ public class BinaryProtocol {
|
|||
private final Object lock = new Object();
|
||||
private ConfigurationImage controller;
|
||||
|
||||
public BinaryProtocol(final Logger logger, SerialPort serialPort) throws SerialPortException {
|
||||
public BinaryProtocol(final Logger logger, SerialPort serialPort) {
|
||||
this.logger = logger;
|
||||
this.serialPort = serialPort;
|
||||
|
||||
|
@ -54,15 +53,20 @@ public class BinaryProtocol {
|
|||
}
|
||||
}
|
||||
};
|
||||
try {
|
||||
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();
|
||||
|
||||
while (true) {
|
||||
dropPending();
|
||||
|
||||
try {
|
||||
serialPort.writeBytes("~\n".getBytes());
|
||||
synchronized (cbb) {
|
||||
waitForBytes(2, start);
|
||||
|
@ -72,6 +76,9 @@ public class BinaryProtocol {
|
|||
continue;
|
||||
}
|
||||
}
|
||||
} catch (SerialPortException | EOFException | InterruptedException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -193,10 +200,14 @@ public class BinaryProtocol {
|
|||
setController(image);
|
||||
}
|
||||
|
||||
public byte[] exchange(byte[] packet) throws SerialPortException, InterruptedException, EOFException {
|
||||
public byte[] exchange(byte[] packet) {
|
||||
dropPending();
|
||||
try {
|
||||
sendCrcPacket(packet);
|
||||
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 {
|
||||
|
@ -226,7 +237,7 @@ public class BinaryProtocol {
|
|||
}
|
||||
}
|
||||
|
||||
public void burn() throws InterruptedException, EOFException, SerialPortException {
|
||||
private void burn() throws InterruptedException, EOFException, SerialPortException {
|
||||
if (!isBurnPending)
|
||||
return;
|
||||
|
||||
|
@ -295,7 +306,12 @@ public class BinaryProtocol {
|
|||
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[] command = new byte[asBytes.length + 1];
|
||||
command[0] = 'E';
|
||||
|
@ -306,14 +322,20 @@ public class BinaryProtocol {
|
|||
if (!checkResponseCode(response, RESPONSE_COMMAND_OK) || response.length != 1) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void requestText() throws InterruptedException, EOFException, SerialPortException {
|
||||
byte[] response = exchange(new byte[]{'G'});
|
||||
public String requestText() {
|
||||
try {
|
||||
byte[] response = new byte[0];
|
||||
response = exchange(new byte[]{'G'});
|
||||
if (response != null && response.length == 1)
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,4 +12,6 @@ public interface LinkConnector {
|
|||
void restart();
|
||||
|
||||
boolean hasError();
|
||||
|
||||
String unpack(String packet);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ public class LinkManager {
|
|||
}
|
||||
});
|
||||
public static final String LOG_VIEWER = "log viewer";
|
||||
private static final LinkConnector VOID = new LinkConnector() {
|
||||
public static final LinkConnector VOID = new LinkConnector() {
|
||||
@Override
|
||||
public void connect(LinkStateListener listener) {
|
||||
}
|
||||
|
@ -42,6 +42,11 @@ public class LinkManager {
|
|||
public boolean hasError() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String unpack(String packet) {
|
||||
return EngineState.unpackString(packet);
|
||||
}
|
||||
};
|
||||
public static EngineState engineState = new EngineState(new EngineState.EngineStateListenerImpl() {
|
||||
@Override
|
||||
|
@ -50,7 +55,7 @@ public class LinkManager {
|
|||
}
|
||||
});
|
||||
public static boolean onlyUI = false;
|
||||
private static LinkConnector connector;
|
||||
public static LinkConnector connector;
|
||||
|
||||
/**
|
||||
* This flag controls if mock controls are needed
|
||||
|
@ -104,6 +109,10 @@ public class LinkManager {
|
|||
connector.restart();
|
||||
}
|
||||
|
||||
public static String unpack(String packet) {
|
||||
return connector.unpack(packet);
|
||||
}
|
||||
|
||||
public static boolean hasError() {
|
||||
return connector.hasError();
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public class PortHolder {
|
|||
private static PortHolder instance = new PortHolder();
|
||||
private final Object portLock = new Object();
|
||||
|
||||
public PortHolderListener listener = PortHolderListener.VOID;
|
||||
public PortHolderListener portHolderListener = PortHolderListener.VOID;
|
||||
|
||||
private PortHolder() {
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ public class PortHolder {
|
|||
private SerialPort serialPort;
|
||||
|
||||
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)
|
||||
return false;
|
||||
boolean result = open(port, dataListener);
|
||||
|
@ -108,14 +108,14 @@ public class PortHolder {
|
|||
*/
|
||||
public void packAndSend(String command) throws InterruptedException {
|
||||
FileLog.MAIN.logLine("Sending [" + command + "]");
|
||||
listener.onPortHolderMessage(PortHolder.class, "Sending [" + command + "]");
|
||||
portHolderListener.onPortHolderMessage(PortHolder.class, "Sending [" + command + "]");
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
synchronized (portLock) {
|
||||
while (serialPort == null) {
|
||||
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);
|
||||
}
|
||||
// we are here only when serialPort!=null, that means we have a connection
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.rusefi.io.serial;
|
||||
|
||||
import com.rusefi.FileLog;
|
||||
import com.rusefi.core.EngineState;
|
||||
import com.rusefi.io.LinkConnector;
|
||||
import com.rusefi.io.LinkManager;
|
||||
|
||||
|
@ -29,6 +30,11 @@ public class SerialConnector implements LinkConnector {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String unpack(String packet) {
|
||||
return EngineState.unpackString(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(String command) throws InterruptedException {
|
||||
PortHolder.getInstance().packAndSend(command);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.rusefi.io.tcp;
|
||||
|
||||
import com.rusefi.FileLog;
|
||||
import com.rusefi.core.EngineState;
|
||||
import com.rusefi.io.LinkConnector;
|
||||
import com.rusefi.io.LinkManager;
|
||||
|
||||
|
@ -105,6 +106,11 @@ public class TcpConnector implements LinkConnector {
|
|||
return withError;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String unpack(String packet) {
|
||||
return EngineState.unpackString(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(String command) throws InterruptedException {
|
||||
FileLog.rlog("Writing " + command);
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.rusefi.core;
|
|||
|
||||
import com.rusefi.FileLog;
|
||||
import com.rusefi.SensorConversion;
|
||||
import com.rusefi.io.LinkManager;
|
||||
import com.rusefi.waves.WaveReport;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
@ -59,7 +60,7 @@ public class EngineState {
|
|||
public EngineState(@NotNull final EngineStateListener listener) {
|
||||
buffer = new ResponseBuffer(new ResponseBuffer.ResponseListener() {
|
||||
public void onResponse(String message) {
|
||||
String response = unpackString(message);
|
||||
String response = LinkManager.unpack(message);
|
||||
if (response != null) {
|
||||
int i = response.indexOf(FileLog.END_OF_TIMESTAND_TAG);
|
||||
if (i != -1)
|
||||
|
|
|
@ -19,7 +19,7 @@ public class MessagesCentral {
|
|||
private final List<MessageListener> listeners = new CopyOnWriteArrayList<>();
|
||||
|
||||
private MessagesCentral() {
|
||||
PortHolder.getInstance().listener = new PortHolderListener() {
|
||||
PortHolder.getInstance().portHolderListener = new PortHolderListener() {
|
||||
@Override
|
||||
public void onPortHolderMessage(Class clazz, String message) {
|
||||
postMessage(clazz, message);
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.rusefi.core.test;
|
|||
|
||||
import com.rusefi.core.SensorCentral;
|
||||
import com.rusefi.core.EngineState;
|
||||
import com.rusefi.io.LinkManager;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
@ -41,6 +42,7 @@ public class EngineStateTest {
|
|||
es.processNewData("line:7:");
|
||||
es.processNewData(SensorCentral.RPM_KEY + SEPARATOR);
|
||||
assertEquals(0, rpmResult.get());
|
||||
LinkManager.connector = LinkManager.VOID;
|
||||
es.processNewData("600\r");
|
||||
assertEquals(600, rpmResult.get());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue