reducing complexity
This commit is contained in:
parent
372d321878
commit
22041753f5
|
@ -27,6 +27,11 @@ import java.util.concurrent.TimeoutException;
|
|||
import static com.rusefi.binaryprotocol.IoHelper.*;
|
||||
|
||||
/**
|
||||
* This object represents logical state of physical connection.
|
||||
*
|
||||
* Instance is connected until we experience issues. Once we decide to close the connection there is no restart -
|
||||
* new instance of this class would need to be created once we establish a new physical connection.
|
||||
*
|
||||
* (c) Andrey Belomutskiy
|
||||
* 3/6/2015
|
||||
* @see BinaryProtocolHolder
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.rusefi.io.serial;
|
||||
|
||||
public enum BaudRateHolder {
|
||||
INSTANCE;
|
||||
|
||||
/**
|
||||
* Nasty code: this field is not final, we have UI which overrides this default!
|
||||
*/
|
||||
public int baudRate = 115200;
|
||||
|
||||
}
|
|
@ -19,10 +19,7 @@ import java.awt.*;
|
|||
* (c) Andrey Belomutskiy
|
||||
*/
|
||||
public class PortHolder {
|
||||
/**
|
||||
* Nasty code: this field is not final, we have UI which overrides this default!
|
||||
*/
|
||||
public static int BAUD_RATE = 115200;
|
||||
public ConnectionStateListener listener;
|
||||
private static PortHolder instance = new PortHolder();
|
||||
private final Object portLock = new Object();
|
||||
|
||||
|
@ -34,11 +31,21 @@ public class PortHolder {
|
|||
@Nullable
|
||||
private IoStream serialPort;
|
||||
|
||||
boolean openPort(String port, DataListener dataListener, ConnectionStateListener listener) {
|
||||
CommunicationLoggingHolder.communicationLoggingListener.onPortHolderMessage(SerialManager.class, "Opening port: " + port);
|
||||
boolean connectAndReadConfiguration(String port, DataListener dataListener) {
|
||||
if (port == null)
|
||||
return false;
|
||||
boolean result = open(port, dataListener);
|
||||
|
||||
CommunicationLoggingHolder.communicationLoggingListener.onPortHolderMessage(SerialManager.class, "Opening port: " + port);
|
||||
|
||||
IoStream stream = SerialIoStreamJSerialComm.openPort(port);
|
||||
synchronized (portLock) {
|
||||
this.serialPort = stream;
|
||||
portLock.notifyAll();
|
||||
}
|
||||
|
||||
bp = BinaryProtocolHolder.getInstance().create(FileLog.LOGGER, stream);
|
||||
|
||||
boolean result = bp.connectAndReadConfiguration(dataListener);
|
||||
if (listener != null) {
|
||||
if (result) {
|
||||
listener.onConnectionEstablished();
|
||||
|
@ -49,21 +56,6 @@ public class PortHolder {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if everything fine
|
||||
*/
|
||||
private boolean open(String port, final DataListener listener) {
|
||||
IoStream stream = EstablishConnection.create(port);
|
||||
synchronized (portLock) {
|
||||
PortHolder.this.serialPort = stream;
|
||||
portLock.notifyAll();
|
||||
}
|
||||
|
||||
bp = BinaryProtocolHolder.getInstance().create(FileLog.LOGGER, stream);
|
||||
|
||||
return bp.connectAndReadConfiguration(listener);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
synchronized (portLock) {
|
||||
if (serialPort != null) {
|
||||
|
@ -95,10 +87,4 @@ public class PortHolder {
|
|||
public static PortHolder getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static class EstablishConnection {
|
||||
public static IoStream create(String port) {
|
||||
return SerialIoStreamJSerialComm.open(port, BAUD_RATE, FileLog.LOGGER);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,13 +18,13 @@ public class SerialConnector implements LinkConnector {
|
|||
@Override
|
||||
public void connect(ConnectionStateListener listener) {
|
||||
FileLog.MAIN.logLine("SerialConnector: connecting");
|
||||
SerialManager.listener = listener;
|
||||
PortHolder.getInstance().listener = listener;
|
||||
FileLog.MAIN.logLine("scheduleOpening");
|
||||
LinkManager.COMMUNICATION_EXECUTOR.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
FileLog.MAIN.logLine("scheduleOpening>openPort");
|
||||
PortHolder.getInstance().openPort(SerialManager.port, SerialManager.dataListener, SerialManager.listener);
|
||||
PortHolder.getInstance().connectAndReadConfiguration(SerialManager.port, SerialManager.dataListener);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public class SerialConnector implements LinkConnector {
|
|||
// if (closed)
|
||||
// return;
|
||||
PortHolder.getInstance().close();
|
||||
PortHolder.getInstance().openPort(SerialManager.port, SerialManager.dataListener, SerialManager.listener);
|
||||
PortHolder.getInstance().connectAndReadConfiguration(SerialManager.port, SerialManager.dataListener);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.fazecast.jSerialComm.SerialPortDataListener;
|
|||
import com.fazecast.jSerialComm.SerialPortEvent;
|
||||
import com.opensr5.Logger;
|
||||
import com.opensr5.io.DataListener;
|
||||
import com.rusefi.FileLog;
|
||||
import com.rusefi.io.IoStream;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
@ -67,14 +68,16 @@ public class SerialIoStreamJSerialComm implements IoStream {
|
|||
sp.writeBytes(bytes, bytes.length);
|
||||
}
|
||||
|
||||
public static IoStream openPort(String port) {
|
||||
return openPort(port, BaudRateHolder.INSTANCE.baudRate, FileLog.LOGGER);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static IoStream open(String port, int baudRate, Logger logger) {
|
||||
private static IoStream openPort(String port, int baudRate, Logger logger) {
|
||||
logger.info("[SerialIoStreamJSerialComm] " + port);
|
||||
SerialPort sp = SerialPort.getCommPort(port);
|
||||
sp.setBaudRate(baudRate);
|
||||
sp.openPort();
|
||||
|
||||
|
||||
return new SerialIoStreamJSerialComm(sp);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,49 +11,10 @@ import com.rusefi.io.LinkManager;
|
|||
class SerialManager {
|
||||
public static String port;
|
||||
|
||||
// private static boolean closed;
|
||||
|
||||
static DataListener dataListener = new DataListener() {
|
||||
public void onDataArrived(byte freshData[]) {
|
||||
// jTextAreaIn.append(string);
|
||||
LinkManager.engineState.processNewData(new String(freshData), LinkManager.ENCODER);
|
||||
}
|
||||
};
|
||||
public static ConnectionStateListener listener;
|
||||
|
||||
/*
|
||||
static String[] findSerialPorts() {
|
||||
List<String> result = new ArrayList<String>();
|
||||
|
||||
Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
|
||||
while (portEnum.hasMoreElements()) {
|
||||
CommPortIdentifier portIdentifier = portEnum.nextElement();
|
||||
System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()));
|
||||
if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL)
|
||||
result.add(portIdentifier.getName());
|
||||
}
|
||||
return result.toArray(new String[result.size()]);
|
||||
}
|
||||
|
||||
static String getPortTypeName(int portType) {
|
||||
switch (portType) {
|
||||
case CommPortIdentifier.PORT_I2C:
|
||||
return "I2C";
|
||||
case CommPortIdentifier.PORT_PARALLEL:
|
||||
return "Parallel";
|
||||
case CommPortIdentifier.PORT_RAW:
|
||||
return "Raw";
|
||||
case CommPortIdentifier.PORT_RS485:
|
||||
return "RS485";
|
||||
case CommPortIdentifier.PORT_SERIAL:
|
||||
return "Serial";
|
||||
default:
|
||||
return "unknown type";
|
||||
}
|
||||
}
|
||||
*/
|
||||
// public static void close() {
|
||||
// closed = true;
|
||||
// SerialIO.getInstance().stop();
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -8,8 +8,9 @@ import com.rusefi.core.MessagesCentral;
|
|||
import com.rusefi.core.Sensor;
|
||||
import com.rusefi.core.SensorCentral;
|
||||
import com.rusefi.io.*;
|
||||
import com.rusefi.io.serial.PortHolder;
|
||||
import com.rusefi.io.serial.BaudRateHolder;
|
||||
import com.rusefi.io.serial.SerialConnector;
|
||||
import com.rusefi.io.serial.SerialIoStreamJSerialComm;
|
||||
import com.rusefi.maintenance.FirmwareFlasher;
|
||||
import com.rusefi.maintenance.VersionChecker;
|
||||
import com.rusefi.ui.*;
|
||||
|
@ -96,7 +97,7 @@ public class Launcher {
|
|||
FileLog.MAIN.logLine("Hardware: " + FirmwareFlasher.getHardwareKind());
|
||||
|
||||
getConfig().getRoot().setProperty(PORT_KEY, port);
|
||||
getConfig().getRoot().setProperty(SPEED_KEY, PortHolder.BAUD_RATE);
|
||||
getConfig().getRoot().setProperty(SPEED_KEY, BaudRateHolder.INSTANCE.baudRate);
|
||||
|
||||
LinkManager.start(port);
|
||||
|
||||
|
@ -278,7 +279,7 @@ public class Launcher {
|
|||
String autoDetectedPort = autoDetectPort();
|
||||
if (autoDetectedPort == null)
|
||||
return;
|
||||
IoStream stream = PortHolder.EstablishConnection.create(autoDetectedPort);
|
||||
IoStream stream = SerialIoStreamJSerialComm.openPort(autoDetectedPort);
|
||||
byte[] commandBytes = BinaryProtocol.getTextCommandBytes(command);
|
||||
stream.sendPacket(commandBytes, FileLog.LOGGER);
|
||||
}
|
||||
|
@ -329,7 +330,7 @@ public class Launcher {
|
|||
boolean isPortDefined = args.length > 0;
|
||||
boolean isBaudRateDefined = args.length > 1;
|
||||
if (isBaudRateDefined)
|
||||
PortHolder.BAUD_RATE = Integer.parseInt(args[1]);
|
||||
BaudRateHolder.INSTANCE.baudRate = Integer.parseInt(args[1]);
|
||||
|
||||
String port = null;
|
||||
if (isPortDefined)
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.rusefi;
|
|||
|
||||
import com.rusefi.autodetect.PortDetector;
|
||||
import com.rusefi.io.LinkManager;
|
||||
import com.rusefi.io.serial.BaudRateHolder;
|
||||
import com.rusefi.io.serial.PortHolder;
|
||||
import com.rusefi.maintenance.*;
|
||||
import com.rusefi.ui.util.HorizontalLine;
|
||||
|
@ -112,7 +113,7 @@ public class StartupFrame {
|
|||
connectButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
PortHolder.BAUD_RATE = Integer.parseInt((String) comboSpeeds.getSelectedItem());
|
||||
BaudRateHolder.INSTANCE.baudRate = Integer.parseInt((String) comboSpeeds.getSelectedItem());
|
||||
String selectedPort = comboPorts.getSelectedItem().toString();
|
||||
if (SerialPortScanner.AUTO_SERIAL.equals(selectedPort)) {
|
||||
String autoDetectedPort = PortDetector.autoDetectPort(StartupFrame.this.frame);
|
||||
|
|
|
@ -6,7 +6,7 @@ import com.rusefi.binaryprotocol.BinaryProtocolCommands;
|
|||
import com.rusefi.binaryprotocol.IncomingDataBuffer;
|
||||
import com.rusefi.config.generated.Fields;
|
||||
import com.rusefi.io.IoStream;
|
||||
import com.rusefi.io.serial.PortHolder;
|
||||
import com.rusefi.io.serial.SerialIoStreamJSerialComm;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
@ -27,7 +27,7 @@ class SerialAutoChecker implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
IoStream stream = PortHolder.EstablishConnection.create(serialPort);
|
||||
IoStream stream = SerialIoStreamJSerialComm.openPort(serialPort);
|
||||
Logger logger = FileLog.LOGGER;
|
||||
IncomingDataBuffer incomingData = new IncomingDataBuffer(logger);
|
||||
stream.setInputListener(incomingData::addData);
|
||||
|
|
|
@ -7,7 +7,7 @@ import com.rusefi.autodetect.PortDetector;
|
|||
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||
import com.rusefi.config.generated.Fields;
|
||||
import com.rusefi.io.IoStream;
|
||||
import com.rusefi.io.serial.PortHolder;
|
||||
import com.rusefi.io.serial.BaudRateHolder;
|
||||
import com.rusefi.io.serial.SerialIoStreamJSerialComm;
|
||||
import com.rusefi.ui.StatusWindow;
|
||||
import com.rusefi.ui.util.URLLabel;
|
||||
|
@ -50,7 +50,7 @@ public class DfuFlasher {
|
|||
JOptionPane.showMessageDialog(Launcher.getFrame(), "rusEfi serial port not detected");
|
||||
return;
|
||||
}
|
||||
IoStream stream = SerialIoStreamJSerialComm.open(port, PortHolder.BAUD_RATE, FileLog.LOGGER);
|
||||
IoStream stream = SerialIoStreamJSerialComm.openPort(port);
|
||||
byte[] command = BinaryProtocol.getTextCommandBytes(Fields.CMD_REBOOT_DFU);
|
||||
try {
|
||||
stream.sendPacket(command, FileLog.LOGGER);
|
||||
|
|
Loading…
Reference in New Issue