mirror of https://github.com/rusefi/rusefi-1.git
refactoring
This commit is contained in:
parent
dd0c645f05
commit
72f80c6be7
|
@ -18,24 +18,25 @@ import java.awt.*;
|
||||||
* 7/25/13
|
* 7/25/13
|
||||||
* Andrey Belomutskiy, (c) 2013-2020
|
* Andrey Belomutskiy, (c) 2013-2020
|
||||||
*/
|
*/
|
||||||
public class PortHolder {
|
public abstract class PortHolder {
|
||||||
private final DataListener dataListener;
|
private final DataListener dataListener;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final LinkManager linkManager;
|
private final LinkManager linkManager;
|
||||||
|
|
||||||
public ConnectionStateListener listener;
|
public ConnectionStateListener listener;
|
||||||
private final Object portLock = new Object();
|
private final Object portLock = new Object();
|
||||||
|
private final String port;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private BinaryProtocol bp;
|
private BinaryProtocol bp;
|
||||||
|
|
||||||
protected PortHolder(LinkManager linkManager, Logger logger) {
|
protected PortHolder(String port, LinkManager linkManager, Logger logger) {
|
||||||
|
this.port = port;
|
||||||
this.linkManager = linkManager;
|
this.linkManager = linkManager;
|
||||||
dataListener = freshData -> linkManager.getEngineState().processNewData(new String(freshData), LinkManager.ENCODER);
|
dataListener = freshData -> linkManager.getEngineState().processNewData(new String(freshData), LinkManager.ENCODER);
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String port;
|
|
||||||
|
|
||||||
boolean connectAndReadConfiguration() {
|
boolean connectAndReadConfiguration() {
|
||||||
if (port == null)
|
if (port == null)
|
||||||
|
@ -43,7 +44,7 @@ public class PortHolder {
|
||||||
|
|
||||||
MessagesCentral.getInstance().postMessage(logger, getClass(), "Opening port: " + port);
|
MessagesCentral.getInstance().postMessage(logger, getClass(), "Opening port: " + port);
|
||||||
|
|
||||||
IoStream stream = SerialIoStreamJSerialComm.openPort(port, logger);
|
IoStream stream = openStream();
|
||||||
synchronized (portLock) {
|
synchronized (portLock) {
|
||||||
bp = new BinaryProtocol(linkManager, logger, stream);
|
bp = new BinaryProtocol(linkManager, logger, stream);
|
||||||
portLock.notifyAll();
|
portLock.notifyAll();
|
||||||
|
@ -60,6 +61,8 @@ public class PortHolder {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract IoStream openStream();
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
synchronized (portLock) {
|
synchronized (portLock) {
|
||||||
if (bp != null) {
|
if (bp != null) {
|
||||||
|
|
|
@ -4,8 +4,10 @@ import com.opensr5.Logger;
|
||||||
import com.rusefi.binaryprotocol.BinaryProtocol;
|
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||||
import com.rusefi.core.MessagesCentral;
|
import com.rusefi.core.MessagesCentral;
|
||||||
import com.rusefi.io.ConnectionStateListener;
|
import com.rusefi.io.ConnectionStateListener;
|
||||||
|
import com.rusefi.io.IoStream;
|
||||||
import com.rusefi.io.LinkConnector;
|
import com.rusefi.io.LinkConnector;
|
||||||
import com.rusefi.io.LinkManager;
|
import com.rusefi.io.LinkManager;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andrey Belomutskiy
|
* @author Andrey Belomutskiy
|
||||||
|
@ -19,9 +21,13 @@ public class SerialConnector implements LinkConnector {
|
||||||
|
|
||||||
public SerialConnector(LinkManager linkManager, String serialPort, Logger logger) {
|
public SerialConnector(LinkManager linkManager, String serialPort, Logger logger) {
|
||||||
this.linkManager = linkManager;
|
this.linkManager = linkManager;
|
||||||
portHolder = new PortHolder(linkManager, logger);
|
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
portHolder.port = serialPort;
|
portHolder = new PortHolder(serialPort, linkManager, logger) {
|
||||||
|
@NotNull
|
||||||
|
public IoStream openStream() {
|
||||||
|
return SerialIoStreamJSerialComm.openPort(serialPort, logger);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue