making code greener

This commit is contained in:
rusefillc 2021-12-04 12:19:48 -05:00
parent e407ae73df
commit f0fd53ed3c
1 changed files with 16 additions and 21 deletions

View File

@ -6,7 +6,6 @@ import com.rusefi.io.IoStream;
import com.rusefi.io.serial.BaudRateHolder; 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.ServerSocketReference;
import com.rusefi.io.tcp.TcpConnector; import com.rusefi.io.tcp.TcpConnector;
import java.io.Closeable; import java.io.Closeable;
@ -35,12 +34,10 @@ public class Elm327Connector implements Closeable, DataListener {
private final static int ISO_TP_FRAME_FLOW_CONTROL = 3; private final static int ISO_TP_FRAME_FLOW_CONTROL = 3;
private IoStream stream = null; private IoStream stream = null;
private String partialLine = new String(); private String partialLine = "";
private List<String> completeLines = new ArrayList<String>(); private final List<String> completeLines = new ArrayList<>();
private boolean waitForEcho = true;
private boolean isCommandMode = false; private boolean isCommandMode = false;
private ServerSocketReference serverHolder;
private Elm327IoStream elmStream; private Elm327IoStream elmStream;
// CAN multiframe decoder state // CAN multiframe decoder state
@ -51,7 +48,7 @@ public class Elm327Connector implements Closeable, DataListener {
public byte [] decodePacket(byte [] data) throws Exception { public byte [] decodePacket(byte [] data) throws Exception {
int frameType = (data[0] >> 4) & 0xf; int frameType = (data[0] >> 4) & 0xf;
int numBytesAvailable, frameIdx; int numBytesAvailable, frameIdx;
int dataOffset = 0; int dataOffset;
switch (frameType) { switch (frameType) {
case ISO_TP_FRAME_SINGLE: case ISO_TP_FRAME_SINGLE:
numBytesAvailable = data[0] & 0xf; numBytesAvailable = data[0] & 0xf;
@ -100,7 +97,7 @@ public class Elm327Connector implements Closeable, DataListener {
// Echo off // Echo off
sendCommand("ATE0", "OK"); sendCommand("ATE0", "OK");
waitForEcho = false; //waitForEcho = false;
// protocol #6 - ISO 15765-4 CAN (11 bit ID, 500 kbaud) // protocol #6 - ISO 15765-4 CAN (11 bit ID, 500 kbaud)
sendCommand("ATSP6", "OK"); sendCommand("ATSP6", "OK");
@ -151,7 +148,7 @@ public class Elm327Connector implements Closeable, DataListener {
} }
@Override @Override
public void onDataArrived(byte 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) {
@ -161,7 +158,7 @@ public class Elm327Connector implements Closeable, DataListener {
// split the stream into separate lines // split the stream into separate lines
if (newL >= 0) { if (newL >= 0) {
String curLine = this.partialLine; String curLine = this.partialLine;
this.partialLine = new String(); this.partialLine = "";
if (newL > 0) if (newL > 0)
curLine += freshStr.substring(0, newL); curLine += freshStr.substring(0, newL);
if (curLine.length() > 0) if (curLine.length() > 0)
@ -174,7 +171,7 @@ public class Elm327Connector implements Closeable, DataListener {
} }
} }
public void sendBytesToSerial(byte [] bytes) throws IOException { public void sendBytesToSerial(byte [] bytes) {
log.info("-------sendBytesToSerial "+bytes.length+" bytes:"); log.info("-------sendBytesToSerial "+bytes.length+" bytes:");
for (int i = 0; i < bytes.length; i++) { for (int i = 0; i < bytes.length; i++) {
@ -191,7 +188,7 @@ public class Elm327Connector implements Closeable, DataListener {
// send the first header frame // send the first header frame
sendFrame((ISO_TP_FRAME_FIRST << 4) | ((bytes.length >> 8) & 0x0f), bytes.length & 0xff, bytes, 0, 6); sendFrame((ISO_TP_FRAME_FIRST << 4) | ((bytes.length >> 8) & 0x0f), bytes.length & 0xff, bytes, 0, 6);
// get a flow control frame // get a flow control frame
byte[] fc = receiveData(); receiveData();
// send the rest of the data // send the rest of the data
int idx = 1, offset = 6; int idx = 1, offset = 6;
@ -233,9 +230,7 @@ public class Elm327Connector implements Closeable, DataListener {
try { try {
this.stream.write((command + "\r").getBytes()); this.stream.write((command + "\r").getBytes());
waitForResponse(timeout); waitForResponse(timeout);
} catch (IOException ignore) { } catch (IOException | InterruptedException ignore) {
return null;
} catch (InterruptedException ignore) {
return null; return null;
} finally { } finally {
isCommandMode = false; isCommandMode = false;
@ -256,7 +251,7 @@ public class Elm327Connector implements Closeable, DataListener {
Matcher matcher = pattern.matcher(this.completeLines.get(responseIdx)); Matcher matcher = pattern.matcher(this.completeLines.get(responseIdx));
if (matcher.find()) { if (matcher.find()) {
// store the echo mode // store the echo mode
this.waitForEcho = responseIdx != 0; //this.waitForEcho = responseIdx != 0;
return (matcher.groupCount() > 0) ? matcher.group(1) : matcher.group(); return (matcher.groupCount() > 0) ? matcher.group(1) : matcher.group();
} }
@ -288,7 +283,7 @@ public class Elm327Connector implements Closeable, DataListener {
try { try {
this.stream.write(hexData); this.stream.write(hexData);
} catch (IOException ignore) { } catch (IOException ignore) {
return; // ignore
} }
} }
@ -342,11 +337,11 @@ public class Elm327Connector implements Closeable, DataListener {
private boolean startNetworkConnector(int controllerPort) { private boolean startNetworkConnector(int controllerPort) {
try { try {
elmStream = new Elm327IoStream(this, "elm327Stream"); elmStream = new Elm327IoStream(this, "elm327Stream");
serverHolder = BinaryProtocolProxy.createProxy(elmStream, controllerPort, new BinaryProtocolProxy.ClientApplicationActivityListener() { BinaryProtocolProxy.createProxy(elmStream, controllerPort, new BinaryProtocolProxy.ClientApplicationActivityListener() {
@Override @Override
public void onActivity() { public void onActivity() {
} }
}); });
} catch (IOException ignore) { } catch (IOException ignore) {
return false; return false;
} }