From b8fcd4cdc0f4ad395a3c59bc5fab45910bde84e3 Mon Sep 17 00:00:00 2001 From: Elton Viana Date: Thu, 13 Jul 2017 15:35:13 -0300 Subject: [PATCH 1/3] Minor fixes --- pom.xml | 469 ++++++----- .../github/pires/obd/commands/ObdCommand.java | 796 +++++++++--------- .../pires/obd/commands/ObdMultiCommand.java | 104 +-- .../obd/commands/PercentageObdCommand.java | 101 ++- .../pires/obd/commands/PersistentCommand.java | 121 +-- .../pires/obd/commands/SpeedCommand.java | 145 ++-- .../pires/obd/commands/SystemOfUnits.java | 14 +- .../control/DistanceMILOnCommand.java | 132 +-- .../control/DistanceSinceCCCommand.java | 147 ++-- .../commands/control/DtcNumberCommand.java | 126 +-- .../control/EquivalentRatioCommand.java | 77 +- .../control/IgnitionMonitorCommand.java | 79 +- .../control/ModuleVoltageCommand.java | 113 +-- .../control/PendingTroubleCodesCommand.java | 266 +++--- .../control/PermanentTroubleCodesCommand.java | 266 +++--- .../control/TimingAdvanceCommand.java | 45 +- .../commands/control/TroubleCodesCommand.java | 265 +++--- .../obd/commands/control/VinCommand.java | 148 ++-- .../commands/engine/AbsoluteLoadCommand.java | 78 +- .../obd/commands/engine/LoadCommand.java | 53 +- .../commands/engine/MassAirFlowCommand.java | 102 +-- .../obd/commands/engine/OilTempCommand.java | 40 +- .../pires/obd/commands/engine/RPMCommand.java | 101 +-- .../obd/commands/engine/RuntimeCommand.java | 92 +- .../engine/ThrottlePositionCommand.java | 41 +- .../commands/fuel/AirFuelRatioCommand.java | 80 +- .../commands/fuel/ConsumptionRateCommand.java | 107 +-- .../commands/fuel/FindFuelTypeCommand.java | 82 +- .../obd/commands/fuel/FuelLevelCommand.java | 54 +- .../obd/commands/fuel/FuelTrimCommand.java | 120 +-- .../fuel/WidebandAirFuelRatioCommand.java | 80 +- .../pressure/BarometricPressureCommand.java | 44 +- .../pressure/FuelPressureCommand.java | 67 +- .../pressure/FuelRailPressureCommand.java | 71 +- .../IntakeManifoldPressureCommand.java | 41 +- .../commands/pressure/PressureCommand.java | 148 ++-- .../protocol/AdaptiveTimingCommand.java | 69 +- .../protocol/AvailablePidsCommand.java | 76 +- .../protocol/AvailablePidsCommand_01_20.java | 41 +- .../protocol/AvailablePidsCommand_21_40.java | 41 +- .../protocol/AvailablePidsCommand_41_60.java | 41 +- .../obd/commands/protocol/CloseCommand.java | 76 +- .../protocol/DescribeProtocolCommand.java | 44 +- .../DescribeProtocolNumberCommand.java | 117 +-- .../obd/commands/protocol/EchoOffCommand.java | 54 +- .../commands/protocol/HeadersOffCommand.java | 54 +- .../commands/protocol/LineFeedOffCommand.java | 55 +- .../commands/protocol/ObdProtocolCommand.java | 78 +- .../obd/commands/protocol/ObdRawCommand.java | 39 +- .../commands/protocol/ObdResetCommand.java | 54 +- .../protocol/ObdWarmstartCommand.java | 55 +- .../protocol/ResetTroubleCodesCommand.java | 63 +- .../protocol/SelectProtocolCommand.java | 43 +- .../commands/protocol/SpacesOffCommand.java | 42 +- .../obd/commands/protocol/TimeoutCommand.java | 61 +- .../AirIntakeTemperatureCommand.java | 45 +- .../AmbientAirTemperatureCommand.java | 45 +- .../EngineCoolantTemperatureCommand.java | 45 +- .../temperature/TemperatureCommand.java | 163 ++-- .../obd/enums/AvailableCommandNames.java | 107 ++- .../com/github/pires/obd/enums/FuelTrim.java | 106 +-- .../com/github/pires/obd/enums/FuelType.java | 120 +-- .../github/pires/obd/enums/ObdProtocols.java | 160 ++-- .../obd/exceptions/BusInitException.java | 19 +- .../MisunderstoodCommandException.java | 19 +- .../pires/obd/exceptions/NoDataException.java | 19 +- .../NonNumericResponseException.java | 24 +- .../obd/exceptions/ResponseException.java | 122 +-- .../obd/exceptions/StoppedException.java | 19 +- .../exceptions/UnableToConnectException.java | 19 +- .../obd/exceptions/UnknownErrorException.java | 19 +- .../UnsupportedCommandException.java | 19 +- .../obd/utils/CommandAvailabilityHelper.java | 416 ++++----- .../commands/AirIntakeTempCommandTest.java | 214 ++--- .../DescribeProtocolNumberCommandTest.java | 124 +-- .../commands/DistanceSinceCCCommandTest.java | 230 ++--- .../obd/commands/DtcNumberCommandTest.java | 166 ++-- .../pires/obd/commands/ExtraMessagesTest.java | 146 ++-- .../obd/commands/FindFuelTypeCommandTest.java | 212 ++--- .../obd/commands/FuelLevelCommandTest.java | 212 ++--- .../obd/commands/FuelTrimCommandTest.java | 196 ++--- .../IntakeManifoldPressureCommandTest.java | 158 ++-- .../obd/commands/MassAirFlowCommandTest.java | 230 ++--- .../PendingTroubleCodesCommandTest.java | 600 ++++++------- .../PermanentTroubleCodesCommandTest.java | 600 ++++++------- .../pires/obd/commands/RPMCommandTest.java | 230 ++--- .../obd/commands/RuntimeCommandTest.java | 230 ++--- .../pires/obd/commands/SpeedCommandTest.java | 220 ++--- .../obd/commands/ThrottleCommandTest.java | 212 ++--- .../obd/commands/TroubleCodesCommandTest.java | 600 ++++++------- .../pires/obd/commands/VinCommandTest.java | 131 ++- .../obd/exceptions/BusInitExceptionTest.java | 85 +- .../obd/exceptions/NoDataExceptionTest.java | 65 +- .../NonNumericResponseExceptionTest.java | 55 +- .../obd/exceptions/StoppedExceptionTest.java | 65 +- .../exceptions/UnknownErrorExceptionTest.java | 87 +- .../UnsupportedCommandExceptionTest.java | 108 +-- .../utils/CommandAvailabilityHelperTest.java | 76 +- 98 files changed, 6558 insertions(+), 6098 deletions(-) diff --git a/pom.xml b/pom.xml index 629bff3b..5d51508b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,248 +1,249 @@ - - 4.0.0 + + 4.0.0 - - org.sonatype.oss - oss-parent - 9 - + + org.sonatype.oss + oss-parent + 9 + - com.github.pires - obd-java-api - 1.1-SNAPSHOT - jar + com.github.pires + obd-java-api + 1.1-SNAPSHOT + jar - OBD Java API - https://github.com/pires/obd-java-api - OBD Java API compatible with ELM327. + OBD Java API + https://github.com/pires/obd-java-api + OBD Java API compatible with ELM327. - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + - - scm:git:git@github.com:pires/obd-java-api.git - scm:git:git@github.com:pires/obd-java-api.git - git@github.com:pires/obd-java-api.git - HEAD - + + scm:git:git@github.com:pires/obd-java-api.git + scm:git:git@github.com:pires/obd-java-api.git + git@github.com:pires/obd-java-api.git + HEAD + - - - pjpires - Paulo Pires - pjpires@gmail.com - - developer - - - + + + pjpires + Paulo Pires + pjpires@gmail.com + + developer + + + - - - sonatype-nexus-staging - Sonatype OSS Staging - https://oss.sonatype.org/service/local/staging/deploy/maven2 - - - sonatype-nexus-snapshot - Sonatype OSS Snapshots - https://oss.sonatype.org/content/repositories/snapshots - - + + + sonatype-nexus-staging + Sonatype OSS Staging + https://oss.sonatype.org/service/local/staging/deploy/maven2 + + + sonatype-nexus-snapshot + Sonatype OSS Snapshots + https://oss.sonatype.org/content/repositories/snapshots + + - - UTF-8 - UTF-8 - 1.7 - 2.10.3 - + + UTF-8 + UTF-8 + 1.8 + 2.10.3 + - - - org.testng - testng - 6.8.13 - test - - - org.easymock - easymock - 3.3 - test - - - org.powermock - powermock-module-testng - 1.6.0 - test - - - org.powermock - powermock-api-easymock - 1.6.0 - test - - + + + org.testng + testng + 6.8.13 + test + + + org.easymock + easymock + 3.3 + test + + + org.powermock + powermock-module-testng + 1.6.0 + test + + + org.powermock + powermock-api-easymock + 1.6.0 + test + + - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.2 - - ${java.version} - ${java.version} - - - - org.apache.maven.plugins - maven-enforcer-plugin - 1.3.1 - - - enforce-java - - enforce - - - - - [3.0.4,) - - - [1.7,) - - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.18.1 - - -Xms128m -Xmx512m -XX:MaxPermSize=512m - 1 - methods - - **/SetupTestSuite.java - **/*Test.java - - - - - com.mycila - license-maven-plugin - 2.11 - false - - true -
${basedir}/COPYRIGHT
- true - - **/src/main/java/**/*.java - **/src/test/java/**/*.java - **/src/main/**/*.xml - **/src/test/**/*.xml - - true - UTF-8 -
- - - process-test-resources - - format - - - -
- - maven-release-plugin - 2.5.1 - - true - true - true - false - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven-javadoc-plugin.version} - - - - jar - aggregate - - - false - -Xdoclint:none - - - - -
-
+ + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-enforcer-plugin + 1.3.1 + + + enforce-java + + enforce + + + + + [3.0.4,) + + + [1.7,) + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + -Xms128m -Xmx512m -XX:MaxPermSize=512m + 1 + methods + + **/SetupTestSuite.java + **/*Test.java + + + + + com.mycila + license-maven-plugin + 2.11 + false + + true +
${basedir}/COPYRIGHT
+ true + + **/src/main/java/**/*.java + **/src/test/java/**/*.java + **/src/main/**/*.xml + **/src/test/**/*.xml + + true + UTF-8 +
+ + + process-test-resources + + format + + + +
+ + maven-release-plugin + 2.5.1 + + true + true + true + false + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + + jar + aggregate + + + false + -Xdoclint:none + + + + +
+
- - - release-sign-artifacts - - - performRelease - true - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven-javadoc-plugin.version} - - - - jar - aggregate - - - false - -Xdoclint:none - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - 7E4356B0 - - - - - - - - + + + release-sign-artifacts + + + performRelease + true + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + + jar + aggregate + + + false + -Xdoclint:none + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + 7E4356B0 + + + + + + + +
diff --git a/src/main/java/com/github/pires/obd/commands/ObdCommand.java b/src/main/java/com/github/pires/obd/commands/ObdCommand.java index 42892f06..e61e5e79 100644 --- a/src/main/java/com/github/pires/obd/commands/ObdCommand.java +++ b/src/main/java/com/github/pires/obd/commands/ObdCommand.java @@ -26,384 +26,422 @@ */ public abstract class ObdCommand { - /** - * Error classes to be tested in order - */ - private final Class[] ERROR_CLASSES = { - UnableToConnectException.class, - BusInitException.class, - MisunderstoodCommandException.class, - NoDataException.class, - StoppedException.class, - UnknownErrorException.class, - UnsupportedCommandException.class - }; - protected ArrayList buffer = null; - protected String cmd = null; - protected boolean useImperialUnits = false; - protected String rawData = null; - protected Long responseDelayInMs = null; - private long start; - private long end; - - /** - * Default ctor to use - * - * @param command the command to send - */ - public ObdCommand(String command) { - this.cmd = command; - this.buffer = new ArrayList<>(); - } - - /** - * Prevent empty instantiation - */ - private ObdCommand() { - } - - /** - * Copy ctor. - * - * @param other the ObdCommand to copy. - */ - public ObdCommand(ObdCommand other) { - this(other.cmd); - } - - /** - * Sends the OBD-II request and deals with the response. - *

- * This method CAN be overriden in fake commands. - * - * @param in a {@link java.io.InputStream} object. - * @param out a {@link java.io.OutputStream} object. - * @throws java.io.IOException if any. - * @throws java.lang.InterruptedException if any. - */ - public void run(InputStream in, OutputStream out) throws IOException, - InterruptedException { - synchronized (ObdCommand.class) {//Only one command can write and read a data in one time. - start = System.currentTimeMillis(); - sendCommand(out); - readResult(in); - end = System.currentTimeMillis(); - } - } - - /** - * Sends the OBD-II request. - *

- * This method may be overriden in subclasses, such as ObMultiCommand or - * TroubleCodesCommand. - * - * @param out The output stream. - * @throws java.io.IOException if any. - * @throws java.lang.InterruptedException if any. - */ - protected void sendCommand(OutputStream out) throws IOException, - InterruptedException { - // write to OutputStream (i.e.: a BluetoothSocket) with an added - // Carriage return - out.write((cmd + "\r").getBytes()); - out.flush(); - if (responseDelayInMs != null && responseDelayInMs > 0) { - Thread.sleep(responseDelayInMs); - } - } - - /** - * Resends this command. - * - * @param out a {@link java.io.OutputStream} object. - * @throws java.io.IOException if any. - * @throws java.lang.InterruptedException if any. - */ - protected void resendCommand(OutputStream out) throws IOException, - InterruptedException { - out.write("\r".getBytes()); - out.flush(); - if (responseDelayInMs != null && responseDelayInMs > 0) { - Thread.sleep(responseDelayInMs); - } - } - - /** - * Reads the OBD-II response. - *

- * This method may be overriden in subclasses, such as ObdMultiCommand. - * - * @param in a {@link java.io.InputStream} object. - * @throws java.io.IOException if any. - */ - protected void readResult(InputStream in) throws IOException { - readRawData(in); - checkForErrors(); - fillBuffer(); - performCalculations(); - } - - /** - * This method exists so that for each command, there must be a method that is - * called only once to perform calculations. - */ - protected abstract void performCalculations(); - - - private static Pattern WHITESPACE_PATTERN = Pattern.compile("\\s"); - private static Pattern BUSINIT_PATTERN = Pattern.compile("(BUS INIT)|(BUSINIT)|(\\.)"); - private static Pattern SEARCHING_PATTERN = Pattern.compile("SEARCHING"); - private static Pattern DIGITS_LETTERS_PATTERN = Pattern.compile("([0-9A-F])+"); - - protected String replaceAll(Pattern pattern, String input, String replacement) { - return pattern.matcher(input).replaceAll(replacement); - } - - protected String removeAll(Pattern pattern, String input) { - return pattern.matcher(input).replaceAll(""); - } - - /** - *

fillBuffer.

- */ - protected void fillBuffer() { - rawData = removeAll(WHITESPACE_PATTERN, rawData); //removes all [ \t\n\x0B\f\r] - rawData = removeAll(BUSINIT_PATTERN, rawData); - - if (!DIGITS_LETTERS_PATTERN.matcher(rawData).matches()) { - throw new NonNumericResponseException(rawData); - } - - // read string each two chars - buffer.clear(); - int begin = 0; - int end = 2; - while (end <= rawData.length()) { - buffer.add(Integer.decode("0x" + rawData.substring(begin, end))); - begin = end; - end += 2; - } - } - - /** - *

- * readRawData.

- * - * @param in a {@link java.io.InputStream} object. - * @throws java.io.IOException if any. - */ - protected void readRawData(InputStream in) throws IOException { - byte b = 0; - StringBuilder res = new StringBuilder(); - - // read until '>' arrives OR end of stream reached - char c; - // -1 if the end of the stream is reached - while (((b = (byte) in.read()) > -1)) { - c = (char) b; - if (c == '>') // read until '>' arrives - { - break; - } - res.append(c); - } - - /* - * Imagine the following response 41 0c 00 0d. - * - * ELM sends strings!! So, ELM puts spaces between each "byte". And pay - * attention to the fact that I've put the word byte in quotes, because 41 - * is actually TWO bytes (two chars) in the socket. So, we must do some more - * processing.. - */ - rawData = removeAll(SEARCHING_PATTERN, res.toString()); - - /* - * Data may have echo or informative text like "INIT BUS..." or similar. - * The response ends with two carriage return characters. So we need to take - * everything from the last carriage return before those two (trimmed above). - */ - //kills multiline.. rawData = rawData.substring(rawData.lastIndexOf(13) + 1); - rawData = removeAll(WHITESPACE_PATTERN, rawData);//removes all [ \t\n\x0B\f\r] - } - - void checkForErrors() { - for (Class errorClass : ERROR_CLASSES) { - ResponseException messageError; - - try { - messageError = errorClass.newInstance(); - messageError.setCommand(this.cmd); - } catch (InstantiationException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - - if (messageError.isError(rawData)) { - throw messageError; - } - } - } - - /** - *

getResult.

- * - * @return the raw command response in string representation. - */ - public String getResult() { - return rawData; - } - - /** - *

getFormattedResult.

- * - * @return a formatted command response in string representation. - */ - public abstract String getFormattedResult(); - - /** - *

getCalculatedResult.

- * - * @return the command response in string representation, without formatting. - */ - public abstract String getCalculatedResult(); - - /** - *

Getter for the field buffer.

- * - * @return a list of integers - */ - protected ArrayList getBuffer() { - return buffer; - } - - /** - *

useImperialUnits.

- * - * @return true if imperial units are used, or false otherwise - */ - public boolean useImperialUnits() { - return useImperialUnits; - } - - /** - * The unit of the result, as used in {@link #getFormattedResult()} - * - * @return a String representing a unit or "", never null - */ - public String getResultUnit() { - return "";//no unit by default - } - - /** - * Set to 'true' if you want to use imperial units, false otherwise. By - * default this value is set to 'false'. - * - * @param isImperial a boolean. - */ - public void useImperialUnits(boolean isImperial) { - this.useImperialUnits = isImperial; - } - - /** - *

getName.

- * - * @return the OBD command name. - */ - public abstract String getName(); - - /** - * Time the command waits before returning from #sendCommand() - * - * @return delay in ms (may be null) - */ - public Long getResponseTimeDelay() { - return responseDelayInMs; - } - - /** - * Time the command waits before returning from #sendCommand() - * - * @param responseDelayInMs a Long (can be null) - */ - public void setResponseTimeDelay(Long responseDelayInMs) { - this.responseDelayInMs = responseDelayInMs; - } - - //fixme resultunit - /** - *

Getter for the field start.

- * - * @return a long. - */ - public long getStart() { - return start; - } - - /** - *

Setter for the field start.

- * - * @param start a long. - */ - public void setStart(long start) { - this.start = start; - } - - /** - *

Getter for the field end.

- * - * @return a long. - */ - public long getEnd() { - return end; - } - - /** - *

Setter for the field end.

- * - * @param end a long. - */ - public void setEnd(long end) { - this.end = end; - } - - /** - *

getCommandPID.

- * - * @return a {@link java.lang.String} object. - * @since 1.0-RC12 - */ - public final String getCommandPID() { - return cmd.substring(3); - } - - /** - *

getCommandMode.

- * - * @return a {@link java.lang.String} object. - */ - public final String getCommandMode() { - if (cmd.length() >= 2) { - return cmd.substring(0, 2); - } else { - return cmd; - } - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ObdCommand that = (ObdCommand) o; - - return cmd != null ? cmd.equals(that.cmd) : that.cmd == null; - } - - @Override - public int hashCode() { - return cmd != null ? cmd.hashCode() : 0; - } + /** + * Error classes to be tested in order + */ + private static final Class[] ERROR_CLASSES = { UnableToConnectException.class, BusInitException.class, + MisunderstoodCommandException.class, NoDataException.class, StoppedException.class, + UnknownErrorException.class, UnsupportedCommandException.class }; + protected ArrayList buffer = null; + protected String cmd = null; + protected boolean useImperialUnits = false; + protected String rawData = null; + protected Long responseDelayInMs = null; + private long start; + private long end; + + /** + * Default ctor to use + * + * @param command + * the command to send + */ + public ObdCommand(String command) { + this.cmd = command; + this.buffer = new ArrayList<>(); + } + + /** + * Prevent empty instantiation + */ + @SuppressWarnings("unused") + private ObdCommand() { + } + + /** + * Copy ctor. + * + * @param other + * the ObdCommand to copy. + */ + public ObdCommand(ObdCommand other) { + this(other.cmd); + } + + /** + * Sends the OBD-II request and deals with the response. + *

+ * This method CAN be overriden in fake commands. + * + * @param in + * a {@link java.io.InputStream} object. + * @param out + * a {@link java.io.OutputStream} object. + * @throws java.io.IOException + * if any. + * @throws java.lang.InterruptedException + * if any. + */ + public void run(InputStream in, OutputStream out) throws IOException, InterruptedException { + synchronized (ObdCommand.class) {// Only one command can write and read a data in one time. + start = System.currentTimeMillis(); + sendCommand(out); + readResult(in); + end = System.currentTimeMillis(); + } + } + + /** + * Sends the OBD-II request. + *

+ * This method may be overriden in subclasses, such as ObMultiCommand or + * TroubleCodesCommand. + * + * @param out + * The output stream. + * @throws java.io.IOException + * if any. + * @throws java.lang.InterruptedException + * if any. + */ + protected void sendCommand(OutputStream out) throws IOException, InterruptedException { + // write to OutputStream (i.e.: a BluetoothSocket) with an added + // Carriage return + out.write((cmd + "\r").getBytes()); + out.flush(); + if (responseDelayInMs != null && responseDelayInMs > 0) { + Thread.sleep(responseDelayInMs); + } + } + + /** + * Resends this command. + * + * @param out + * a {@link java.io.OutputStream} object. + * @throws java.io.IOException + * if any. + * @throws java.lang.InterruptedException + * if any. + */ + protected void resendCommand(OutputStream out) throws IOException, InterruptedException { + out.write("\r".getBytes()); + out.flush(); + if (responseDelayInMs != null && responseDelayInMs > 0) { + Thread.sleep(responseDelayInMs); + } + } + + /** + * Reads the OBD-II response. + *

+ * This method may be overriden in subclasses, such as ObdMultiCommand. + * + * @param in + * a {@link java.io.InputStream} object. + * @throws java.io.IOException + * if any. + */ + protected void readResult(InputStream in) throws IOException { + readRawData(in); + checkForErrors(); + fillBuffer(); + performCalculations(); + } + + /** + * This method exists so that for each command, there must be a method that is + * called only once to perform calculations. + */ + protected abstract void performCalculations(); + + private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s"); + private static final Pattern BUSINIT_PATTERN = Pattern.compile("(BUS INIT)|(BUSINIT)|(\\.)"); + private static final Pattern SEARCHING_PATTERN = Pattern.compile("SEARCHING"); + private static final Pattern DIGITS_LETTERS_PATTERN = Pattern.compile("([0-9A-F])+"); + + protected String replaceAll(Pattern pattern, String input, String replacement) { + return pattern.matcher(input).replaceAll(replacement); + } + + protected String removeAll(Pattern pattern, String input) { + return pattern.matcher(input).replaceAll(""); + } + + /** + *

+ * fillBuffer. + *

+ */ + protected void fillBuffer() { + rawData = removeAll(WHITESPACE_PATTERN, rawData); // removes all [ \t\n\x0B\f\r] + rawData = removeAll(BUSINIT_PATTERN, rawData); + + if (!DIGITS_LETTERS_PATTERN.matcher(rawData).matches()) { + throw new NonNumericResponseException(rawData); + } + + // read string each two chars + buffer.clear(); + int begin = 0; + int end = 2; + while (end <= rawData.length()) { + buffer.add(Integer.decode("0x" + rawData.substring(begin, end))); + begin = end; + end += 2; + } + } + + /** + *

+ * readRawData. + *

+ * + * @param in + * a {@link java.io.InputStream} object. + * @throws java.io.IOException + * if any. + */ + protected void readRawData(InputStream in) throws IOException { + byte b = 0; + StringBuilder res = new StringBuilder(); + + // read until '>' arrives OR end of stream reached + char c; + // -1 if the end of the stream is reached + while (((b = (byte) in.read()) > -1)) { + c = (char) b; + if (c == '>') // read until '>' arrives + { + break; + } + res.append(c); + } + + /* + * Imagine the following response 41 0c 00 0d. + * + * ELM sends strings!! So, ELM puts spaces between each "byte". And pay + * attention to the fact that I've put the word byte in quotes, because 41 is + * actually TWO bytes (two chars) in the socket. So, we must do some more + * processing.. + */ + rawData = removeAll(SEARCHING_PATTERN, res.toString()); + + /* + * Data may have echo or informative text like "INIT BUS..." or similar. The + * response ends with two carriage return characters. So we need to take + * everything from the last carriage return before those two (trimmed above). + */ + // kills multiline.. rawData = rawData.substring(rawData.lastIndexOf(13) + 1); + rawData = removeAll(WHITESPACE_PATTERN, rawData);// removes all [ \t\n\x0B\f\r] + } + + void checkForErrors() { + for (Class errorClass : ERROR_CLASSES) { + ResponseException messageError; + + try { + messageError = (ResponseException) errorClass.newInstance(); + messageError.setCommand(this.cmd); + } catch (InstantiationException | IllegalAccessException e) { + throw new RuntimeException(e); + } + + if (messageError.isError(rawData)) { + throw messageError; + } + } + } + + /** + *

+ * getResult. + *

+ * + * @return the raw command response in string representation. + */ + public String getResult() { + return rawData; + } + + /** + *

+ * getFormattedResult. + *

+ * + * @return a formatted command response in string representation. + */ + public abstract String getFormattedResult(); + + /** + *

+ * getCalculatedResult. + *

+ * + * @return the command response in string representation, without formatting. + */ + public abstract String getCalculatedResult(); + + /** + *

+ * Getter for the field buffer. + *

+ * + * @return a list of integers + */ + protected ArrayList getBuffer() { + return buffer; + } + + /** + *

+ * useImperialUnits. + *

+ * + * @return true if imperial units are used, or false otherwise + */ + public boolean useImperialUnits() { + return useImperialUnits; + } + + /** + * The unit of the result, as used in {@link #getFormattedResult()} + * + * @return a String representing a unit or "", never null + */ + public String getResultUnit() { + return ""; // no unit by default + } + + /** + * Set to 'true' if you want to use imperial units, false otherwise. By default + * this value is set to 'false'. + * + * @param isImperial + * a boolean. + */ + public void useImperialUnits(boolean isImperial) { + this.useImperialUnits = isImperial; + } + + /** + *

+ * getName. + *

+ * + * @return the OBD command name. + */ + public abstract String getName(); + + /** + * Time the command waits before returning from #sendCommand() + * + * @return delay in ms (may be null) + */ + public Long getResponseTimeDelay() { + return responseDelayInMs; + } + + /** + * Time the command waits before returning from #sendCommand() + * + * @param responseDelayInMs + * a Long (can be null) + */ + public void setResponseTimeDelay(Long responseDelayInMs) { + this.responseDelayInMs = responseDelayInMs; + } + + // fixme resultunit + /** + *

+ * Getter for the field start. + *

+ * + * @return a long. + */ + public long getStart() { + return start; + } + + /** + *

+ * Setter for the field start. + *

+ * + * @param start + * a long. + */ + public void setStart(long start) { + this.start = start; + } + + /** + *

+ * Getter for the field end. + *

+ * + * @return a long. + */ + public long getEnd() { + return end; + } + + /** + *

+ * Setter for the field end. + *

+ * + * @param end + * a long. + */ + public void setEnd(long end) { + this.end = end; + } + + /** + *

+ * getCommandPID. + *

+ * + * @return a {@link java.lang.String} object. + * @since 1.0-RC12 + */ + public final String getCommandPID() { + return cmd.substring(3); + } + + /** + *

+ * getCommandMode. + *

+ * + * @return a {@link java.lang.String} object. + */ + public final String getCommandMode() { + if (cmd.length() >= 2) { + return cmd.substring(0, 2); + } else { + return cmd; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + ObdCommand that = (ObdCommand) o; + + return cmd != null ? cmd.equals(that.cmd) : that.cmd == null; + } + + @Override + public int hashCode() { + return cmd != null ? cmd.hashCode() : 0; + } } diff --git a/src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java b/src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java index 08b58788..2983b784 100644 --- a/src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java +++ b/src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java @@ -18,63 +18,71 @@ import java.util.ArrayList; /** - * Container for multiple {@link com.github.pires.obd.commands.ObdMultiCommand} instances. + * Container for multiple {@link com.github.pires.obd.commands.ObdMultiCommand} + * instances. * */ public class ObdMultiCommand { - private ArrayList commands; + private ArrayList commands; - /** - * Default ctor. - */ - public ObdMultiCommand() { - this.commands = new ArrayList<>(); - } + /** + * Default ctor. + */ + public ObdMultiCommand() { + this.commands = new ArrayList<>(); + } - /** - * Add ObdCommand to list of ObdCommands. - * - * @param command a {@link com.github.pires.obd.commands.ObdCommand} object. - */ - public void add(ObdCommand command) { - this.commands.add(command); - } + /** + * Add ObdCommand to list of ObdCommands. + * + * @param command + * a {@link com.github.pires.obd.commands.ObdCommand} object. + */ + public void add(ObdCommand command) { + this.commands.add(command); + } - /** - * Removes ObdCommand from the list of ObdCommands. - * - * @param command a {@link com.github.pires.obd.commands.ObdCommand} object. - */ - public void remove(ObdCommand command) { - this.commands.remove(command); - } + /** + * Removes ObdCommand from the list of ObdCommands. + * + * @param command + * a {@link com.github.pires.obd.commands.ObdCommand} object. + */ + public void remove(ObdCommand command) { + this.commands.remove(command); + } - /** - * Iterate all commands, send them and read response. - * - * @param in a {@link java.io.InputStream} object. - * @param out a {@link java.io.OutputStream} object. - * @throws java.io.IOException if any. - * @throws java.lang.InterruptedException if any. - */ - public void sendCommands(InputStream in, OutputStream out) - throws IOException, InterruptedException { - for (ObdCommand command : commands) - command.run(in, out); - } + /** + * Iterate all commands, send them and read response. + * + * @param in + * a {@link java.io.InputStream} object. + * @param out + * a {@link java.io.OutputStream} object. + * @throws java.io.IOException + * if any. + * @throws java.lang.InterruptedException + * if any. + */ + public void sendCommands(InputStream in, OutputStream out) throws IOException, InterruptedException { + for (ObdCommand command : commands) + command.run(in, out); + } - /** - *

getFormattedResult.

- * - * @return a {@link java.lang.String} object. - */ - public String getFormattedResult() { - StringBuilder res = new StringBuilder(); - for (ObdCommand command : commands) - res.append(command.getFormattedResult()).append(","); + /** + *

+ * getFormattedResult. + *

+ * + * @return a {@link java.lang.String} object. + */ + public String getFormattedResult() { + StringBuilder res = new StringBuilder(); + for (ObdCommand command : commands) + res.append(command.getFormattedResult()).append(","); - return res.toString(); - } + return res.toString(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/PercentageObdCommand.java b/src/main/java/com/github/pires/obd/commands/PercentageObdCommand.java index 52adbdc5..6c5239b5 100644 --- a/src/main/java/com/github/pires/obd/commands/PercentageObdCommand.java +++ b/src/main/java/com/github/pires/obd/commands/PercentageObdCommand.java @@ -18,58 +18,67 @@ */ public abstract class PercentageObdCommand extends ObdCommand { - protected float percentage = 0f; + protected float percentage = 0f; - /** - *

Constructor for PercentageObdCommand.

- * - * @param command a {@link java.lang.String} object. - */ - public PercentageObdCommand(String command) { - super(command); - } + /** + *

+ * Constructor for PercentageObdCommand. + *

+ * + * @param command + * a {@link java.lang.String} object. + */ + public PercentageObdCommand(String command) { + super(command); + } - /** - *

Constructor for PercentageObdCommand.

- * - * @param other a {@link com.github.pires.obd.commands.PercentageObdCommand} object. - */ - public PercentageObdCommand(PercentageObdCommand other) { - super(other); - } + /** + *

+ * Constructor for PercentageObdCommand. + *

+ * + * @param other + * a {@link com.github.pires.obd.commands.PercentageObdCommand} + * object. + */ + public PercentageObdCommand(PercentageObdCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - percentage = (buffer.get(2) * 100.0f) / 255.0f; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + percentage = (buffer.get(2) * 100.0f) / 255.0f; + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return String.format("%.1f%s", percentage, getResultUnit()); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return String.format("%.1f%s", percentage, getResultUnit()); + } - /** - *

Getter for the field percentage.

- * - * @return a float. - */ - public float getPercentage() { - return percentage; - } + /** + *

+ * Getter for the field percentage. + *

+ * + * @return a float. + */ + public float getPercentage() { + return percentage; + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return "%"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return "%"; + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(percentage); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(percentage); + } } diff --git a/src/main/java/com/github/pires/obd/commands/PersistentCommand.java b/src/main/java/com/github/pires/obd/commands/PersistentCommand.java index 8a66d0f9..47fb14d9 100644 --- a/src/main/java/com/github/pires/obd/commands/PersistentCommand.java +++ b/src/main/java/com/github/pires/obd/commands/PersistentCommand.java @@ -25,65 +25,76 @@ */ public abstract class PersistentCommand extends ObdCommand { - private static Map knownValues = new HashMap<>(); - private static Map> knownBuffers = new HashMap<>(); + private static Map knownValues = new HashMap<>(); + private static Map> knownBuffers = new HashMap<>(); - /** - *

Constructor for PersistentCommand.

- * - * @param command a {@link java.lang.String} object. - */ - public PersistentCommand(String command) { - super(command); - } + /** + *

+ * Constructor for PersistentCommand. + *

+ * + * @param command + * a {@link java.lang.String} object. + */ + public PersistentCommand(String command) { + super(command); + } - /** - *

Constructor for PersistentCommand.

- * - * @param other a {@link com.github.pires.obd.commands.ObdCommand} object. - */ - public PersistentCommand(ObdCommand other) { - this(other.cmd); - } + /** + *

+ * Constructor for PersistentCommand. + *

+ * + * @param other + * a {@link com.github.pires.obd.commands.ObdCommand} object. + */ + public PersistentCommand(ObdCommand other) { + this(other.cmd); + } - /** - *

reset.

- */ - public static void reset() { - knownValues = new HashMap<>(); - knownBuffers = new HashMap<>(); - } + /** + *

+ * reset. + *

+ */ + public static void reset() { + knownValues = new HashMap<>(); + knownBuffers = new HashMap<>(); + } - /** - *

knows.

- * - * @param cmd a {@link java.lang.Class} object. - * @return a boolean. - */ - public static boolean knows(Class cmd) { - String key = cmd.getSimpleName(); - return knownValues.containsKey(key); - } + /** + *

+ * knows. + *

+ * + * @param cmd + * a {@link java.lang.Class} object. + * @return a boolean. + */ + public static boolean knows(Class cmd) { + String key = cmd.getSimpleName(); + return knownValues.containsKey(key); + } - /** {@inheritDoc} */ - @Override - protected void readResult(InputStream in) throws IOException { - super.readResult(in); - String key = getClass().getSimpleName(); - knownValues.put(key, rawData); - knownBuffers.put(key, new ArrayList<>(buffer)); - } + /** {@inheritDoc} */ + @Override + protected void readResult(InputStream in) throws IOException { + super.readResult(in); + String key = getClass().getSimpleName(); + knownValues.put(key, rawData); + knownBuffers.put(key, new ArrayList<>(buffer)); + } - /** {@inheritDoc} */ - @Override - public void run(InputStream in, OutputStream out) throws IOException, InterruptedException { - String key = getClass().getSimpleName(); - if (knownValues.containsKey(key)) { - rawData = knownValues.get(key); - buffer = knownBuffers.get(key); - performCalculations(); - } else { - super.run(in, out); - } - } + /** {@inheritDoc} */ + @Override + public void run(InputStream in, OutputStream out) throws IOException, InterruptedException { + String key = getClass().getSimpleName(); + if (knownValues.containsKey(key)) { + rawData = knownValues.get(key); + buffer = knownBuffers.get(key); + performCalculations(); + } else { + super.run(in, out); + } + } } diff --git a/src/main/java/com/github/pires/obd/commands/SpeedCommand.java b/src/main/java/com/github/pires/obd/commands/SpeedCommand.java index 77a7a85c..26774cd4 100644 --- a/src/main/java/com/github/pires/obd/commands/SpeedCommand.java +++ b/src/main/java/com/github/pires/obd/commands/SpeedCommand.java @@ -20,84 +20,91 @@ */ public class SpeedCommand extends ObdCommand implements SystemOfUnits { - private int metricSpeed = 0; + private int metricSpeed = 0; - /** - * Default ctor. - */ - public SpeedCommand() { - super("01 0D"); - } + /** + * Default ctor. + */ + public SpeedCommand() { + super("01 0D"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.SpeedCommand} object. - */ - public SpeedCommand(SpeedCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.SpeedCommand} object. + */ + public SpeedCommand(SpeedCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // Ignore first two bytes [hh hh] of the response. - metricSpeed = buffer.get(2); - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // Ignore first two bytes [hh hh] of the response. + metricSpeed = buffer.get(2); + } - /** - *

Getter for the field metricSpeed.

- * - * @return the speed in metric units. - */ - public int getMetricSpeed() { - return metricSpeed; - } + /** + *

+ * Getter for the field metricSpeed. + *

+ * + * @return the speed in metric units. + */ + public int getMetricSpeed() { + return metricSpeed; + } - /** - *

getImperialSpeed.

- * - * @return the speed in imperial units. - */ - public float getImperialSpeed() { - return getImperialUnit(); - } + /** + *

+ * getImperialSpeed. + *

+ * + * @return the speed in imperial units. + */ + public float getImperialSpeed() { + return getImperialUnit(); + } - /** - * Convert from km/h to mph - * - * @return a float. - */ - public float getImperialUnit() { - return metricSpeed * 0.621371192F; - } + /** + * Convert from km/h to mph + * + * @return a float. + */ + public float getImperialUnit() { + return metricSpeed * 0.621371192F; + } - /** - *

getFormattedResult.

- * - * @return a {@link java.lang.String} object. - */ - public String getFormattedResult() { - return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) - : String.format("%d%s", getMetricSpeed(), getResultUnit()); - } + /** + *

+ * getFormattedResult. + *

+ * + * @return a {@link java.lang.String} object. + */ + public String getFormattedResult() { + return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) + : String.format("%d%s", getMetricSpeed(), getResultUnit()); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(getMetricSpeed()); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(getMetricSpeed()); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return useImperialUnits ? "mph" : "km/h"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return useImperialUnits ? "mph" : "km/h"; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.SPEED.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.SPEED.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/SystemOfUnits.java b/src/main/java/com/github/pires/obd/commands/SystemOfUnits.java index 53193366..cb7db6cc 100644 --- a/src/main/java/com/github/pires/obd/commands/SystemOfUnits.java +++ b/src/main/java/com/github/pires/obd/commands/SystemOfUnits.java @@ -19,11 +19,13 @@ */ public interface SystemOfUnits { - /** - *

getImperialUnit.

- * - * @return a float. - */ - float getImperialUnit(); + /** + *

+ * getImperialUnit. + *

+ * + * @return a float. + */ + float getImperialUnit(); } diff --git a/src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java b/src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java index 74c5ff38..7e460b87 100644 --- a/src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java @@ -17,80 +17,86 @@ import com.github.pires.obd.enums.AvailableCommandNames; /** - *

DistanceMILOnCommand class.

+ *

+ * DistanceMILOnCommand class. + *

* */ -public class DistanceMILOnCommand extends ObdCommand - implements SystemOfUnits { +public class DistanceMILOnCommand extends ObdCommand implements SystemOfUnits { - private int km = 0; + private int km = 0; - /** - * Default ctor. - */ - public DistanceMILOnCommand() { - super("01 21"); - } + /** + * Default ctor. + */ + public DistanceMILOnCommand() { + super("01 21"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.DistanceMILOnCommand} object. - */ - public DistanceMILOnCommand( - DistanceMILOnCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.control.DistanceMILOnCommand} + * object. + */ + public DistanceMILOnCommand(DistanceMILOnCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [01 31] of the response - km = buffer.get(2) * 256 + buffer.get(3); - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [01 31] of the response + km = buffer.get(2) * 256 + buffer.get(3); + } - /** - *

getFormattedResult.

- * - * @return a {@link java.lang.String} object. - */ - public String getFormattedResult() { - return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) - : String.format("%d%s", km, getResultUnit()); - } + /** + *

+ * getFormattedResult. + *

+ * + * @return a {@link java.lang.String} object. + */ + public String getFormattedResult() { + return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) + : String.format("%d%s", km, getResultUnit()); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(km); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(km); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return useImperialUnits ? "m" : "km"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return useImperialUnits ? "m" : "km"; + } - /** {@inheritDoc} */ - @Override - public float getImperialUnit() { - return km * 0.621371192F; - } + /** {@inheritDoc} */ + @Override + public float getImperialUnit() { + return km * 0.621371192F; + } - /** - *

Getter for the field km.

- * - * @return a int. - */ - public int getKm() { - return km; - } + /** + *

+ * Getter for the field km. + *

+ * + * @return a int. + */ + public int getKm() { + return km; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.DISTANCE_TRAVELED_MIL_ON - .getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.DISTANCE_TRAVELED_MIL_ON.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/DistanceSinceCCCommand.java b/src/main/java/com/github/pires/obd/commands/control/DistanceSinceCCCommand.java index f3d10678..9a36d2a5 100644 --- a/src/main/java/com/github/pires/obd/commands/control/DistanceSinceCCCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/DistanceSinceCCCommand.java @@ -20,86 +20,93 @@ * Distance traveled since codes cleared-up. * */ -public class DistanceSinceCCCommand extends ObdCommand - implements SystemOfUnits { +public class DistanceSinceCCCommand extends ObdCommand implements SystemOfUnits { - private int km = 0; + private int km = 0; - /** - * Default ctor. - */ - public DistanceSinceCCCommand() { - super("01 31"); - } + /** + * Default ctor. + */ + public DistanceSinceCCCommand() { + super("01 31"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.DistanceSinceCCCommand} object. - */ - public DistanceSinceCCCommand( - DistanceSinceCCCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.control.DistanceSinceCCCommand} + * object. + */ + public DistanceSinceCCCommand(DistanceSinceCCCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [01 31] of the response - km = buffer.get(2) * 256 + buffer.get(3); - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [01 31] of the response + km = buffer.get(2) * 256 + buffer.get(3); + } - /** - *

getFormattedResult.

- * - * @return a {@link java.lang.String} object. - */ - public String getFormattedResult() { - return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) - : String.format("%d%s", km, getResultUnit()); - } + /** + *

+ * getFormattedResult. + *

+ * + * @return a {@link java.lang.String} object. + */ + public String getFormattedResult() { + return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) + : String.format("%d%s", km, getResultUnit()); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(km); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(km); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return useImperialUnits ? "m" : "km"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return useImperialUnits ? "m" : "km"; + } - /** {@inheritDoc} */ - @Override - public float getImperialUnit() { - return km * 0.621371192F; - } + /** {@inheritDoc} */ + @Override + public float getImperialUnit() { + return km * 0.621371192F; + } - /** - *

Getter for the field km.

- * - * @return a int. - */ - public int getKm() { - return km; - } + /** + *

+ * Getter for the field km. + *

+ * + * @return a int. + */ + public int getKm() { + return km; + } - /** - *

Setter for the field km.

- * - * @param km a int. - */ - public void setKm(int km) { - this.km = km; - } + /** + *

+ * Setter for the field km. + *

+ * + * @param km + * a int. + */ + public void setKm(int km) { + this.km = km; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.DISTANCE_TRAVELED_AFTER_CODES_CLEARED - .getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.DISTANCE_TRAVELED_AFTER_CODES_CLEARED.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/DtcNumberCommand.java b/src/main/java/com/github/pires/obd/commands/control/DtcNumberCommand.java index 18ec1a0f..abc228f5 100644 --- a/src/main/java/com/github/pires/obd/commands/control/DtcNumberCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/DtcNumberCommand.java @@ -26,72 +26,80 @@ */ public class DtcNumberCommand extends ObdCommand { - private int codeCount = 0; - private boolean milOn = false; + private int codeCount = 0; + private boolean milOn = false; - /** - * Default ctor. - */ - public DtcNumberCommand() { - super("01 01"); - } + /** + * Default ctor. + */ + public DtcNumberCommand() { + super("01 01"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.DtcNumberCommand} object. - */ - public DtcNumberCommand(DtcNumberCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.control.DtcNumberCommand} + * object. + */ + public DtcNumberCommand(DtcNumberCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - final int mil = buffer.get(2); - milOn = (mil & 0x80) == 128; - codeCount = mil & 0x7F; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + final int mil = buffer.get(2); + milOn = (mil & 0x80) == 128; + codeCount = mil & 0x7F; + } - /** - *

getFormattedResult.

- * - * @return a {@link java.lang.String} object. - */ - public String getFormattedResult() { - final String res = milOn ? "MIL is ON" : "MIL is OFF"; - return res + codeCount + " codes"; - } + /** + *

+ * getFormattedResult. + *

+ * + * @return a {@link java.lang.String} object. + */ + public String getFormattedResult() { + final String res = milOn ? "MIL is ON" : "MIL is OFF"; + return res + codeCount + " codes"; + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(codeCount); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(codeCount); + } - /** - *

getTotalAvailableCodes.

- * - * @return the number of trouble codes currently flaggd in the ECU. - */ - public int getTotalAvailableCodes() { - return codeCount; - } + /** + *

+ * getTotalAvailableCodes. + *

+ * + * @return the number of trouble codes currently flaggd in the ECU. + */ + public int getTotalAvailableCodes() { + return codeCount; + } - /** - *

Getter for the field milOn.

- * - * @return the state of the check engine light state. - */ - public boolean getMilOn() { - return milOn; - } + /** + *

+ * Getter for the field milOn. + *

+ * + * @return the state of the check engine light state. + */ + public boolean getMilOn() { + return milOn; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.DTC_NUMBER.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.DTC_NUMBER.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/EquivalentRatioCommand.java b/src/main/java/com/github/pires/obd/commands/control/EquivalentRatioCommand.java index d4303034..168963b6 100644 --- a/src/main/java/com/github/pires/obd/commands/control/EquivalentRatioCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/EquivalentRatioCommand.java @@ -29,46 +29,49 @@ */ public class EquivalentRatioCommand extends PercentageObdCommand { + /** + * Default ctor. + */ + public EquivalentRatioCommand() { + super("01 44"); + } - /** - * Default ctor. - */ - public EquivalentRatioCommand() { - super("01 44"); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.control.EquivalentRatioCommand} + * object. + */ + public EquivalentRatioCommand(EquivalentRatioCommand other) { + super(other); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.EquivalentRatioCommand} object. - */ - public EquivalentRatioCommand(EquivalentRatioCommand other) { - super(other); - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + int a = buffer.get(2); + int b = buffer.get(3); + percentage = (a * 256 + b) / 32768; + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - int a = buffer.get(2); - int b = buffer.get(3); - percentage = (a * 256 + b) / 32768; - } + /** + *

+ * getRatio. + *

+ * + * @return a double. + */ + public double getRatio() { + return (double) percentage; + } - - /** - *

getRatio.

- * - * @return a double. - */ - public double getRatio() { - return (double) percentage; - } - - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.EQUIV_RATIO.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.EQUIV_RATIO.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java b/src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java index 12c12bda..59aeb619 100644 --- a/src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java @@ -17,52 +17,51 @@ public class IgnitionMonitorCommand extends ObdCommand { - private boolean ignitionOn = false; + private boolean ignitionOn = false; - /** - * Default ctor. - */ - public IgnitionMonitorCommand() { - super("AT IGN"); - } + /** + * Default ctor. + */ + public IgnitionMonitorCommand() { + super("AT IGN"); + } - /** - * Copy ctor. - * - * @param other a {@link IgnitionMonitorCommand} object. - */ - public IgnitionMonitorCommand(IgnitionMonitorCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link IgnitionMonitorCommand} object. + */ + public IgnitionMonitorCommand(IgnitionMonitorCommand other) { + super(other); + } - @Override - protected void performCalculations() { - final String result = getResult(); - ignitionOn = result.equalsIgnoreCase("ON"); - } + @Override + protected void performCalculations() { + final String result = getResult(); + ignitionOn = result.equalsIgnoreCase("ON"); + } - @Override - public String getFormattedResult() { - return getResult(); - } + @Override + public String getFormattedResult() { + return getResult(); + } - @Override - public String getName() { - return AvailableCommandNames.IGNITION_MONITOR.getValue(); - } + @Override + public String getName() { + return AvailableCommandNames.IGNITION_MONITOR.getValue(); + } - @Override - public String getCalculatedResult() { - return getResult(); - } + @Override + public String getCalculatedResult() { + return getResult(); + } - @Override - protected void fillBuffer() { - } + @Override + protected void fillBuffer() { + } - public boolean isIgnitionOn() { - return ignitionOn; - } + public boolean isIgnitionOn() { + return ignitionOn; + } } - - diff --git a/src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java b/src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java index c33bb732..e904586e 100644 --- a/src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java @@ -16,70 +16,77 @@ import com.github.pires.obd.enums.AvailableCommandNames; /** - *

ModuleVoltageCommand class.

+ *

+ * ModuleVoltageCommand class. + *

* */ public class ModuleVoltageCommand extends ObdCommand { - // Equivalent ratio (V) - private double voltage = 0.00; + // Equivalent ratio (V) + private double voltage = 0.00; - /** - * Default ctor. - */ - public ModuleVoltageCommand() { - super("01 42"); - } + /** + * Default ctor. + */ + public ModuleVoltageCommand() { + super("01 42"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.ModuleVoltageCommand} object. - */ - public ModuleVoltageCommand(ModuleVoltageCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.control.ModuleVoltageCommand} + * object. + */ + public ModuleVoltageCommand(ModuleVoltageCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - int a = buffer.get(2); - int b = buffer.get(3); - voltage = (a * 256 + b) / 1000; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + int a = buffer.get(2); + int b = buffer.get(3); + voltage = (a * 256 + b) / 1000; + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return String.format("%.1f%s", voltage, getResultUnit()); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return String.format("%.1f%s", voltage, getResultUnit()); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return "V"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return "V"; + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(voltage); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(voltage); + } - /** - *

Getter for the field voltage.

- * - * @return a double. - */ - public double getVoltage() { - return voltage; - } + /** + *

+ * Getter for the field voltage. + *

+ * + * @return a double. + */ + public double getVoltage() { + return voltage; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.CONTROL_MODULE_VOLTAGE.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.CONTROL_MODULE_VOLTAGE.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/PendingTroubleCodesCommand.java b/src/main/java/com/github/pires/obd/commands/control/PendingTroubleCodesCommand.java index c8178b3e..52c30e83 100644 --- a/src/main/java/com/github/pires/obd/commands/control/PendingTroubleCodesCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/PendingTroubleCodesCommand.java @@ -19,139 +19,145 @@ import java.io.InputStream; /** - * It is not needed no know how many DTC are stored. - * Because when no DTC are stored response will be NO DATA - * And where are more messages it will be stored in frames that have 7 bytes. - * In one frame are stored 3 DTC. - * If we find out DTC P0000 that mean no message are we can end. + * It is not needed no know how many DTC are stored. Because when no DTC are + * stored response will be NO DATA And where are more messages it will be stored + * in frames that have 7 bytes. In one frame are stored 3 DTC. If we find out + * DTC P0000 that mean no message are we can end. * */ public class PendingTroubleCodesCommand extends ObdCommand { - /** Constant dtcLetters={'P', 'C', 'B', 'U'} */ - protected final static char[] dtcLetters = {'P', 'C', 'B', 'U'}; - /** Constant hexArray="0123456789ABCDEF".toCharArray() */ - protected final static char[] hexArray = "0123456789ABCDEF".toCharArray(); - - protected StringBuilder codes = null; - - /** - *

Constructor for PendingTroubleCodesCommand.

- */ - public PendingTroubleCodesCommand() { - super("07"); - codes = new StringBuilder(); - } - - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.PendingTroubleCodesCommand} object. - */ - public PendingTroubleCodesCommand(PendingTroubleCodesCommand other) { - super(other); - codes = new StringBuilder(); - } - - /** {@inheritDoc} */ - @Override - protected void fillBuffer() { - } - - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - final String result = getResult(); - String workingData; - int startIndex = 0;//Header size. - - String canOneFrame = result.replaceAll("[\r\n]", ""); - int canOneFrameLength = canOneFrame.length(); - if (canOneFrameLength <= 16 && canOneFrameLength % 4 == 0) {//CAN(ISO-15765) protocol one frame. - workingData = canOneFrame;//47yy{codes} - startIndex = 4;//Header is 47yy, yy showing the number of data items. - } else if (result.contains(":")) {//CAN(ISO-15765) protocol two and more frames. - workingData = result.replaceAll("[\r\n].:", "");//xxx47yy{codes} - startIndex = 7;//Header is xxx47yy, xxx is bytes of information to follow, yy showing the number of data items. - } else {//ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. - workingData = result.replaceAll("^47|[\r\n]47|[\r\n]", ""); - } - for (int begin = startIndex; begin < workingData.length(); begin += 4) { - String dtc = ""; - byte b1 = hexStringToByteArray(workingData.charAt(begin)); - int ch1 = ((b1 & 0xC0) >> 6); - int ch2 = ((b1 & 0x30) >> 4); - dtc += dtcLetters[ch1]; - dtc += hexArray[ch2]; - dtc += workingData.substring(begin+1, begin + 4); - if (dtc.equals("P0000")) { - return; - } - codes.append(dtc); - codes.append('\n'); - } - } - - private byte hexStringToByteArray(char s) { - return (byte) ((Character.digit(s, 16) << 4)); - } - - /** - *

formatResult.

- * - * @return the formatted result of this command in string representation. - * @deprecated use #getCalculatedResult instead - */ - public String formatResult() { - return codes.toString(); - } - - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(codes); - } - - - /** {@inheritDoc} */ - @Override - protected void readRawData(InputStream in) throws IOException { - byte b; - StringBuilder res = new StringBuilder(); - - // read until '>' arrives OR end of stream reached (and skip ' ') - char c; - while (true) { - b = (byte) in.read(); - if (b == -1) // -1 if the end of the stream is reached - { - break; - } - c = (char) b; - if (c == '>') // read until '>' arrives - { - break; - } - if (c != ' ') // skip ' ' - { - res.append(c); - } - } - - rawData = res.toString().trim(); - - } - - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return codes.toString(); - } - - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.PENDING_TROUBLE_CODES.getValue(); - } + /** Constant dtcLetters={'P', 'C', 'B', 'U'} */ + protected final static char[] dtcLetters = { 'P', 'C', 'B', 'U' }; + /** Constant hexArray="0123456789ABCDEF".toCharArray() */ + protected final static char[] hexArray = "0123456789ABCDEF".toCharArray(); + + protected StringBuilder codes = null; + + /** + *

+ * Constructor for PendingTroubleCodesCommand. + *

+ */ + public PendingTroubleCodesCommand() { + super("07"); + codes = new StringBuilder(); + } + + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.control.PendingTroubleCodesCommand} + * object. + */ + public PendingTroubleCodesCommand(PendingTroubleCodesCommand other) { + super(other); + codes = new StringBuilder(); + } + + /** {@inheritDoc} */ + @Override + protected void fillBuffer() { + } + + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + final String result = getResult(); + String workingData; + int startIndex = 0;// Header size. + + String canOneFrame = result.replaceAll("[\r\n]", ""); + int canOneFrameLength = canOneFrame.length(); + if (canOneFrameLength <= 16 && canOneFrameLength % 4 == 0) {// CAN(ISO-15765) protocol one frame. + workingData = canOneFrame;// 47yy{codes} + startIndex = 4;// Header is 47yy, yy showing the number of data items. + } else if (result.contains(":")) {// CAN(ISO-15765) protocol two and more frames. + workingData = result.replaceAll("[\r\n].:", "");// xxx47yy{codes} + startIndex = 7;// Header is xxx47yy, xxx is bytes of information to follow, yy showing the + // number of data items. + } else {// ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. + workingData = result.replaceAll("^47|[\r\n]47|[\r\n]", ""); + } + for (int begin = startIndex; begin < workingData.length(); begin += 4) { + String dtc = ""; + byte b1 = hexStringToByteArray(workingData.charAt(begin)); + int ch1 = ((b1 & 0xC0) >> 6); + int ch2 = ((b1 & 0x30) >> 4); + dtc += dtcLetters[ch1]; + dtc += hexArray[ch2]; + dtc += workingData.substring(begin + 1, begin + 4); + if (dtc.equals("P0000")) { + return; + } + codes.append(dtc); + codes.append('\n'); + } + } + + private byte hexStringToByteArray(char s) { + return (byte) ((Character.digit(s, 16) << 4)); + } + + /** + *

+ * formatResult. + *

+ * + * @return the formatted result of this command in string representation. + * @deprecated use #getCalculatedResult instead + */ + public String formatResult() { + return codes.toString(); + } + + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(codes); + } + + /** {@inheritDoc} */ + @Override + protected void readRawData(InputStream in) throws IOException { + byte b; + StringBuilder res = new StringBuilder(); + + // read until '>' arrives OR end of stream reached (and skip ' ') + char c; + while (true) { + b = (byte) in.read(); + if (b == -1) // -1 if the end of the stream is reached + { + break; + } + c = (char) b; + if (c == '>') // read until '>' arrives + { + break; + } + if (c != ' ') // skip ' ' + { + res.append(c); + } + } + + rawData = res.toString().trim(); + + } + + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return codes.toString(); + } + + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.PENDING_TROUBLE_CODES.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/PermanentTroubleCodesCommand.java b/src/main/java/com/github/pires/obd/commands/control/PermanentTroubleCodesCommand.java index 427e042c..f425b87c 100644 --- a/src/main/java/com/github/pires/obd/commands/control/PermanentTroubleCodesCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/PermanentTroubleCodesCommand.java @@ -19,139 +19,145 @@ import java.io.InputStream; /** - * It is not needed no know how many DTC are stored. - * Because when no DTC are stored response will be NO DATA - * And where are more messages it will be stored in frames that have 7 bytes. - * In one frame are stored 3 DTC. - * If we find out DTC P0000 that mean no message are we can end. + * It is not needed no know how many DTC are stored. Because when no DTC are + * stored response will be NO DATA And where are more messages it will be stored + * in frames that have 7 bytes. In one frame are stored 3 DTC. If we find out + * DTC P0000 that mean no message are we can end. * */ public class PermanentTroubleCodesCommand extends ObdCommand { - /** Constant dtcLetters={'P', 'C', 'B', 'U'} */ - protected final static char[] dtcLetters = {'P', 'C', 'B', 'U'}; - /** Constant hexArray="0123456789ABCDEF".toCharArray() */ - protected final static char[] hexArray = "0123456789ABCDEF".toCharArray(); - - protected StringBuilder codes = null; - - /** - *

Constructor for PermanentTroubleCodesCommand.

- */ - public PermanentTroubleCodesCommand() { - super("0A"); - codes = new StringBuilder(); - } - - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.PermanentTroubleCodesCommand} object. - */ - public PermanentTroubleCodesCommand(PermanentTroubleCodesCommand other) { - super(other); - codes = new StringBuilder(); - } - - /** {@inheritDoc} */ - @Override - protected void fillBuffer() { - } - - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - final String result = getResult(); - String workingData; - int startIndex = 0;//Header size. - - String canOneFrame = result.replaceAll("[\r\n]", ""); - int canOneFrameLength = canOneFrame.length(); - if (canOneFrameLength <= 16 && canOneFrameLength % 4 == 0) {//CAN(ISO-15765) protocol one frame. - workingData = canOneFrame;//4Ayy{codes} - startIndex = 4;//Header is 4Ayy, yy showing the number of data items. - } else if (result.contains(":")) {//CAN(ISO-15765) protocol two and more frames. - workingData = result.replaceAll("[\r\n].:", "");//xxx4Ayy{codes} - startIndex = 7;//Header is xxx4Ayy, xxx is bytes of information to follow, yy showing the number of data items. - } else {//ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. - workingData = result.replaceAll("^4A|[\r\n]4A|[\r\n]", ""); - } - for (int begin = startIndex; begin < workingData.length(); begin += 4) { - String dtc = ""; - byte b1 = hexStringToByteArray(workingData.charAt(begin)); - int ch1 = ((b1 & 0xC0) >> 6); - int ch2 = ((b1 & 0x30) >> 4); - dtc += dtcLetters[ch1]; - dtc += hexArray[ch2]; - dtc += workingData.substring(begin+1, begin + 4); - if (dtc.equals("P0000")) { - return; - } - codes.append(dtc); - codes.append('\n'); - } - } - - private byte hexStringToByteArray(char s) { - return (byte) ((Character.digit(s, 16) << 4)); - } - - /** - *

formatResult.

- * - * @return the formatted result of this command in string representation. - * @deprecated use #getCalculatedResult instead - */ - public String formatResult() { - return codes.toString(); - } - - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(codes); - } - - - /** {@inheritDoc} */ - @Override - protected void readRawData(InputStream in) throws IOException { - byte b; - StringBuilder res = new StringBuilder(); - - // read until '>' arrives OR end of stream reached (and skip ' ') - char c; - while (true) { - b = (byte) in.read(); - if (b == -1) // -1 if the end of the stream is reached - { - break; - } - c = (char) b; - if (c == '>') // read until '>' arrives - { - break; - } - if (c != ' ') // skip ' ' - { - res.append(c); - } - } - - rawData = res.toString().trim(); - - } - - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return codes.toString(); - } - - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.PERMANENT_TROUBLE_CODES.getValue(); - } + /** Constant dtcLetters={'P', 'C', 'B', 'U'} */ + protected final static char[] dtcLetters = { 'P', 'C', 'B', 'U' }; + /** Constant hexArray="0123456789ABCDEF".toCharArray() */ + protected final static char[] hexArray = "0123456789ABCDEF".toCharArray(); + + protected StringBuilder codes = null; + + /** + *

+ * Constructor for PermanentTroubleCodesCommand. + *

+ */ + public PermanentTroubleCodesCommand() { + super("0A"); + codes = new StringBuilder(); + } + + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.control.PermanentTroubleCodesCommand} + * object. + */ + public PermanentTroubleCodesCommand(PermanentTroubleCodesCommand other) { + super(other); + codes = new StringBuilder(); + } + + /** {@inheritDoc} */ + @Override + protected void fillBuffer() { + } + + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + final String result = getResult(); + String workingData; + int startIndex = 0;// Header size. + + String canOneFrame = result.replaceAll("[\r\n]", ""); + int canOneFrameLength = canOneFrame.length(); + if (canOneFrameLength <= 16 && canOneFrameLength % 4 == 0) {// CAN(ISO-15765) protocol one frame. + workingData = canOneFrame;// 4Ayy{codes} + startIndex = 4;// Header is 4Ayy, yy showing the number of data items. + } else if (result.contains(":")) {// CAN(ISO-15765) protocol two and more frames. + workingData = result.replaceAll("[\r\n].:", "");// xxx4Ayy{codes} + startIndex = 7;// Header is xxx4Ayy, xxx is bytes of information to follow, yy showing the + // number of data items. + } else {// ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. + workingData = result.replaceAll("^4A|[\r\n]4A|[\r\n]", ""); + } + for (int begin = startIndex; begin < workingData.length(); begin += 4) { + String dtc = ""; + byte b1 = hexStringToByteArray(workingData.charAt(begin)); + int ch1 = ((b1 & 0xC0) >> 6); + int ch2 = ((b1 & 0x30) >> 4); + dtc += dtcLetters[ch1]; + dtc += hexArray[ch2]; + dtc += workingData.substring(begin + 1, begin + 4); + if (dtc.equals("P0000")) { + return; + } + codes.append(dtc); + codes.append('\n'); + } + } + + private byte hexStringToByteArray(char s) { + return (byte) ((Character.digit(s, 16) << 4)); + } + + /** + *

+ * formatResult. + *

+ * + * @return the formatted result of this command in string representation. + * @deprecated use #getCalculatedResult instead + */ + public String formatResult() { + return codes.toString(); + } + + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(codes); + } + + /** {@inheritDoc} */ + @Override + protected void readRawData(InputStream in) throws IOException { + byte b; + StringBuilder res = new StringBuilder(); + + // read until '>' arrives OR end of stream reached (and skip ' ') + char c; + while (true) { + b = (byte) in.read(); + if (b == -1) // -1 if the end of the stream is reached + { + break; + } + c = (char) b; + if (c == '>') // read until '>' arrives + { + break; + } + if (c != ' ') // skip ' ' + { + res.append(c); + } + } + + rawData = res.toString().trim(); + + } + + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return codes.toString(); + } + + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.PERMANENT_TROUBLE_CODES.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/TimingAdvanceCommand.java b/src/main/java/com/github/pires/obd/commands/control/TimingAdvanceCommand.java index da52fccd..5e2ae169 100755 --- a/src/main/java/com/github/pires/obd/commands/control/TimingAdvanceCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/TimingAdvanceCommand.java @@ -21,26 +21,33 @@ */ public class TimingAdvanceCommand extends PercentageObdCommand { - /** - *

Constructor for TimingAdvanceCommand.

- */ - public TimingAdvanceCommand() { - super("01 0E"); - } + /** + *

+ * Constructor for TimingAdvanceCommand. + *

+ */ + public TimingAdvanceCommand() { + super("01 0E"); + } - /** - *

Constructor for TimingAdvanceCommand.

- * - * @param other a {@link com.github.pires.obd.commands.control.TimingAdvanceCommand} object. - */ - public TimingAdvanceCommand(TimingAdvanceCommand other) { - super(other); - } + /** + *

+ * Constructor for TimingAdvanceCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.control.TimingAdvanceCommand} + * object. + */ + public TimingAdvanceCommand(TimingAdvanceCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.TIMING_ADVANCE.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.TIMING_ADVANCE.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java b/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java index 03d47209..b0d339ea 100644 --- a/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java @@ -19,139 +19,144 @@ import java.io.InputStream; /** - * It is not needed no know how many DTC are stored. - * Because when no DTC are stored response will be NO DATA - * And where are more messages it will be stored in frames that have 7 bytes. - * In one frame are stored 3 DTC. - * If we find out DTC P0000 that mean no message are we can end. + * It is not needed no know how many DTC are stored. Because when no DTC are + * stored response will be NO DATA And where are more messages it will be stored + * in frames that have 7 bytes. In one frame are stored 3 DTC. If we find out + * DTC P0000 that mean no message are we can end. * */ public class TroubleCodesCommand extends ObdCommand { - /** Constant dtcLetters={'P', 'C', 'B', 'U'} */ - protected final static char[] dtcLetters = {'P', 'C', 'B', 'U'}; - /** Constant hexArray="0123456789ABCDEF".toCharArray() */ - protected final static char[] hexArray = "0123456789ABCDEF".toCharArray(); - - protected StringBuilder codes = null; - - /** - *

Constructor for TroubleCodesCommand.

- */ - public TroubleCodesCommand() { - super("03"); - codes = new StringBuilder(); - } - - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.TroubleCodesCommand} object. - */ - public TroubleCodesCommand(TroubleCodesCommand other) { - super(other); - codes = new StringBuilder(); - } - - /** {@inheritDoc} */ - @Override - protected void fillBuffer() { - } - - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - final String result = getResult(); - String workingData; - int startIndex = 0;//Header size. - - String canOneFrame = result.replaceAll("[\r\n]", ""); - int canOneFrameLength = canOneFrame.length(); - if (canOneFrameLength <= 16 && canOneFrameLength % 4 == 0) {//CAN(ISO-15765) protocol one frame. - workingData = canOneFrame;//43yy{codes} - startIndex = 4;//Header is 43yy, yy showing the number of data items. - } else if (result.contains(":")) {//CAN(ISO-15765) protocol two and more frames. - workingData = result.replaceAll("[\r\n].:", "");//xxx43yy{codes} - startIndex = 7;//Header is xxx43yy, xxx is bytes of information to follow, yy showing the number of data items. - } else {//ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. - workingData = result.replaceAll("^43|[\r\n]43|[\r\n]", ""); - } - for (int begin = startIndex; begin < workingData.length(); begin += 4) { - String dtc = ""; - byte b1 = hexStringToByteArray(workingData.charAt(begin)); - int ch1 = ((b1 & 0xC0) >> 6); - int ch2 = ((b1 & 0x30) >> 4); - dtc += dtcLetters[ch1]; - dtc += hexArray[ch2]; - dtc += workingData.substring(begin+1, begin + 4); - if (dtc.equals("P0000")) { - return; - } - codes.append(dtc); - codes.append('\n'); - } - } - - private byte hexStringToByteArray(char s) { - return (byte) ((Character.digit(s, 16) << 4)); - } - - /** - *

formatResult.

- * - * @return the formatted result of this command in string representation. - * @deprecated use #getCalculatedResult instead - */ - public String formatResult() { - return codes.toString(); - } - - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(codes); - } - - - /** {@inheritDoc} */ - @Override - protected void readRawData(InputStream in) throws IOException { - byte b; - StringBuilder res = new StringBuilder(); - - // read until '>' arrives OR end of stream reached (and skip ' ') - char c; - while (true) { - b = (byte) in.read(); - if (b == -1) // -1 if the end of the stream is reached - { - break; - } - c = (char) b; - if (c == '>') // read until '>' arrives - { - break; - } - if (c != ' ') // skip ' ' - { - res.append(c); - } - } - - rawData = res.toString().trim(); - - } - - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return codes.toString(); - } - - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.TROUBLE_CODES.getValue(); - } + /** Constant dtcLetters={'P', 'C', 'B', 'U'} */ + protected final static char[] dtcLetters = { 'P', 'C', 'B', 'U' }; + /** Constant hexArray="0123456789ABCDEF".toCharArray() */ + protected final static char[] hexArray = "0123456789ABCDEF".toCharArray(); + + protected StringBuilder codes = null; + + /** + *

+ * Constructor for TroubleCodesCommand. + *

+ */ + public TroubleCodesCommand() { + super("03"); + codes = new StringBuilder(); + } + + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.control.TroubleCodesCommand} + * object. + */ + public TroubleCodesCommand(TroubleCodesCommand other) { + super(other); + codes = new StringBuilder(); + } + + /** {@inheritDoc} */ + @Override + protected void fillBuffer() { + } + + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + final String result = getResult(); + String workingData; + int startIndex = 0;// Header size. + + String canOneFrame = result.replaceAll("[\r\n]", ""); + int canOneFrameLength = canOneFrame.length(); + if (canOneFrameLength <= 16 && canOneFrameLength % 4 == 0) {// CAN(ISO-15765) protocol one frame. + workingData = canOneFrame;// 43yy{codes} + startIndex = 4;// Header is 43yy, yy showing the number of data items. + } else if (result.contains(":")) {// CAN(ISO-15765) protocol two and more frames. + workingData = result.replaceAll("[\r\n].:", "");// xxx43yy{codes} + startIndex = 7;// Header is xxx43yy, xxx is bytes of information to follow, yy showing the + // number of data items. + } else {// ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. + workingData = result.replaceAll("^43|[\r\n]43|[\r\n]", ""); + } + for (int begin = startIndex; begin < workingData.length(); begin += 4) { + String dtc = ""; + byte b1 = hexStringToByteArray(workingData.charAt(begin)); + int ch1 = ((b1 & 0xC0) >> 6); + int ch2 = ((b1 & 0x30) >> 4); + dtc += Character.toString(dtcLetters[ch1]); + dtc += Character.toString(hexArray[ch2]); + dtc += workingData.substring(begin + 1, begin + 4); + if ("P0000".equals(dtc)) { + return; + } + codes.append(dtc); + codes.append('\n'); + } + } + + private byte hexStringToByteArray(char s) { + return (byte) ((Character.digit(s, 16) << 4)); + } + + /** + *

+ * formatResult. + *

+ * + * @return the formatted result of this command in string representation. + * @deprecated use #getCalculatedResult instead + */ + public String formatResult() { + return codes.toString(); + } + + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(codes); + } + + /** {@inheritDoc} */ + @Override + protected void readRawData(InputStream in) throws IOException { + byte b; + StringBuilder res = new StringBuilder(); + + // read until '>' arrives OR end of stream reached (and skip ' ') + char c; + while (true) { + b = (byte) in.read(); + c = (char) b; + + // -1 if the end of the stream is reached + // or read until '>' arrives + if (b == -1 || c == '>') { + break; + } + + // skip ' ' + if (c != ' ') { + res.append(c); + } + } + + rawData = res.toString().trim(); + + } + + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return codes.toString(); + } + + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.TROUBLE_CODES.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/control/VinCommand.java b/src/main/java/com/github/pires/obd/commands/control/VinCommand.java index f85c16a6..9ab81b52 100644 --- a/src/main/java/com/github/pires/obd/commands/control/VinCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/VinCommand.java @@ -19,86 +19,88 @@ public class VinCommand extends PersistentCommand { - String vin = ""; + String vin = ""; - /** - * Default ctor. - */ - public VinCommand() { - super("09 02"); - } + /** + * Default ctor. + */ + public VinCommand() { + super("09 02"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.control.VinCommand} object. - */ - public VinCommand(VinCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.control.VinCommand} object. + */ + public VinCommand(VinCommand other) { + super(other); + } - /** - * {@inheritDoc} - */ - @Override - protected void performCalculations() { - final String result = getResult(); - String workingData; - if (result.contains(":")) {//CAN(ISO-15765) protocol. - workingData = result.replaceAll(".:", "").substring(9);//9 is xxx490201, xxx is bytes of information to follow. - Matcher m = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE).matcher(convertHexToString(workingData)); - if(m.find()) workingData = result.replaceAll("0:49", "").replaceAll(".:", ""); - } else {//ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. - workingData = result.replaceAll("49020.", ""); - } - vin = convertHexToString(workingData).replaceAll("[\u0000-\u001f]", ""); - } + /** + * {@inheritDoc} + */ + @Override + protected void performCalculations() { + final String result = getResult(); + String workingData; + if (result.contains(":")) {// CAN(ISO-15765) protocol. + workingData = result.replaceAll(".:", "").substring(9);// 9 is xxx490201, xxx is bytes of information to + // follow. + Matcher m = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE) + .matcher(convertHexToString(workingData)); + if (m.find()) + workingData = result.replaceAll("0:49", "").replaceAll(".:", ""); + } else {// ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. + workingData = result.replaceAll("49020.", ""); + } + vin = convertHexToString(workingData).replaceAll("[\u0000-\u001f]", ""); + } - /** - * {@inheritDoc} - */ - @Override - public String getFormattedResult() { - return String.valueOf(vin); - } + /** + * {@inheritDoc} + */ + @Override + public String getFormattedResult() { + return String.valueOf(vin); + } - /** - * {@inheritDoc} - */ - @Override - public String getName() { - return AvailableCommandNames.VIN.getValue(); - } + /** + * {@inheritDoc} + */ + @Override + public String getName() { + return AvailableCommandNames.VIN.getValue(); + } - /** - * {@inheritDoc} - */ - @Override - public String getCalculatedResult() { - return String.valueOf(vin); - } + /** + * {@inheritDoc} + */ + @Override + public String getCalculatedResult() { + return String.valueOf(vin); + } - /** - * {@inheritDoc} - */ - @Override - protected void fillBuffer() { - } + /** + * {@inheritDoc} + */ + @Override + protected void fillBuffer() { + } - public String convertHexToString(String hex) { - StringBuilder sb = new StringBuilder(); - //49204c6f7665204a617661 split into two characters 49, 20, 4c... - for (int i = 0; i < hex.length() - 1; i += 2) { + public String convertHexToString(String hex) { + StringBuilder sb = new StringBuilder(); + // 49204c6f7665204a617661 split into two characters 49, 20, 4c... + for (int i = 0; i < hex.length() - 1; i += 2) { - //grab the hex in pairs - String output = hex.substring(i, (i + 2)); - //convert hex to decimal - int decimal = Integer.parseInt(output, 16); - //convert the decimal to character - sb.append((char) decimal); - } - return sb.toString(); - } + // grab the hex in pairs + String output = hex.substring(i, (i + 2)); + // convert hex to decimal + int decimal = Integer.parseInt(output, 16); + // convert the decimal to character + sb.append((char) decimal); + } + return sb.toString(); + } } - - diff --git a/src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java b/src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java index 8ce0d07d..b089c722 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java @@ -16,49 +16,55 @@ import com.github.pires.obd.enums.AvailableCommandNames; /** - *

AbsoluteLoadCommand class.

+ *

+ * AbsoluteLoadCommand class. + *

* */ public class AbsoluteLoadCommand extends PercentageObdCommand { - /** - * Default ctor. - */ - public AbsoluteLoadCommand() { - super("01 43"); - } + /** + * Default ctor. + */ + public AbsoluteLoadCommand() { + super("01 43"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.engine.AbsoluteLoadCommand} object. - */ - public AbsoluteLoadCommand(AbsoluteLoadCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.engine.AbsoluteLoadCommand} + * object. + */ + public AbsoluteLoadCommand(AbsoluteLoadCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - int a = buffer.get(2); - int b = buffer.get(3); - percentage = (a * 256 + b) * 100 / 255; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + int a = buffer.get(2); + int b = buffer.get(3); + percentage = (a * 256 + b) * 100 / 255; + } - /** - *

getRatio.

- * - * @return a double. - */ - public double getRatio() { - return percentage; - } + /** + *

+ * getRatio. + *

+ * + * @return a double. + */ + public double getRatio() { + return percentage; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.ABS_LOAD.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.ABS_LOAD.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/engine/LoadCommand.java b/src/main/java/com/github/pires/obd/commands/engine/LoadCommand.java index 720b0dd2..dd8c46c9 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/LoadCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/LoadCommand.java @@ -21,31 +21,36 @@ */ public class LoadCommand extends PercentageObdCommand { - /** - *

Constructor for LoadCommand.

- */ - public LoadCommand() { - super("01 04"); - } + /** + *

+ * Constructor for LoadCommand. + *

+ */ + public LoadCommand() { + super("01 04"); + } - /** - *

Constructor for LoadCommand.

- * - * @param other a {@link com.github.pires.obd.commands.engine.LoadCommand} object. - */ - public LoadCommand(LoadCommand other) { - super(other); - } + /** + *

+ * Constructor for LoadCommand. + *

+ * + * @param other + * a {@link com.github.pires.obd.commands.engine.LoadCommand} object. + */ + public LoadCommand(LoadCommand other) { + super(other); + } - /* - * (non-Javadoc) - * - * @see pt.lighthouselabs.obd.commands.ObdCommand#getName() - */ - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.ENGINE_LOAD.getValue(); - } + /* + * (non-Javadoc) + * + * @see pt.lighthouselabs.obd.commands.ObdCommand#getName() + */ + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.ENGINE_LOAD.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java b/src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java index 5940839e..3aeb094f 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java @@ -21,62 +21,66 @@ */ public class MassAirFlowCommand extends ObdCommand { - private float maf = -1.0f; + private float maf = -1.0f; - /** - * Default ctor. - */ - public MassAirFlowCommand() { - super("01 10"); - } + /** + * Default ctor. + */ + public MassAirFlowCommand() { + super("01 10"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.engine.MassAirFlowCommand} object. - */ - public MassAirFlowCommand(MassAirFlowCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.engine.MassAirFlowCommand} + * object. + */ + public MassAirFlowCommand(MassAirFlowCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - maf = (buffer.get(2) * 256 + buffer.get(3)) / 100.0f; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + maf = (buffer.get(2) * 256 + buffer.get(3)) / 100.0f; + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return String.format("%.2f%s", maf, getResultUnit()); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return String.format("%.2f%s", maf, getResultUnit()); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(maf); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(maf); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return "g/s"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return "g/s"; + } - /** - *

getMAF.

- * - * @return MAF value for further calculus. - */ - public double getMAF() { - return maf; - } + /** + *

+ * getMAF. + *

+ * + * @return MAF value for further calculus. + */ + public double getMAF() { + return maf; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.MAF.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.MAF.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java b/src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java index 7e99b1a4..4c0ccf72 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java @@ -21,26 +21,28 @@ */ public class OilTempCommand extends TemperatureCommand { - /** - * Default ctor. - */ - public OilTempCommand() { - super("01 5C"); - } + /** + * Default ctor. + */ + public OilTempCommand() { + super("01 5C"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.engine.OilTempCommand} object. - */ - public OilTempCommand(OilTempCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.engine.OilTempCommand} + * object. + */ + public OilTempCommand(OilTempCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.ENGINE_OIL_TEMP.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.ENGINE_OIL_TEMP.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java b/src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java index 79cb9a1a..5d130bd9 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java @@ -21,62 +21,65 @@ */ public class RPMCommand extends ObdCommand { - private int rpm = -1; + private int rpm = -1; - /** - * Default ctor. - */ - public RPMCommand() { - super("01 0C"); - } + /** + * Default ctor. + */ + public RPMCommand() { + super("01 0C"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.engine.RPMCommand} object. - */ - public RPMCommand(RPMCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.engine.RPMCommand} object. + */ + public RPMCommand(RPMCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [41 0C] of the response((A*256)+B)/4 - rpm = (buffer.get(2) * 256 + buffer.get(3)) / 4; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [41 0C] of the response((A*256)+B)/4 + rpm = (buffer.get(2) * 256 + buffer.get(3)) / 4; + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return String.format("%d%s", rpm, getResultUnit()); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return String.format("%d%s", rpm, getResultUnit()); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(rpm); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(rpm); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return "RPM"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return "RPM"; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.ENGINE_RPM.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.ENGINE_RPM.getValue(); + } - /** - *

getRPM.

- * - * @return a int. - */ - public int getRPM() { - return rpm; - } + /** + *

+ * getRPM. + *

+ * + * @return a int. + */ + public int getRPM() { + return rpm; + } } diff --git a/src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java b/src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java index f8a8ce7f..fa6892a8 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java @@ -21,57 +21,59 @@ */ public class RuntimeCommand extends ObdCommand { - private int value = 0; + private int value = 0; - /** - * Default ctor. - */ - public RuntimeCommand() { - super("01 1F"); - } + /** + * Default ctor. + */ + public RuntimeCommand() { + super("01 1F"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.engine.RuntimeCommand} object. - */ - public RuntimeCommand(RuntimeCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.engine.RuntimeCommand} + * object. + */ + public RuntimeCommand(RuntimeCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [01 0C] of the response - value = buffer.get(2) * 256 + buffer.get(3); - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [01 0C] of the response + value = buffer.get(2) * 256 + buffer.get(3); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - // determine time - final String hh = String.format("%02d", value / 3600); - final String mm = String.format("%02d", (value % 3600) / 60); - final String ss = String.format("%02d", value % 60); - return String.format("%s:%s:%s", hh, mm, ss); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + // determine time + final String hh = String.format("%02d", value / 3600); + final String mm = String.format("%02d", (value % 3600) / 60); + final String ss = String.format("%02d", value % 60); + return String.format("%s:%s:%s", hh, mm, ss); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(value); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(value); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return "s"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return "s"; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.ENGINE_RUNTIME.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.ENGINE_RUNTIME.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java b/src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java index b460b924..4887f964 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java @@ -21,26 +21,29 @@ */ public class ThrottlePositionCommand extends PercentageObdCommand { - /** - * Default ctor. - */ - public ThrottlePositionCommand() { - super("01 11"); - } + /** + * Default ctor. + */ + public ThrottlePositionCommand() { + super("01 11"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.engine.ThrottlePositionCommand} object. - */ - public ThrottlePositionCommand(ThrottlePositionCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.engine.ThrottlePositionCommand} + * object. + */ + public ThrottlePositionCommand(ThrottlePositionCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.THROTTLE_POS.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.THROTTLE_POS.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/fuel/AirFuelRatioCommand.java b/src/main/java/com/github/pires/obd/commands/fuel/AirFuelRatioCommand.java index 9baa7dc1..504585fa 100644 --- a/src/main/java/com/github/pires/obd/commands/fuel/AirFuelRatioCommand.java +++ b/src/main/java/com/github/pires/obd/commands/fuel/AirFuelRatioCommand.java @@ -21,49 +21,53 @@ */ public class AirFuelRatioCommand extends ObdCommand { - private float afr = 0; + private float afr = 0; - /** - *

Constructor for AirFuelRatioCommand.

- */ - public AirFuelRatioCommand() { - super("01 44"); - } + /** + *

+ * Constructor for AirFuelRatioCommand. + *

+ */ + public AirFuelRatioCommand() { + super("01 44"); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [01 44] of the response - float A = buffer.get(2); - float B = buffer.get(3); - afr = (((A * 256) + B) / 32768) * 14.7f;//((A*256)+B)/32768 - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [01 44] of the response + float A = buffer.get(2); + float B = buffer.get(3); + afr = (((A * 256) + B) / 32768) * 14.7f;// ((A*256)+B)/32768 + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return String.format("%.2f", getAirFuelRatio()) + ":1 AFR"; - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return String.format("%.2f", getAirFuelRatio()) + ":1 AFR"; + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(getAirFuelRatio()); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(getAirFuelRatio()); + } - /** - *

getAirFuelRatio.

- * - * @return a double. - */ - public double getAirFuelRatio() { - return afr; - } + /** + *

+ * getAirFuelRatio. + *

+ * + * @return a double. + */ + public double getAirFuelRatio() { + return afr; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.AIR_FUEL_RATIO.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.AIR_FUEL_RATIO.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/fuel/ConsumptionRateCommand.java b/src/main/java/com/github/pires/obd/commands/fuel/ConsumptionRateCommand.java index 7211ef07..6e80c044 100644 --- a/src/main/java/com/github/pires/obd/commands/fuel/ConsumptionRateCommand.java +++ b/src/main/java/com/github/pires/obd/commands/fuel/ConsumptionRateCommand.java @@ -21,62 +21,71 @@ */ public class ConsumptionRateCommand extends ObdCommand { - private float fuelRate = -1.0f; + private float fuelRate = -1.0f; - /** - *

Constructor for ConsumptionRateCommand.

- */ - public ConsumptionRateCommand() { - super("01 5E"); - } + /** + *

+ * Constructor for ConsumptionRateCommand. + *

+ */ + public ConsumptionRateCommand() { + super("01 5E"); + } - /** - *

Constructor for ConsumptionRateCommand.

- * - * @param other a {@link com.github.pires.obd.commands.fuel.ConsumptionRateCommand} object. - */ - public ConsumptionRateCommand(ConsumptionRateCommand other) { - super(other); - } + /** + *

+ * Constructor for ConsumptionRateCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.fuel.ConsumptionRateCommand} + * object. + */ + public ConsumptionRateCommand(ConsumptionRateCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - fuelRate = (buffer.get(2) * 256 + buffer.get(3)) * 0.05f; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + fuelRate = (buffer.get(2) * 256 + buffer.get(3)) * 0.05f; + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return String.format("%.1f%s", fuelRate, getResultUnit()); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return String.format("%.1f%s", fuelRate, getResultUnit()); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(fuelRate); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(fuelRate); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return "L/h"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return "L/h"; + } - /** - *

getLitersPerHour.

- * - * @return a float. - */ - public float getLitersPerHour() { - return fuelRate; - } + /** + *

+ * getLitersPerHour. + *

+ * + * @return a float. + */ + public float getLitersPerHour() { + return fuelRate; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.FUEL_CONSUMPTION_RATE.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.FUEL_CONSUMPTION_RATE.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java b/src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java index af41d55c..3ac6a5f6 100644 --- a/src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java +++ b/src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java @@ -22,51 +22,53 @@ */ public class FindFuelTypeCommand extends ObdCommand { - private int fuelType = 0; + private int fuelType = 0; - /** - * Default ctor. - */ - public FindFuelTypeCommand() { - super("01 51"); - } + /** + * Default ctor. + */ + public FindFuelTypeCommand() { + super("01 51"); + } - /** - * Copy ctor - * - * @param other a {@link com.github.pires.obd.commands.fuel.FindFuelTypeCommand} object. - */ - public FindFuelTypeCommand(FindFuelTypeCommand other) { - super(other); - } + /** + * Copy ctor + * + * @param other + * a {@link com.github.pires.obd.commands.fuel.FindFuelTypeCommand} + * object. + */ + public FindFuelTypeCommand(FindFuelTypeCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - fuelType = buffer.get(2); - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + fuelType = buffer.get(2); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - try { - return FuelType.fromValue(fuelType).getDescription(); - } catch (NullPointerException e) { - return "-"; - } - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + try { + return FuelType.fromValue(fuelType).getDescription(); + } catch (NullPointerException e) { + return "-"; + } + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(fuelType); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(fuelType); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.FUEL_TYPE.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.FUEL_TYPE.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/fuel/FuelLevelCommand.java b/src/main/java/com/github/pires/obd/commands/fuel/FuelLevelCommand.java index 2ac67188..e30554f5 100644 --- a/src/main/java/com/github/pires/obd/commands/fuel/FuelLevelCommand.java +++ b/src/main/java/com/github/pires/obd/commands/fuel/FuelLevelCommand.java @@ -21,33 +21,37 @@ */ public class FuelLevelCommand extends PercentageObdCommand { - /** - *

Constructor for FuelLevelCommand.

- */ - public FuelLevelCommand() { - super("01 2F"); - } + /** + *

+ * Constructor for FuelLevelCommand. + *

+ */ + public FuelLevelCommand() { + super("01 2F"); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - percentage = 100.0f * buffer.get(2) / 255.0f; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + percentage = 100.0f * buffer.get(2) / 255.0f; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.FUEL_LEVEL.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.FUEL_LEVEL.getValue(); + } - /** - *

getFuelLevel.

- * - * @return a float. - */ - public float getFuelLevel() { - return percentage; - } + /** + *

+ * getFuelLevel. + *

+ * + * @return a float. + */ + public float getFuelLevel() { + return percentage; + } } diff --git a/src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java b/src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java index 30f0955f..789c6335 100644 --- a/src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java +++ b/src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java @@ -21,67 +21,77 @@ */ public class FuelTrimCommand extends PercentageObdCommand { - private final FuelTrim bank; + private final FuelTrim bank; - /** - * Default ctor. - *

- * Will read the bank from parameters and construct the command accordingly. - * Please, see FuelTrim enum for more details. - * - * @param bank a {@link com.github.pires.obd.enums.FuelTrim} object. - */ - public FuelTrimCommand(final FuelTrim bank) { - super(bank.buildObdCommand()); - this.bank = bank; - } + /** + * Default ctor. + *

+ * Will read the bank from parameters and construct the command accordingly. + * Please, see FuelTrim enum for more details. + * + * @param bank + * a {@link com.github.pires.obd.enums.FuelTrim} object. + */ + public FuelTrimCommand(final FuelTrim bank) { + super(bank.buildObdCommand()); + this.bank = bank; + } - /** - *

Constructor for FuelTrimCommand.

- */ - public FuelTrimCommand() { - this(FuelTrim.SHORT_TERM_BANK_1); - } + /** + *

+ * Constructor for FuelTrimCommand. + *

+ */ + public FuelTrimCommand() { + this(FuelTrim.SHORT_TERM_BANK_1); + } - /** - * @param value - * @return - */ - private float prepareTempValue(final int value) { - return (value - 128) * (100.0F / 128); - } + /** + * @param value + * @return + */ + private float prepareTempValue(final int value) { + return (value - 128) * (100.0F / 128); + } - /** - *

performCalculations.

- */ - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - percentage = prepareTempValue(buffer.get(2)); - } + /** + *

+ * performCalculations. + *

+ */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + percentage = prepareTempValue(buffer.get(2)); + } - /** - *

getValue.

- * - * @return the readed Fuel Trim percentage value. - * @deprecated use #getCalculatedResult() - */ - public final float getValue() { - return percentage; - } + /** + *

+ * getValue. + *

+ * + * @return the readed Fuel Trim percentage value. + * @deprecated use #getCalculatedResult() + */ + public final float getValue() { + return percentage; + } - /** - *

Getter for the field bank.

- * - * @return the name of the bank in string representation. - */ - public final String getBank() { - return bank.getBank(); - } + /** + *

+ * Getter for the field bank. + *

+ * + * @return the name of the bank in string representation. + */ + public final String getBank() { + return bank.getBank(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return bank.getBank(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return bank.getBank(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/fuel/WidebandAirFuelRatioCommand.java b/src/main/java/com/github/pires/obd/commands/fuel/WidebandAirFuelRatioCommand.java index 39d5f226..baeea000 100644 --- a/src/main/java/com/github/pires/obd/commands/fuel/WidebandAirFuelRatioCommand.java +++ b/src/main/java/com/github/pires/obd/commands/fuel/WidebandAirFuelRatioCommand.java @@ -21,49 +21,53 @@ */ public class WidebandAirFuelRatioCommand extends ObdCommand { - private float wafr = 0; + private float wafr = 0; - /** - *

Constructor for WidebandAirFuelRatioCommand.

- */ - public WidebandAirFuelRatioCommand() { - super("01 34"); - } + /** + *

+ * Constructor for WidebandAirFuelRatioCommand. + *

+ */ + public WidebandAirFuelRatioCommand() { + super("01 34"); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [01 44] of the response - float A = buffer.get(2); - float B = buffer.get(3); - wafr = (((A * 256) + B) / 32768) * 14.7f;//((A*256)+B)/32768 - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [01 44] of the response + float A = buffer.get(2); + float B = buffer.get(3); + wafr = (((A * 256) + B) / 32768) * 14.7f;// ((A*256)+B)/32768 + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return String.format("%.2f", getWidebandAirFuelRatio()) + ":1 AFR"; - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return String.format("%.2f", getWidebandAirFuelRatio()) + ":1 AFR"; + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(getWidebandAirFuelRatio()); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(getWidebandAirFuelRatio()); + } - /** - *

getWidebandAirFuelRatio.

- * - * @return a double. - */ - public double getWidebandAirFuelRatio() { - return wafr; - } + /** + *

+ * getWidebandAirFuelRatio. + *

+ * + * @return a double. + */ + public double getWidebandAirFuelRatio() { + return wafr; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.WIDEBAND_AIR_FUEL_RATIO.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.WIDEBAND_AIR_FUEL_RATIO.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/pressure/BarometricPressureCommand.java b/src/main/java/com/github/pires/obd/commands/pressure/BarometricPressureCommand.java index cafe4784..1c26fa7d 100644 --- a/src/main/java/com/github/pires/obd/commands/pressure/BarometricPressureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/pressure/BarometricPressureCommand.java @@ -20,26 +20,32 @@ */ public class BarometricPressureCommand extends PressureCommand { - /** - *

Constructor for BarometricPressureCommand.

- */ - public BarometricPressureCommand() { - super("01 33"); - } + /** + *

+ * Constructor for BarometricPressureCommand. + *

+ */ + public BarometricPressureCommand() { + super("01 33"); + } - /** - *

Constructor for BarometricPressureCommand.

- * - * @param other a {@link com.github.pires.obd.commands.pressure.PressureCommand} object. - */ - public BarometricPressureCommand(PressureCommand other) { - super(other); - } + /** + *

+ * Constructor for BarometricPressureCommand. + *

+ * + * @param other + * a {@link com.github.pires.obd.commands.pressure.PressureCommand} + * object. + */ + public BarometricPressureCommand(PressureCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.BAROMETRIC_PRESSURE.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.BAROMETRIC_PRESSURE.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/pressure/FuelPressureCommand.java b/src/main/java/com/github/pires/obd/commands/pressure/FuelPressureCommand.java index 91ead518..6d8233ea 100644 --- a/src/main/java/com/github/pires/obd/commands/pressure/FuelPressureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/pressure/FuelPressureCommand.java @@ -15,41 +15,50 @@ import com.github.pires.obd.enums.AvailableCommandNames; /** - *

FuelPressureCommand class.

+ *

+ * FuelPressureCommand class. + *

* */ public class FuelPressureCommand extends PressureCommand { - /** - *

Constructor for FuelPressureCommand.

- */ - public FuelPressureCommand() { - super("01 0A"); - } + /** + *

+ * Constructor for FuelPressureCommand. + *

+ */ + public FuelPressureCommand() { + super("01 0A"); + } - /** - *

Constructor for FuelPressureCommand.

- * - * @param other a {@link com.github.pires.obd.commands.pressure.FuelPressureCommand} object. - */ - public FuelPressureCommand(FuelPressureCommand other) { - super(other); - } + /** + *

+ * Constructor for FuelPressureCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.pressure.FuelPressureCommand} + * object. + */ + public FuelPressureCommand(FuelPressureCommand other) { + super(other); + } - /** - * {@inheritDoc} - *

- * TODO describe of why we multiply by 3 - */ - @Override - protected final int preparePressureValue() { - return buffer.get(2) * 3; - } + /** + * {@inheritDoc} + *

+ * TODO describe of why we multiply by 3 + */ + @Override + protected final int preparePressureValue() { + return buffer.get(2) * 3; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.FUEL_PRESSURE.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.FUEL_PRESSURE.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/pressure/FuelRailPressureCommand.java b/src/main/java/com/github/pires/obd/commands/pressure/FuelRailPressureCommand.java index 189bf38e..2cd08d56 100644 --- a/src/main/java/com/github/pires/obd/commands/pressure/FuelRailPressureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/pressure/FuelRailPressureCommand.java @@ -15,43 +15,52 @@ import com.github.pires.obd.enums.AvailableCommandNames; /** - *

FuelRailPressureCommand class.

+ *

+ * FuelRailPressureCommand class. + *

* */ public class FuelRailPressureCommand extends PressureCommand { - /** - *

Constructor for FuelRailPressureCommand.

- */ - public FuelRailPressureCommand() { - super("01 23"); - } + /** + *

+ * Constructor for FuelRailPressureCommand. + *

+ */ + public FuelRailPressureCommand() { + super("01 23"); + } - /** - *

Constructor for FuelRailPressureCommand.

- * - * @param other a {@link com.github.pires.obd.commands.pressure.FuelRailPressureCommand} object. - */ - public FuelRailPressureCommand(FuelRailPressureCommand other) { - super(other); - } + /** + *

+ * Constructor for FuelRailPressureCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.pressure.FuelRailPressureCommand} + * object. + */ + public FuelRailPressureCommand(FuelRailPressureCommand other) { + super(other); + } - /** - * {@inheritDoc} - *

- * TODO describe of why we multiply by 3 - */ - @Override - protected final int preparePressureValue() { - int a = buffer.get(2); - int b = buffer.get(3); - return ((a * 256) + b) * 10; - } + /** + * {@inheritDoc} + *

+ * TODO describe of why we multiply by 3 + */ + @Override + protected final int preparePressureValue() { + int a = buffer.get(2); + int b = buffer.get(3); + return ((a * 256) + b) * 10; + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.FUEL_RAIL_PRESSURE.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.FUEL_RAIL_PRESSURE.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java b/src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java index cd198f7d..d88951b7 100644 --- a/src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java @@ -20,26 +20,29 @@ */ public class IntakeManifoldPressureCommand extends PressureCommand { - /** - * Default ctor. - */ - public IntakeManifoldPressureCommand() { - super("01 0B"); - } + /** + * Default ctor. + */ + public IntakeManifoldPressureCommand() { + super("01 0B"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.pressure.IntakeManifoldPressureCommand} object. - */ - public IntakeManifoldPressureCommand(IntakeManifoldPressureCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.pressure.IntakeManifoldPressureCommand} + * object. + */ + public IntakeManifoldPressureCommand(IntakeManifoldPressureCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.INTAKE_MANIFOLD_PRESSURE.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.INTAKE_MANIFOLD_PRESSURE.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java b/src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java index b67f1c83..b9ff4839 100644 --- a/src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java @@ -19,85 +19,93 @@ * Abstract pressure command. * */ -public abstract class PressureCommand extends ObdCommand implements - SystemOfUnits { +public abstract class PressureCommand extends ObdCommand implements SystemOfUnits { - protected int tempValue = 0; - protected int pressure = 0; + protected int tempValue = 0; + protected int pressure = 0; - /** - * Default ctor - * - * @param cmd a {@link java.lang.String} object. - */ - public PressureCommand(String cmd) { - super(cmd); - } + /** + * Default ctor + * + * @param cmd + * a {@link java.lang.String} object. + */ + public PressureCommand(String cmd) { + super(cmd); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.pressure.PressureCommand} object. - */ - public PressureCommand(PressureCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a {@link com.github.pires.obd.commands.pressure.PressureCommand} + * object. + */ + public PressureCommand(PressureCommand other) { + super(other); + } - /** - * Some PressureCommand subclasses will need to implement this method in - * order to determine the final kPa value. - *

- * *NEED* to read tempValue - * - * @return a int. - */ - protected int preparePressureValue() { - return buffer.get(2); - } + /** + * Some PressureCommand subclasses will need to implement this method in order + * to determine the final kPa value. + *

+ * *NEED* to read tempValue + * + * @return a int. + */ + protected int preparePressureValue() { + return buffer.get(2); + } - /** - *

performCalculations.

- */ - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - pressure = preparePressureValue(); - } + /** + *

+ * performCalculations. + *

+ */ + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + pressure = preparePressureValue(); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return useImperialUnits ? String.format("%.1f%s", getImperialUnit(), getResultUnit()) - : String.format("%d%s", pressure, getResultUnit()); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return useImperialUnits ? String.format("%.1f%s", getImperialUnit(), getResultUnit()) + : String.format("%d%s", pressure, getResultUnit()); + } - /** - *

getMetricUnit.

- * - * @return the pressure in kPa - */ - public int getMetricUnit() { - return pressure; - } + /** + *

+ * getMetricUnit. + *

+ * + * @return the pressure in kPa + */ + public int getMetricUnit() { + return pressure; + } - /** - *

getImperialUnit.

- * - * @return the pressure in psi - */ - public float getImperialUnit() { - return pressure * 0.145037738F; - } + /** + *

+ * getImperialUnit. + *

+ * + * @return the pressure in psi + */ + public float getImperialUnit() { + return pressure * 0.145037738F; + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(pressure); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(pressure); + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return useImperialUnits ? "psi" : "kPa"; - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return useImperialUnits ? "psi" : "kPa"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AdaptiveTimingCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/AdaptiveTimingCommand.java index df8544c0..f393d7df 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AdaptiveTimingCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AdaptiveTimingCommand.java @@ -13,42 +13,51 @@ package com.github.pires.obd.commands.protocol; /** - * By default, Adaptive Timing option 1 (AT1) is enabled, and is the recommended setting. - * AT0 is used to disable Adaptive timing (so the timeout is always as set by AT ST), - * while AT2 is a more aggressive version of AT1 (the effect is more noticeable for very - * slow connections – you may not see much difference with faster OBD systems. + * By default, Adaptive Timing option 1 (AT1) is enabled, and is the recommended + * setting. AT0 is used to disable Adaptive timing (so the timeout is always as + * set by AT ST), while AT2 is a more aggressive version of AT1 (the effect is + * more noticeable for very slow connections – you may not see much difference + * with faster OBD systems. * */ public class AdaptiveTimingCommand extends ObdProtocolCommand { - /** - *

Constructor for AdaptiveTimingCommand.

- * - * @param mode a int. - */ - public AdaptiveTimingCommand(int mode) { - super("AT AT" + mode); - } + /** + *

+ * Constructor for AdaptiveTimingCommand. + *

+ * + * @param mode + * a int. + */ + public AdaptiveTimingCommand(int mode) { + super("AT AT" + mode); + } - /** - *

Constructor for AdaptiveTimingCommand.

- * - * @param other a {@link com.github.pires.obd.commands.protocol.AdaptiveTimingCommand} object. - */ - public AdaptiveTimingCommand(AdaptiveTimingCommand other) { - super(other); - } + /** + *

+ * Constructor for AdaptiveTimingCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.protocol.AdaptiveTimingCommand} + * object. + */ + public AdaptiveTimingCommand(AdaptiveTimingCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Adaptive timing set"; - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Adaptive timing set"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java index 85cc6369..d217fd6f 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java @@ -20,40 +20,44 @@ */ public abstract class AvailablePidsCommand extends PersistentCommand { - /** - * Default ctor. - * - * @param command a {@link java.lang.String} object. - */ - public AvailablePidsCommand(String command) { - super(command); - } - - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} object. - */ - public AvailablePidsCommand(AvailablePidsCommand other) { - super(other); - } - - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - - } - - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getCalculatedResult(); - } - - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - //First 4 characters are a copy of the command code, don't return those - return String.valueOf(rawData).substring(4); - } + /** + * Default ctor. + * + * @param command + * a {@link java.lang.String} object. + */ + public AvailablePidsCommand(String command) { + super(command); + } + + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} + * object. + */ + public AvailablePidsCommand(AvailablePidsCommand other) { + super(other); + } + + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + + } + + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getCalculatedResult(); + } + + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + // First 4 characters are a copy of the command code, don't return those + return String.valueOf(rawData).substring(4); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java index 78ff4055..8ab46064 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java @@ -20,25 +20,28 @@ */ public class AvailablePidsCommand_01_20 extends AvailablePidsCommand { - /** - * Default ctor. - */ - public AvailablePidsCommand_01_20() { - super("01 00"); - } + /** + * Default ctor. + */ + public AvailablePidsCommand_01_20() { + super("01 00"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} object. - */ - public AvailablePidsCommand_01_20(AvailablePidsCommand_01_20 other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} + * object. + */ + public AvailablePidsCommand_01_20(AvailablePidsCommand_01_20 other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.PIDS_01_20.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.PIDS_01_20.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java index ee3e47e2..1b301562 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java @@ -20,25 +20,28 @@ */ public class AvailablePidsCommand_21_40 extends AvailablePidsCommand { - /** - * Default ctor. - */ - public AvailablePidsCommand_21_40() { - super("01 20"); - } + /** + * Default ctor. + */ + public AvailablePidsCommand_21_40() { + super("01 20"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} object. - */ - public AvailablePidsCommand_21_40(AvailablePidsCommand_21_40 other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} + * object. + */ + public AvailablePidsCommand_21_40(AvailablePidsCommand_21_40 other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.PIDS_21_40.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.PIDS_21_40.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java index adeefc8d..3ea01139 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java @@ -20,25 +20,28 @@ */ public class AvailablePidsCommand_41_60 extends AvailablePidsCommand { - /** - * Default ctor. - */ - public AvailablePidsCommand_41_60() { - super("01 40"); - } + /** + * Default ctor. + */ + public AvailablePidsCommand_41_60() { + super("01 40"); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} object. - */ - public AvailablePidsCommand_41_60(AvailablePidsCommand_41_60 other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} + * object. + */ + public AvailablePidsCommand_41_60(AvailablePidsCommand_41_60 other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.PIDS_41_60.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.PIDS_41_60.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/CloseCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/CloseCommand.java index 4a30bf78..516192ec 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/CloseCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/CloseCommand.java @@ -15,48 +15,52 @@ /** * As per https://www.elmelectronics.com/help/obd/tips/#327_Commands: *

- * If a connection is lost, you will need to tell the ELM327 to ‘close’ - * the current connection, with a Protocol Close command (AT PC). - * This will ensure that the ELM327 starts from the beginning when - * the next request is made. This is particularly important for the - * ISO 9141 and ISO 14230 protocols, as they need to send a special - * initiation sequence. + * If a connection is lost, you will need to tell the ELM327 to ‘close’ the + * current connection, with a Protocol Close command (AT PC). This will ensure + * that the ELM327 starts from the beginning when the next request is made. This + * is particularly important for the ISO 9141 and ISO 14230 protocols, as they + * need to send a special initiation sequence. *

- * Once the protocol has been closed, it can be re-opened by making a - * request such as 01 00 (do not send ATZ or AT SP0, as many do). + * Once the protocol has been closed, it can be re-opened by making a request + * such as 01 00 (do not send ATZ or AT SP0, as many do). */ public class CloseCommand extends ObdProtocolCommand { - /** - *

Constructor for CloseCommand.

- */ - public CloseCommand() { - super("AT PC"); - } + /** + *

+ * Constructor for CloseCommand. + *

+ */ + public CloseCommand() { + super("AT PC"); + } - /** - *

Constructor for CloseCommand.

- * - * @param other a {@link CloseCommand} object. - */ - public CloseCommand(CloseCommand other) { - super(other); - } + /** + *

+ * Constructor for CloseCommand. + *

+ * + * @param other + * a {@link CloseCommand} object. + */ + public CloseCommand(CloseCommand other) { + super(other); + } - /** - * {@inheritDoc} - */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** + * {@inheritDoc} + */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** - * {@inheritDoc} - */ - @Override - public String getName() { - return "Protocol Close"; - } + /** + * {@inheritDoc} + */ + @Override + public String getName() { + return "Protocol Close"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolCommand.java index ceaafd6e..84d4b671 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolCommand.java @@ -15,34 +15,34 @@ import com.github.pires.obd.enums.AvailableCommandNames; /** - * Describe the current Protocol. - * If a protocol is chosen and the automatic option is - * also selected, AT DP will show the word 'AUTO' before - * the protocol description. Note that the description - * shows the actual protocol names, not the numbers - * used by the protocol setting commands. + * Describe the current Protocol. If a protocol is chosen and the automatic + * option is also selected, AT DP will show the word 'AUTO' before the protocol + * description. Note that the description shows the actual protocol names, not + * the numbers used by the protocol setting commands. * * @since 1.0-RC12 */ public class DescribeProtocolCommand extends ObdProtocolCommand { - /** - *

Constructor for DescribeProtocolCommand.

- */ - public DescribeProtocolCommand() { - super("AT DP"); - } + /** + *

+ * Constructor for DescribeProtocolCommand. + *

+ */ + public DescribeProtocolCommand() { + super("AT DP"); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.DESCRIBE_PROTOCOL.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.DESCRIBE_PROTOCOL.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolNumberCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolNumberCommand.java index 6e437d75..f72d20c7 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolNumberCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolNumberCommand.java @@ -17,72 +17,75 @@ import com.github.pires.obd.enums.ObdProtocols; /** - * Describe the Protocol by Number. - * It returns a number which represents the current - * obdProtocol. If the automatic search function is also - * enabled, the number will be preceded with the letter - * ‘A’. The number is the same one that is used with the - * set obdProtocol and test obdProtocol commands. + * Describe the Protocol by Number. It returns a number which represents the + * current obdProtocol. If the automatic search function is also enabled, the + * number will be preceded with the letter ‘A’. The number is the same one that + * is used with the set obdProtocol and test obdProtocol commands. * * @since 1.0-RC12 */ public class DescribeProtocolNumberCommand extends ObdCommand { - private ObdProtocols obdProtocol = ObdProtocols.AUTO; + private ObdProtocols obdProtocol = ObdProtocols.AUTO; - /** - *

Constructor for DescribeProtocolNumberCommand.

- */ - public DescribeProtocolNumberCommand() { - super("AT DPN"); - } + /** + *

+ * Constructor for DescribeProtocolNumberCommand. + *

+ */ + public DescribeProtocolNumberCommand() { + super("AT DPN"); + } - /** - * {@inheritDoc} - * - * This method exists so that for each command, there must be a method that is - * called only once to perform calculations. - */ - @Override - protected void performCalculations() { - String result = getResult(); - char protocolNumber; - if (result.length() == 2) {//the obdProtocol was set automatic and its format A# - protocolNumber = result.charAt(1); - } else protocolNumber = result.charAt(0); - ObdProtocols[] protocols = ObdProtocols.values(); - for (ObdProtocols protocol : protocols) { - if (protocol.getValue() == protocolNumber) { - this.obdProtocol = protocol; - break; - } - } - } + /** + * {@inheritDoc} + * + * This method exists so that for each command, there must be a method that is + * called only once to perform calculations. + */ + @Override + protected void performCalculations() { + String result = getResult(); + char protocolNumber; + if (result.length() == 2) {// the obdProtocol was set automatic and its format A# + protocolNumber = result.charAt(1); + } else + protocolNumber = result.charAt(0); + ObdProtocols[] protocols = ObdProtocols.values(); + for (ObdProtocols protocol : protocols) { + if (protocol.getValue() == protocolNumber) { + this.obdProtocol = protocol; + break; + } + } + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return obdProtocol.name(); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return obdProtocol.name(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.DESCRIBE_PROTOCOL_NUMBER.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.DESCRIBE_PROTOCOL_NUMBER.getValue(); + } - /** - *

Getter for the field obdProtocol.

- * - * @return a {@link com.github.pires.obd.enums.ObdProtocols} object. - */ - public ObdProtocols getObdProtocol() { - return obdProtocol; - } + /** + *

+ * Getter for the field obdProtocol. + *

+ * + * @return a {@link com.github.pires.obd.enums.ObdProtocols} object. + */ + public ObdProtocols getObdProtocol() { + return obdProtocol; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/EchoOffCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/EchoOffCommand.java index 4ea923d8..d6eb929f 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/EchoOffCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/EchoOffCommand.java @@ -18,32 +18,38 @@ */ public class EchoOffCommand extends ObdProtocolCommand { - /** - *

Constructor for EchoOffCommand.

- */ - public EchoOffCommand() { - super("AT E0"); - } + /** + *

+ * Constructor for EchoOffCommand. + *

+ */ + public EchoOffCommand() { + super("AT E0"); + } - /** - *

Constructor for EchoOffCommand.

- * - * @param other a {@link com.github.pires.obd.commands.protocol.EchoOffCommand} object. - */ - public EchoOffCommand(EchoOffCommand other) { - super(other); - } + /** + *

+ * Constructor for EchoOffCommand. + *

+ * + * @param other + * a {@link com.github.pires.obd.commands.protocol.EchoOffCommand} + * object. + */ + public EchoOffCommand(EchoOffCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Echo Off"; - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Echo Off"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/HeadersOffCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/HeadersOffCommand.java index f0a2da3b..1be0b18d 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/HeadersOffCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/HeadersOffCommand.java @@ -18,32 +18,38 @@ */ public class HeadersOffCommand extends ObdProtocolCommand { - /** - *

Constructor for HeadersOffCommand.

- */ - public HeadersOffCommand() { - super("ATH0"); - } + /** + *

+ * Constructor for HeadersOffCommand. + *

+ */ + public HeadersOffCommand() { + super("ATH0"); + } - /** - *

Constructor for HeadersOffCommand.

- * - * @param other a {@link com.github.pires.obd.commands.protocol.HeadersOffCommand} object. - */ - public HeadersOffCommand(HeadersOffCommand other) { - super(other); - } + /** + *

+ * Constructor for HeadersOffCommand. + *

+ * + * @param other + * a {@link com.github.pires.obd.commands.protocol.HeadersOffCommand} + * object. + */ + public HeadersOffCommand(HeadersOffCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Headers disabled"; - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Headers disabled"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/LineFeedOffCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/LineFeedOffCommand.java index b5ba499a..d2071166 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/LineFeedOffCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/LineFeedOffCommand.java @@ -18,32 +18,39 @@ */ public class LineFeedOffCommand extends ObdProtocolCommand { - /** - *

Constructor for LineFeedOffCommand.

- */ - public LineFeedOffCommand() { - super("AT L0"); - } + /** + *

+ * Constructor for LineFeedOffCommand. + *

+ */ + public LineFeedOffCommand() { + super("AT L0"); + } - /** - *

Constructor for LineFeedOffCommand.

- * - * @param other a {@link com.github.pires.obd.commands.protocol.LineFeedOffCommand} object. - */ - public LineFeedOffCommand(LineFeedOffCommand other) { - super(other); - } + /** + *

+ * Constructor for LineFeedOffCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.protocol.LineFeedOffCommand} + * object. + */ + public LineFeedOffCommand(LineFeedOffCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Line Feed Off"; - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Line Feed Off"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java index f0a5f6c8..674ff40a 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java @@ -15,45 +15,53 @@ import com.github.pires.obd.commands.ObdCommand; /** - *

Abstract ObdProtocolCommand class.

+ *

+ * Abstract ObdProtocolCommand class. + *

*/ public abstract class ObdProtocolCommand extends ObdCommand { - /** - * Default ctor to use - * - * @param command the command to send - */ - public ObdProtocolCommand(String command) { - super(command); - } + /** + * Default ctor to use + * + * @param command + * the command to send + */ + public ObdProtocolCommand(String command) { + super(command); + } - /** - * Copy ctor. - * - * @param other the ObdCommand to copy. - */ - public ObdProtocolCommand(ObdProtocolCommand other) { - this(other.cmd); - } + /** + * Copy ctor. + * + * @param other + * the ObdCommand to copy. + */ + public ObdProtocolCommand(ObdProtocolCommand other) { + this(other.cmd); + } - /** - *

performCalculations.

- */ - protected void performCalculations() { - // ignore - } + /** + *

+ * performCalculations. + *

+ */ + protected void performCalculations() { + // ignore + } - /** - *

fillBuffer.

- */ - protected void fillBuffer() { - // settings commands don't return a value appropriate to place into the - // buffer, so do nothing - } + /** + *

+ * fillBuffer. + *

+ */ + protected void fillBuffer() { + // settings commands don't return a value appropriate to place into the + // buffer, so do nothing + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return String.valueOf(getResult()); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return String.valueOf(getResult()); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/ObdRawCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/ObdRawCommand.java index 43681a38..56214467 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/ObdRawCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/ObdRawCommand.java @@ -17,25 +17,28 @@ */ public class ObdRawCommand extends ObdProtocolCommand { - /** - *

Constructor for ObdRawCommand.

- * - * @param command a {@link java.lang.String} object. - */ - public ObdRawCommand(String command) { - super(command); - } + /** + *

+ * Constructor for ObdRawCommand. + *

+ * + * @param command + * a {@link java.lang.String} object. + */ + public ObdRawCommand(String command) { + super(command); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Custom command " + getCommandPID(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Custom command " + getCommandPID(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/ObdResetCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/ObdResetCommand.java index dfefae91..63e886f6 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/ObdResetCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/ObdResetCommand.java @@ -18,32 +18,38 @@ */ public class ObdResetCommand extends ObdProtocolCommand { - /** - *

Constructor for ObdResetCommand.

- */ - public ObdResetCommand() { - super("AT Z"); - } + /** + *

+ * Constructor for ObdResetCommand. + *

+ */ + public ObdResetCommand() { + super("AT Z"); + } - /** - *

Constructor for ObdResetCommand.

- * - * @param other a {@link com.github.pires.obd.commands.protocol.ObdResetCommand} object. - */ - public ObdResetCommand(ObdResetCommand other) { - super(other); - } + /** + *

+ * Constructor for ObdResetCommand. + *

+ * + * @param other + * a {@link com.github.pires.obd.commands.protocol.ObdResetCommand} + * object. + */ + public ObdResetCommand(ObdResetCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Reset OBD"; - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Reset OBD"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/ObdWarmstartCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/ObdWarmstartCommand.java index b2117e7f..156dc643 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/ObdWarmstartCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/ObdWarmstartCommand.java @@ -18,32 +18,39 @@ */ public class ObdWarmstartCommand extends ObdProtocolCommand { - /** - *

Constructor for ObdWarmstartCommand.

- */ - public ObdWarmstartCommand() { - super("AT WS"); - } + /** + *

+ * Constructor for ObdWarmstartCommand. + *

+ */ + public ObdWarmstartCommand() { + super("AT WS"); + } - /** - *

Constructor for ObdWarmstartCommand.

- * - * @param other a {@link com.github.pires.obd.commands.protocol.ObdWarmstartCommand} object. - */ - public ObdWarmstartCommand(ObdWarmstartCommand other) { - super(other); - } + /** + *

+ * Constructor for ObdWarmstartCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.protocol.ObdWarmstartCommand} + * object. + */ + public ObdWarmstartCommand(ObdWarmstartCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Warmstart OBD"; - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Warmstart OBD"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/ResetTroubleCodesCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/ResetTroubleCodesCommand.java index c4f68b4a..fba56443 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/ResetTroubleCodesCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/ResetTroubleCodesCommand.java @@ -20,36 +20,37 @@ */ public class ResetTroubleCodesCommand extends ObdCommand { - /** - *

Constructor for ResetTroubleCodesCommand.

- */ - public ResetTroubleCodesCommand() { - super("04"); - } - - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - - } - - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } - - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return getResult(); - } - - - /** {@inheritDoc} */ - @Override - public String getName() { - return getResult(); - } + /** + *

+ * Constructor for ResetTroubleCodesCommand. + *

+ */ + public ResetTroubleCodesCommand() { + super("04"); + } + + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + + } + + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } + + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return getResult(); + } + + /** {@inheritDoc} */ + @Override + public String getName() { + return getResult(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/SelectProtocolCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/SelectProtocolCommand.java index b0a77235..6d0959e3 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/SelectProtocolCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/SelectProtocolCommand.java @@ -20,28 +20,31 @@ */ public class SelectProtocolCommand extends ObdProtocolCommand { - private final ObdProtocols protocol; + private final ObdProtocols protocol; - /** - *

Constructor for SelectProtocolCommand.

- * - * @param protocol a {@link com.github.pires.obd.enums.ObdProtocols} object. - */ - public SelectProtocolCommand(final ObdProtocols protocol) { - super("AT SP " + protocol.getValue()); - this.protocol = protocol; - } + /** + *

+ * Constructor for SelectProtocolCommand. + *

+ * + * @param protocol + * a {@link com.github.pires.obd.enums.ObdProtocols} object. + */ + public SelectProtocolCommand(final ObdProtocols protocol) { + super("AT SP " + protocol.getValue()); + this.protocol = protocol; + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Select Protocol " + protocol.name(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Select Protocol " + protocol.name(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/SpacesOffCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/SpacesOffCommand.java index 261f5c9a..5c2100ff 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/SpacesOffCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/SpacesOffCommand.java @@ -12,32 +12,34 @@ */ package com.github.pires.obd.commands.protocol; - /** * Turn-off spaces. */ public class SpacesOffCommand extends ObdProtocolCommand { - public SpacesOffCommand() { - super("ATS0"); - } + public SpacesOffCommand() { + super("ATS0"); + } - /** - *

Constructor for SpacesOffCommand.

- * - * @param other a {@link SpacesOffCommand} object. - */ - public SpacesOffCommand(SpacesOffCommand other) { - super(other); - } + /** + *

+ * Constructor for SpacesOffCommand. + *

+ * + * @param other + * a {@link SpacesOffCommand} object. + */ + public SpacesOffCommand(SpacesOffCommand other) { + super(other); + } - @Override - public String getFormattedResult() { - return getResult(); - } + @Override + public String getFormattedResult() { + return getResult(); + } - @Override - public String getName() { - return "Spaces Off"; - } + @Override + public String getName() { + return "Spaces Off"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/TimeoutCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/TimeoutCommand.java index 0e15eb02..bea6199c 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/TimeoutCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/TimeoutCommand.java @@ -19,35 +19,42 @@ */ public class TimeoutCommand extends ObdProtocolCommand { - /** - *

Constructor for TimeoutCommand.

- * - * @param timeout value between 0 and 255 that multiplied by 4 results in the - * desired timeout in milliseconds (ms). - */ - public TimeoutCommand(int timeout) { - super("AT ST " + Integer.toHexString(0xFF & timeout)); - } + /** + *

+ * Constructor for TimeoutCommand. + *

+ * + * @param timeout + * value between 0 and 255 that multiplied by 4 results in the + * desired timeout in milliseconds (ms). + */ + public TimeoutCommand(int timeout) { + super("AT ST " + Integer.toHexString(0xFF & timeout)); + } - /** - *

Constructor for TimeoutCommand.

- * - * @param other a {@link com.github.pires.obd.commands.protocol.TimeoutCommand} object. - */ - public TimeoutCommand(TimeoutCommand other) { - super(other); - } + /** + *

+ * Constructor for TimeoutCommand. + *

+ * + * @param other + * a {@link com.github.pires.obd.commands.protocol.TimeoutCommand} + * object. + */ + public TimeoutCommand(TimeoutCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getFormattedResult() { - return getResult(); - } + /** {@inheritDoc} */ + @Override + public String getFormattedResult() { + return getResult(); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return "Timeout"; - } + /** {@inheritDoc} */ + @Override + public String getName() { + return "Timeout"; + } } diff --git a/src/main/java/com/github/pires/obd/commands/temperature/AirIntakeTemperatureCommand.java b/src/main/java/com/github/pires/obd/commands/temperature/AirIntakeTemperatureCommand.java index 13f35268..21cc2884 100644 --- a/src/main/java/com/github/pires/obd/commands/temperature/AirIntakeTemperatureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/temperature/AirIntakeTemperatureCommand.java @@ -20,26 +20,33 @@ */ public class AirIntakeTemperatureCommand extends TemperatureCommand { - /** - *

Constructor for AirIntakeTemperatureCommand.

- */ - public AirIntakeTemperatureCommand() { - super("01 0F"); - } + /** + *

+ * Constructor for AirIntakeTemperatureCommand. + *

+ */ + public AirIntakeTemperatureCommand() { + super("01 0F"); + } - /** - *

Constructor for AirIntakeTemperatureCommand.

- * - * @param other a {@link com.github.pires.obd.commands.temperature.AirIntakeTemperatureCommand} object. - */ - public AirIntakeTemperatureCommand(AirIntakeTemperatureCommand other) { - super(other); - } + /** + *

+ * Constructor for AirIntakeTemperatureCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.temperature.AirIntakeTemperatureCommand} + * object. + */ + public AirIntakeTemperatureCommand(AirIntakeTemperatureCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.AIR_INTAKE_TEMP.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.AIR_INTAKE_TEMP.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/temperature/AmbientAirTemperatureCommand.java b/src/main/java/com/github/pires/obd/commands/temperature/AmbientAirTemperatureCommand.java index f7b7a6d7..2a3720f1 100644 --- a/src/main/java/com/github/pires/obd/commands/temperature/AmbientAirTemperatureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/temperature/AmbientAirTemperatureCommand.java @@ -20,26 +20,33 @@ */ public class AmbientAirTemperatureCommand extends TemperatureCommand { - /** - *

Constructor for AmbientAirTemperatureCommand.

- */ - public AmbientAirTemperatureCommand() { - super("01 46"); - } + /** + *

+ * Constructor for AmbientAirTemperatureCommand. + *

+ */ + public AmbientAirTemperatureCommand() { + super("01 46"); + } - /** - *

Constructor for AmbientAirTemperatureCommand.

- * - * @param other a {@link com.github.pires.obd.commands.temperature.TemperatureCommand} object. - */ - public AmbientAirTemperatureCommand(TemperatureCommand other) { - super(other); - } + /** + *

+ * Constructor for AmbientAirTemperatureCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.temperature.TemperatureCommand} + * object. + */ + public AmbientAirTemperatureCommand(TemperatureCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.AMBIENT_AIR_TEMP.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.AMBIENT_AIR_TEMP.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/temperature/EngineCoolantTemperatureCommand.java b/src/main/java/com/github/pires/obd/commands/temperature/EngineCoolantTemperatureCommand.java index a671d36f..fedbad66 100644 --- a/src/main/java/com/github/pires/obd/commands/temperature/EngineCoolantTemperatureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/temperature/EngineCoolantTemperatureCommand.java @@ -20,26 +20,33 @@ */ public class EngineCoolantTemperatureCommand extends TemperatureCommand { - /** - *

Constructor for EngineCoolantTemperatureCommand.

- */ - public EngineCoolantTemperatureCommand() { - super("01 05"); - } + /** + *

+ * Constructor for EngineCoolantTemperatureCommand. + *

+ */ + public EngineCoolantTemperatureCommand() { + super("01 05"); + } - /** - *

Constructor for EngineCoolantTemperatureCommand.

- * - * @param other a {@link com.github.pires.obd.commands.temperature.TemperatureCommand} object. - */ - public EngineCoolantTemperatureCommand(TemperatureCommand other) { - super(other); - } + /** + *

+ * Constructor for EngineCoolantTemperatureCommand. + *

+ * + * @param other + * a + * {@link com.github.pires.obd.commands.temperature.TemperatureCommand} + * object. + */ + public EngineCoolantTemperatureCommand(TemperatureCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - public String getName() { - return AvailableCommandNames.ENGINE_COOLANT_TEMP.getValue(); - } + /** {@inheritDoc} */ + @Override + public String getName() { + return AvailableCommandNames.ENGINE_COOLANT_TEMP.getValue(); + } } diff --git a/src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java b/src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java index 29f53420..156603f5 100644 --- a/src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java @@ -19,93 +19,102 @@ * Abstract temperature command. * */ -public abstract class TemperatureCommand extends ObdCommand implements - SystemOfUnits { +public abstract class TemperatureCommand extends ObdCommand implements SystemOfUnits { - private float temperature = 0.0f; + private float temperature = 0.0f; - /** - * Default ctor. - * - * @param cmd a {@link java.lang.String} object. - */ - public TemperatureCommand(String cmd) { - super(cmd); - } + /** + * Default ctor. + * + * @param cmd + * a {@link java.lang.String} object. + */ + public TemperatureCommand(String cmd) { + super(cmd); + } - /** - * Copy ctor. - * - * @param other a {@link com.github.pires.obd.commands.temperature.TemperatureCommand} object. - */ - public TemperatureCommand(TemperatureCommand other) { - super(other); - } + /** + * Copy ctor. + * + * @param other + * a + * {@link com.github.pires.obd.commands.temperature.TemperatureCommand} + * object. + */ + public TemperatureCommand(TemperatureCommand other) { + super(other); + } - /** {@inheritDoc} */ - @Override - protected void performCalculations() { - // ignore first two bytes [hh hh] of the response - temperature = buffer.get(2) - 40; - } + /** {@inheritDoc} */ + @Override + protected void performCalculations() { + // ignore first two bytes [hh hh] of the response + temperature = buffer.get(2) - 40; + } + /** + * {@inheritDoc} + *

+ * Get values from 'buff', since we can't rely on char/string for calculations. + */ + @Override + public String getFormattedResult() { + return useImperialUnits ? String.format("%.1f%s", getImperialUnit(), getResultUnit()) + : String.format("%.0f%s", temperature, getResultUnit()); + } - /** - * {@inheritDoc} - *

- * Get values from 'buff', since we can't rely on char/string for - * calculations. - */ - @Override - public String getFormattedResult() { - return useImperialUnits ? String.format("%.1f%s", getImperialUnit(), getResultUnit()) - : String.format("%.0f%s", temperature, getResultUnit()); - } + /** {@inheritDoc} */ + @Override + public String getCalculatedResult() { + return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(temperature); + } - /** {@inheritDoc} */ - @Override - public String getCalculatedResult() { - return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(temperature); - } + /** {@inheritDoc} */ + @Override + public String getResultUnit() { + return useImperialUnits ? "F" : "C"; + } - /** {@inheritDoc} */ - @Override - public String getResultUnit() { - return useImperialUnits ? "F" : "C"; - } + /** + *

+ * Getter for the field temperature. + *

+ * + * @return the temperature in Celsius. + */ + public float getTemperature() { + return temperature; + } - /** - *

Getter for the field temperature.

- * - * @return the temperature in Celsius. - */ - public float getTemperature() { - return temperature; - } + /** + *

+ * getImperialUnit. + *

+ * + * @return the temperature in Fahrenheit. + */ + public float getImperialUnit() { + return temperature * 1.8f + 32; + } - /** - *

getImperialUnit.

- * - * @return the temperature in Fahrenheit. - */ - public float getImperialUnit() { - return temperature * 1.8f + 32; - } + /** + *

+ * getKelvin. + *

+ * + * @return the temperature in Kelvin. + */ + public float getKelvin() { + return temperature + 273.15f; + } - /** - *

getKelvin.

- * - * @return the temperature in Kelvin. - */ - public float getKelvin() { - return temperature + 273.15f; - } - - /** - *

getName.

- * - * @return the OBD command name. - */ - public abstract String getName(); + /** + *

+ * getName. + *

+ * + * @return the OBD command name. + */ + public abstract String getName(); } diff --git a/src/main/java/com/github/pires/obd/enums/AvailableCommandNames.java b/src/main/java/com/github/pires/obd/enums/AvailableCommandNames.java index a1971cdd..4c2ac14e 100644 --- a/src/main/java/com/github/pires/obd/enums/AvailableCommandNames.java +++ b/src/main/java/com/github/pires/obd/enums/AvailableCommandNames.java @@ -18,64 +18,59 @@ */ public enum AvailableCommandNames { - AIR_INTAKE_TEMP("Air Intake Temperature"), - AMBIENT_AIR_TEMP("Ambient Air Temperature"), - ENGINE_COOLANT_TEMP("Engine Coolant Temperature"), - BAROMETRIC_PRESSURE("Barometric Pressure"), - FUEL_PRESSURE("Fuel Pressure"), - INTAKE_MANIFOLD_PRESSURE("Intake Manifold Pressure"), - ENGINE_LOAD("Engine Load"), - ENGINE_RUNTIME("Engine Runtime"), - ENGINE_RPM("Engine RPM"), - SPEED("Vehicle Speed"), - MAF("Mass Air Flow"), - THROTTLE_POS("Throttle Position"), - TROUBLE_CODES("Trouble Codes"), - PENDING_TROUBLE_CODES("Pending Trouble Codes"), - PERMANENT_TROUBLE_CODES("Permanent Trouble Codes"), - FUEL_LEVEL("Fuel Level"), - FUEL_TYPE("Fuel Type"), - FUEL_CONSUMPTION_RATE("Fuel Consumption Rate"), - TIMING_ADVANCE("Timing Advance"), - DTC_NUMBER("Diagnostic Trouble Codes"), - EQUIV_RATIO("Command Equivalence Ratio"), - DISTANCE_TRAVELED_AFTER_CODES_CLEARED("Distance since codes cleared"), - CONTROL_MODULE_VOLTAGE("Control Module Power Supply "), - ENGINE_FUEL_RATE("Engine Fuel Rate"), - FUEL_RAIL_PRESSURE("Fuel Rail Pressure"), - VIN("Vehicle Identification Number (VIN)"), - DISTANCE_TRAVELED_MIL_ON("Distance traveled with MIL on"), - TIME_TRAVELED_MIL_ON("Time run with MIL on"), - TIME_SINCE_TC_CLEARED("Time since trouble codes cleared"), - REL_THROTTLE_POS("Relative throttle position"), - PIDS_01_20("Available PIDs 01-20"), - PIDS_21_40("Available PIDs 21-40"), - PIDS_41_60("Available PIDs 41-60"), - ABS_LOAD("Absolute load"), - ENGINE_OIL_TEMP("Engine oil temperature"), - AIR_FUEL_RATIO("Air/Fuel Ratio"), - WIDEBAND_AIR_FUEL_RATIO("Wideband Air/Fuel Ratio"), - DESCRIBE_PROTOCOL("Describe protocol"), - DESCRIBE_PROTOCOL_NUMBER("Describe protocol number"), - IGNITION_MONITOR("Ignition monitor") - ; + AIR_INTAKE_TEMP("Air Intake Temperature"), AMBIENT_AIR_TEMP("Ambient Air Temperature"), ENGINE_COOLANT_TEMP( + "Engine Coolant Temperature"), BAROMETRIC_PRESSURE("Barometric Pressure"), FUEL_PRESSURE( + "Fuel Pressure"), INTAKE_MANIFOLD_PRESSURE("Intake Manifold Pressure"), ENGINE_LOAD( + "Engine Load"), ENGINE_RUNTIME("Engine Runtime"), ENGINE_RPM("Engine RPM"), SPEED( + "Vehicle Speed"), MAF("Mass Air Flow"), THROTTLE_POS( + "Throttle Position"), TROUBLE_CODES("Trouble Codes"), PENDING_TROUBLE_CODES( + "Pending Trouble Codes"), PERMANENT_TROUBLE_CODES( + "Permanent Trouble Codes"), FUEL_LEVEL( + "Fuel Level"), FUEL_TYPE( + "Fuel Type"), FUEL_CONSUMPTION_RATE( + "Fuel Consumption Rate"), TIMING_ADVANCE( + "Timing Advance"), DTC_NUMBER( + "Diagnostic Trouble Codes"), EQUIV_RATIO( + "Command Equivalence Ratio"), DISTANCE_TRAVELED_AFTER_CODES_CLEARED( + "Distance since codes cleared"), CONTROL_MODULE_VOLTAGE( + "Control Module Power Supply "), ENGINE_FUEL_RATE( + "Engine Fuel Rate"), FUEL_RAIL_PRESSURE( + "Fuel Rail Pressure"), VIN( + "Vehicle Identification Number (VIN)"), DISTANCE_TRAVELED_MIL_ON( + "Distance traveled with MIL on"), TIME_TRAVELED_MIL_ON( + "Time run with MIL on"), TIME_SINCE_TC_CLEARED( + "Time since trouble codes cleared"), REL_THROTTLE_POS( + "Relative throttle position"), PIDS_01_20( + "Available PIDs 01-20"), PIDS_21_40( + "Available PIDs 21-40"), PIDS_41_60( + "Available PIDs 41-60"), ABS_LOAD( + "Absolute load"), ENGINE_OIL_TEMP( + "Engine oil temperature"), AIR_FUEL_RATIO( + "Air/Fuel Ratio"), WIDEBAND_AIR_FUEL_RATIO( + "Wideband Air/Fuel Ratio"), DESCRIBE_PROTOCOL( + "Describe protocol"), DESCRIBE_PROTOCOL_NUMBER( + "Describe protocol number"), IGNITION_MONITOR( + "Ignition monitor"); - private final String value; + private final String value; - /** - * @param value Command description - */ - AvailableCommandNames(String value) { - this.value = value; - } + /** + * @param value + * Command description + */ + AvailableCommandNames(String value) { + this.value = value; + } - /** - *

Getter for the field value.

- * - * @return a {@link java.lang.String} object. - */ - public final String getValue() { - return value; - } + /** + *

+ * Getter for the field value. + *

+ * + * @return a {@link java.lang.String} object. + */ + public final String getValue() { + return value; + } } diff --git a/src/main/java/com/github/pires/obd/enums/FuelTrim.java b/src/main/java/com/github/pires/obd/enums/FuelTrim.java index ad147e1c..ad0b2a5a 100644 --- a/src/main/java/com/github/pires/obd/enums/FuelTrim.java +++ b/src/main/java/com/github/pires/obd/enums/FuelTrim.java @@ -21,62 +21,70 @@ */ public enum FuelTrim { - SHORT_TERM_BANK_1(0x06, "Short Term Fuel Trim Bank 1"), - LONG_TERM_BANK_1(0x07, "Long Term Fuel Trim Bank 1"), - SHORT_TERM_BANK_2(0x08, "Short Term Fuel Trim Bank 2"), - LONG_TERM_BANK_2(0x09, "Long Term Fuel Trim Bank 2"); + SHORT_TERM_BANK_1(0x06, "Short Term Fuel Trim Bank 1"), LONG_TERM_BANK_1(0x07, + "Long Term Fuel Trim Bank 1"), SHORT_TERM_BANK_2(0x08, + "Short Term Fuel Trim Bank 2"), LONG_TERM_BANK_2(0x09, "Long Term Fuel Trim Bank 2"); - /** Constant map */ - private static Map map = new HashMap<>(); + /** Constant map */ + private static Map map = new HashMap<>(); - static { - for (FuelTrim error : FuelTrim.values()) - map.put(error.getValue(), error); - } + static { + for (FuelTrim error : FuelTrim.values()) + map.put(error.getValue(), error); + } - private final int value; - private final String bank; + private final int value; + private final String bank; - private FuelTrim(final int value, final String bank) { - this.value = value; - this.bank = bank; - } + private FuelTrim(final int value, final String bank) { + this.value = value; + this.bank = bank; + } - /** - *

fromValue.

- * - * @param value a int. - * @return a {@link com.github.pires.obd.enums.FuelTrim} object. - */ - public static FuelTrim fromValue(final int value) { - return map.get(value); - } + /** + *

+ * fromValue. + *

+ * + * @param value + * a int. + * @return a {@link com.github.pires.obd.enums.FuelTrim} object. + */ + public static FuelTrim fromValue(final int value) { + return map.get(value); + } - /** - *

Getter for the field value.

- * - * @return a int. - */ - public int getValue() { - return value; - } + /** + *

+ * Getter for the field value. + *

+ * + * @return a int. + */ + public int getValue() { + return value; + } - /** - *

Getter for the field bank.

- * - * @return a {@link java.lang.String} object. - */ - public String getBank() { - return bank; - } + /** + *

+ * Getter for the field bank. + *

+ * + * @return a {@link java.lang.String} object. + */ + public String getBank() { + return bank; + } - /** - *

buildObdCommand.

- * - * @return a {@link java.lang.String} object. - */ - public final String buildObdCommand() { - return new String("01 0" + value); - } + /** + *

+ * buildObdCommand. + *

+ * + * @return a {@link java.lang.String} object. + */ + public final String buildObdCommand() { + return new String("01 0" + value); + } } diff --git a/src/main/java/com/github/pires/obd/enums/FuelType.java b/src/main/java/com/github/pires/obd/enums/FuelType.java index 18ea8ed3..ecab25d4 100644 --- a/src/main/java/com/github/pires/obd/enums/FuelType.java +++ b/src/main/java/com/github/pires/obd/enums/FuelType.java @@ -21,71 +21,73 @@ * */ public enum FuelType { - GASOLINE(0x01, "Gasoline"), - METHANOL(0x02, "Methanol"), - ETHANOL(0x03, "Ethanol"), - DIESEL(0x04, "Diesel"), - LPG(0x05, "GPL/LGP"), - CNG(0x06, "Natural Gas"), - PROPANE(0x07, "Propane"), - ELECTRIC(0x08, "Electric"), - BIFUEL_GASOLINE(0x09, "Biodiesel + Gasoline"), - BIFUEL_METHANOL(0x0A, "Biodiesel + Methanol"), - BIFUEL_ETHANOL(0x0B, "Biodiesel + Ethanol"), - BIFUEL_LPG(0x0C, "Biodiesel + GPL/LGP"), - BIFUEL_CNG(0x0D, "Biodiesel + Natural Gas"), - BIFUEL_PROPANE(0x0E, "Biodiesel + Propane"), - BIFUEL_ELECTRIC(0x0F, "Biodiesel + Electric"), - BIFUEL_GASOLINE_ELECTRIC(0x10, "Biodiesel + Gasoline/Electric"), - HYBRID_GASOLINE(0x11, "Hybrid Gasoline"), - HYBRID_ETHANOL(0x12, "Hybrid Ethanol"), - HYBRID_DIESEL(0x13, "Hybrid Diesel"), - HYBRID_ELECTRIC(0x14, "Hybrid Electric"), - HYBRID_MIXED(0x15, "Hybrid Mixed"), - HYBRID_REGENERATIVE(0x16, "Hybrid Regenerative"); + GASOLINE(0x01, "Gasoline"), METHANOL(0x02, "Methanol"), ETHANOL(0x03, "Ethanol"), DIESEL(0x04, "Diesel"), LPG(0x05, + "GPL/LGP"), CNG(0x06, "Natural Gas"), PROPANE(0x07, "Propane"), ELECTRIC(0x08, "Electric"), BIFUEL_GASOLINE( + 0x09, "Biodiesel + Gasoline"), BIFUEL_METHANOL(0x0A, "Biodiesel + Methanol"), BIFUEL_ETHANOL(0x0B, + "Biodiesel + Ethanol"), BIFUEL_LPG(0x0C, "Biodiesel + GPL/LGP"), BIFUEL_CNG(0x0D, + "Biodiesel + Natural Gas"), BIFUEL_PROPANE(0x0E, + "Biodiesel + Propane"), BIFUEL_ELECTRIC(0x0F, + "Biodiesel + Electric"), BIFUEL_GASOLINE_ELECTRIC(0x10, + "Biodiesel + Gasoline/Electric"), HYBRID_GASOLINE(0x11, + "Hybrid Gasoline"), HYBRID_ETHANOL(0x12, + "Hybrid Ethanol"), HYBRID_DIESEL(0x13, + "Hybrid Diesel"), HYBRID_ELECTRIC( + 0x14, + "Hybrid Electric"), HYBRID_MIXED( + 0x15, + "Hybrid Mixed"), HYBRID_REGENERATIVE( + 0x16, + "Hybrid Regenerative"); - /** Constant map */ - private static Map map = new HashMap<>(); + /** Constant map */ + private static Map map = new HashMap<>(); - static { - for (FuelType error : FuelType.values()) - map.put(error.getValue(), error); - } + static { + for (FuelType error : FuelType.values()) + map.put(error.getValue(), error); + } - private final int value; - private final String description; + private final int value; + private final String description; - private FuelType(final int value, final String description) { - this.value = value; - this.description = description; - } + private FuelType(final int value, final String description) { + this.value = value; + this.description = description; + } - /** - *

fromValue.

- * - * @param value a int. - * @return a {@link com.github.pires.obd.enums.FuelType} object. - */ - public static FuelType fromValue(final int value) { - return map.get(value); - } + /** + *

+ * fromValue. + *

+ * + * @param value + * a int. + * @return a {@link com.github.pires.obd.enums.FuelType} object. + */ + public static FuelType fromValue(final int value) { + return map.get(value); + } - /** - *

Getter for the field value.

- * - * @return a int. - */ - public int getValue() { - return value; - } + /** + *

+ * Getter for the field value. + *

+ * + * @return a int. + */ + public int getValue() { + return value; + } - /** - *

Getter for the field description.

- * - * @return a {@link java.lang.String} object. - */ - public String getDescription() { - return description; - } + /** + *

+ * Getter for the field description. + *

+ * + * @return a {@link java.lang.String} object. + */ + public String getDescription() { + return description; + } } diff --git a/src/main/java/com/github/pires/obd/enums/ObdProtocols.java b/src/main/java/com/github/pires/obd/enums/ObdProtocols.java index 16262d92..08e10f81 100644 --- a/src/main/java/com/github/pires/obd/enums/ObdProtocols.java +++ b/src/main/java/com/github/pires/obd/enums/ObdProtocols.java @@ -18,83 +18,85 @@ */ public enum ObdProtocols { - /** - * Auto select protocol and save. - */ - AUTO('0'), - - /** - * 41.6 kbaud - */ - SAE_J1850_PWM('1'), - - /** - * 10.4 kbaud - */ - SAE_J1850_VPW('2'), - - /** - * 5 baud init - */ - ISO_9141_2('3'), - - /** - * 5 baud init - */ - ISO_14230_4_KWP('4'), - - /** - * Fast init - */ - ISO_14230_4_KWP_FAST('5'), - - /** - * 11 bit ID, 500 kbaud - */ - ISO_15765_4_CAN('6'), - - /** - * 29 bit ID, 500 kbaud - */ - ISO_15765_4_CAN_B('7'), - - /** - * 11 bit ID, 250 kbaud - */ - ISO_15765_4_CAN_C('8'), - - /** - * 29 bit ID, 250 kbaud - */ - ISO_15765_4_CAN_D('9'), - - /** - * 29 bit ID, 250 kbaud (user adjustable) - */ - SAE_J1939_CAN('A'), - - /** - * 11 bit ID (user adjustable), 125 kbaud (user adjustable) - */ - USER1_CAN('B'), - - /** - * 11 bit ID (user adjustable), 50 kbaud (user adjustable) - */ - USER2_CAN('C'); - - private final char value; - - ObdProtocols(char value) { - this.value = value; - } - - /** - *

Getter for the field value.

- * - * @return a char. - */ - public char getValue() { - return value; - } + /** + * Auto select protocol and save. + */ + AUTO('0'), + + /** + * 41.6 kbaud + */ + SAE_J1850_PWM('1'), + + /** + * 10.4 kbaud + */ + SAE_J1850_VPW('2'), + + /** + * 5 baud init + */ + ISO_9141_2('3'), + + /** + * 5 baud init + */ + ISO_14230_4_KWP('4'), + + /** + * Fast init + */ + ISO_14230_4_KWP_FAST('5'), + + /** + * 11 bit ID, 500 kbaud + */ + ISO_15765_4_CAN('6'), + + /** + * 29 bit ID, 500 kbaud + */ + ISO_15765_4_CAN_B('7'), + + /** + * 11 bit ID, 250 kbaud + */ + ISO_15765_4_CAN_C('8'), + + /** + * 29 bit ID, 250 kbaud + */ + ISO_15765_4_CAN_D('9'), + + /** + * 29 bit ID, 250 kbaud (user adjustable) + */ + SAE_J1939_CAN('A'), + + /** + * 11 bit ID (user adjustable), 125 kbaud (user adjustable) + */ + USER1_CAN('B'), + + /** + * 11 bit ID (user adjustable), 50 kbaud (user adjustable) + */ + USER2_CAN('C'); + + private final char value; + + ObdProtocols(char value) { + this.value = value; + } + + /** + *

+ * Getter for the field value. + *

+ * + * @return a char. + */ + public char getValue() { + return value; + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/BusInitException.java b/src/main/java/com/github/pires/obd/exceptions/BusInitException.java index e186ffb1..1a324caf 100644 --- a/src/main/java/com/github/pires/obd/exceptions/BusInitException.java +++ b/src/main/java/com/github/pires/obd/exceptions/BusInitException.java @@ -18,11 +18,18 @@ */ public class BusInitException extends ResponseException { - /** - *

Constructor for BusInitException.

- */ - public BusInitException() { - super("BUS INIT... ERROR"); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Constructor for BusInitException. + *

+ */ + public BusInitException() { + super("BUS INIT... ERROR"); + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/MisunderstoodCommandException.java b/src/main/java/com/github/pires/obd/exceptions/MisunderstoodCommandException.java index 41ca381e..3332022c 100644 --- a/src/main/java/com/github/pires/obd/exceptions/MisunderstoodCommandException.java +++ b/src/main/java/com/github/pires/obd/exceptions/MisunderstoodCommandException.java @@ -18,11 +18,18 @@ */ public class MisunderstoodCommandException extends ResponseException { - /** - *

Constructor for MisunderstoodCommandException.

- */ - public MisunderstoodCommandException() { - super("?"); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Constructor for MisunderstoodCommandException. + *

+ */ + public MisunderstoodCommandException() { + super("?"); + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/NoDataException.java b/src/main/java/com/github/pires/obd/exceptions/NoDataException.java index ea0fae12..c291afe3 100644 --- a/src/main/java/com/github/pires/obd/exceptions/NoDataException.java +++ b/src/main/java/com/github/pires/obd/exceptions/NoDataException.java @@ -18,11 +18,18 @@ */ public class NoDataException extends ResponseException { - /** - *

Constructor for NoDataException.

- */ - public NoDataException() { - super("NO DATA"); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Constructor for NoDataException. + *

+ */ + public NoDataException() { + super("NO DATA"); + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/NonNumericResponseException.java b/src/main/java/com/github/pires/obd/exceptions/NonNumericResponseException.java index eec45468..9acea22e 100644 --- a/src/main/java/com/github/pires/obd/exceptions/NonNumericResponseException.java +++ b/src/main/java/com/github/pires/obd/exceptions/NonNumericResponseException.java @@ -18,13 +18,21 @@ */ public class NonNumericResponseException extends RuntimeException { - /** - *

Constructor for NonNumericResponseException.

- * - * @param message a {@link java.lang.String} object. - */ - public NonNumericResponseException(String message) { - super("Error reading response: " + message); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Constructor for NonNumericResponseException. + *

+ * + * @param message + * a {@link java.lang.String} object. + */ + public NonNumericResponseException(String message) { + super("Error reading response: " + message); + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/ResponseException.java b/src/main/java/com/github/pires/obd/exceptions/ResponseException.java index 7848f93d..566aebc5 100644 --- a/src/main/java/com/github/pires/obd/exceptions/ResponseException.java +++ b/src/main/java/com/github/pires/obd/exceptions/ResponseException.java @@ -18,66 +18,84 @@ */ public class ResponseException extends RuntimeException { - private String message; + /** + * + */ + private static final long serialVersionUID = 1L; - private String response; + private String message; - private String command; + private String response; - private boolean matchRegex; + private String command; - /** - *

Constructor for ResponseException.

- * - * @param message a {@link java.lang.String} object. - */ - protected ResponseException(String message) { - this.message = message; - } + private boolean matchRegex; - /** - *

Constructor for ResponseException.

- * - * @param message a {@link java.lang.String} object. - * @param matchRegex a boolean. - */ - protected ResponseException(String message, boolean matchRegex) { - this.message = message; - this.matchRegex = matchRegex; - } + /** + *

+ * Constructor for ResponseException. + *

+ * + * @param message + * a {@link java.lang.String} object. + */ + protected ResponseException(String message) { + this.message = message; + } - private static String clean(String s) { - return s == null ? "" : s.replaceAll("\\s", "").toUpperCase(); - } + /** + *

+ * Constructor for ResponseException. + *

+ * + * @param message + * a {@link java.lang.String} object. + * @param matchRegex + * a boolean. + */ + protected ResponseException(String message, boolean matchRegex) { + this.message = message; + this.matchRegex = matchRegex; + } - /** - *

isError.

- * - * @param response a {@link java.lang.String} object. - * @return a boolean. - */ - public boolean isError(String response) { - this.response = response; - if (matchRegex) { - return clean(response).matches(clean(message)); - } else { - return clean(response).contains(clean(message)); - } - } + private static String clean(String s) { + return s == null ? "" : s.replaceAll("\\s", "").toUpperCase(); + } - /** - *

Setter for the field command.

- * - * @param command a {@link java.lang.String} object. - */ - public void setCommand(String command) { - this.command = command; - } + /** + *

+ * isError. + *

+ * + * @param response + * a {@link java.lang.String} object. + * @return a boolean. + */ + public boolean isError(String response) { + this.response = response; + if (matchRegex) { + return clean(response).matches(clean(message)); + } else { + return clean(response).contains(clean(message)); + } + } - /** {@inheritDoc} */ - @Override - public String getMessage() { - return "Error running " + command + ", response: " + response; - } + /** + *

+ * Setter for the field command. + *

+ * + * @param command + * a {@link java.lang.String} object. + */ + public void setCommand(String command) { + this.command = command; + } + + /** {@inheritDoc} */ + @Override + public String getMessage() { + return "Error running " + command + ", response: " + response; + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/StoppedException.java b/src/main/java/com/github/pires/obd/exceptions/StoppedException.java index e4725414..d203b33b 100644 --- a/src/main/java/com/github/pires/obd/exceptions/StoppedException.java +++ b/src/main/java/com/github/pires/obd/exceptions/StoppedException.java @@ -18,11 +18,18 @@ */ public class StoppedException extends ResponseException { - /** - *

Constructor for StoppedException.

- */ - public StoppedException() { - super("STOPPED"); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Constructor for StoppedException. + *

+ */ + public StoppedException() { + super("STOPPED"); + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/UnableToConnectException.java b/src/main/java/com/github/pires/obd/exceptions/UnableToConnectException.java index eb5d5a4e..00ad63a8 100644 --- a/src/main/java/com/github/pires/obd/exceptions/UnableToConnectException.java +++ b/src/main/java/com/github/pires/obd/exceptions/UnableToConnectException.java @@ -18,11 +18,18 @@ */ public class UnableToConnectException extends ResponseException { - /** - *

Constructor for UnableToConnectException.

- */ - public UnableToConnectException() { - super("UNABLE TO CONNECT"); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Constructor for UnableToConnectException. + *

+ */ + public UnableToConnectException() { + super("UNABLE TO CONNECT"); + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/UnknownErrorException.java b/src/main/java/com/github/pires/obd/exceptions/UnknownErrorException.java index 4f2f29b1..3b2ca594 100644 --- a/src/main/java/com/github/pires/obd/exceptions/UnknownErrorException.java +++ b/src/main/java/com/github/pires/obd/exceptions/UnknownErrorException.java @@ -18,11 +18,18 @@ */ public class UnknownErrorException extends ResponseException { - /** - *

Constructor for UnknownErrorException.

- */ - public UnknownErrorException() { - super("ERROR"); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Constructor for UnknownErrorException. + *

+ */ + public UnknownErrorException() { + super("ERROR"); + } } diff --git a/src/main/java/com/github/pires/obd/exceptions/UnsupportedCommandException.java b/src/main/java/com/github/pires/obd/exceptions/UnsupportedCommandException.java index 97c49477..c30d1874 100644 --- a/src/main/java/com/github/pires/obd/exceptions/UnsupportedCommandException.java +++ b/src/main/java/com/github/pires/obd/exceptions/UnsupportedCommandException.java @@ -18,11 +18,18 @@ */ public class UnsupportedCommandException extends ResponseException { - /** - *

Constructor for UnsupportedCommandException.

- */ - public UnsupportedCommandException() { - super("7F 0[0-A] 1[1-2]", true); - } + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + *

+ * Constructor for UnsupportedCommandException. + *

+ */ + public UnsupportedCommandException() { + super("7F 0[0-A] 1[1-2]", true); + } } diff --git a/src/main/java/com/github/pires/obd/utils/CommandAvailabilityHelper.java b/src/main/java/com/github/pires/obd/utils/CommandAvailabilityHelper.java index 6a02ad45..ce9e0697 100644 --- a/src/main/java/com/github/pires/obd/utils/CommandAvailabilityHelper.java +++ b/src/main/java/com/github/pires/obd/utils/CommandAvailabilityHelper.java @@ -13,200 +13,234 @@ package com.github.pires.obd.utils; /** - *

Abstract CommandAvailabilityHelper class.

+ *

+ * Abstract CommandAvailabilityHelper class. + *

* * @since 1.0-RC12 */ public abstract class CommandAvailabilityHelper { - /* - All around this class, integers are used where unsigned bytes should be used. Too bad they don't exist in Java. - Thank you Oracle. - */ - - /** - * Digests the given string into an array of integers which can be used to check for command availability - * - * @param availabilityString An 8*n (where n is an integer) character string containing only numbers and uppercase letters from A to F - * @return An integer array containing the digested information - * @throws java.lang.IllegalArgumentException if any. - */ - public static int[] digestAvailabilityString(final String availabilityString) throws IllegalArgumentException { - //The string must have 8*n characters, n being an integer - if (availabilityString.length() % 8 != 0) { - throw new IllegalArgumentException("Invalid length for Availability String supplied: " + availabilityString); - } - - //Each two characters of the string will be digested into one byte, thus the resulting array will - //have half the elements the string has - int[] availabilityArray = new int[availabilityString.length() / 2]; - - for (int i = 0, a = 0; i < availabilityArray.length; ++i, a += 2) { - //First character is more significant - availabilityArray[i] = 16 * parseHexChar(availabilityString.charAt(a)) + parseHexChar(availabilityString.charAt(a + 1)); - } - - return availabilityArray; - } - - private static int parseHexChar(char hexChar) { - switch (hexChar) { - case '0': - return 0; - - case '1': - return 1; - - case '2': - return 2; - - case '3': - return 3; - - case '4': - return 4; - - case '5': - return 5; - - case '6': - return 6; - - case '7': - return 7; - - case '8': - return 8; - - case '9': - return 9; - - case 'A': - return 10; - - case 'B': - return 11; - - case 'C': - return 12; - - case 'D': - return 13; - - case 'E': - return 14; - - case 'F': - return 15; - - default: - throw new IllegalArgumentException("Invalid character [" + hexChar + "] supplied"); - } - } - - /** - * Implementation of {@link #isAvailable(String, int[])} isAvailable} which returns the specified safetyReturn boolean instead of - * throwing and exception in the event of supplying an availabilityString which doesn't include information about the specified command - * - * This is a direct call to {@link #isAvailable(String, int[], boolean)} with built-in String digestion - * - * @param commandPid a {@link java.lang.String} object. - * @param availabilityString a {@link java.lang.String} object. - * @param safetyReturn a boolean. - * @return a boolean. - */ - public static boolean isAvailable(final String commandPid, final String availabilityString, boolean safetyReturn) { - return isAvailable(commandPid, digestAvailabilityString(availabilityString), safetyReturn); - } - - /** - * Checks whether the command identified by commandPid is available, as noted by availabilityString. - * - * This is a direct call to {@link com.github.pires.obd.utils.CommandAvailabilityHelper#isAvailable(String, int[])} with built-in String digestion - * - * @param commandPid a {@link java.lang.String} object. - * @param availabilityString a {@link java.lang.String} object. - * @return a boolean. - * @throws java.lang.IllegalArgumentException if any. - */ - public static boolean isAvailable(final String commandPid, final String availabilityString) throws IllegalArgumentException { - return isAvailable(commandPid, digestAvailabilityString(availabilityString)); - } - - /** - * Implementation of {@link #isAvailable(String, int[])} isAvailable} which returns the specified safetyReturn boolean instead of - * throwing and exception in the event of supplying an availabilityString which doesn't include information about the specified command - * - * @param commandPid a {@link java.lang.String} object. - * @param availabilityArray an array of int. - * @param safetyReturn a boolean. - * @return a boolean. - */ - public static boolean isAvailable(final String commandPid, final int[] availabilityArray, boolean safetyReturn) { - try { - return isAvailable(commandPid, availabilityArray); - } catch (IllegalArgumentException e) { - return safetyReturn; - } - } - - /** - * Checks whether the command identified by commandPid is available, as noted by availabilityArray - * - * @param commandPid a {@link java.lang.String} object. - * @param availabilityArray an array of int. - * @return a boolean. - * @throws java.lang.IllegalArgumentException if any. - */ - public static boolean isAvailable(final String commandPid, final int[] availabilityArray) throws IllegalArgumentException { - - //Command 00 is always supported - if (commandPid.equals("00")) - return true; - - //Which byte from the array contains the info we want? - int cmdNumber = Integer.parseInt(commandPid, 16); - int arrayIndex = (cmdNumber - 1) / 8; //the -1 corrects the command code offset, as 00, 20, 40 are not the first commands in each response to be evaluated - - if (arrayIndex > availabilityArray.length - 1) - throw new IllegalArgumentException("availabilityArray does not contain enough entries to check for command " + commandPid); - - //Subtract 8 from cmdNumber until we have it in the 1-8 range - while (cmdNumber > 8) { - cmdNumber -= 8; - } - - int requestedAvailability; - - switch (cmdNumber) { - case 1: - requestedAvailability = 128; - break; - case 2: - requestedAvailability = 64; - break; - case 3: - requestedAvailability = 32; - break; - case 4: - requestedAvailability = 16; - break; - case 5: - requestedAvailability = 8; - break; - case 6: - requestedAvailability = 4; - break; - case 7: - requestedAvailability = 2; - break; - case 8: - requestedAvailability = 1; - break; - default: - throw new RuntimeException("This is not supposed to happen."); - } - - return requestedAvailability == (requestedAvailability & availabilityArray[arrayIndex]); - } + /* + * All around this class, integers are used where unsigned bytes should be used. + * Too bad they don't exist in Java. Thank you Oracle. + */ + + /** + * Digests the given string into an array of integers which can be used to check + * for command availability + * + * @param availabilityString + * An 8*n (where n is an integer) character string containing only + * numbers and uppercase letters from A to F + * @return An integer array containing the digested information + * @throws java.lang.IllegalArgumentException + * if any. + */ + public static int[] digestAvailabilityString(final String availabilityString) throws IllegalArgumentException { + // The string must have 8*n characters, n being an integer + if (availabilityString.length() % 8 != 0) { + throw new IllegalArgumentException( + "Invalid length for Availability String supplied: " + availabilityString); + } + + // Each two characters of the string will be digested into one byte, thus the + // resulting array will + // have half the elements the string has + int[] availabilityArray = new int[availabilityString.length() / 2]; + + for (int i = 0, a = 0; i < availabilityArray.length; ++i, a += 2) { + // First character is more significant + availabilityArray[i] = 16 * parseHexChar(availabilityString.charAt(a)) + + parseHexChar(availabilityString.charAt(a + 1)); + } + + return availabilityArray; + } + + private static int parseHexChar(char hexChar) { + switch (hexChar) { + case '0': + return 0; + + case '1': + return 1; + + case '2': + return 2; + + case '3': + return 3; + + case '4': + return 4; + + case '5': + return 5; + + case '6': + return 6; + + case '7': + return 7; + + case '8': + return 8; + + case '9': + return 9; + + case 'A': + return 10; + + case 'B': + return 11; + + case 'C': + return 12; + + case 'D': + return 13; + + case 'E': + return 14; + + case 'F': + return 15; + + default: + throw new IllegalArgumentException("Invalid character [" + hexChar + "] supplied"); + } + } + + /** + * Implementation of {@link #isAvailable(String, int[])} isAvailable} which + * returns the specified safetyReturn boolean instead of throwing and exception + * in the event of supplying an availabilityString which doesn't include + * information about the specified command + * + * This is a direct call to {@link #isAvailable(String, int[], boolean)} with + * built-in String digestion + * + * @param commandPid + * a {@link java.lang.String} object. + * @param availabilityString + * a {@link java.lang.String} object. + * @param safetyReturn + * a boolean. + * @return a boolean. + */ + public static boolean isAvailable(final String commandPid, final String availabilityString, boolean safetyReturn) { + return isAvailable(commandPid, digestAvailabilityString(availabilityString), safetyReturn); + } + + /** + * Checks whether the command identified by commandPid is available, as noted by + * availabilityString. + * + * This is a direct call to + * {@link com.github.pires.obd.utils.CommandAvailabilityHelper#isAvailable(String, int[])} + * with built-in String digestion + * + * @param commandPid + * a {@link java.lang.String} object. + * @param availabilityString + * a {@link java.lang.String} object. + * @return a boolean. + * @throws java.lang.IllegalArgumentException + * if any. + */ + public static boolean isAvailable(final String commandPid, final String availabilityString) + throws IllegalArgumentException { + return isAvailable(commandPid, digestAvailabilityString(availabilityString)); + } + + /** + * Implementation of {@link #isAvailable(String, int[])} isAvailable} which + * returns the specified safetyReturn boolean instead of throwing and exception + * in the event of supplying an availabilityString which doesn't include + * information about the specified command + * + * @param commandPid + * a {@link java.lang.String} object. + * @param availabilityArray + * an array of int. + * @param safetyReturn + * a boolean. + * @return a boolean. + */ + public static boolean isAvailable(final String commandPid, final int[] availabilityArray, boolean safetyReturn) { + try { + return isAvailable(commandPid, availabilityArray); + } catch (IllegalArgumentException e) { + return safetyReturn; + } + } + + /** + * Checks whether the command identified by commandPid is available, as noted by + * availabilityArray + * + * @param commandPid + * a {@link java.lang.String} object. + * @param availabilityArray + * an array of int. + * @return a boolean. + * @throws java.lang.IllegalArgumentException + * if any. + */ + public static boolean isAvailable(final String commandPid, final int[] availabilityArray) + throws IllegalArgumentException { + + // Command 00 is always supported + if (commandPid.equals("00")) + return true; + + // Which byte from the array contains the info we want? + int cmdNumber = Integer.parseInt(commandPid, 16); + int arrayIndex = (cmdNumber - 1) / 8; // the -1 corrects the command code offset, as 00, 20, 40 are not the + // first commands in each response to be evaluated + + if (arrayIndex > availabilityArray.length - 1) + throw new IllegalArgumentException( + "availabilityArray does not contain enough entries to check for command " + commandPid); + + // Subtract 8 from cmdNumber until we have it in the 1-8 range + while (cmdNumber > 8) { + cmdNumber -= 8; + } + + int requestedAvailability; + + switch (cmdNumber) { + case 1: + requestedAvailability = 128; + break; + case 2: + requestedAvailability = 64; + break; + case 3: + requestedAvailability = 32; + break; + case 4: + requestedAvailability = 16; + break; + case 5: + requestedAvailability = 8; + break; + case 6: + requestedAvailability = 4; + break; + case 7: + requestedAvailability = 2; + break; + case 8: + requestedAvailability = 1; + break; + default: + throw new RuntimeException("This is not supposed to happen."); + } + + return requestedAvailability == (requestedAvailability & availabilityArray[arrayIndex]); + } } diff --git a/src/test/java/com/github/pires/obd/commands/AirIntakeTempCommandTest.java b/src/test/java/com/github/pires/obd/commands/AirIntakeTempCommandTest.java index 8c00c7c0..b0c32138 100644 --- a/src/test/java/com/github/pires/obd/commands/AirIntakeTempCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/AirIntakeTempCommandTest.java @@ -30,112 +30,112 @@ @PrepareForTest(InputStream.class) public class AirIntakeTempCommandTest { - private AirIntakeTemperatureCommand command = null; - private InputStream mockIn = null; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new AirIntakeTemperatureCommand(); - } - - /** - * Test for valid InputStream read, 24ºC - * - * @throws IOException - */ - @Test - public void testValidTemperatureCelsius() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getTemperature(), 24f); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 75.2F - * - * @throws IOException - */ - @Test - public void testValidTemperatureFahrenheit() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - command.useImperialUnits = true; - assertEquals(command.getImperialUnit(), 84.2f); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 0ºC - * - * @throws IOException - */ - @Test - public void testValidTemperatureZeroCelsius() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getTemperature(), 0f); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private AirIntakeTemperatureCommand command = null; + private InputStream mockIn = null; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new AirIntakeTemperatureCommand(); + } + + /** + * Test for valid InputStream read, 24ºC + * + * @throws IOException + */ + @Test + public void testValidTemperatureCelsius() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getTemperature(), 24f); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 75.2F + * + * @throws IOException + */ + @Test + public void testValidTemperatureFahrenheit() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + command.useImperialUnits = true; + assertEquals(command.getImperialUnit(), 84.2f); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 0ºC + * + * @throws IOException + */ + @Test + public void testValidTemperatureZeroCelsius() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getTemperature(), 0f); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/DescribeProtocolNumberCommandTest.java b/src/test/java/com/github/pires/obd/commands/DescribeProtocolNumberCommandTest.java index 590f9103..9048df33 100644 --- a/src/test/java/com/github/pires/obd/commands/DescribeProtocolNumberCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/DescribeProtocolNumberCommandTest.java @@ -28,66 +28,66 @@ */ public class DescribeProtocolNumberCommandTest { - private DescribeProtocolNumberCommand command; - private InputStream mockIn; - - @BeforeMethod - public void setUp() throws Exception { - command = new DescribeProtocolNumberCommand(); - } - - @Test - public void testGetCalculatedResult() throws Exception { - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) '>'); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getCalculatedResult(), ObdProtocols.ISO_9141_2.name());//AUTO ISO_9141_2 - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getCalculatedResult(), ObdProtocols.SAE_J1850_VPW.name());//SAE_J1850_VPW - - verifyAll(); - } - - @Test - public void testGetProtocol() throws Exception { - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '6'); - expectLastCall().andReturn((byte) '>'); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getObdProtocol(), ObdProtocols.ISO_15765_4_CAN);//AUTO ISO_15765_4_CAN - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getObdProtocol(), ObdProtocols.ISO_15765_4_CAN_B);//ISO_15765_4_CAN_B - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private DescribeProtocolNumberCommand command; + private InputStream mockIn; + + @BeforeMethod + public void setUp() throws Exception { + command = new DescribeProtocolNumberCommand(); + } + + @Test + public void testGetCalculatedResult() throws Exception { + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) '>'); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getCalculatedResult(), ObdProtocols.ISO_9141_2.name());// AUTO ISO_9141_2 + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getCalculatedResult(), ObdProtocols.SAE_J1850_VPW.name());// SAE_J1850_VPW + + verifyAll(); + } + + @Test + public void testGetProtocol() throws Exception { + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '6'); + expectLastCall().andReturn((byte) '>'); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getObdProtocol(), ObdProtocols.ISO_15765_4_CAN);// AUTO ISO_15765_4_CAN + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getObdProtocol(), ObdProtocols.ISO_15765_4_CAN_B);// ISO_15765_4_CAN_B + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } \ No newline at end of file diff --git a/src/test/java/com/github/pires/obd/commands/DistanceSinceCCCommandTest.java b/src/test/java/com/github/pires/obd/commands/DistanceSinceCCCommandTest.java index c478dc99..f559e24e 100644 --- a/src/test/java/com/github/pires/obd/commands/DistanceSinceCCCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/DistanceSinceCCCommandTest.java @@ -30,120 +30,120 @@ @PrepareForTest(InputStream.class) public class DistanceSinceCCCommandTest { - private DistanceSinceCCCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new DistanceSinceCCCommand(); - } - - /** - * Test for valid InputStream read, 65535 km. - * - * @throws IOException - */ - @Test - public void testMaxDistanceValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getKm(), 65535); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 17731 kms - * - * @throws IOException - */ - @Test - public void testSomeRuntimeValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getKm(), 17731); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 0 km. - * - * @throws IOException - */ - @Test - public void testMinRuntimeValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getKm(), 0); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private DistanceSinceCCCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new DistanceSinceCCCommand(); + } + + /** + * Test for valid InputStream read, 65535 km. + * + * @throws IOException + */ + @Test + public void testMaxDistanceValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getKm(), 65535); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 17731 kms + * + * @throws IOException + */ + @Test + public void testSomeRuntimeValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getKm(), 17731); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 0 km. + * + * @throws IOException + */ + @Test + public void testMinRuntimeValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getKm(), 0); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/DtcNumberCommandTest.java b/src/test/java/com/github/pires/obd/commands/DtcNumberCommandTest.java index 628f0844..a64d7272 100644 --- a/src/test/java/com/github/pires/obd/commands/DtcNumberCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/DtcNumberCommandTest.java @@ -30,88 +30,88 @@ @PrepareForTest(InputStream.class) public class DtcNumberCommandTest { - private DtcNumberCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new DtcNumberCommand(); - } - - /** - * Test for valid InputStream read, MIL on. - * - * @throws IOException - */ - @Test - public void testMILOn() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '9'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - command.getFormattedResult(); - - assertTrue(command.getMilOn()); - assertEquals(command.getTotalAvailableCodes(), 31); - - verifyAll(); - } - - /** - * Test for valid InputStream read, MIL off. - * - * @throws IOException - */ - @Test - public void testMILOff() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - command.getFormattedResult(); - - assertFalse(command.getMilOn()); - assertEquals(command.getTotalAvailableCodes(), 15); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private DtcNumberCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new DtcNumberCommand(); + } + + /** + * Test for valid InputStream read, MIL on. + * + * @throws IOException + */ + @Test + public void testMILOn() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '9'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + command.getFormattedResult(); + + assertTrue(command.getMilOn()); + assertEquals(command.getTotalAvailableCodes(), 31); + + verifyAll(); + } + + /** + * Test for valid InputStream read, MIL off. + * + * @throws IOException + */ + @Test + public void testMILOff() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + command.getFormattedResult(); + + assertFalse(command.getMilOn()); + assertEquals(command.getTotalAvailableCodes(), 15); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/ExtraMessagesTest.java b/src/test/java/com/github/pires/obd/commands/ExtraMessagesTest.java index 42f310d3..d7dccedd 100644 --- a/src/test/java/com/github/pires/obd/commands/ExtraMessagesTest.java +++ b/src/test/java/com/github/pires/obd/commands/ExtraMessagesTest.java @@ -28,87 +28,87 @@ @PrepareForTest(InputStream.class) public class ExtraMessagesTest { - private SpeedCommand command; - private InputStream mockIn; + private SpeedCommand command; + private InputStream mockIn; - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new SpeedCommand(); - } + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new SpeedCommand(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException - */ - @Test - public void testValidSpeedMetricWithMessage() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) 'B'); - expectLastCall().andReturn((byte) 'U'); - expectLastCall().andReturn((byte) 'S'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'I'); - expectLastCall().andReturn((byte) 'N'); - expectLastCall().andReturn((byte) 'I'); - expectLastCall().andReturn((byte) 'T'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException + */ + @Test + public void testValidSpeedMetricWithMessage() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) 'B'); + expectLastCall().andReturn((byte) 'U'); + expectLastCall().andReturn((byte) 'S'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'I'); + expectLastCall().andReturn((byte) 'N'); + expectLastCall().andReturn((byte) 'I'); + expectLastCall().andReturn((byte) 'T'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.readResult(mockIn); - command.getFormattedResult(); - assertEquals(command.getMetricSpeed(), 64); + // call the method to test + command.readResult(mockIn); + command.getFormattedResult(); + assertEquals(command.getMetricSpeed(), 64); - verifyAll(); - } + verifyAll(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException - */ - @Test - public void testValidSpeedMetricWithoutMessage() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException + */ + @Test + public void testValidSpeedMetricWithoutMessage() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.readResult(mockIn); - command.getFormattedResult(); - assertEquals(command.getMetricSpeed(), 64); + // call the method to test + command.readResult(mockIn); + command.getFormattedResult(); + assertEquals(command.getMetricSpeed(), 64); - verifyAll(); - } + verifyAll(); + } } diff --git a/src/test/java/com/github/pires/obd/commands/FindFuelTypeCommandTest.java b/src/test/java/com/github/pires/obd/commands/FindFuelTypeCommandTest.java index 09a927cc..bd3c9cb1 100644 --- a/src/test/java/com/github/pires/obd/commands/FindFuelTypeCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/FindFuelTypeCommandTest.java @@ -28,111 +28,111 @@ */ public class FindFuelTypeCommandTest { - private FindFuelTypeCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new FindFuelTypeCommand(); - } - - /** - * Test for valid InputStream read, Gasoline - * - * @throws IOException - */ - @Test - public void testFindGasoline() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFormattedResult(), "Gasoline"); - - verifyAll(); - } - - /** - * Test for valid InputStream read, Diesel - * - * @throws IOException - */ - @Test - public void testDiesel() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFormattedResult(), "Diesel"); - - verifyAll(); - } - - /** - * Test for valid InputStream read, Ethanol - * - * @throws IOException - */ - @Test - public void testHybridEthanol() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFormattedResult(), "Hybrid Ethanol"); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private FindFuelTypeCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new FindFuelTypeCommand(); + } + + /** + * Test for valid InputStream read, Gasoline + * + * @throws IOException + */ + @Test + public void testFindGasoline() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFormattedResult(), "Gasoline"); + + verifyAll(); + } + + /** + * Test for valid InputStream read, Diesel + * + * @throws IOException + */ + @Test + public void testDiesel() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFormattedResult(), "Diesel"); + + verifyAll(); + } + + /** + * Test for valid InputStream read, Ethanol + * + * @throws IOException + */ + @Test + public void testHybridEthanol() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFormattedResult(), "Hybrid Ethanol"); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/FuelLevelCommandTest.java b/src/test/java/com/github/pires/obd/commands/FuelLevelCommandTest.java index ecb7924f..fc1f866e 100644 --- a/src/test/java/com/github/pires/obd/commands/FuelLevelCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/FuelLevelCommandTest.java @@ -27,111 +27,111 @@ * Tests for FuelLevelCommand class. */ public class FuelLevelCommandTest { - private FuelLevelCommand command = null; - private InputStream mockIn = null; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new FuelLevelCommand(); - } - - /** - * Test for valid InputStream read, full tank - * - * @throws IOException - */ - @Test - public void testFullTank() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFuelLevel(), 100f); - - verifyAll(); - } - - /** - * Test for valid InputStream read. 78.4% - * - * @throws IOException - */ - @Test - public void testSomeValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'C'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFuelLevel(), 78.43137f); - - verifyAll(); - } - - /** - * Test for valid InputStream read, empty tank - * - * @throws IOException - */ - @Test - public void testEmptyTank() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFuelLevel(), 0f); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private FuelLevelCommand command = null; + private InputStream mockIn = null; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new FuelLevelCommand(); + } + + /** + * Test for valid InputStream read, full tank + * + * @throws IOException + */ + @Test + public void testFullTank() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFuelLevel(), 100f); + + verifyAll(); + } + + /** + * Test for valid InputStream read. 78.4% + * + * @throws IOException + */ + @Test + public void testSomeValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'C'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFuelLevel(), 78.43137f); + + verifyAll(); + } + + /** + * Test for valid InputStream read, empty tank + * + * @throws IOException + */ + @Test + public void testEmptyTank() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFuelLevel(), 0f); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/FuelTrimCommandTest.java b/src/test/java/com/github/pires/obd/commands/FuelTrimCommandTest.java index 25070622..f3d64a40 100644 --- a/src/test/java/com/github/pires/obd/commands/FuelTrimCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/FuelTrimCommandTest.java @@ -34,103 +34,103 @@ @PrepareForTest(InputStream.class) public class FuelTrimCommandTest { - private FuelTrimCommand command; - private InputStream mockIn; - private FuelTrim fuelTrimBank = FuelTrim.LONG_TERM_BANK_1; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new FuelTrimCommand(FuelTrim.LONG_TERM_BANK_1); - } - - /** - * Test for valid InputStream read, 99.22% - * - * @throws IOException - */ - @Test(enabled = false) - public void testMaxFuelTrimValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn(0x41); - expectLastCall().andReturn(fuelTrimBank.getValue()); - expectLastCall().andReturn(0xFF); - expectLastCall().andReturn(0x3E); // '>' - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getValue(), 99.22f); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 56.25% - * - * @throws IOException - */ - @Test(enabled = false) - public void testSomeValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn(0x41); - expectLastCall().andReturn(0x20); - expectLastCall().andReturn(fuelTrimBank.getValue()); - expectLastCall().andReturn(0x20); - expectLastCall().andReturn(0xC8); - expectLastCall().andReturn(0x20); - expectLastCall().andReturn(0x3E); // '>' - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getValue(), 56.25f); - - verifyAll(); - } - - /** - * Test for valid InputStream read, -100.00% - * - * @throws IOException - */ - @Test(enabled = false) - public void testMinFuelTrimValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn(0x41); - expectLastCall().andReturn(0x20); - expectLastCall().andReturn(fuelTrimBank.getValue()); - expectLastCall().andReturn(0x20); - expectLastCall().andReturn(0x00); - expectLastCall().andReturn(0x20); - expectLastCall().andReturn(0x3E); // '>' - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getValue(), -100f); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private FuelTrimCommand command; + private InputStream mockIn; + private FuelTrim fuelTrimBank = FuelTrim.LONG_TERM_BANK_1; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new FuelTrimCommand(FuelTrim.LONG_TERM_BANK_1); + } + + /** + * Test for valid InputStream read, 99.22% + * + * @throws IOException + */ + @Test(enabled = false) + public void testMaxFuelTrimValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn(0x41); + expectLastCall().andReturn(fuelTrimBank.getValue()); + expectLastCall().andReturn(0xFF); + expectLastCall().andReturn(0x3E); // '>' + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getCalculatedResult(), 99.22f); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 56.25% + * + * @throws IOException + */ + @Test(enabled = false) + public void testSomeValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn(0x41); + expectLastCall().andReturn(0x20); + expectLastCall().andReturn(fuelTrimBank.getValue()); + expectLastCall().andReturn(0x20); + expectLastCall().andReturn(0xC8); + expectLastCall().andReturn(0x20); + expectLastCall().andReturn(0x3E); // '>' + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getCalculatedResult(), 56.25f); + + verifyAll(); + } + + /** + * Test for valid InputStream read, -100.00% + * + * @throws IOException + */ + @Test(enabled = false) + public void testMinFuelTrimValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn(0x41); + expectLastCall().andReturn(0x20); + expectLastCall().andReturn(fuelTrimBank.getValue()); + expectLastCall().andReturn(0x20); + expectLastCall().andReturn(0x00); + expectLastCall().andReturn(0x20); + expectLastCall().andReturn(0x3E); // '>' + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getCalculatedResult(), -100f); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/IntakeManifoldPressureCommandTest.java b/src/test/java/com/github/pires/obd/commands/IntakeManifoldPressureCommandTest.java index 851cc9cc..8f47b874 100644 --- a/src/test/java/com/github/pires/obd/commands/IntakeManifoldPressureCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/IntakeManifoldPressureCommandTest.java @@ -30,84 +30,84 @@ @PrepareForTest(InputStream.class) public class IntakeManifoldPressureCommandTest { - private IntakeManifoldPressureCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new IntakeManifoldPressureCommand(); - } - - /** - * Test for valid InputStream read, 100kPa - * - * @throws IOException - */ - @Test - public void testValidPressureMetric() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'B'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '6'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - command.useImperialUnits(false); - assertEquals(command.getMetricUnit(), 100); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 14.50psi - * - * @throws IOException - */ - @Test - public void testValidPressureImperial() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'B'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '6'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - command.useImperialUnits(true); - assertEquals(command.getImperialUnit(), 14.503774f); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private IntakeManifoldPressureCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new IntakeManifoldPressureCommand(); + } + + /** + * Test for valid InputStream read, 100kPa + * + * @throws IOException + */ + @Test + public void testValidPressureMetric() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'B'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '6'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + command.useImperialUnits(false); + assertEquals(command.getMetricUnit(), 100); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 14.50psi + * + * @throws IOException + */ + @Test + public void testValidPressureImperial() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'B'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '6'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + command.useImperialUnits(true); + assertEquals(command.getImperialUnit(), 14.503774f); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/MassAirFlowCommandTest.java b/src/test/java/com/github/pires/obd/commands/MassAirFlowCommandTest.java index 9e5681e8..3ca926f8 100644 --- a/src/test/java/com/github/pires/obd/commands/MassAirFlowCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/MassAirFlowCommandTest.java @@ -29,120 +29,120 @@ */ @PrepareForTest(InputStream.class) public class MassAirFlowCommandTest { - private MassAirFlowCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new MassAirFlowCommand(); - } - - /** - * Test for valid InputStream read, maximum value of 655.35g/s - * - * @throws IOException - */ - @Test - public void testMaxMAFValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getMAF(), 655.3499755859375d); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 381.61g/s - * - * @throws IOException - */ - @Test - public void testSomeMAFValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '9'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getMAF(), 381.6099853515625d); - - verifyAll(); - } - - /** - * Test for valid InputStream read, minimum value 0g/s - * - * @throws IOException - */ - @Test - public void testMinMAFValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getMAF(), 0d); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private MassAirFlowCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new MassAirFlowCommand(); + } + + /** + * Test for valid InputStream read, maximum value of 655.35g/s + * + * @throws IOException + */ + @Test + public void testMaxMAFValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getMAF(), 655.3499755859375d); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 381.61g/s + * + * @throws IOException + */ + @Test + public void testSomeMAFValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '9'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getMAF(), 381.6099853515625d); + + verifyAll(); + } + + /** + * Test for valid InputStream read, minimum value 0g/s + * + * @throws IOException + */ + @Test + public void testMinMAFValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getMAF(), 0d); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/PendingTroubleCodesCommandTest.java b/src/test/java/com/github/pires/obd/commands/PendingTroubleCodesCommandTest.java index f24d1a2e..a108d156 100644 --- a/src/test/java/com/github/pires/obd/commands/PendingTroubleCodesCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/PendingTroubleCodesCommandTest.java @@ -30,305 +30,305 @@ */ @PrepareForTest(InputStream.class) public class PendingTroubleCodesCommandTest { - private PendingTroubleCodesCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new PendingTroubleCodesCommand(); - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - } - - /** - * Test for two frames with four dtc - * - * @throws java.io.IOException - */ - @Test - public void twoFramesWithFourDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 'B'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '6'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0003\n"; - res += "C1104\n"; - res += "B21AB\n"; - res += "U3106\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frame with three dtc - * - * @throws java.io.IOException - */ - @Test - public void oneFrameWithThreeDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0103\n"; - res += "P0104\n"; - res += "P0105\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frame with two dtc - * - * @throws java.io.IOException - */ - @Test - public void oneFrameWithTwoDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0103\n"; - res += "P0104\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for two frames with four dtc CAN (ISO-15765) format - * - * @throws IOException - */ - @Test - public void twoFramesWithFourDTCCAN() throws IOException { - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ':'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ':'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '9'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0108\n"; - res += "P0118\n"; - res += "P0119\n"; - res += "P0120\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frames with two dtc CAN (ISO-15765) format - * - * @throws IOException - */ - @Test - public void oneFrameWithTwoDTCCAN() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0120\n"; - res += "P0121\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for no data - * - * @throws java.io.IOException - */ - @Test(expectedExceptions = NoDataException.class) - public void noData() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'N'); - expectLastCall().andReturn((byte) 'O'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 'T'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private PendingTroubleCodesCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new PendingTroubleCodesCommand(); + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + } + + /** + * Test for two frames with four dtc + * + * @throws java.io.IOException + */ + @Test + public void twoFramesWithFourDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 'B'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '6'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0003\n"; + res += "C1104\n"; + res += "B21AB\n"; + res += "U3106\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frame with three dtc + * + * @throws java.io.IOException + */ + @Test + public void oneFrameWithThreeDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0103\n"; + res += "P0104\n"; + res += "P0105\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frame with two dtc + * + * @throws java.io.IOException + */ + @Test + public void oneFrameWithTwoDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0103\n"; + res += "P0104\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for two frames with four dtc CAN (ISO-15765) format + * + * @throws IOException + */ + @Test + public void twoFramesWithFourDTCCAN() throws IOException { + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ':'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ':'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '9'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0108\n"; + res += "P0118\n"; + res += "P0119\n"; + res += "P0120\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frames with two dtc CAN (ISO-15765) format + * + * @throws IOException + */ + @Test + public void oneFrameWithTwoDTCCAN() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0120\n"; + res += "P0121\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for no data + * + * @throws java.io.IOException + */ + @Test(expectedExceptions = NoDataException.class) + public void noData() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'N'); + expectLastCall().andReturn((byte) 'O'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 'T'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } \ No newline at end of file diff --git a/src/test/java/com/github/pires/obd/commands/PermanentTroubleCodesCommandTest.java b/src/test/java/com/github/pires/obd/commands/PermanentTroubleCodesCommandTest.java index ae05486b..9ac8cebb 100644 --- a/src/test/java/com/github/pires/obd/commands/PermanentTroubleCodesCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/PermanentTroubleCodesCommandTest.java @@ -30,305 +30,305 @@ */ @PrepareForTest(InputStream.class) public class PermanentTroubleCodesCommandTest { - private PermanentTroubleCodesCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new PermanentTroubleCodesCommand(); - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - } - - /** - * Test for two frames with four dtc - * - * @throws java.io.IOException - */ - @Test - public void twoFramesWithFourDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 'B'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '6'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0003\n"; - res += "C1104\n"; - res += "B21AB\n"; - res += "U3106\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frame with three dtc - * - * @throws java.io.IOException - */ - @Test - public void oneFrameWithThreeDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0103\n"; - res += "P0104\n"; - res += "P0105\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frame with two dtc - * - * @throws java.io.IOException - */ - @Test - public void oneFrameWithTwoDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0103\n"; - res += "P0104\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for two frames with four dtc CAN (ISO-15765) format - * - * @throws IOException - */ - @Test - public void twoFramesWithFourDTCCAN() throws IOException { - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ':'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ':'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '9'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0108\n"; - res += "P0118\n"; - res += "P0119\n"; - res += "P0120\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frames with two dtc CAN (ISO-15765) format - * - * @throws IOException - */ - @Test - public void oneFrameWithTwoDTCCAN() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0120\n"; - res += "P0121\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for no data - * - * @throws java.io.IOException - */ - @Test(expectedExceptions = NoDataException.class) - public void noData() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'N'); - expectLastCall().andReturn((byte) 'O'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 'T'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private PermanentTroubleCodesCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new PermanentTroubleCodesCommand(); + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + } + + /** + * Test for two frames with four dtc + * + * @throws java.io.IOException + */ + @Test + public void twoFramesWithFourDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 'B'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '6'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0003\n"; + res += "C1104\n"; + res += "B21AB\n"; + res += "U3106\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frame with three dtc + * + * @throws java.io.IOException + */ + @Test + public void oneFrameWithThreeDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0103\n"; + res += "P0104\n"; + res += "P0105\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frame with two dtc + * + * @throws java.io.IOException + */ + @Test + public void oneFrameWithTwoDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0103\n"; + res += "P0104\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for two frames with four dtc CAN (ISO-15765) format + * + * @throws IOException + */ + @Test + public void twoFramesWithFourDTCCAN() throws IOException { + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ':'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ':'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '9'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0108\n"; + res += "P0118\n"; + res += "P0119\n"; + res += "P0120\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frames with two dtc CAN (ISO-15765) format + * + * @throws IOException + */ + @Test + public void oneFrameWithTwoDTCCAN() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0120\n"; + res += "P0121\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for no data + * + * @throws java.io.IOException + */ + @Test(expectedExceptions = NoDataException.class) + public void noData() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'N'); + expectLastCall().andReturn((byte) 'O'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 'T'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } \ No newline at end of file diff --git a/src/test/java/com/github/pires/obd/commands/RPMCommandTest.java b/src/test/java/com/github/pires/obd/commands/RPMCommandTest.java index 7277e5ae..2b0c51a5 100644 --- a/src/test/java/com/github/pires/obd/commands/RPMCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/RPMCommandTest.java @@ -30,120 +30,120 @@ @PrepareForTest(InputStream.class) public class RPMCommandTest { - private RPMCommand command = null; - private InputStream mockIn = null; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new RPMCommand(); - } - - /** - * Test for valid InputStream read, max RPM - * - * @throws IOException - */ - @Test - public void testMaximumRPMValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'C'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getRPM(), 16383); - - verifyAll(); - } - - /** - * Test for valid InputStream read - * - * @throws IOException - */ - @Test - public void testHighRPM() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'C'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) 'C'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getRPM(), 2575); - - verifyAll(); - } - - /** - * Test for valid InputStream read - * - * @throws IOException - */ - @Test - public void testLowRPM() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'C'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getRPM(), 640); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private RPMCommand command = null; + private InputStream mockIn = null; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new RPMCommand(); + } + + /** + * Test for valid InputStream read, max RPM + * + * @throws IOException + */ + @Test + public void testMaximumRPMValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'C'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getRPM(), 16383); + + verifyAll(); + } + + /** + * Test for valid InputStream read + * + * @throws IOException + */ + @Test + public void testHighRPM() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'C'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) 'C'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getRPM(), 2575); + + verifyAll(); + } + + /** + * Test for valid InputStream read + * + * @throws IOException + */ + @Test + public void testLowRPM() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'C'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getRPM(), 640); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/RuntimeCommandTest.java b/src/test/java/com/github/pires/obd/commands/RuntimeCommandTest.java index ea4b31f8..c8de83d4 100644 --- a/src/test/java/com/github/pires/obd/commands/RuntimeCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/RuntimeCommandTest.java @@ -30,120 +30,120 @@ @PrepareForTest(InputStream.class) public class RuntimeCommandTest { - private RuntimeCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new RuntimeCommand(); - } - - /** - * Test for valid InputStream read, 65535 seconds. - * - * @throws IOException - */ - @Test - public void testMaxRuntimeValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFormattedResult(), "18:12:15"); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 67 seconds - * - * @throws IOException - */ - @Test - public void testSomeRuntimeValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFormattedResult(), "04:55:31"); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 0 seconds. - * - * @throws IOException - */ - @Test - public void testMinRuntimeValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getFormattedResult(), "00:00:00"); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private RuntimeCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new RuntimeCommand(); + } + + /** + * Test for valid InputStream read, 65535 seconds. + * + * @throws IOException + */ + @Test + public void testMaxRuntimeValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFormattedResult(), "18:12:15"); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 67 seconds + * + * @throws IOException + */ + @Test + public void testSomeRuntimeValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFormattedResult(), "04:55:31"); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 0 seconds. + * + * @throws IOException + */ + @Test + public void testMinRuntimeValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getFormattedResult(), "00:00:00"); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/SpeedCommandTest.java b/src/test/java/com/github/pires/obd/commands/SpeedCommandTest.java index 034ef9f3..c6fef7da 100644 --- a/src/test/java/com/github/pires/obd/commands/SpeedCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/SpeedCommandTest.java @@ -29,115 +29,115 @@ @PrepareForTest(InputStream.class) public class SpeedCommandTest { - private SpeedCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new SpeedCommand(); - } - - /** - * Test for valid InputStream read, 64km/h - * - * @throws IOException - */ - @Test - public void testValidSpeedMetric() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - command.getFormattedResult(); - assertEquals(command.getMetricSpeed(), 64); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 42.87mph - * - * @throws IOException - */ - @Test - public void testValidSpeedImperial() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - command.useImperialUnits = true; - command.getFormattedResult(); - assertEquals(command.getImperialSpeed(), 42.874615f); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 0km/h - * - * @throws IOException - */ - @Test - public void testZeroSpeedMetric() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - command.getFormattedResult(); - assertEquals(command.getMetricSpeed(), 0); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private SpeedCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new SpeedCommand(); + } + + /** + * Test for valid InputStream read, 64km/h + * + * @throws IOException + */ + @Test + public void testValidSpeedMetric() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + command.getFormattedResult(); + assertEquals(command.getMetricSpeed(), 64); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 42.87mph + * + * @throws IOException + */ + @Test + public void testValidSpeedImperial() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + command.useImperialUnits = true; + command.getFormattedResult(); + assertEquals(command.getImperialSpeed(), 42.874615f); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 0km/h + * + * @throws IOException + */ + @Test + public void testZeroSpeedMetric() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + command.getFormattedResult(); + assertEquals(command.getMetricSpeed(), 0); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/ThrottleCommandTest.java b/src/test/java/com/github/pires/obd/commands/ThrottleCommandTest.java index ff21a38d..07a50c66 100644 --- a/src/test/java/com/github/pires/obd/commands/ThrottleCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/ThrottleCommandTest.java @@ -29,111 +29,111 @@ */ @PrepareForTest(InputStream.class) public class ThrottleCommandTest { - private ThrottlePositionCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new ThrottlePositionCommand(); - } - - /** - * Test for valid InputStream read, maximum value of 100% - * - * @throws IOException - */ - @Test - public void testMaxThrottlePositionValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getPercentage(), 100f); - - verifyAll(); - } - - /** - * Test for valid InputStream read, 58.4% - * - * @throws IOException - */ - @Test - public void testSomeThrottlePositionValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '9'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getPercentage(), 58.431374f); - - verifyAll(); - } - - /** - * Test for valid InputStream read, minimum value 0% - * - * @throws IOException - */ - @Test - public void testMinThrottlePositionValue() throws IOException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - assertEquals(command.getPercentage(), 0f); - - verifyAll(); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private ThrottlePositionCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new ThrottlePositionCommand(); + } + + /** + * Test for valid InputStream read, maximum value of 100% + * + * @throws IOException + */ + @Test + public void testMaxThrottlePositionValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getPercentage(), 100f); + + verifyAll(); + } + + /** + * Test for valid InputStream read, 58.4% + * + * @throws IOException + */ + @Test + public void testSomeThrottlePositionValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '9'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getPercentage(), 58.431374f); + + verifyAll(); + } + + /** + * Test for valid InputStream read, minimum value 0% + * + * @throws IOException + */ + @Test + public void testMinThrottlePositionValue() throws IOException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + assertEquals(command.getPercentage(), 0f); + + verifyAll(); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/TroubleCodesCommandTest.java b/src/test/java/com/github/pires/obd/commands/TroubleCodesCommandTest.java index ec3f406a..9572efa1 100644 --- a/src/test/java/com/github/pires/obd/commands/TroubleCodesCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/TroubleCodesCommandTest.java @@ -30,305 +30,305 @@ */ @PrepareForTest(InputStream.class) public class TroubleCodesCommandTest { - private TroubleCodesCommand command; - private InputStream mockIn; - - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new TroubleCodesCommand(); - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - } - - /** - * Test for two frames with four dtc - * - * @throws java.io.IOException - */ - @Test - public void twoFramesWithFourDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 'B'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '6'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0003\n"; - res += "C1104\n"; - res += "B21AB\n"; - res += "U3106\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frame with three dtc - * - * @throws java.io.IOException - */ - @Test - public void oneFrameWithThreeDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '5'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0103\n"; - res += "P0104\n"; - res += "P0105\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frame with two dtc - * - * @throws java.io.IOException - */ - @Test - public void oneFrameWithTwoDTC() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0103\n"; - res += "P0104\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for two frames with four dtc CAN (ISO-15765) format - * - * @throws IOException - */ - @Test - public void twoFramesWithFourDTCCAN() throws IOException { - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ':'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '8'); - expectLastCall().andReturn((byte) 13); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ':'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '9'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0108\n"; - res += "P0118\n"; - res += "P0119\n"; - res += "P0120\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for one frames with two dtc CAN (ISO-15765) format - * - * @throws IOException - */ - @Test - public void oneFrameWithTwoDTCCAN() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - String res = "P0120\n"; - res += "P0121\n"; - - // call the method to test - command.readResult(mockIn); - - assertEquals(command.getFormattedResult(), res); - - verifyAll(); - } - - /** - * Test for no data - * - * @throws java.io.IOException - */ - @Test(expectedExceptions = NoDataException.class) - public void noData() throws IOException { - expectLastCall().andReturn((byte) '4'); - expectLastCall().andReturn((byte) '3'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'N'); - expectLastCall().andReturn((byte) 'O'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 'T'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '>'); - - replayAll(); - - // call the method to test - command.readResult(mockIn); - } - - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + private TroubleCodesCommand command; + private InputStream mockIn; + + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new TroubleCodesCommand(); + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + } + + /** + * Test for two frames with four dtc + * + * @throws java.io.IOException + */ + @Test + public void twoFramesWithFourDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 'B'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '6'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0003\n"; + res += "C1104\n"; + res += "B21AB\n"; + res += "U3106\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frame with three dtc + * + * @throws java.io.IOException + */ + @Test + public void oneFrameWithThreeDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '5'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0103\n"; + res += "P0104\n"; + res += "P0105\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frame with two dtc + * + * @throws java.io.IOException + */ + @Test + public void oneFrameWithTwoDTC() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0103\n"; + res += "P0104\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for two frames with four dtc CAN (ISO-15765) format + * + * @throws IOException + */ + @Test + public void twoFramesWithFourDTCCAN() throws IOException { + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ':'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '8'); + expectLastCall().andReturn((byte) 13); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ':'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '9'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0108\n"; + res += "P0118\n"; + res += "P0119\n"; + res += "P0120\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for one frames with two dtc CAN (ISO-15765) format + * + * @throws IOException + */ + @Test + public void oneFrameWithTwoDTCCAN() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + String res = "P0120\n"; + res += "P0121\n"; + + // call the method to test + command.readResult(mockIn); + + assertEquals(command.getFormattedResult(), res); + + verifyAll(); + } + + /** + * Test for no data + * + * @throws java.io.IOException + */ + @Test(expectedExceptions = NoDataException.class) + public void noData() throws IOException { + expectLastCall().andReturn((byte) '4'); + expectLastCall().andReturn((byte) '3'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'N'); + expectLastCall().andReturn((byte) 'O'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 'T'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '>'); + + replayAll(); + + // call the method to test + command.readResult(mockIn); + } + + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } } diff --git a/src/test/java/com/github/pires/obd/commands/VinCommandTest.java b/src/test/java/com/github/pires/obd/commands/VinCommandTest.java index f5d6f9c6..aaf2aa89 100644 --- a/src/test/java/com/github/pires/obd/commands/VinCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/VinCommandTest.java @@ -27,82 +27,79 @@ * Tests for VinCommand class. */ public class VinCommandTest { - private VinCommand command; - private InputStream mockIn; + private VinCommand command; + private InputStream mockIn; - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new VinCommand(); - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - } + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new VinCommand(); + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + } - /** - * Clear resources. - */ - @AfterClass - public void tearDown() { - command = null; - mockIn = null; - } + /** + * Clear resources. + */ + @AfterClass + public void tearDown() { + command = null; + mockIn = null; + } - /** - * Test VIN CAN (ISO-15765) format - * - * @throws IOException - */ - @Test - public void vinCAN() throws IOException { - byte[] v = new byte[]{ - '0', '1', '4', '\n', - '0', ':', ' ', '4', '9', ' ', '0', '2', ' ', '0', '1', ' ', '5', '7', ' ', '5', '0', ' ', '3', '0', '\n', - '1', ':', ' ', '5', 'A', ' ', '5', 'A', ' ', '5', 'A', ' ', '3', '9', ' ', '3', '9', ' ', '5', 'A', ' ', '5', '4', '\n', - '2', ':', ' ', '5', '3', ' ', '3', '3', ' ', '3', '9', ' ', '3', '2', ' ', '3', '1', ' ', '3', '2', ' ', '3', '4', '>' - }; - for (byte b : v) { - expectLastCall().andReturn(b); - } - replayAll(); - String res = "WP0ZZZ99ZTS392124"; + /** + * Test VIN CAN (ISO-15765) format + * + * @throws IOException + */ + @Test + public void vinCAN() throws IOException { + byte[] v = new byte[] { '0', '1', '4', '\n', '0', ':', ' ', '4', '9', ' ', '0', '2', ' ', '0', '1', ' ', '5', + '7', ' ', '5', '0', ' ', '3', '0', '\n', '1', ':', ' ', '5', 'A', ' ', '5', 'A', ' ', '5', 'A', ' ', + '3', '9', ' ', '3', '9', ' ', '5', 'A', ' ', '5', '4', '\n', '2', ':', ' ', '5', '3', ' ', '3', '3', + ' ', '3', '9', ' ', '3', '2', ' ', '3', '1', ' ', '3', '2', ' ', '3', '4', '>' }; + for (byte b : v) { + expectLastCall().andReturn(b); + } + replayAll(); + String res = "WP0ZZZ99ZTS392124"; - // call the method to test - command.readResult(mockIn); + // call the method to test + command.readResult(mockIn); - assertEquals(command.getFormattedResult(), res); + assertEquals(command.getFormattedResult(), res); - verifyAll(); - } + verifyAll(); + } - /** - * Test VIN ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) format - * - * @throws IOException - */ - @Test - public void vin() throws IOException { - byte[] v = new byte[]{ - '4', '9', ' ', '0', '2', ' ', '0', '1', ' ', '0', '0', ' ', '0', '0', ' ', '0', '0', ' ', '5', '7', '\n', - '4', '9', ' ', '0', '2', ' ', '0', '2', ' ', '5', '0', ' ', '3', '0', ' ', '5', 'A', ' ', '5', 'A', '\n', - '4', '9', ' ', '0', '2', ' ', '0', '3', ' ', '5', 'A', ' ', '3', '9', ' ', '3', '9', ' ', '5', 'A', '\n', - '4', '9', ' ', '0', '2', ' ', '0', '4', ' ', '5', '4', ' ', '5', '3', ' ', '3', '3', ' ', '3', '9', '\n', - '4', '9', ' ', '0', '2', ' ', '0', '5', ' ', '3', '2', ' ', '3', '1', ' ', '3', '2', ' ', '3', '4', '>' - }; - for (byte b : v) { - expectLastCall().andReturn(b); - } + /** + * Test VIN ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) format + * + * @throws IOException + */ + @Test + public void vin() throws IOException { + byte[] v = new byte[] { '4', '9', ' ', '0', '2', ' ', '0', '1', ' ', '0', '0', ' ', '0', '0', ' ', '0', '0', + ' ', '5', '7', '\n', '4', '9', ' ', '0', '2', ' ', '0', '2', ' ', '5', '0', ' ', '3', '0', ' ', '5', + 'A', ' ', '5', 'A', '\n', '4', '9', ' ', '0', '2', ' ', '0', '3', ' ', '5', 'A', ' ', '3', '9', ' ', + '3', '9', ' ', '5', 'A', '\n', '4', '9', ' ', '0', '2', ' ', '0', '4', ' ', '5', '4', ' ', '5', '3', + ' ', '3', '3', ' ', '3', '9', '\n', '4', '9', ' ', '0', '2', ' ', '0', '5', ' ', '3', '2', ' ', '3', + '1', ' ', '3', '2', ' ', '3', '4', '>' }; + for (byte b : v) { + expectLastCall().andReturn(b); + } - replayAll(); - String res = "WP0ZZZ99ZTS392124"; + replayAll(); + String res = "WP0ZZZ99ZTS392124"; - // call the method to test - command.readResult(mockIn); + // call the method to test + command.readResult(mockIn); - assertEquals(command.getFormattedResult(), res); + assertEquals(command.getFormattedResult(), res); - verifyAll(); - } + verifyAll(); + } } \ No newline at end of file diff --git a/src/test/java/com/github/pires/obd/exceptions/BusInitExceptionTest.java b/src/test/java/com/github/pires/obd/exceptions/BusInitExceptionTest.java index f446108f..d73637c9 100644 --- a/src/test/java/com/github/pires/obd/exceptions/BusInitExceptionTest.java +++ b/src/test/java/com/github/pires/obd/exceptions/BusInitExceptionTest.java @@ -29,52 +29,53 @@ @PrepareForTest(InputStream.class) public class BusInitExceptionTest { - private SpeedCommand command; - private InputStream mockIn; + private SpeedCommand command; + private InputStream mockIn; - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new SpeedCommand(); - } + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new SpeedCommand(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException, java.lang.InterruptedException - */ - @Test(expectedExceptions = BusInitException.class) - public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) 'B'); - expectLastCall().andReturn((byte) 'U'); - expectLastCall().andReturn((byte) 'S'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'I'); - expectLastCall().andReturn((byte) 'N'); - expectLastCall().andReturn((byte) 'I'); - expectLastCall().andReturn((byte) 'T'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'E'); - expectLastCall().andReturn((byte) 'R'); - expectLastCall().andReturn((byte) 'R'); - expectLastCall().andReturn((byte) 'O'); - expectLastCall().andReturn((byte) 'R'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException, + * java.lang.InterruptedException + */ + @Test(expectedExceptions = BusInitException.class) + public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) 'B'); + expectLastCall().andReturn((byte) 'U'); + expectLastCall().andReturn((byte) 'S'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'I'); + expectLastCall().andReturn((byte) 'N'); + expectLastCall().andReturn((byte) 'I'); + expectLastCall().andReturn((byte) 'T'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'E'); + expectLastCall().andReturn((byte) 'R'); + expectLastCall().andReturn((byte) 'R'); + expectLastCall().andReturn((byte) 'O'); + expectLastCall().andReturn((byte) 'R'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.run(mockIn, new ByteArrayOutputStream()); + // call the method to test + command.run(mockIn, new ByteArrayOutputStream()); - verifyAll(); - } + verifyAll(); + } } diff --git a/src/test/java/com/github/pires/obd/exceptions/NoDataExceptionTest.java b/src/test/java/com/github/pires/obd/exceptions/NoDataExceptionTest.java index 27680b13..ab836920 100644 --- a/src/test/java/com/github/pires/obd/exceptions/NoDataExceptionTest.java +++ b/src/test/java/com/github/pires/obd/exceptions/NoDataExceptionTest.java @@ -29,42 +29,43 @@ @PrepareForTest(InputStream.class) public class NoDataExceptionTest { - private SpeedCommand command; - private InputStream mockIn; + private SpeedCommand command; + private InputStream mockIn; - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new SpeedCommand(); - } + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new SpeedCommand(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException, java.lang.InterruptedException - */ - @Test(expectedExceptions = NoDataException.class) - public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) 'N'); - expectLastCall().andReturn((byte) 'O'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 'T'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException, + * java.lang.InterruptedException + */ + @Test(expectedExceptions = NoDataException.class) + public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) 'N'); + expectLastCall().andReturn((byte) 'O'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 'T'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.run(mockIn, new ByteArrayOutputStream()); + // call the method to test + command.run(mockIn, new ByteArrayOutputStream()); - verifyAll(); - } + verifyAll(); + } } diff --git a/src/test/java/com/github/pires/obd/exceptions/NonNumericResponseExceptionTest.java b/src/test/java/com/github/pires/obd/exceptions/NonNumericResponseExceptionTest.java index 3631e14d..63682b7a 100644 --- a/src/test/java/com/github/pires/obd/exceptions/NonNumericResponseExceptionTest.java +++ b/src/test/java/com/github/pires/obd/exceptions/NonNumericResponseExceptionTest.java @@ -29,37 +29,38 @@ @PrepareForTest(InputStream.class) public class NonNumericResponseExceptionTest { - private SpeedCommand command; - private InputStream mockIn; + private SpeedCommand command; + private InputStream mockIn; - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new SpeedCommand(); - } + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new SpeedCommand(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException, java.lang.InterruptedException - */ - @Test(expectedExceptions = NonNumericResponseException.class) - public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) 'O'); - expectLastCall().andReturn((byte) 'K'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException, + * java.lang.InterruptedException + */ + @Test(expectedExceptions = NonNumericResponseException.class) + public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) 'O'); + expectLastCall().andReturn((byte) 'K'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.run(mockIn, new ByteArrayOutputStream()); + // call the method to test + command.run(mockIn, new ByteArrayOutputStream()); - verifyAll(); - } + verifyAll(); + } } diff --git a/src/test/java/com/github/pires/obd/exceptions/StoppedExceptionTest.java b/src/test/java/com/github/pires/obd/exceptions/StoppedExceptionTest.java index a60aff73..e5f35222 100644 --- a/src/test/java/com/github/pires/obd/exceptions/StoppedExceptionTest.java +++ b/src/test/java/com/github/pires/obd/exceptions/StoppedExceptionTest.java @@ -29,42 +29,43 @@ @PrepareForTest(InputStream.class) public class StoppedExceptionTest { - private SpeedCommand command; - private InputStream mockIn; + private SpeedCommand command; + private InputStream mockIn; - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new SpeedCommand(); - } + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new SpeedCommand(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException, java.lang.InterruptedException - */ - @Test(expectedExceptions = StoppedException.class) - public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) 'S'); - expectLastCall().andReturn((byte) 'T'); - expectLastCall().andReturn((byte) 'O'); - expectLastCall().andReturn((byte) 'P'); - expectLastCall().andReturn((byte) 'P'); - expectLastCall().andReturn((byte) 'E'); - expectLastCall().andReturn((byte) 'D'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException, + * java.lang.InterruptedException + */ + @Test(expectedExceptions = StoppedException.class) + public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) 'S'); + expectLastCall().andReturn((byte) 'T'); + expectLastCall().andReturn((byte) 'O'); + expectLastCall().andReturn((byte) 'P'); + expectLastCall().andReturn((byte) 'P'); + expectLastCall().andReturn((byte) 'E'); + expectLastCall().andReturn((byte) 'D'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.run(mockIn, new ByteArrayOutputStream()); + // call the method to test + command.run(mockIn, new ByteArrayOutputStream()); - verifyAll(); - } + verifyAll(); + } } diff --git a/src/test/java/com/github/pires/obd/exceptions/UnknownErrorExceptionTest.java b/src/test/java/com/github/pires/obd/exceptions/UnknownErrorExceptionTest.java index 671a134e..a4332345 100644 --- a/src/test/java/com/github/pires/obd/exceptions/UnknownErrorExceptionTest.java +++ b/src/test/java/com/github/pires/obd/exceptions/UnknownErrorExceptionTest.java @@ -29,53 +29,54 @@ @PrepareForTest(InputStream.class) public class UnknownErrorExceptionTest { - private SpeedCommand command; - private InputStream mockIn; + private SpeedCommand command; + private InputStream mockIn; - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new SpeedCommand(); - } + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new SpeedCommand(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException, java.lang.InterruptedException - */ - @Test(expectedExceptions = UnknownErrorException.class) - public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) 'S'); - expectLastCall().andReturn((byte) 'E'); - expectLastCall().andReturn((byte) 'A'); - expectLastCall().andReturn((byte) 'R'); - expectLastCall().andReturn((byte) 'C'); - expectLastCall().andReturn((byte) 'H'); - expectLastCall().andReturn((byte) 'I'); - expectLastCall().andReturn((byte) 'N'); - expectLastCall().andReturn((byte) 'G'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) '.'); - expectLastCall().andReturn((byte) ' '); - expectLastCall().andReturn((byte) 'E'); - expectLastCall().andReturn((byte) 'R'); - expectLastCall().andReturn((byte) 'R'); - expectLastCall().andReturn((byte) 'O'); - expectLastCall().andReturn((byte) 'R'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException, + * java.lang.InterruptedException + */ + @Test(expectedExceptions = UnknownErrorException.class) + public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) 'S'); + expectLastCall().andReturn((byte) 'E'); + expectLastCall().andReturn((byte) 'A'); + expectLastCall().andReturn((byte) 'R'); + expectLastCall().andReturn((byte) 'C'); + expectLastCall().andReturn((byte) 'H'); + expectLastCall().andReturn((byte) 'I'); + expectLastCall().andReturn((byte) 'N'); + expectLastCall().andReturn((byte) 'G'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) '.'); + expectLastCall().andReturn((byte) ' '); + expectLastCall().andReturn((byte) 'E'); + expectLastCall().andReturn((byte) 'R'); + expectLastCall().andReturn((byte) 'R'); + expectLastCall().andReturn((byte) 'O'); + expectLastCall().andReturn((byte) 'R'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.run(mockIn, new ByteArrayOutputStream()); + // call the method to test + command.run(mockIn, new ByteArrayOutputStream()); - verifyAll(); - } + verifyAll(); + } } diff --git a/src/test/java/com/github/pires/obd/exceptions/UnsupportedCommandExceptionTest.java b/src/test/java/com/github/pires/obd/exceptions/UnsupportedCommandExceptionTest.java index 956f4f23..a0cf04fb 100644 --- a/src/test/java/com/github/pires/obd/exceptions/UnsupportedCommandExceptionTest.java +++ b/src/test/java/com/github/pires/obd/exceptions/UnsupportedCommandExceptionTest.java @@ -29,67 +29,69 @@ @PrepareForTest(InputStream.class) public class UnsupportedCommandExceptionTest { - private SpeedCommand command; - private InputStream mockIn; + private SpeedCommand command; + private InputStream mockIn; - /** - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - command = new SpeedCommand(); - } + /** + * @throws Exception + */ + @BeforeMethod + public void setUp() throws Exception { + command = new SpeedCommand(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException, java.lang.InterruptedException - */ - @Test(expectedExceptions = UnsupportedCommandException.class) - public void testUnsupportedVin() throws IOException, InterruptedException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '9'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException, + * java.lang.InterruptedException + */ + @Test(expectedExceptions = UnsupportedCommandException.class) + public void testUnsupportedVin() throws IOException, InterruptedException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '9'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.run(mockIn, new ByteArrayOutputStream()); + // call the method to test + command.run(mockIn, new ByteArrayOutputStream()); - verifyAll(); - } + verifyAll(); + } - /** - * Test for valid InputStream read with echo - * - * @throws java.io.IOException, java.lang.InterruptedException - */ - @Test(expectedExceptions = UnsupportedCommandException.class) - public void testUnsupportedSpeed() throws IOException, InterruptedException { - // mock InputStream read - mockIn = createMock(InputStream.class); - mockIn.read(); - expectLastCall().andReturn((byte) '7'); - expectLastCall().andReturn((byte) 'F'); - expectLastCall().andReturn((byte) '0'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '1'); - expectLastCall().andReturn((byte) '2'); - expectLastCall().andReturn((byte) '>'); + /** + * Test for valid InputStream read with echo + * + * @throws java.io.IOException, + * java.lang.InterruptedException + */ + @Test(expectedExceptions = UnsupportedCommandException.class) + public void testUnsupportedSpeed() throws IOException, InterruptedException { + // mock InputStream read + mockIn = createMock(InputStream.class); + mockIn.read(); + expectLastCall().andReturn((byte) '7'); + expectLastCall().andReturn((byte) 'F'); + expectLastCall().andReturn((byte) '0'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '1'); + expectLastCall().andReturn((byte) '2'); + expectLastCall().andReturn((byte) '>'); - replayAll(); + replayAll(); - // call the method to test - command.run(mockIn, new ByteArrayOutputStream()); + // call the method to test + command.run(mockIn, new ByteArrayOutputStream()); - verifyAll(); - } + verifyAll(); + } } diff --git a/src/test/java/com/github/pires/obd/utils/CommandAvailabilityHelperTest.java b/src/test/java/com/github/pires/obd/utils/CommandAvailabilityHelperTest.java index 5d56b857..8c8ac8cd 100644 --- a/src/test/java/com/github/pires/obd/utils/CommandAvailabilityHelperTest.java +++ b/src/test/java/com/github/pires/obd/utils/CommandAvailabilityHelperTest.java @@ -18,43 +18,43 @@ public class CommandAvailabilityHelperTest { - @Test - public void testDigestAvailabilityString() throws Exception { - int[] expected = new int[]{Integer.parseInt("10111110", 2), Integer.parseInt("00011111", 2), - Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2)}; - int[] result = CommandAvailabilityHelper.digestAvailabilityString("BE1FA813"); - Assert.assertEquals(expected, result); - - //Now with 16 characters - expected = new int[]{Integer.parseInt("10111110", 2), Integer.parseInt("00011111", 2), - Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2), - Integer.parseInt("10111110", 2), Integer.parseInt("00011111", 2), - Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2)}; - - result = CommandAvailabilityHelper.digestAvailabilityString("BE1FA813BE1FA813"); - Assert.assertEquals(expected, result); - } - - @Test - public void testIsAvailable() throws Exception { - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("02", "BE1FA813"), false); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("07", "BE1FA813"), true); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable(new ThrottlePositionCommand().getCommandPID() /*11*/, "BE1FA813"), true); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1A", "BE1FA813"), false); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1D", "BE1FA813"), false); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1F", "BE1FA813"), true); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("22", "BE1FA813BE1FA813"), false); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("27", "BE1FA813BE1FA813"), true); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3A", "BE1FA813BE1FA813"), false); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3D", "BE1FA813BE1FA813"), false); - Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3F", "BE1FA813BE1FA813"), true); - } - - @Test(expectedExceptions = IllegalArgumentException.class) - public void testFail() throws Exception { - CommandAvailabilityHelper.digestAvailabilityString("AAA"); - CommandAvailabilityHelper.digestAvailabilityString("AAAAAAAR"); - CommandAvailabilityHelper.isAvailable("2F", "BE1FA813"); - } + @Test + public void testDigestAvailabilityString() throws Exception { + int[] expected = new int[] { Integer.parseInt("10111110", 2), Integer.parseInt("00011111", 2), + Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2) }; + int[] result = CommandAvailabilityHelper.digestAvailabilityString("BE1FA813"); + Assert.assertEquals(expected, result); + + // Now with 16 characters + expected = new int[] { Integer.parseInt("10111110", 2), Integer.parseInt("00011111", 2), + Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2), Integer.parseInt("10111110", 2), + Integer.parseInt("00011111", 2), Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2) }; + + result = CommandAvailabilityHelper.digestAvailabilityString("BE1FA813BE1FA813"); + Assert.assertEquals(expected, result); + } + + @Test + public void testIsAvailable() throws Exception { + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("02", "BE1FA813"), false); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("07", "BE1FA813"), true); + Assert.assertEquals(CommandAvailabilityHelper + .isAvailable(new ThrottlePositionCommand().getCommandPID() /* 11 */, "BE1FA813"), true); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1A", "BE1FA813"), false); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1D", "BE1FA813"), false); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1F", "BE1FA813"), true); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("22", "BE1FA813BE1FA813"), false); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("27", "BE1FA813BE1FA813"), true); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3A", "BE1FA813BE1FA813"), false); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3D", "BE1FA813BE1FA813"), false); + Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3F", "BE1FA813BE1FA813"), true); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testFail() throws Exception { + CommandAvailabilityHelper.digestAvailabilityString("AAA"); + CommandAvailabilityHelper.digestAvailabilityString("AAAAAAAR"); + CommandAvailabilityHelper.isAvailable("2F", "BE1FA813"); + } } From 33c4e62b0f6ba05d81ce131bf0a1bdc82fdb77da Mon Sep 17 00:00:00 2001 From: Elton Viana Date: Thu, 13 Jul 2017 15:49:12 -0300 Subject: [PATCH 2/3] Fix some typos and format code --- .../github/pires/obd/commands/ObdCommand.java | 18 +++++++++++++----- .../pires/obd/commands/ObdMultiCommand.java | 2 +- .../pires/obd/commands/SpeedCommand.java | 6 ++++-- .../control/DistanceMILOnCommand.java | 5 +++-- .../control/DistanceSinceCCCommand.java | 5 +++-- .../commands/control/DtcNumberCommand.java | 5 +++-- .../control/EquivalentRatioCommand.java | 6 +++--- .../control/IgnitionMonitorCommand.java | 4 ++-- .../control/ModuleVoltageCommand.java | 4 ++-- .../control/PendingTroubleCodesCommand.java | 9 +++++---- .../control/PermanentTroubleCodesCommand.java | 9 +++++---- .../commands/control/TroubleCodesCommand.java | 9 +++++---- .../obd/commands/control/VinCommand.java | 9 +++++---- .../commands/engine/AbsoluteLoadCommand.java | 4 ++-- .../commands/engine/MassAirFlowCommand.java | 4 ++-- .../obd/commands/engine/OilTempCommand.java | 4 ++-- .../pires/obd/commands/engine/RPMCommand.java | 4 ++-- .../obd/commands/engine/RuntimeCommand.java | 4 ++-- .../engine/ThrottlePositionCommand.java | 4 ++-- .../commands/fuel/FindFuelTypeCommand.java | 2 +- .../obd/commands/fuel/FuelTrimCommand.java | 3 ++- .../IntakeManifoldPressureCommand.java | 4 ++-- .../commands/pressure/PressureCommand.java | 4 +++- .../protocol/AvailablePidsCommand.java | 4 ++-- .../protocol/AvailablePidsCommand_01_20.java | 4 ++-- .../protocol/AvailablePidsCommand_21_40.java | 4 ++-- .../protocol/AvailablePidsCommand_41_60.java | 4 ++-- .../commands/protocol/ObdProtocolCommand.java | 4 +++- .../temperature/TemperatureCommand.java | 6 ++++-- src/test/resources/testng.xml | 19 +++++++++++++------ 30 files changed, 102 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/github/pires/obd/commands/ObdCommand.java b/src/main/java/com/github/pires/obd/commands/ObdCommand.java index e61e5e79..ee9c81fe 100644 --- a/src/main/java/com/github/pires/obd/commands/ObdCommand.java +++ b/src/main/java/com/github/pires/obd/commands/ObdCommand.java @@ -12,14 +12,22 @@ */ package com.github.pires.obd.commands; -import com.github.pires.obd.exceptions.*; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.regex.Pattern; +import com.github.pires.obd.exceptions.BusInitException; +import com.github.pires.obd.exceptions.MisunderstoodCommandException; +import com.github.pires.obd.exceptions.NoDataException; +import com.github.pires.obd.exceptions.NonNumericResponseException; +import com.github.pires.obd.exceptions.ResponseException; +import com.github.pires.obd.exceptions.StoppedException; +import com.github.pires.obd.exceptions.UnableToConnectException; +import com.github.pires.obd.exceptions.UnknownErrorException; +import com.github.pires.obd.exceptions.UnsupportedCommandException; + /** * Base OBD command. * @@ -41,7 +49,7 @@ public abstract class ObdCommand { private long end; /** - * Default ctor to use + * Default Constructor to use * * @param command * the command to send @@ -59,7 +67,7 @@ private ObdCommand() { } /** - * Copy ctor. + * Copy Constructor. * * @param other * the ObdCommand to copy. @@ -71,7 +79,7 @@ public ObdCommand(ObdCommand other) { /** * Sends the OBD-II request and deals with the response. *

- * This method CAN be overriden in fake commands. + * This method CAN be override in fake commands. * * @param in * a {@link java.io.InputStream} object. diff --git a/src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java b/src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java index 2983b784..56026fa4 100644 --- a/src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java +++ b/src/main/java/com/github/pires/obd/commands/ObdMultiCommand.java @@ -27,7 +27,7 @@ public class ObdMultiCommand { private ArrayList commands; /** - * Default ctor. + * Default Constructor. */ public ObdMultiCommand() { this.commands = new ArrayList<>(); diff --git a/src/main/java/com/github/pires/obd/commands/SpeedCommand.java b/src/main/java/com/github/pires/obd/commands/SpeedCommand.java index 26774cd4..547a1d00 100644 --- a/src/main/java/com/github/pires/obd/commands/SpeedCommand.java +++ b/src/main/java/com/github/pires/obd/commands/SpeedCommand.java @@ -23,14 +23,14 @@ public class SpeedCommand extends ObdCommand implements SystemOfUnits { private int metricSpeed = 0; /** - * Default ctor. + * Default Constructor. */ public SpeedCommand() { super("01 0D"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.SpeedCommand} object. @@ -73,6 +73,7 @@ public float getImperialSpeed() { * * @return a float. */ + @Override public float getImperialUnit() { return metricSpeed * 0.621371192F; } @@ -84,6 +85,7 @@ public float getImperialUnit() { * * @return a {@link java.lang.String} object. */ + @Override public String getFormattedResult() { return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) : String.format("%d%s", getMetricSpeed(), getResultUnit()); diff --git a/src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java b/src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java index 7e460b87..ec5e1ad9 100644 --- a/src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java @@ -27,14 +27,14 @@ public class DistanceMILOnCommand extends ObdCommand implements SystemOfUnits { private int km = 0; /** - * Default ctor. + * Default Constructor. */ public DistanceMILOnCommand() { super("01 21"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a @@ -59,6 +59,7 @@ protected void performCalculations() { * * @return a {@link java.lang.String} object. */ + @Override public String getFormattedResult() { return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) : String.format("%d%s", km, getResultUnit()); diff --git a/src/main/java/com/github/pires/obd/commands/control/DistanceSinceCCCommand.java b/src/main/java/com/github/pires/obd/commands/control/DistanceSinceCCCommand.java index 9a36d2a5..0112c1fe 100644 --- a/src/main/java/com/github/pires/obd/commands/control/DistanceSinceCCCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/DistanceSinceCCCommand.java @@ -25,14 +25,14 @@ public class DistanceSinceCCCommand extends ObdCommand implements SystemOfUnits private int km = 0; /** - * Default ctor. + * Default Constructor. */ public DistanceSinceCCCommand() { super("01 31"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a @@ -57,6 +57,7 @@ protected void performCalculations() { * * @return a {@link java.lang.String} object. */ + @Override public String getFormattedResult() { return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) : String.format("%d%s", km, getResultUnit()); diff --git a/src/main/java/com/github/pires/obd/commands/control/DtcNumberCommand.java b/src/main/java/com/github/pires/obd/commands/control/DtcNumberCommand.java index abc228f5..800c7caf 100644 --- a/src/main/java/com/github/pires/obd/commands/control/DtcNumberCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/DtcNumberCommand.java @@ -30,14 +30,14 @@ public class DtcNumberCommand extends ObdCommand { private boolean milOn = false; /** - * Default ctor. + * Default Constructor. */ public DtcNumberCommand() { super("01 01"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.control.DtcNumberCommand} @@ -63,6 +63,7 @@ protected void performCalculations() { * * @return a {@link java.lang.String} object. */ + @Override public String getFormattedResult() { final String res = milOn ? "MIL is ON" : "MIL is OFF"; return res + codeCount + " codes"; diff --git a/src/main/java/com/github/pires/obd/commands/control/EquivalentRatioCommand.java b/src/main/java/com/github/pires/obd/commands/control/EquivalentRatioCommand.java index 168963b6..78b94bd1 100644 --- a/src/main/java/com/github/pires/obd/commands/control/EquivalentRatioCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/EquivalentRatioCommand.java @@ -30,14 +30,14 @@ public class EquivalentRatioCommand extends PercentageObdCommand { /** - * Default ctor. + * Default Constructor. */ public EquivalentRatioCommand() { super("01 44"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a @@ -65,7 +65,7 @@ protected void performCalculations() { * @return a double. */ public double getRatio() { - return (double) percentage; + return percentage; } /** {@inheritDoc} */ diff --git a/src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java b/src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java index 59aeb619..cde9c9ec 100644 --- a/src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java @@ -20,14 +20,14 @@ public class IgnitionMonitorCommand extends ObdCommand { private boolean ignitionOn = false; /** - * Default ctor. + * Default Constructor. */ public IgnitionMonitorCommand() { super("AT IGN"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link IgnitionMonitorCommand} object. diff --git a/src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java b/src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java index e904586e..af7e538b 100644 --- a/src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java @@ -27,14 +27,14 @@ public class ModuleVoltageCommand extends ObdCommand { private double voltage = 0.00; /** - * Default ctor. + * Default Constructor. */ public ModuleVoltageCommand() { super("01 42"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a diff --git a/src/main/java/com/github/pires/obd/commands/control/PendingTroubleCodesCommand.java b/src/main/java/com/github/pires/obd/commands/control/PendingTroubleCodesCommand.java index 52c30e83..8b04198b 100644 --- a/src/main/java/com/github/pires/obd/commands/control/PendingTroubleCodesCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/PendingTroubleCodesCommand.java @@ -12,12 +12,12 @@ */ package com.github.pires.obd.commands.control; -import com.github.pires.obd.commands.ObdCommand; -import com.github.pires.obd.enums.AvailableCommandNames; - import java.io.IOException; import java.io.InputStream; +import com.github.pires.obd.commands.ObdCommand; +import com.github.pires.obd.enums.AvailableCommandNames; + /** * It is not needed no know how many DTC are stored. Because when no DTC are * stored response will be NO DATA And where are more messages it will be stored @@ -45,7 +45,7 @@ public PendingTroubleCodesCommand() { } /** - * Copy ctor. + * Copy Constructor. * * @param other * a @@ -109,6 +109,7 @@ private byte hexStringToByteArray(char s) { * @return the formatted result of this command in string representation. * @deprecated use #getCalculatedResult instead */ + @Deprecated public String formatResult() { return codes.toString(); } diff --git a/src/main/java/com/github/pires/obd/commands/control/PermanentTroubleCodesCommand.java b/src/main/java/com/github/pires/obd/commands/control/PermanentTroubleCodesCommand.java index f425b87c..f1a28431 100644 --- a/src/main/java/com/github/pires/obd/commands/control/PermanentTroubleCodesCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/PermanentTroubleCodesCommand.java @@ -12,12 +12,12 @@ */ package com.github.pires.obd.commands.control; -import com.github.pires.obd.commands.ObdCommand; -import com.github.pires.obd.enums.AvailableCommandNames; - import java.io.IOException; import java.io.InputStream; +import com.github.pires.obd.commands.ObdCommand; +import com.github.pires.obd.enums.AvailableCommandNames; + /** * It is not needed no know how many DTC are stored. Because when no DTC are * stored response will be NO DATA And where are more messages it will be stored @@ -45,7 +45,7 @@ public PermanentTroubleCodesCommand() { } /** - * Copy ctor. + * Copy Constructor. * * @param other * a @@ -109,6 +109,7 @@ private byte hexStringToByteArray(char s) { * @return the formatted result of this command in string representation. * @deprecated use #getCalculatedResult instead */ + @Deprecated public String formatResult() { return codes.toString(); } diff --git a/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java b/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java index b0d339ea..b112184d 100644 --- a/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java @@ -12,12 +12,12 @@ */ package com.github.pires.obd.commands.control; -import com.github.pires.obd.commands.ObdCommand; -import com.github.pires.obd.enums.AvailableCommandNames; - import java.io.IOException; import java.io.InputStream; +import com.github.pires.obd.commands.ObdCommand; +import com.github.pires.obd.enums.AvailableCommandNames; + /** * It is not needed no know how many DTC are stored. Because when no DTC are * stored response will be NO DATA And where are more messages it will be stored @@ -45,7 +45,7 @@ public TroubleCodesCommand() { } /** - * Copy ctor. + * Copy Constructor. * * @param other * a @@ -109,6 +109,7 @@ private byte hexStringToByteArray(char s) { * @return the formatted result of this command in string representation. * @deprecated use #getCalculatedResult instead */ + @Deprecated public String formatResult() { return codes.toString(); } diff --git a/src/main/java/com/github/pires/obd/commands/control/VinCommand.java b/src/main/java/com/github/pires/obd/commands/control/VinCommand.java index 9ab81b52..00d12177 100644 --- a/src/main/java/com/github/pires/obd/commands/control/VinCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/VinCommand.java @@ -12,24 +12,25 @@ */ package com.github.pires.obd.commands.control; -import com.github.pires.obd.commands.PersistentCommand; -import com.github.pires.obd.enums.AvailableCommandNames; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.github.pires.obd.commands.PersistentCommand; +import com.github.pires.obd.enums.AvailableCommandNames; + public class VinCommand extends PersistentCommand { String vin = ""; /** - * Default ctor. + * Default Constructor. */ public VinCommand() { super("09 02"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.control.VinCommand} object. diff --git a/src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java b/src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java index b089c722..0b9d8472 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java @@ -24,14 +24,14 @@ public class AbsoluteLoadCommand extends PercentageObdCommand { /** - * Default ctor. + * Default Constructor. */ public AbsoluteLoadCommand() { super("01 43"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.engine.AbsoluteLoadCommand} diff --git a/src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java b/src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java index 3aeb094f..f9f53f07 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java @@ -24,14 +24,14 @@ public class MassAirFlowCommand extends ObdCommand { private float maf = -1.0f; /** - * Default ctor. + * Default Constructor. */ public MassAirFlowCommand() { super("01 10"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.engine.MassAirFlowCommand} diff --git a/src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java b/src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java index 4c0ccf72..2bea81fe 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java @@ -22,14 +22,14 @@ public class OilTempCommand extends TemperatureCommand { /** - * Default ctor. + * Default Constructor. */ public OilTempCommand() { super("01 5C"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.engine.OilTempCommand} diff --git a/src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java b/src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java index 5d130bd9..e5b52c4e 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java @@ -24,14 +24,14 @@ public class RPMCommand extends ObdCommand { private int rpm = -1; /** - * Default ctor. + * Default Constructor. */ public RPMCommand() { super("01 0C"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.engine.RPMCommand} object. diff --git a/src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java b/src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java index fa6892a8..680556a5 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java @@ -24,14 +24,14 @@ public class RuntimeCommand extends ObdCommand { private int value = 0; /** - * Default ctor. + * Default Constructor. */ public RuntimeCommand() { super("01 1F"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.engine.RuntimeCommand} diff --git a/src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java b/src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java index 4887f964..71999bf5 100644 --- a/src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java +++ b/src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java @@ -22,14 +22,14 @@ public class ThrottlePositionCommand extends PercentageObdCommand { /** - * Default ctor. + * Default Constructor. */ public ThrottlePositionCommand() { super("01 11"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a diff --git a/src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java b/src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java index 3ac6a5f6..17c0d43c 100644 --- a/src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java +++ b/src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java @@ -25,7 +25,7 @@ public class FindFuelTypeCommand extends ObdCommand { private int fuelType = 0; /** - * Default ctor. + * Default Constructor. */ public FindFuelTypeCommand() { super("01 51"); diff --git a/src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java b/src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java index 789c6335..608974a6 100644 --- a/src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java +++ b/src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java @@ -24,7 +24,7 @@ public class FuelTrimCommand extends PercentageObdCommand { private final FuelTrim bank; /** - * Default ctor. + * Default Constructor. *

* Will read the bank from parameters and construct the command accordingly. * Please, see FuelTrim enum for more details. @@ -73,6 +73,7 @@ protected void performCalculations() { * @return the readed Fuel Trim percentage value. * @deprecated use #getCalculatedResult() */ + @Deprecated public final float getValue() { return percentage; } diff --git a/src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java b/src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java index d88951b7..89d37acb 100644 --- a/src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java @@ -21,14 +21,14 @@ public class IntakeManifoldPressureCommand extends PressureCommand { /** - * Default ctor. + * Default Constructor. */ public IntakeManifoldPressureCommand() { super("01 0B"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a diff --git a/src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java b/src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java index b9ff4839..7c0a1835 100644 --- a/src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java @@ -35,7 +35,7 @@ public PressureCommand(String cmd) { } /** - * Copy ctor. + * Copy Constructor. * * @param other * a {@link com.github.pires.obd.commands.pressure.PressureCommand} @@ -62,6 +62,7 @@ protected int preparePressureValue() { * performCalculations. *

*/ + @Override protected void performCalculations() { // ignore first two bytes [hh hh] of the response pressure = preparePressureValue(); @@ -92,6 +93,7 @@ public int getMetricUnit() { * * @return the pressure in psi */ + @Override public float getImperialUnit() { return pressure * 0.145037738F; } diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java index d217fd6f..1c1538a7 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java @@ -21,7 +21,7 @@ public abstract class AvailablePidsCommand extends PersistentCommand { /** - * Default ctor. + * Default Constructor. * * @param command * a {@link java.lang.String} object. @@ -31,7 +31,7 @@ public AvailablePidsCommand(String command) { } /** - * Copy ctor. + * Copy Constructor. * * @param other * a diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java index 8ab46064..6cf24321 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java @@ -21,14 +21,14 @@ public class AvailablePidsCommand_01_20 extends AvailablePidsCommand { /** - * Default ctor. + * Default Constructor. */ public AvailablePidsCommand_01_20() { super("01 00"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java index 1b301562..d887d5d2 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java @@ -21,14 +21,14 @@ public class AvailablePidsCommand_21_40 extends AvailablePidsCommand { /** - * Default ctor. + * Default Constructor. */ public AvailablePidsCommand_21_40() { super("01 20"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a diff --git a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java index 3ea01139..7e4fe7f7 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java @@ -21,14 +21,14 @@ public class AvailablePidsCommand_41_60 extends AvailablePidsCommand { /** - * Default ctor. + * Default Constructor. */ public AvailablePidsCommand_41_60() { super("01 40"); } /** - * Copy ctor. + * Copy Constructor. * * @param other * a diff --git a/src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java b/src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java index 674ff40a..f96dbf93 100644 --- a/src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java +++ b/src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java @@ -31,7 +31,7 @@ public ObdProtocolCommand(String command) { } /** - * Copy ctor. + * Copy Constructor. * * @param other * the ObdCommand to copy. @@ -45,6 +45,7 @@ public ObdProtocolCommand(ObdProtocolCommand other) { * performCalculations. *

*/ + @Override protected void performCalculations() { // ignore } @@ -54,6 +55,7 @@ protected void performCalculations() { * fillBuffer. *

*/ + @Override protected void fillBuffer() { // settings commands don't return a value appropriate to place into the // buffer, so do nothing diff --git a/src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java b/src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java index 156603f5..8215b6e4 100644 --- a/src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java +++ b/src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java @@ -24,7 +24,7 @@ public abstract class TemperatureCommand extends ObdCommand implements SystemOfU private float temperature = 0.0f; /** - * Default ctor. + * Default Constructor. * * @param cmd * a {@link java.lang.String} object. @@ -34,7 +34,7 @@ public TemperatureCommand(String cmd) { } /** - * Copy ctor. + * Copy Constructor. * * @param other * a @@ -93,6 +93,7 @@ public float getTemperature() { * * @return the temperature in Fahrenheit. */ + @Override public float getImperialUnit() { return temperature * 1.8f + 32; } @@ -115,6 +116,7 @@ public float getKelvin() { * * @return the OBD command name. */ + @Override public abstract String getName(); } diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml index 08b78817..b6e48910 100644 --- a/src/test/resources/testng.xml +++ b/src/test/resources/testng.xml @@ -12,13 +12,20 @@ the License. --> + - - - - - + object-factory="org.powermock.modules.testng.PowerMockObjectFactory"> + + + + + \ No newline at end of file From 25cec8c98fb5964dbd5e3949ec920d9daa7d7f96 Mon Sep 17 00:00:00 2001 From: Elton Viana Date: Thu, 13 Jul 2017 16:17:17 -0300 Subject: [PATCH 3/3] Use a TreeSet to store DTC's disalowing duplicates and sorting them alphabetically --- .../commands/control/TroubleCodesCommand.java | 44 +++++++++++-------- .../obd/commands/TroubleCodesCommandTest.java | 4 +- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java b/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java index b112184d..4a004533 100644 --- a/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java +++ b/src/main/java/com/github/pires/obd/commands/control/TroubleCodesCommand.java @@ -14,6 +14,8 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Set; +import java.util.TreeSet; import com.github.pires.obd.commands.ObdCommand; import com.github.pires.obd.enums.AvailableCommandNames; @@ -32,7 +34,7 @@ public class TroubleCodesCommand extends ObdCommand { /** Constant hexArray="0123456789ABCDEF".toCharArray() */ protected final static char[] hexArray = "0123456789ABCDEF".toCharArray(); - protected StringBuilder codes = null; + protected Set troubleCodes = null; /** *

@@ -41,7 +43,7 @@ public class TroubleCodesCommand extends ObdCommand { */ public TroubleCodesCommand() { super("03"); - codes = new StringBuilder(); + troubleCodes = new TreeSet<>(); } /** @@ -54,7 +56,7 @@ public TroubleCodesCommand() { */ public TroubleCodesCommand(TroubleCodesCommand other) { super(other); - codes = new StringBuilder(); + troubleCodes = new TreeSet<>(); } /** {@inheritDoc} */ @@ -67,18 +69,22 @@ protected void fillBuffer() { protected void performCalculations() { final String result = getResult(); String workingData; - int startIndex = 0;// Header size. + int startIndex = 0; // Header size. String canOneFrame = result.replaceAll("[\r\n]", ""); int canOneFrameLength = canOneFrame.length(); - if (canOneFrameLength <= 16 && canOneFrameLength % 4 == 0) {// CAN(ISO-15765) protocol one frame. - workingData = canOneFrame;// 43yy{codes} - startIndex = 4;// Header is 43yy, yy showing the number of data items. - } else if (result.contains(":")) {// CAN(ISO-15765) protocol two and more frames. - workingData = result.replaceAll("[\r\n].:", "");// xxx43yy{codes} - startIndex = 7;// Header is xxx43yy, xxx is bytes of information to follow, yy showing the - // number of data items. - } else {// ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. + + // CAN(ISO-15765) protocol one frame. + if (canOneFrameLength <= 16 && canOneFrameLength % 4 == 0) { + workingData = canOneFrame; // 43yy{codes} + startIndex = 4; // Header is 43yy, yy showing the number of data items. + // CAN(ISO-15765) protocol two and more frames. + } else if (result.contains(":")) { + workingData = result.replaceAll("[\r\n].:", ""); // xxx43yy{codes} + startIndex = 7; // Header is xxx43yy, xxx is bytes of information to follow, yy showing the + // number of data items. + // ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. + } else { workingData = result.replaceAll("^43|[\r\n]43|[\r\n]", ""); } for (int begin = startIndex; begin < workingData.length(); begin += 4) { @@ -92,8 +98,7 @@ protected void performCalculations() { if ("P0000".equals(dtc)) { return; } - codes.append(dtc); - codes.append('\n'); + troubleCodes.add(dtc); } } @@ -111,13 +116,17 @@ private byte hexStringToByteArray(char s) { */ @Deprecated public String formatResult() { - return codes.toString(); + return getCalculatedResult(); } /** {@inheritDoc} */ @Override public String getCalculatedResult() { - return String.valueOf(codes); + StringBuilder sb = new StringBuilder(); + for (String code : troubleCodes) { + sb.append(code).append("\n"); + } + return sb.toString(); } /** {@inheritDoc} */ @@ -145,13 +154,12 @@ protected void readRawData(InputStream in) throws IOException { } rawData = res.toString().trim(); - } /** {@inheritDoc} */ @Override public String getFormattedResult() { - return codes.toString(); + return getCalculatedResult(); } /** {@inheritDoc} */ diff --git a/src/test/java/com/github/pires/obd/commands/TroubleCodesCommandTest.java b/src/test/java/com/github/pires/obd/commands/TroubleCodesCommandTest.java index 9572efa1..ab8e727b 100644 --- a/src/test/java/com/github/pires/obd/commands/TroubleCodesCommandTest.java +++ b/src/test/java/com/github/pires/obd/commands/TroubleCodesCommandTest.java @@ -95,9 +95,9 @@ public void twoFramesWithFourDTC() throws IOException { expectLastCall().andReturn((byte) '>'); replayAll(); - String res = "P0003\n"; + String res = "B21AB\n"; res += "C1104\n"; - res += "B21AB\n"; + res += "P0003\n"; res += "U3106\n"; // call the method to test