refactoring

This commit is contained in:
rusefi 2020-07-03 13:30:52 -04:00
parent dd0c645f05
commit 72f80c6be7
2 changed files with 15 additions and 6 deletions

View File

@ -18,24 +18,25 @@ import java.awt.*;
* 7/25/13
* Andrey Belomutskiy, (c) 2013-2020
*/
public class PortHolder {
public abstract class PortHolder {
private final DataListener dataListener;
private final Logger logger;
private final LinkManager linkManager;
public ConnectionStateListener listener;
private final Object portLock = new Object();
private final String port;
@Nullable
private BinaryProtocol bp;
protected PortHolder(LinkManager linkManager, Logger logger) {
protected PortHolder(String port, LinkManager linkManager, Logger logger) {
this.port = port;
this.linkManager = linkManager;
dataListener = freshData -> linkManager.getEngineState().processNewData(new String(freshData), LinkManager.ENCODER);
this.logger = logger;
}
public String port;
boolean connectAndReadConfiguration() {
if (port == null)
@ -43,7 +44,7 @@ public class PortHolder {
MessagesCentral.getInstance().postMessage(logger, getClass(), "Opening port: " + port);
IoStream stream = SerialIoStreamJSerialComm.openPort(port, logger);
IoStream stream = openStream();
synchronized (portLock) {
bp = new BinaryProtocol(linkManager, logger, stream);
portLock.notifyAll();
@ -60,6 +61,8 @@ public class PortHolder {
return result;
}
protected abstract IoStream openStream();
public void close() {
synchronized (portLock) {
if (bp != null) {

View File

@ -4,8 +4,10 @@ import com.opensr5.Logger;
import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.core.MessagesCentral;
import com.rusefi.io.ConnectionStateListener;
import com.rusefi.io.IoStream;
import com.rusefi.io.LinkConnector;
import com.rusefi.io.LinkManager;
import org.jetbrains.annotations.NotNull;
/**
* @author Andrey Belomutskiy
@ -19,9 +21,13 @@ public class SerialConnector implements LinkConnector {
public SerialConnector(LinkManager linkManager, String serialPort, Logger logger) {
this.linkManager = linkManager;
portHolder = new PortHolder(linkManager, logger);
this.logger = logger;
portHolder.port = serialPort;
portHolder = new PortHolder(serialPort, linkManager, logger) {
@NotNull
public IoStream openStream() {
return SerialIoStreamJSerialComm.openPort(serialPort, logger);
}
};
}
@Override