aem/mrf plugin fixes

git-svn-id: https://svn2.assembla.com/svn/romraider/trunk@290 38686702-15cf-42e4-a595-3071df8bf5ea
This commit is contained in:
kascade 2010-05-25 12:27:44 +00:00
parent d96f4f81e0
commit 1e3066aee3
12 changed files with 61 additions and 185 deletions

View File

@ -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;
}
}
}

View File

@ -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);
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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<MrfSensorType, MrfDataItem> dataItems;
private final TerminalConnection connection;
private final SerialConnection connection;
private boolean stop;
public MrfRunner(String port, Map<MrfSensorType, MrfDataItem> 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();
}

View File

@ -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<PlxSensorType, PlxDataItem> 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();
}

View File

@ -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<ZT2SensorType, ZT2DataItem> 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();
}