refactoring: explicit data listener

This commit is contained in:
rusefillc 2021-12-04 14:22:48 -05:00
parent 154db2b495
commit d9a7433a03
1 changed files with 4 additions and 7 deletions

View File

@ -3,7 +3,6 @@ package com.rusefi.io.can;
import com.devexperts.logging.Logging; import com.devexperts.logging.Logging;
import com.opensr5.io.DataListener; import com.opensr5.io.DataListener;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import com.rusefi.io.serial.BaudRateHolder;
import com.rusefi.io.serial.SerialIoStreamJSerialComm; import com.rusefi.io.serial.SerialIoStreamJSerialComm;
import com.rusefi.io.tcp.BinaryProtocolProxy; import com.rusefi.io.tcp.BinaryProtocolProxy;
import com.rusefi.io.tcp.TcpConnector; import com.rusefi.io.tcp.TcpConnector;
@ -18,7 +17,7 @@ import java.util.regex.Pattern;
import static com.rusefi.Timeouts.SECOND; import static com.rusefi.Timeouts.SECOND;
public class Elm327Connector implements Closeable, DataListener { public class Elm327Connector implements Closeable {
private final static Logging log = Logging.getLogging(Elm327Connector.class); private final static Logging log = Logging.getLogging(Elm327Connector.class);
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(); private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes();
@ -84,7 +83,6 @@ public class Elm327Connector implements Closeable, DataListener {
} }
} }
public static boolean checkConnection(String serialPort, IoStream stream) { public static boolean checkConnection(String serialPort, IoStream stream) {
Elm327Connector con = new Elm327Connector(); Elm327Connector con = new Elm327Connector();
boolean found = con.initConnection(serialPort, stream); boolean found = con.initConnection(serialPort, stream);
@ -151,8 +149,7 @@ public class Elm327Connector implements Closeable, DataListener {
stream.close(); stream.close();
} }
@Override private final DataListener listener = freshData -> {
public void onDataArrived(byte[] freshData) {
// ELM327 uses a text protocol, so we convert the data to a string // ELM327 uses a text protocol, so we convert the data to a string
String freshStr = new String(freshData); String freshStr = new String(freshData);
while (true) { while (true) {
@ -173,7 +170,7 @@ public class Elm327Connector implements Closeable, DataListener {
this.partialLine += freshStr; this.partialLine += freshStr;
break; break;
} }
} };
public void sendBytesToSerial(byte [] bytes) { public void sendBytesToSerial(byte [] bytes) {
log.info("-------sendBytesToSerial "+bytes.length+" bytes:"); log.info("-------sendBytesToSerial "+bytes.length+" bytes:");
@ -211,7 +208,7 @@ public class Elm327Connector implements Closeable, DataListener {
private boolean initConnection(String msg, IoStream stream) { private boolean initConnection(String msg, IoStream stream) {
this.stream = stream; this.stream = stream;
this.stream.setInputListener(this); this.stream.setInputListener(listener);
if (sendCommand("ATZ", "ELM327 v[0-9]+\\.[0-9]+", BIG_TIMEOUT) != null) { if (sendCommand("ATZ", "ELM327 v[0-9]+\\.[0-9]+", BIG_TIMEOUT) != null) {
log.info("ELM DETECTED on " + msg + "!"); log.info("ELM DETECTED on " + msg + "!");
return true; return true;