diff --git a/src/com/romraider/logger/external/aem/io/AemRunner.java b/src/com/romraider/logger/external/aem/io/AemRunner.java index cc0ce2c5..fe02c216 100644 --- a/src/com/romraider/logger/external/aem/io/AemRunner.java +++ b/src/com/romraider/logger/external/aem/io/AemRunner.java @@ -19,28 +19,35 @@ package com.romraider.logger.external.aem.io; -import com.romraider.logger.external.core.RawDataListener; +import com.romraider.io.serial.connection.SerialConnection; +import com.romraider.io.serial.connection.SerialConnectionImpl; +import com.romraider.logger.external.aem.plugin.AemDataItem; import com.romraider.logger.external.core.Stoppable; -import com.romraider.logger.external.core.TerminalConnection; -import com.romraider.logger.external.core.TerminalConnectionImpl; +import static com.romraider.util.ParamChecker.isNullOrEmpty; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; public final class AemRunner implements Stoppable { + private static final Logger LOGGER = getLogger(AemRunner.class); private static final AemConnectionProperties CONNECTION_PROPS = new AemConnectionProperties(); - private final TerminalConnection connection; - private final RawDataListener listener; + private final SerialConnection connection; + private final AemDataItem dataItem; private boolean stop; - public AemRunner(String port, RawDataListener listener) { - this.connection = new TerminalConnectionImpl("AEM UEGO", port, CONNECTION_PROPS); - this.listener = listener; + public AemRunner(String port, AemDataItem dataItem) { + this.connection = new SerialConnectionImpl(port, CONNECTION_PROPS); + this.dataItem = dataItem; } public void run() { try { while (!stop) { - byte[] bytes = connection.read(); - listener.setBytes(bytes); + String response = connection.readLine(); + LOGGER.trace("AEM UEGO Response: " + response); + if (!isNullOrEmpty(response)) dataItem.setData(parseDouble(response)); } + } catch (Throwable t) { + LOGGER.error("Error occurred", t); } finally { connection.close(); } @@ -49,4 +56,12 @@ public final class AemRunner implements Stoppable { public void stop() { stop = true; } + + private double parseDouble(String value) { + try { + return Double.parseDouble(value); + } catch (Exception e) { + return 0.0; + } + } } diff --git a/src/com/romraider/logger/external/aem/plugin/AemConvertor.java b/src/com/romraider/logger/external/aem/plugin/AemConvertor.java deleted file mode 100644 index ac7be90b..00000000 --- a/src/com/romraider/logger/external/aem/plugin/AemConvertor.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * RomRaider Open-Source Tuning, Logging and Reflashing - * Copyright (C) 2006-2010 RomRaider.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package com.romraider.logger.external.aem.plugin; - -public interface AemConvertor { - double convert(byte[] bytes); -} diff --git a/src/com/romraider/logger/external/aem/plugin/AemConvertorImpl.java b/src/com/romraider/logger/external/aem/plugin/AemConvertorImpl.java deleted file mode 100644 index 3c482921..00000000 --- a/src/com/romraider/logger/external/aem/plugin/AemConvertorImpl.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * RomRaider Open-Source Tuning, Logging and Reflashing - * Copyright (C) 2006-2010 RomRaider.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - - -package com.romraider.logger.external.aem.plugin; - -import static com.romraider.util.HexUtil.asHex; -import static java.lang.Double.parseDouble; -import org.apache.log4j.Logger; -import java.nio.charset.Charset; - -public final class AemConvertorImpl implements AemConvertor { - private static final Logger LOGGER = Logger.getLogger(AemConvertorImpl.class); - private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8"); - - public double convert(byte[] bytes) { - String value = new String(bytes, CHARSET_UTF8); - double result = convert(value); - LOGGER.trace("Converting AEM response: " + asHex(bytes) + " --> \"" + value + "\" --> " + result); - return result; - } - - private double convert(String value) { - try { - return parseDouble(value); - } catch (NumberFormatException e) { - LOGGER.error("Error converting AEM response to double: \"" + value + "\"", e); - return -1.0; - } - } -} diff --git a/src/com/romraider/logger/external/aem/plugin/AemDataItem.java b/src/com/romraider/logger/external/aem/plugin/AemDataItem.java index 0bdfc380..91c57e33 100644 --- a/src/com/romraider/logger/external/aem/plugin/AemDataItem.java +++ b/src/com/romraider/logger/external/aem/plugin/AemDataItem.java @@ -19,12 +19,11 @@ package com.romraider.logger.external.aem.plugin; +import com.romraider.logger.external.core.DataListener; import com.romraider.logger.external.core.ExternalDataItem; -import com.romraider.logger.external.core.RawDataListener; -public final class AemDataItem implements ExternalDataItem, RawDataListener { - private final AemConvertor convertor = new AemConvertorImpl(); - private byte[] bytes; +public final class AemDataItem implements ExternalDataItem, DataListener { + private double data; public String getName() { return "AEM UEGO"; @@ -39,11 +38,10 @@ public final class AemDataItem implements ExternalDataItem, RawDataListener { } public double getData() { - if (bytes == null) return 0.0; - return convertor.convert(bytes); + return data; } - public void setBytes(byte[] bytes) { - this.bytes = bytes; + public void setData(double data) { + this.data = data; } } diff --git a/src/com/romraider/logger/external/core/TerminalConnection.java b/src/com/romraider/logger/external/core/TerminalConnection.java deleted file mode 100644 index 83580b76..00000000 --- a/src/com/romraider/logger/external/core/TerminalConnection.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * RomRaider Open-Source Tuning, Logging and Reflashing - * Copyright (C) 2006-2010 RomRaider.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package com.romraider.logger.external.core; - -public interface TerminalConnection { - byte[] read(); - - void close(); -} diff --git a/src/com/romraider/logger/external/core/TerminalConnectionImpl.java b/src/com/romraider/logger/external/core/TerminalConnectionImpl.java deleted file mode 100644 index 18a1e512..00000000 --- a/src/com/romraider/logger/external/core/TerminalConnectionImpl.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * RomRaider Open-Source Tuning, Logging and Reflashing - * Copyright (C) 2006-2010 RomRaider.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package com.romraider.logger.external.core; - -import com.romraider.io.connection.ConnectionProperties; -import com.romraider.io.serial.connection.SerialConnection; -import com.romraider.io.serial.connection.SerialConnectionImpl; -import com.romraider.logger.ecu.exception.SerialCommunicationException; -import static com.romraider.util.ParamChecker.checkNotNull; -import static com.romraider.util.ParamChecker.checkNotNullOrEmpty; -import static java.nio.charset.Charset.forName; -import org.apache.log4j.Logger; -import static org.apache.log4j.Logger.getLogger; -import java.nio.charset.Charset; - -public final class TerminalConnectionImpl implements TerminalConnection { - private static final Logger LOGGER = getLogger(TerminalConnectionImpl.class); - private static final Charset CHARSET_UTF8 = forName("UTF-8"); - private final SerialConnection connection; - private final String deviceName; - - public TerminalConnectionImpl(String deviceName, String port, ConnectionProperties connectionProperties) { - checkNotNullOrEmpty(deviceName, "deviceName"); - checkNotNullOrEmpty(port, "port"); - checkNotNull(connectionProperties, "connectionProperties"); - this.connection = new SerialConnectionImpl(port, connectionProperties); - this.deviceName = deviceName; - } - - public byte[] read() { - try { - String s = connection.readLine(); - LOGGER.trace(deviceName + " Response: " + s); - return s.getBytes(CHARSET_UTF8); - } catch (Exception e) { - close(); - throw new SerialCommunicationException(e); - } - } - - public void close() { - connection.close(); - } -} diff --git a/src/com/romraider/logger/external/fourteenpoint7/io/NawRunner.java b/src/com/romraider/logger/external/fourteenpoint7/io/NawRunner.java index e0ebba24..c002a070 100644 --- a/src/com/romraider/logger/external/fourteenpoint7/io/NawRunner.java +++ b/src/com/romraider/logger/external/fourteenpoint7/io/NawRunner.java @@ -21,8 +21,11 @@ package com.romraider.logger.external.fourteenpoint7.io; import com.romraider.logger.external.core.RawDataListener; import com.romraider.logger.external.core.Stoppable; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; public final class NawRunner implements Stoppable { + private static final Logger LOGGER = getLogger(NawRunner.class); private static final byte[] NAW_PROMPT = {0x07}; private final NawConnection connection; private final RawDataListener listener; @@ -40,6 +43,8 @@ public final class NawRunner implements Stoppable { byte[] buffer = connection.readBytes(); listener.setBytes(buffer); } + } catch (Throwable t) { + LOGGER.error("Error occurred", t); } finally { connection.close(); } diff --git a/src/com/romraider/logger/external/innovate/generic/mts/io/MTSRunner.java b/src/com/romraider/logger/external/innovate/generic/mts/io/MTSRunner.java index 9c194c29..08d2d94a 100644 --- a/src/com/romraider/logger/external/innovate/generic/mts/io/MTSRunner.java +++ b/src/com/romraider/logger/external/innovate/generic/mts/io/MTSRunner.java @@ -44,6 +44,8 @@ public final class MTSRunner implements Stoppable { running = true; try { doRun(); + } catch (Throwable t) { + LOGGER.error("Error occurred", t); } finally { running = false; } diff --git a/src/com/romraider/logger/external/innovate/generic/serial/io/InnovateRunner.java b/src/com/romraider/logger/external/innovate/generic/serial/io/InnovateRunner.java index 9c7c6a4f..85ca0cce 100644 --- a/src/com/romraider/logger/external/innovate/generic/serial/io/InnovateRunner.java +++ b/src/com/romraider/logger/external/innovate/generic/serial/io/InnovateRunner.java @@ -82,6 +82,8 @@ public final class InnovateRunner implements Stoppable { LOGGER.trace("Innovate discarded: " + hex(b0)); } } + } catch (Throwable t) { + LOGGER.error("Error occurred", t); } finally { connection.close(); } diff --git a/src/com/romraider/logger/external/mrf/io/MrfRunner.java b/src/com/romraider/logger/external/mrf/io/MrfRunner.java index c2a69184..b209529e 100644 --- a/src/com/romraider/logger/external/mrf/io/MrfRunner.java +++ b/src/com/romraider/logger/external/mrf/io/MrfRunner.java @@ -20,40 +20,42 @@ package com.romraider.logger.external.mrf.io; import com.romraider.io.connection.ConnectionProperties; +import com.romraider.io.serial.connection.SerialConnection; +import com.romraider.io.serial.connection.SerialConnectionImpl; import com.romraider.logger.external.core.Stoppable; -import com.romraider.logger.external.core.TerminalConnection; -import com.romraider.logger.external.core.TerminalConnectionImpl; import com.romraider.logger.external.mrf.plugin.MrfDataItem; import com.romraider.logger.external.mrf.plugin.MrfSensorType; import static com.romraider.util.ParamChecker.isNullOrEmpty; -import static java.nio.charset.Charset.forName; -import java.nio.charset.Charset; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; import java.util.Map; public final class MrfRunner implements Stoppable { + private static final Logger LOGGER = getLogger(MrfRunner.class); private static final ConnectionProperties CONNECTION_PROPS = new MrfConnectionProperties(); - private static final Charset CHARSET_UTF8 = forName("UTF-8"); private final Map dataItems; - private final TerminalConnection connection; + private final SerialConnection connection; private boolean stop; public MrfRunner(String port, Map dataItems) { - this.connection = new TerminalConnectionImpl("Mrf Stealth Gauge", port, CONNECTION_PROPS); + this.connection = new SerialConnectionImpl(port, CONNECTION_PROPS); this.dataItems = dataItems; } public void run() { try { while (!stop) { - byte[] bytes = connection.read(); - String response = new String(bytes, CHARSET_UTF8); + String response = connection.readLine(); if (isNullOrEmpty(response)) continue; + LOGGER.trace("Mrf Stealth Gauge Response: " + response); String[] values = response.split(","); for (int i = 0; i < values.length; i++) { MrfDataItem dataItem = dataItems.get(MrfSensorType.valueOf(i)); if (dataItem != null) dataItem.setData(parseDouble(values[i])); } } + } catch (Throwable t) { + LOGGER.error("Error occurred", t); } finally { connection.close(); } diff --git a/src/com/romraider/logger/external/plx/io/PlxRunner.java b/src/com/romraider/logger/external/plx/io/PlxRunner.java index 354f29a2..d2539c98 100644 --- a/src/com/romraider/logger/external/plx/io/PlxRunner.java +++ b/src/com/romraider/logger/external/plx/io/PlxRunner.java @@ -22,9 +22,12 @@ package com.romraider.logger.external.plx.io; import com.romraider.logger.external.core.Stoppable; import static com.romraider.logger.external.plx.io.PlxSensorType.UNKNOWN; import com.romraider.logger.external.plx.plugin.PlxDataItem; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; import java.util.Map; public final class PlxRunner implements Stoppable { + private static final Logger LOGGER = getLogger(PlxRunner.class); private final Map dataItems; private final PlxConnection connection; private boolean stop; @@ -44,6 +47,8 @@ public final class PlxRunner implements Stoppable { PlxDataItem item = dataItems.get(response.sensor); item.setRaw(response.value); } + } catch (Throwable t) { + LOGGER.error("Error occurred", t); } finally { connection.close(); } diff --git a/src/com/romraider/logger/external/zt2/io/ZT2Runner.java b/src/com/romraider/logger/external/zt2/io/ZT2Runner.java index f882d76f..4767c321 100644 --- a/src/com/romraider/logger/external/zt2/io/ZT2Runner.java +++ b/src/com/romraider/logger/external/zt2/io/ZT2Runner.java @@ -27,11 +27,14 @@ import static com.romraider.logger.external.zt2.plugin.ZT2SensorType.EGT; import static com.romraider.logger.external.zt2.plugin.ZT2SensorType.MAP; import static com.romraider.logger.external.zt2.plugin.ZT2SensorType.RPM; import static com.romraider.logger.external.zt2.plugin.ZT2SensorType.TPS; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; import java.util.ArrayList; import java.util.List; import java.util.Map; public final class ZT2Runner implements Stoppable { + private static final Logger LOGGER = getLogger(ZT2Runner.class); private final Map dataItems; private final ZT2Connection connection; private boolean stop; @@ -108,6 +111,8 @@ public final class ZT2Runner implements Stoppable { packetStarted = false; } } + } catch (Throwable t) { + LOGGER.error("Error occurred", t); } finally { connection.close(); }