getFormattedResult.
69 | * 70 | * @return a {@link java.lang.String} object. 71 | */ 72 | public String getFormattedResult() { 73 | StringBuilder res = new StringBuilder(); 74 | for (ObdCommand command : commands) 75 | res.append(command.getFormattedResult()).append(","); 76 | 77 | return res.toString(); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/PercentageObdCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | /** 16 | * Abstract class for percentage commands. 17 | * 18 | */ 19 | public abstract class PercentageObdCommand extends ObdCommand { 20 | 21 | protected float percentage = 0f; 22 | 23 | /** 24 | *Constructor for PercentageObdCommand.
25 | * 26 | * @param command a {@link java.lang.String} object. 27 | */ 28 | public PercentageObdCommand(String command) { 29 | super(command); 30 | } 31 | 32 | /** 33 | *Constructor for PercentageObdCommand.
34 | * 35 | * @param other a {@link com.github.pires.obd.commands.PercentageObdCommand} object. 36 | */ 37 | public PercentageObdCommand(PercentageObdCommand other) { 38 | super(other); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | protected void performCalculations() { 44 | // ignore first two bytes [hh hh] of the response 45 | percentage = (buffer.get(2) * 100.0f) / 255.0f; 46 | } 47 | 48 | /** {@inheritDoc} */ 49 | @Override 50 | public String getFormattedResult() { 51 | return String.format("%.1f%s", percentage, getResultUnit()); 52 | } 53 | 54 | /** 55 | *Getter for the field percentage
.
Constructor for PersistentCommand.
33 | * 34 | * @param command a {@link java.lang.String} object. 35 | */ 36 | public PersistentCommand(String command) { 37 | super(command); 38 | } 39 | 40 | /** 41 | *Constructor for PersistentCommand.
42 | * 43 | * @param other a {@link com.github.pires.obd.commands.ObdCommand} object. 44 | */ 45 | public PersistentCommand(ObdCommand other) { 46 | this(other.cmd); 47 | } 48 | 49 | /** 50 | *reset.
51 | */ 52 | public static void reset() { 53 | knownValues = new HashMap<>(); 54 | knownBuffers = new HashMap<>(); 55 | } 56 | 57 | /** 58 | *knows.
59 | * 60 | * @param cmd a {@link java.lang.Class} object. 61 | * @return a boolean. 62 | */ 63 | public static boolean knows(Class cmd) { 64 | String key = cmd.getSimpleName(); 65 | return knownValues.containsKey(key); 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | protected void readResult(InputStream in) throws IOException { 71 | super.readResult(in); 72 | String key = getClass().getSimpleName(); 73 | knownValues.put(key, rawData); 74 | knownBuffers.put(key, new ArrayList<>(buffer)); 75 | } 76 | 77 | /** {@inheritDoc} */ 78 | @Override 79 | public void run(InputStream in, OutputStream out) throws IOException, InterruptedException { 80 | String key = getClass().getSimpleName(); 81 | if (knownValues.containsKey(key)) { 82 | rawData = knownValues.get(key); 83 | buffer = knownBuffers.get(key); 84 | performCalculations(); 85 | } else { 86 | super.run(in, out); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/SpeedCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Current speed. 19 | * 20 | */ 21 | public class SpeedCommand extends ObdCommand implements SystemOfUnits { 22 | 23 | private int metricSpeed = 0; 24 | 25 | /** 26 | * Default ctor. 27 | */ 28 | public SpeedCommand() { 29 | super("01 0D"); 30 | } 31 | 32 | /** 33 | * Copy ctor. 34 | * 35 | * @param other a {@link com.github.pires.obd.commands.SpeedCommand} object. 36 | */ 37 | public SpeedCommand(SpeedCommand other) { 38 | super(other); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | protected void performCalculations() { 44 | // Ignore first two bytes [hh hh] of the response. 45 | metricSpeed = buffer.get(2); 46 | } 47 | 48 | /** 49 | *Getter for the field metricSpeed
.
getImperialSpeed.
59 | * 60 | * @return the speed in imperial units. 61 | */ 62 | public float getImperialSpeed() { 63 | return getImperialUnit(); 64 | } 65 | 66 | /** 67 | * Convert from km/h to mph 68 | * 69 | * @return a float. 70 | */ 71 | public float getImperialUnit() { 72 | return metricSpeed * 0.621371192F; 73 | } 74 | 75 | /** 76 | *getFormattedResult.
77 | * 78 | * @return a {@link java.lang.String} object. 79 | */ 80 | public String getFormattedResult() { 81 | return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) 82 | : String.format("%d%s", getMetricSpeed(), getResultUnit()); 83 | } 84 | 85 | /** {@inheritDoc} */ 86 | @Override 87 | public String getCalculatedResult() { 88 | return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(getMetricSpeed()); 89 | } 90 | 91 | /** {@inheritDoc} */ 92 | @Override 93 | public String getResultUnit() { 94 | return useImperialUnits ? "mph" : "km/h"; 95 | } 96 | 97 | /** {@inheritDoc} */ 98 | @Override 99 | public String getName() { 100 | return AvailableCommandNames.SPEED.getValue(); 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/SystemOfUnits.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | /** 16 | * This interface will define methods for converting to/from imperial units and 17 | * from/to metric units. 18 | * 19 | */ 20 | public interface SystemOfUnits { 21 | 22 | /** 23 | *getImperialUnit.
24 | * 25 | * @return a float. 26 | */ 27 | float getImperialUnit(); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/control/DistanceMILOnCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.control; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.commands.SystemOfUnits; 17 | import com.github.pires.obd.enums.AvailableCommandNames; 18 | 19 | /** 20 | *DistanceMILOnCommand class.
21 | * 22 | */ 23 | public class DistanceMILOnCommand extends ObdCommand 24 | implements SystemOfUnits { 25 | 26 | private int km = 0; 27 | 28 | /** 29 | * Default ctor. 30 | */ 31 | public DistanceMILOnCommand() { 32 | super("01 21"); 33 | } 34 | 35 | /** 36 | * Copy ctor. 37 | * 38 | * @param other a {@link com.github.pires.obd.commands.control.DistanceMILOnCommand} object. 39 | */ 40 | public DistanceMILOnCommand( 41 | DistanceMILOnCommand other) { 42 | super(other); 43 | } 44 | 45 | /** {@inheritDoc} */ 46 | @Override 47 | protected void performCalculations() { 48 | // ignore first two bytes [01 31] of the response 49 | km = buffer.get(2) * 256 + buffer.get(3); 50 | } 51 | 52 | /** 53 | *getFormattedResult.
54 | * 55 | * @return a {@link java.lang.String} object. 56 | */ 57 | public String getFormattedResult() { 58 | return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) 59 | : String.format("%d%s", km, getResultUnit()); 60 | } 61 | 62 | /** {@inheritDoc} */ 63 | @Override 64 | public String getCalculatedResult() { 65 | return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(km); 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | public String getResultUnit() { 71 | return useImperialUnits ? "m" : "km"; 72 | } 73 | 74 | /** {@inheritDoc} */ 75 | @Override 76 | public float getImperialUnit() { 77 | return km * 0.621371192F; 78 | } 79 | 80 | /** 81 | *Getter for the field km
.
getFormattedResult.
54 | * 55 | * @return a {@link java.lang.String} object. 56 | */ 57 | public String getFormattedResult() { 58 | return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) 59 | : String.format("%d%s", km, getResultUnit()); 60 | } 61 | 62 | /** {@inheritDoc} */ 63 | @Override 64 | public String getCalculatedResult() { 65 | return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(km); 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | public String getResultUnit() { 71 | return useImperialUnits ? "m" : "km"; 72 | } 73 | 74 | /** {@inheritDoc} */ 75 | @Override 76 | public float getImperialUnit() { 77 | return km * 0.621371192F; 78 | } 79 | 80 | /** 81 | *Getter for the field km
.
Setter for the field km
.
22 | * Perhaps in the future we'll extend this to read the 3rd, 4th and 5th bytes of 23 | * the response in order to store information about the availability and 24 | * completeness of certain on-board tests. 25 | * 26 | */ 27 | public class DtcNumberCommand extends ObdCommand { 28 | 29 | private int codeCount = 0; 30 | private boolean milOn = false; 31 | 32 | /** 33 | * Default ctor. 34 | */ 35 | public DtcNumberCommand() { 36 | super("01 01"); 37 | } 38 | 39 | /** 40 | * Copy ctor. 41 | * 42 | * @param other a {@link com.github.pires.obd.commands.control.DtcNumberCommand} object. 43 | */ 44 | public DtcNumberCommand(DtcNumberCommand other) { 45 | super(other); 46 | } 47 | 48 | /** {@inheritDoc} */ 49 | @Override 50 | protected void performCalculations() { 51 | // ignore first two bytes [hh hh] of the response 52 | final int mil = buffer.get(2); 53 | milOn = (mil & 0x80) == 128; 54 | codeCount = mil & 0x7F; 55 | } 56 | 57 | /** 58 | *
getFormattedResult.
59 | * 60 | * @return a {@link java.lang.String} object. 61 | */ 62 | public String getFormattedResult() { 63 | final String res = milOn ? "MIL is ON" : "MIL is OFF"; 64 | return res + codeCount + " codes"; 65 | } 66 | 67 | /** {@inheritDoc} */ 68 | @Override 69 | public String getCalculatedResult() { 70 | return String.valueOf(codeCount); 71 | } 72 | 73 | /** 74 | *getTotalAvailableCodes.
75 | * 76 | * @return the number of trouble codes currently flaggd in the ECU. 77 | */ 78 | public int getTotalAvailableCodes() { 79 | return codeCount; 80 | } 81 | 82 | /** 83 | *Getter for the field milOn
.
23 | * To obtain the actual air/fuel ratio being commanded, multiply the 24 | * stoichiometric A/F ratio by the equivalence ratio. For example, gasoline, 25 | * stoichiometric is 14.64:1 ratio. If the fuel control system was commanded an 26 | * equivalence ratio of 0.95, the commanded A/F ratio to the engine would be 27 | * 14.64 * 0.95 = 13.9 A/F. 28 | * 29 | */ 30 | public class EquivalentRatioCommand extends PercentageObdCommand { 31 | 32 | 33 | /** 34 | * Default ctor. 35 | */ 36 | public EquivalentRatioCommand() { 37 | super("01 44"); 38 | } 39 | 40 | /** 41 | * Copy ctor. 42 | * 43 | * @param other a {@link com.github.pires.obd.commands.control.EquivalentRatioCommand} object. 44 | */ 45 | public EquivalentRatioCommand(EquivalentRatioCommand other) { 46 | super(other); 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | protected void performCalculations() { 52 | // ignore first two bytes [hh hh] of the response 53 | int a = buffer.get(2); 54 | int b = buffer.get(3); 55 | percentage = (a * 256 + b) / 32768; 56 | } 57 | 58 | 59 | /** 60 | *
getRatio.
61 | * 62 | * @return a double. 63 | */ 64 | public double getRatio() { 65 | return (double) percentage; 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | public String getName() { 71 | return AvailableCommandNames.EQUIV_RATIO.getValue(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/control/IgnitionMonitorCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.control; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | public class IgnitionMonitorCommand extends ObdCommand { 19 | 20 | private boolean ignitionOn = false; 21 | 22 | /** 23 | * Default ctor. 24 | */ 25 | public IgnitionMonitorCommand() { 26 | super("AT IGN"); 27 | } 28 | 29 | /** 30 | * Copy ctor. 31 | * 32 | * @param other a {@link IgnitionMonitorCommand} object. 33 | */ 34 | public IgnitionMonitorCommand(IgnitionMonitorCommand other) { 35 | super(other); 36 | } 37 | 38 | @Override 39 | protected void performCalculations() { 40 | final String result = getResult(); 41 | ignitionOn = result.equalsIgnoreCase("ON"); 42 | } 43 | 44 | @Override 45 | public String getFormattedResult() { 46 | return getResult(); 47 | } 48 | 49 | @Override 50 | public String getName() { 51 | return AvailableCommandNames.IGNITION_MONITOR.getValue(); 52 | } 53 | 54 | @Override 55 | public String getCalculatedResult() { 56 | return getResult(); 57 | } 58 | 59 | @Override 60 | protected void fillBuffer() { 61 | } 62 | 63 | public boolean isIgnitionOn() { 64 | return ignitionOn; 65 | } 66 | } 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/control/ModuleVoltageCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.control; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | *ModuleVoltageCommand class.
20 | * 21 | */ 22 | public class ModuleVoltageCommand extends ObdCommand { 23 | 24 | // Equivalent ratio (V) 25 | private double voltage = 0.00; 26 | 27 | /** 28 | * Default ctor. 29 | */ 30 | public ModuleVoltageCommand() { 31 | super("01 42"); 32 | } 33 | 34 | /** 35 | * Copy ctor. 36 | * 37 | * @param other a {@link com.github.pires.obd.commands.control.ModuleVoltageCommand} object. 38 | */ 39 | public ModuleVoltageCommand(ModuleVoltageCommand other) { 40 | super(other); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | protected void performCalculations() { 46 | // ignore first two bytes [hh hh] of the response 47 | int a = buffer.get(2); 48 | int b = buffer.get(3); 49 | voltage = (a * 256 + b) / 1000; 50 | } 51 | 52 | /** {@inheritDoc} */ 53 | @Override 54 | public String getFormattedResult() { 55 | return String.format("%.1f%s", voltage, getResultUnit()); 56 | } 57 | 58 | /** {@inheritDoc} */ 59 | @Override 60 | public String getResultUnit() { 61 | return "V"; 62 | } 63 | 64 | /** {@inheritDoc} */ 65 | @Override 66 | public String getCalculatedResult() { 67 | return String.valueOf(voltage); 68 | } 69 | 70 | /** 71 | *Getter for the field voltage
.
Constructor for TimingAdvanceCommand.
26 | */ 27 | public TimingAdvanceCommand() { 28 | super("01 0E"); 29 | } 30 | 31 | /** 32 | *Constructor for TimingAdvanceCommand.
33 | * 34 | * @param other a {@link com.github.pires.obd.commands.control.TimingAdvanceCommand} object. 35 | */ 36 | public TimingAdvanceCommand(TimingAdvanceCommand other) { 37 | super(other); 38 | } 39 | 40 | /** {@inheritDoc} */ 41 | @Override 42 | public String getName() { 43 | return AvailableCommandNames.TIMING_ADVANCE.getValue(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/control/VinCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.control; 14 | 15 | import com.github.pires.obd.commands.PersistentCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | import java.util.regex.Matcher; 18 | import java.util.regex.Pattern; 19 | 20 | public class VinCommand extends PersistentCommand { 21 | 22 | String vin = ""; 23 | 24 | /** 25 | * Default ctor. 26 | */ 27 | public VinCommand() { 28 | super("09 02"); 29 | } 30 | 31 | /** 32 | * Copy ctor. 33 | * 34 | * @param other a {@link com.github.pires.obd.commands.control.VinCommand} object. 35 | */ 36 | public VinCommand(VinCommand other) { 37 | super(other); 38 | } 39 | 40 | /** 41 | * {@inheritDoc} 42 | */ 43 | @Override 44 | protected void performCalculations() { 45 | final String result = getResult(); 46 | String workingData; 47 | if (result.contains(":")) {//CAN(ISO-15765) protocol. 48 | workingData = result.replaceAll(".:", "").substring(9);//9 is xxx490201, xxx is bytes of information to follow. 49 | Matcher m = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE).matcher(convertHexToString(workingData)); 50 | if(m.find()) workingData = result.replaceAll("0:49", "").replaceAll(".:", ""); 51 | } else {//ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) protocols. 52 | workingData = result.replaceAll("49020.", ""); 53 | } 54 | vin = convertHexToString(workingData).replaceAll("[\u0000-\u001f]", ""); 55 | } 56 | 57 | /** 58 | * {@inheritDoc} 59 | */ 60 | @Override 61 | public String getFormattedResult() { 62 | return String.valueOf(vin); 63 | } 64 | 65 | /** 66 | * {@inheritDoc} 67 | */ 68 | @Override 69 | public String getName() { 70 | return AvailableCommandNames.VIN.getValue(); 71 | } 72 | 73 | /** 74 | * {@inheritDoc} 75 | */ 76 | @Override 77 | public String getCalculatedResult() { 78 | return String.valueOf(vin); 79 | } 80 | 81 | /** 82 | * {@inheritDoc} 83 | */ 84 | @Override 85 | protected void fillBuffer() { 86 | } 87 | 88 | public String convertHexToString(String hex) { 89 | StringBuilder sb = new StringBuilder(); 90 | //49204c6f7665204a617661 split into two characters 49, 20, 4c... 91 | for (int i = 0; i < hex.length() - 1; i += 2) { 92 | 93 | //grab the hex in pairs 94 | String output = hex.substring(i, (i + 2)); 95 | //convert hex to decimal 96 | int decimal = Integer.parseInt(output, 16); 97 | //convert the decimal to character 98 | sb.append((char) decimal); 99 | } 100 | return sb.toString(); 101 | } 102 | } 103 | 104 | 105 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/engine/AbsoluteLoadCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.engine; 14 | 15 | import com.github.pires.obd.commands.PercentageObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | *AbsoluteLoadCommand class.
20 | * 21 | */ 22 | public class AbsoluteLoadCommand extends PercentageObdCommand { 23 | 24 | /** 25 | * Default ctor. 26 | */ 27 | public AbsoluteLoadCommand() { 28 | super("01 43"); 29 | } 30 | 31 | /** 32 | * Copy ctor. 33 | * 34 | * @param other a {@link com.github.pires.obd.commands.engine.AbsoluteLoadCommand} object. 35 | */ 36 | public AbsoluteLoadCommand(AbsoluteLoadCommand other) { 37 | super(other); 38 | } 39 | 40 | /** {@inheritDoc} */ 41 | @Override 42 | protected void performCalculations() { 43 | // ignore first two bytes [hh hh] of the response 44 | int a = buffer.get(2); 45 | int b = buffer.get(3); 46 | percentage = (a * 256 + b) * 100 / 255; 47 | } 48 | 49 | /** 50 | *getRatio.
51 | * 52 | * @return a double. 53 | */ 54 | public double getRatio() { 55 | return percentage; 56 | } 57 | 58 | /** {@inheritDoc} */ 59 | @Override 60 | public String getName() { 61 | return AvailableCommandNames.ABS_LOAD.getValue(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/engine/LoadCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.engine; 14 | 15 | import com.github.pires.obd.commands.PercentageObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * Calculated Engine Load value. 20 | * 21 | */ 22 | public class LoadCommand extends PercentageObdCommand { 23 | 24 | /** 25 | *Constructor for LoadCommand.
26 | */ 27 | public LoadCommand() { 28 | super("01 04"); 29 | } 30 | 31 | /** 32 | *Constructor for LoadCommand.
33 | * 34 | * @param other a {@link com.github.pires.obd.commands.engine.LoadCommand} object. 35 | */ 36 | public LoadCommand(LoadCommand other) { 37 | super(other); 38 | } 39 | 40 | /* 41 | * (non-Javadoc) 42 | * 43 | * @see pt.lighthouselabs.obd.commands.ObdCommand#getName() 44 | */ 45 | /** {@inheritDoc} */ 46 | @Override 47 | public String getName() { 48 | return AvailableCommandNames.ENGINE_LOAD.getValue(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/engine/MassAirFlowCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.engine; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * Mass Air Flow (MAF) 20 | * 21 | */ 22 | public class MassAirFlowCommand extends ObdCommand { 23 | 24 | private float maf = -1.0f; 25 | 26 | /** 27 | * Default ctor. 28 | */ 29 | public MassAirFlowCommand() { 30 | super("01 10"); 31 | } 32 | 33 | /** 34 | * Copy ctor. 35 | * 36 | * @param other a {@link com.github.pires.obd.commands.engine.MassAirFlowCommand} object. 37 | */ 38 | public MassAirFlowCommand(MassAirFlowCommand other) { 39 | super(other); 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | protected void performCalculations() { 45 | // ignore first two bytes [hh hh] of the response 46 | maf = (buffer.get(2) * 256 + buffer.get(3)) / 100.0f; 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public String getFormattedResult() { 52 | return String.format("%.2f%s", maf, getResultUnit()); 53 | } 54 | 55 | /** {@inheritDoc} */ 56 | @Override 57 | public String getCalculatedResult() { 58 | return String.valueOf(maf); 59 | } 60 | 61 | /** {@inheritDoc} */ 62 | @Override 63 | public String getResultUnit() { 64 | return "g/s"; 65 | } 66 | 67 | /** 68 | *getMAF.
69 | * 70 | * @return MAF value for further calculus. 71 | */ 72 | public double getMAF() { 73 | return maf; 74 | } 75 | 76 | /** {@inheritDoc} */ 77 | @Override 78 | public String getName() { 79 | return AvailableCommandNames.MAF.getValue(); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/engine/OilTempCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.engine; 14 | 15 | import com.github.pires.obd.commands.temperature.TemperatureCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * Displays the current engine Oil temperature. 20 | * 21 | */ 22 | public class OilTempCommand extends TemperatureCommand { 23 | 24 | /** 25 | * Default ctor. 26 | */ 27 | public OilTempCommand() { 28 | super("01 5C"); 29 | } 30 | 31 | /** 32 | * Copy ctor. 33 | * 34 | * @param other a {@link com.github.pires.obd.commands.engine.OilTempCommand} object. 35 | */ 36 | public OilTempCommand(OilTempCommand other) { 37 | super(other); 38 | } 39 | 40 | /** {@inheritDoc} */ 41 | @Override 42 | public String getName() { 43 | return AvailableCommandNames.ENGINE_OIL_TEMP.getValue(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/engine/RPMCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.engine; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * Displays the current engine revolutions per minute (RPM). 20 | * 21 | */ 22 | public class RPMCommand extends ObdCommand { 23 | 24 | private int rpm = -1; 25 | 26 | /** 27 | * Default ctor. 28 | */ 29 | public RPMCommand() { 30 | super("01 0C"); 31 | } 32 | 33 | /** 34 | * Copy ctor. 35 | * 36 | * @param other a {@link com.github.pires.obd.commands.engine.RPMCommand} object. 37 | */ 38 | public RPMCommand(RPMCommand other) { 39 | super(other); 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | protected void performCalculations() { 45 | // ignore first two bytes [41 0C] of the response((A*256)+B)/4 46 | rpm = (buffer.get(2) * 256 + buffer.get(3)) / 4; 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public String getFormattedResult() { 52 | return String.format("%d%s", rpm, getResultUnit()); 53 | } 54 | 55 | /** {@inheritDoc} */ 56 | @Override 57 | public String getCalculatedResult() { 58 | return String.valueOf(rpm); 59 | } 60 | 61 | /** {@inheritDoc} */ 62 | @Override 63 | public String getResultUnit() { 64 | return "RPM"; 65 | } 66 | 67 | /** {@inheritDoc} */ 68 | @Override 69 | public String getName() { 70 | return AvailableCommandNames.ENGINE_RPM.getValue(); 71 | } 72 | 73 | /** 74 | *getRPM.
75 | * 76 | * @return a int. 77 | */ 78 | public int getRPM() { 79 | return rpm; 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/engine/RuntimeCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.engine; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * Engine runtime. 20 | * 21 | */ 22 | public class RuntimeCommand extends ObdCommand { 23 | 24 | private int value = 0; 25 | 26 | /** 27 | * Default ctor. 28 | */ 29 | public RuntimeCommand() { 30 | super("01 1F"); 31 | } 32 | 33 | /** 34 | * Copy ctor. 35 | * 36 | * @param other a {@link com.github.pires.obd.commands.engine.RuntimeCommand} object. 37 | */ 38 | public RuntimeCommand(RuntimeCommand other) { 39 | super(other); 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | protected void performCalculations() { 45 | // ignore first two bytes [01 0C] of the response 46 | value = buffer.get(2) * 256 + buffer.get(3); 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public String getFormattedResult() { 52 | // determine time 53 | final String hh = String.format("%02d", value / 3600); 54 | final String mm = String.format("%02d", (value % 3600) / 60); 55 | final String ss = String.format("%02d", value % 60); 56 | return String.format("%s:%s:%s", hh, mm, ss); 57 | } 58 | 59 | /** {@inheritDoc} */ 60 | @Override 61 | public String getCalculatedResult() { 62 | return String.valueOf(value); 63 | } 64 | 65 | /** {@inheritDoc} */ 66 | @Override 67 | public String getResultUnit() { 68 | return "s"; 69 | } 70 | 71 | /** {@inheritDoc} */ 72 | @Override 73 | public String getName() { 74 | return AvailableCommandNames.ENGINE_RUNTIME.getValue(); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/engine/ThrottlePositionCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.engine; 14 | 15 | import com.github.pires.obd.commands.PercentageObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * Read the throttle position in percentage. 20 | * 21 | */ 22 | public class ThrottlePositionCommand extends PercentageObdCommand { 23 | 24 | /** 25 | * Default ctor. 26 | */ 27 | public ThrottlePositionCommand() { 28 | super("01 11"); 29 | } 30 | 31 | /** 32 | * Copy ctor. 33 | * 34 | * @param other a {@link com.github.pires.obd.commands.engine.ThrottlePositionCommand} object. 35 | */ 36 | public ThrottlePositionCommand(ThrottlePositionCommand other) { 37 | super(other); 38 | } 39 | 40 | /** {@inheritDoc} */ 41 | @Override 42 | public String getName() { 43 | return AvailableCommandNames.THROTTLE_POS.getValue(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/fuel/AirFuelRatioCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.fuel; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * AFR 20 | * 21 | */ 22 | public class AirFuelRatioCommand extends ObdCommand { 23 | 24 | private float afr = 0; 25 | 26 | /** 27 | *Constructor for AirFuelRatioCommand.
28 | */ 29 | public AirFuelRatioCommand() { 30 | super("01 44"); 31 | } 32 | 33 | /** {@inheritDoc} */ 34 | @Override 35 | protected void performCalculations() { 36 | // ignore first two bytes [01 44] of the response 37 | float A = buffer.get(2); 38 | float B = buffer.get(3); 39 | afr = (((A * 256) + B) / 32768) * 14.7f;//((A*256)+B)/32768 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | public String getFormattedResult() { 45 | return String.format("%.2f", getAirFuelRatio()) + ":1 AFR"; 46 | } 47 | 48 | /** {@inheritDoc} */ 49 | @Override 50 | public String getCalculatedResult() { 51 | return String.valueOf(getAirFuelRatio()); 52 | } 53 | 54 | /** 55 | *getAirFuelRatio.
56 | * 57 | * @return a double. 58 | */ 59 | public double getAirFuelRatio() { 60 | return afr; 61 | } 62 | 63 | /** {@inheritDoc} */ 64 | @Override 65 | public String getName() { 66 | return AvailableCommandNames.AIR_FUEL_RATIO.getValue(); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/fuel/ConsumptionRateCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.fuel; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * Fuel Consumption Rate per hour. 20 | * 21 | */ 22 | public class ConsumptionRateCommand extends ObdCommand { 23 | 24 | private float fuelRate = -1.0f; 25 | 26 | /** 27 | *Constructor for ConsumptionRateCommand.
28 | */ 29 | public ConsumptionRateCommand() { 30 | super("01 5E"); 31 | } 32 | 33 | /** 34 | *Constructor for ConsumptionRateCommand.
35 | * 36 | * @param other a {@link com.github.pires.obd.commands.fuel.ConsumptionRateCommand} object. 37 | */ 38 | public ConsumptionRateCommand(ConsumptionRateCommand other) { 39 | super(other); 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | protected void performCalculations() { 45 | // ignore first two bytes [hh hh] of the response 46 | fuelRate = (buffer.get(2) * 256 + buffer.get(3)) * 0.05f; 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public String getFormattedResult() { 52 | return String.format("%.1f%s", fuelRate, getResultUnit()); 53 | } 54 | 55 | /** {@inheritDoc} */ 56 | @Override 57 | public String getCalculatedResult() { 58 | return String.valueOf(fuelRate); 59 | } 60 | 61 | /** {@inheritDoc} */ 62 | @Override 63 | public String getResultUnit() { 64 | return "L/h"; 65 | } 66 | 67 | /** 68 | *getLitersPerHour.
69 | * 70 | * @return a float. 71 | */ 72 | public float getLitersPerHour() { 73 | return fuelRate; 74 | } 75 | 76 | /** {@inheritDoc} */ 77 | @Override 78 | public String getName() { 79 | return AvailableCommandNames.FUEL_CONSUMPTION_RATE.getValue(); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/fuel/FindFuelTypeCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.fuel; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | import com.github.pires.obd.enums.FuelType; 18 | 19 | /** 20 | * This command is intended to determine the vehicle fuel type. 21 | * 22 | */ 23 | public class FindFuelTypeCommand extends ObdCommand { 24 | 25 | private int fuelType = 0; 26 | 27 | /** 28 | * Default ctor. 29 | */ 30 | public FindFuelTypeCommand() { 31 | super("01 51"); 32 | } 33 | 34 | /** 35 | * Copy ctor 36 | * 37 | * @param other a {@link com.github.pires.obd.commands.fuel.FindFuelTypeCommand} object. 38 | */ 39 | public FindFuelTypeCommand(FindFuelTypeCommand other) { 40 | super(other); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | protected void performCalculations() { 46 | // ignore first two bytes [hh hh] of the response 47 | fuelType = buffer.get(2); 48 | } 49 | 50 | /** {@inheritDoc} */ 51 | @Override 52 | public String getFormattedResult() { 53 | try { 54 | return FuelType.fromValue(fuelType).getDescription(); 55 | } catch (NullPointerException e) { 56 | return "-"; 57 | } 58 | } 59 | 60 | /** {@inheritDoc} */ 61 | @Override 62 | public String getCalculatedResult() { 63 | return String.valueOf(fuelType); 64 | } 65 | 66 | /** {@inheritDoc} */ 67 | @Override 68 | public String getName() { 69 | return AvailableCommandNames.FUEL_TYPE.getValue(); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/fuel/FuelLevelCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.fuel; 14 | 15 | import com.github.pires.obd.commands.PercentageObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | 18 | /** 19 | * Get fuel level in percentage 20 | * 21 | */ 22 | public class FuelLevelCommand extends PercentageObdCommand { 23 | 24 | /** 25 | *Constructor for FuelLevelCommand.
26 | */ 27 | public FuelLevelCommand() { 28 | super("01 2F"); 29 | } 30 | 31 | /** {@inheritDoc} */ 32 | @Override 33 | protected void performCalculations() { 34 | // ignore first two bytes [hh hh] of the response 35 | percentage = 100.0f * buffer.get(2) / 255.0f; 36 | } 37 | 38 | /** {@inheritDoc} */ 39 | @Override 40 | public String getName() { 41 | return AvailableCommandNames.FUEL_LEVEL.getValue(); 42 | } 43 | 44 | /** 45 | *getFuelLevel.
46 | * 47 | * @return a float. 48 | */ 49 | public float getFuelLevel() { 50 | return percentage; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/fuel/FuelTrimCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.fuel; 14 | 15 | import com.github.pires.obd.commands.PercentageObdCommand; 16 | import com.github.pires.obd.enums.FuelTrim; 17 | 18 | /** 19 | * Fuel Trim. 20 | * 21 | */ 22 | public class FuelTrimCommand extends PercentageObdCommand { 23 | 24 | private final FuelTrim bank; 25 | 26 | /** 27 | * Default ctor. 28 | *29 | * Will read the bank from parameters and construct the command accordingly. 30 | * Please, see FuelTrim enum for more details. 31 | * 32 | * @param bank a {@link com.github.pires.obd.enums.FuelTrim} object. 33 | */ 34 | public FuelTrimCommand(final FuelTrim bank) { 35 | super(bank.buildObdCommand()); 36 | this.bank = bank; 37 | } 38 | 39 | /** 40 | *
Constructor for FuelTrimCommand.
41 | */ 42 | public FuelTrimCommand() { 43 | this(FuelTrim.SHORT_TERM_BANK_1); 44 | } 45 | 46 | /** 47 | * @param value 48 | * @return 49 | */ 50 | private float prepareTempValue(final int value) { 51 | return (value - 128) * (100.0F / 128); 52 | } 53 | 54 | /** 55 | *performCalculations.
56 | */ 57 | protected void performCalculations() { 58 | // ignore first two bytes [hh hh] of the response 59 | percentage = prepareTempValue(buffer.get(2)); 60 | } 61 | 62 | /** 63 | *getValue.
64 | * 65 | * @return the readed Fuel Trim percentage value. 66 | * @deprecated use #getCalculatedResult() 67 | */ 68 | public final float getValue() { 69 | return percentage; 70 | } 71 | 72 | /** 73 | *Getter for the field bank
.
Constructor for WidebandAirFuelRatioCommand.
28 | */ 29 | public WidebandAirFuelRatioCommand() { 30 | super("01 34"); 31 | } 32 | 33 | /** {@inheritDoc} */ 34 | @Override 35 | protected void performCalculations() { 36 | // ignore first two bytes [01 44] of the response 37 | float A = buffer.get(2); 38 | float B = buffer.get(3); 39 | wafr = (((A * 256) + B) / 32768) * 14.7f;//((A*256)+B)/32768 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | public String getFormattedResult() { 45 | return String.format("%.2f", getWidebandAirFuelRatio()) + ":1 AFR"; 46 | } 47 | 48 | /** {@inheritDoc} */ 49 | @Override 50 | public String getCalculatedResult() { 51 | return String.valueOf(getWidebandAirFuelRatio()); 52 | } 53 | 54 | /** 55 | *getWidebandAirFuelRatio.
56 | * 57 | * @return a double. 58 | */ 59 | public double getWidebandAirFuelRatio() { 60 | return wafr; 61 | } 62 | 63 | /** {@inheritDoc} */ 64 | @Override 65 | public String getName() { 66 | return AvailableCommandNames.WIDEBAND_AIR_FUEL_RATIO.getValue(); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/pressure/BarometricPressureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.pressure; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Barometric pressure. 19 | * 20 | */ 21 | public class BarometricPressureCommand extends PressureCommand { 22 | 23 | /** 24 | *Constructor for BarometricPressureCommand.
25 | */ 26 | public BarometricPressureCommand() { 27 | super("01 33"); 28 | } 29 | 30 | /** 31 | *Constructor for BarometricPressureCommand.
32 | * 33 | * @param other a {@link com.github.pires.obd.commands.pressure.PressureCommand} object. 34 | */ 35 | public BarometricPressureCommand(PressureCommand other) { 36 | super(other); 37 | } 38 | 39 | /** {@inheritDoc} */ 40 | @Override 41 | public String getName() { 42 | return AvailableCommandNames.BAROMETRIC_PRESSURE.getValue(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/pressure/FuelPressureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.pressure; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | *FuelPressureCommand class.
19 | * 20 | */ 21 | public class FuelPressureCommand extends PressureCommand { 22 | 23 | /** 24 | *Constructor for FuelPressureCommand.
25 | */ 26 | public FuelPressureCommand() { 27 | super("01 0A"); 28 | } 29 | 30 | /** 31 | *Constructor for FuelPressureCommand.
32 | * 33 | * @param other a {@link com.github.pires.obd.commands.pressure.FuelPressureCommand} object. 34 | */ 35 | public FuelPressureCommand(FuelPressureCommand other) { 36 | super(other); 37 | } 38 | 39 | /** 40 | * {@inheritDoc} 41 | *42 | * TODO describe of why we multiply by 3 43 | */ 44 | @Override 45 | protected final int preparePressureValue() { 46 | return buffer.get(2) * 3; 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public String getName() { 52 | return AvailableCommandNames.FUEL_PRESSURE.getValue(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/pressure/FuelRailPressureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.pressure; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | *
FuelRailPressureCommand class.
19 | * 20 | */ 21 | public class FuelRailPressureCommand extends PressureCommand { 22 | 23 | /** 24 | *Constructor for FuelRailPressureCommand.
25 | */ 26 | public FuelRailPressureCommand() { 27 | super("01 23"); 28 | } 29 | 30 | /** 31 | *Constructor for FuelRailPressureCommand.
32 | * 33 | * @param other a {@link com.github.pires.obd.commands.pressure.FuelRailPressureCommand} object. 34 | */ 35 | public FuelRailPressureCommand(FuelRailPressureCommand other) { 36 | super(other); 37 | } 38 | 39 | /** 40 | * {@inheritDoc} 41 | *42 | * TODO describe of why we multiply by 3 43 | */ 44 | @Override 45 | protected final int preparePressureValue() { 46 | int a = buffer.get(2); 47 | int b = buffer.get(3); 48 | return ((a * 256) + b) * 10; 49 | } 50 | 51 | /** {@inheritDoc} */ 52 | @Override 53 | public String getName() { 54 | return AvailableCommandNames.FUEL_RAIL_PRESSURE.getValue(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/pressure/IntakeManifoldPressureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.pressure; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Intake Manifold Pressure 19 | * 20 | */ 21 | public class IntakeManifoldPressureCommand extends PressureCommand { 22 | 23 | /** 24 | * Default ctor. 25 | */ 26 | public IntakeManifoldPressureCommand() { 27 | super("01 0B"); 28 | } 29 | 30 | /** 31 | * Copy ctor. 32 | * 33 | * @param other a {@link com.github.pires.obd.commands.pressure.IntakeManifoldPressureCommand} object. 34 | */ 35 | public IntakeManifoldPressureCommand(IntakeManifoldPressureCommand other) { 36 | super(other); 37 | } 38 | 39 | /** {@inheritDoc} */ 40 | @Override 41 | public String getName() { 42 | return AvailableCommandNames.INTAKE_MANIFOLD_PRESSURE.getValue(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/pressure/PressureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.pressure; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.commands.SystemOfUnits; 17 | 18 | /** 19 | * Abstract pressure command. 20 | * 21 | */ 22 | public abstract class PressureCommand extends ObdCommand implements 23 | SystemOfUnits { 24 | 25 | protected int tempValue = 0; 26 | protected int pressure = 0; 27 | 28 | /** 29 | * Default ctor 30 | * 31 | * @param cmd a {@link java.lang.String} object. 32 | */ 33 | public PressureCommand(String cmd) { 34 | super(cmd); 35 | } 36 | 37 | /** 38 | * Copy ctor. 39 | * 40 | * @param other a {@link com.github.pires.obd.commands.pressure.PressureCommand} object. 41 | */ 42 | public PressureCommand(PressureCommand other) { 43 | super(other); 44 | } 45 | 46 | /** 47 | * Some PressureCommand subclasses will need to implement this method in 48 | * order to determine the final kPa value. 49 | *
50 | * *NEED* to read tempValue 51 | * 52 | * @return a int. 53 | */ 54 | protected int preparePressureValue() { 55 | return buffer.get(2); 56 | } 57 | 58 | /** 59 | *
performCalculations.
60 | */ 61 | protected void performCalculations() { 62 | // ignore first two bytes [hh hh] of the response 63 | pressure = preparePressureValue(); 64 | } 65 | 66 | /** {@inheritDoc} */ 67 | @Override 68 | public String getFormattedResult() { 69 | return useImperialUnits ? String.format("%.1f%s", getImperialUnit(), getResultUnit()) 70 | : String.format("%d%s", pressure, getResultUnit()); 71 | } 72 | 73 | /** 74 | *getMetricUnit.
75 | * 76 | * @return the pressure in kPa 77 | */ 78 | public int getMetricUnit() { 79 | return pressure; 80 | } 81 | 82 | /** 83 | *getImperialUnit.
84 | * 85 | * @return the pressure in psi 86 | */ 87 | public float getImperialUnit() { 88 | return pressure * 0.145037738F; 89 | } 90 | 91 | /** {@inheritDoc} */ 92 | @Override 93 | public String getCalculatedResult() { 94 | return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(pressure); 95 | } 96 | 97 | /** {@inheritDoc} */ 98 | @Override 99 | public String getResultUnit() { 100 | return useImperialUnits ? "psi" : "kPa"; 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/AdaptiveTimingCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | /** 16 | * By default, Adaptive Timing option 1 (AT1) is enabled, and is the recommended setting. 17 | * AT0 is used to disable Adaptive timing (so the timeout is always as set by AT ST), 18 | * while AT2 is a more aggressive version of AT1 (the effect is more noticeable for very 19 | * slow connections – you may not see much difference with faster OBD systems. 20 | * 21 | */ 22 | public class AdaptiveTimingCommand extends ObdProtocolCommand { 23 | 24 | /** 25 | *Constructor for AdaptiveTimingCommand.
26 | * 27 | * @param mode a int. 28 | */ 29 | public AdaptiveTimingCommand(int mode) { 30 | super("AT AT" + mode); 31 | } 32 | 33 | /** 34 | *Constructor for AdaptiveTimingCommand.
35 | * 36 | * @param other a {@link com.github.pires.obd.commands.protocol.AdaptiveTimingCommand} object. 37 | */ 38 | public AdaptiveTimingCommand(AdaptiveTimingCommand other) { 39 | super(other); 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | public String getFormattedResult() { 45 | return getResult(); 46 | } 47 | 48 | /** {@inheritDoc} */ 49 | @Override 50 | public String getName() { 51 | return "Adaptive timing set"; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.commands.PersistentCommand; 16 | 17 | /** 18 | * Retrieve available PIDs ranging from 21 to 40. 19 | * 20 | */ 21 | public abstract class AvailablePidsCommand extends PersistentCommand { 22 | 23 | /** 24 | * Default ctor. 25 | * 26 | * @param command a {@link java.lang.String} object. 27 | */ 28 | public AvailablePidsCommand(String command) { 29 | super(command); 30 | } 31 | 32 | /** 33 | * Copy ctor. 34 | * 35 | * @param other a {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} object. 36 | */ 37 | public AvailablePidsCommand(AvailablePidsCommand other) { 38 | super(other); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | protected void performCalculations() { 44 | 45 | } 46 | 47 | /** {@inheritDoc} */ 48 | @Override 49 | public String getFormattedResult() { 50 | return getCalculatedResult(); 51 | } 52 | 53 | /** {@inheritDoc} */ 54 | @Override 55 | public String getCalculatedResult() { 56 | //First 4 characters are a copy of the command code, don't return those 57 | return String.valueOf(rawData).substring(4); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_01_20.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Retrieve available PIDs ranging from 01 to 20. 19 | * 20 | */ 21 | public class AvailablePidsCommand_01_20 extends AvailablePidsCommand { 22 | 23 | /** 24 | * Default ctor. 25 | */ 26 | public AvailablePidsCommand_01_20() { 27 | super("01 00"); 28 | } 29 | 30 | /** 31 | * Copy ctor. 32 | * 33 | * @param other a {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} object. 34 | */ 35 | public AvailablePidsCommand_01_20(AvailablePidsCommand_01_20 other) { 36 | super(other); 37 | } 38 | 39 | /** {@inheritDoc} */ 40 | @Override 41 | public String getName() { 42 | return AvailableCommandNames.PIDS_01_20.getValue(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_21_40.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Retrieve available PIDs ranging from 21 to 40. 19 | * 20 | */ 21 | public class AvailablePidsCommand_21_40 extends AvailablePidsCommand { 22 | 23 | /** 24 | * Default ctor. 25 | */ 26 | public AvailablePidsCommand_21_40() { 27 | super("01 20"); 28 | } 29 | 30 | /** 31 | * Copy ctor. 32 | * 33 | * @param other a {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} object. 34 | */ 35 | public AvailablePidsCommand_21_40(AvailablePidsCommand_21_40 other) { 36 | super(other); 37 | } 38 | 39 | /** {@inheritDoc} */ 40 | @Override 41 | public String getName() { 42 | return AvailableCommandNames.PIDS_21_40.getValue(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/AvailablePidsCommand_41_60.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Retrieve available PIDs ranging from 41 to 60. 19 | * 20 | */ 21 | public class AvailablePidsCommand_41_60 extends AvailablePidsCommand { 22 | 23 | /** 24 | * Default ctor. 25 | */ 26 | public AvailablePidsCommand_41_60() { 27 | super("01 40"); 28 | } 29 | 30 | /** 31 | * Copy ctor. 32 | * 33 | * @param other a {@link com.github.pires.obd.commands.protocol.AvailablePidsCommand} object. 34 | */ 35 | public AvailablePidsCommand_41_60(AvailablePidsCommand_41_60 other) { 36 | super(other); 37 | } 38 | 39 | /** {@inheritDoc} */ 40 | @Override 41 | public String getName() { 42 | return AvailableCommandNames.PIDS_41_60.getValue(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/CloseCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | /** 16 | * As per https://www.elmelectronics.com/help/obd/tips/#327_Commands: 17 | *18 | * If a connection is lost, you will need to tell the ELM327 to ‘close’ 19 | * the current connection, with a Protocol Close command (AT PC). 20 | * This will ensure that the ELM327 starts from the beginning when 21 | * the next request is made. This is particularly important for the 22 | * ISO 9141 and ISO 14230 protocols, as they need to send a special 23 | * initiation sequence. 24 | *
25 | * Once the protocol has been closed, it can be re-opened by making a 26 | * request such as 01 00 (do not send ATZ or AT SP0, as many do). 27 | */ 28 | public class CloseCommand extends ObdProtocolCommand { 29 | 30 | /** 31 | *
Constructor for CloseCommand.
32 | */ 33 | public CloseCommand() { 34 | super("AT PC"); 35 | } 36 | 37 | /** 38 | *Constructor for CloseCommand.
39 | * 40 | * @param other a {@link CloseCommand} object. 41 | */ 42 | public CloseCommand(CloseCommand other) { 43 | super(other); 44 | } 45 | 46 | /** 47 | * {@inheritDoc} 48 | */ 49 | @Override 50 | public String getFormattedResult() { 51 | return getResult(); 52 | } 53 | 54 | /** 55 | * {@inheritDoc} 56 | */ 57 | @Override 58 | public String getName() { 59 | return "Protocol Close"; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Describe the current Protocol. 19 | * If a protocol is chosen and the automatic option is 20 | * also selected, AT DP will show the word 'AUTO' before 21 | * the protocol description. Note that the description 22 | * shows the actual protocol names, not the numbers 23 | * used by the protocol setting commands. 24 | * 25 | * @since 1.0-RC12 26 | */ 27 | public class DescribeProtocolCommand extends ObdProtocolCommand { 28 | 29 | /** 30 | *Constructor for DescribeProtocolCommand.
31 | */ 32 | public DescribeProtocolCommand() { 33 | super("AT DP"); 34 | } 35 | 36 | /** {@inheritDoc} */ 37 | @Override 38 | public String getFormattedResult() { 39 | return getResult(); 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | public String getName() { 45 | return AvailableCommandNames.DESCRIBE_PROTOCOL.getValue(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/DescribeProtocolNumberCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.enums.AvailableCommandNames; 17 | import com.github.pires.obd.enums.ObdProtocols; 18 | 19 | /** 20 | * Describe the Protocol by Number. 21 | * It returns a number which represents the current 22 | * obdProtocol. If the automatic search function is also 23 | * enabled, the number will be preceded with the letter 24 | * ‘A’. The number is the same one that is used with the 25 | * set obdProtocol and test obdProtocol commands. 26 | * 27 | * @since 1.0-RC12 28 | */ 29 | public class DescribeProtocolNumberCommand extends ObdCommand { 30 | 31 | private ObdProtocols obdProtocol = ObdProtocols.AUTO; 32 | 33 | /** 34 | *Constructor for DescribeProtocolNumberCommand.
35 | */ 36 | public DescribeProtocolNumberCommand() { 37 | super("AT DPN"); 38 | } 39 | 40 | /** 41 | * {@inheritDoc} 42 | * 43 | * This method exists so that for each command, there must be a method that is 44 | * called only once to perform calculations. 45 | */ 46 | @Override 47 | protected void performCalculations() { 48 | String result = getResult(); 49 | char protocolNumber; 50 | if (result.length() == 2) {//the obdProtocol was set automatic and its format A# 51 | protocolNumber = result.charAt(1); 52 | } else protocolNumber = result.charAt(0); 53 | ObdProtocols[] protocols = ObdProtocols.values(); 54 | for (ObdProtocols protocol : protocols) { 55 | if (protocol.getValue() == protocolNumber) { 56 | this.obdProtocol = protocol; 57 | break; 58 | } 59 | } 60 | } 61 | 62 | /** {@inheritDoc} */ 63 | @Override 64 | public String getFormattedResult() { 65 | return getResult(); 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | public String getCalculatedResult() { 71 | return obdProtocol.name(); 72 | } 73 | 74 | /** {@inheritDoc} */ 75 | @Override 76 | public String getName() { 77 | return AvailableCommandNames.DESCRIBE_PROTOCOL_NUMBER.getValue(); 78 | } 79 | 80 | /** 81 | *Getter for the field obdProtocol
.
Constructor for EchoOffCommand.
23 | */ 24 | public EchoOffCommand() { 25 | super("AT E0"); 26 | } 27 | 28 | /** 29 | *Constructor for EchoOffCommand.
30 | * 31 | * @param other a {@link com.github.pires.obd.commands.protocol.EchoOffCommand} object. 32 | */ 33 | public EchoOffCommand(EchoOffCommand other) { 34 | super(other); 35 | } 36 | 37 | /** {@inheritDoc} */ 38 | @Override 39 | public String getFormattedResult() { 40 | return getResult(); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | public String getName() { 46 | return "Echo Off"; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/HeadersOffCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | /** 16 | * Turn-off headers. 17 | * 18 | */ 19 | public class HeadersOffCommand extends ObdProtocolCommand { 20 | 21 | /** 22 | *Constructor for HeadersOffCommand.
23 | */ 24 | public HeadersOffCommand() { 25 | super("ATH0"); 26 | } 27 | 28 | /** 29 | *Constructor for HeadersOffCommand.
30 | * 31 | * @param other a {@link com.github.pires.obd.commands.protocol.HeadersOffCommand} object. 32 | */ 33 | public HeadersOffCommand(HeadersOffCommand other) { 34 | super(other); 35 | } 36 | 37 | /** {@inheritDoc} */ 38 | @Override 39 | public String getFormattedResult() { 40 | return getResult(); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | public String getName() { 46 | return "Headers disabled"; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/LineFeedOffCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | /** 16 | * Turns off line-feed. 17 | * 18 | */ 19 | public class LineFeedOffCommand extends ObdProtocolCommand { 20 | 21 | /** 22 | *Constructor for LineFeedOffCommand.
23 | */ 24 | public LineFeedOffCommand() { 25 | super("AT L0"); 26 | } 27 | 28 | /** 29 | *Constructor for LineFeedOffCommand.
30 | * 31 | * @param other a {@link com.github.pires.obd.commands.protocol.LineFeedOffCommand} object. 32 | */ 33 | public LineFeedOffCommand(LineFeedOffCommand other) { 34 | super(other); 35 | } 36 | 37 | /** {@inheritDoc} */ 38 | @Override 39 | public String getFormattedResult() { 40 | return getResult(); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | public String getName() { 46 | return "Line Feed Off"; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/ObdProtocolCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | 17 | /** 18 | *Abstract ObdProtocolCommand class.
19 | */ 20 | public abstract class ObdProtocolCommand extends ObdCommand { 21 | /** 22 | * Default ctor to use 23 | * 24 | * @param command the command to send 25 | */ 26 | public ObdProtocolCommand(String command) { 27 | super(command); 28 | } 29 | 30 | /** 31 | * Copy ctor. 32 | * 33 | * @param other the ObdCommand to copy. 34 | */ 35 | public ObdProtocolCommand(ObdProtocolCommand other) { 36 | this(other.cmd); 37 | } 38 | 39 | /** 40 | *performCalculations.
41 | */ 42 | protected void performCalculations() { 43 | // ignore 44 | } 45 | 46 | /** 47 | *fillBuffer.
48 | */ 49 | protected void fillBuffer() { 50 | // settings commands don't return a value appropriate to place into the 51 | // buffer, so do nothing 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override 56 | public String getCalculatedResult() { 57 | return String.valueOf(getResult()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/ObdRawCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | /** 16 | * This class allows for an unspecified command to be sent. 17 | */ 18 | public class ObdRawCommand extends ObdProtocolCommand { 19 | 20 | /** 21 | *Constructor for ObdRawCommand.
22 | * 23 | * @param command a {@link java.lang.String} object. 24 | */ 25 | public ObdRawCommand(String command) { 26 | super(command); 27 | } 28 | 29 | /** {@inheritDoc} */ 30 | @Override 31 | public String getFormattedResult() { 32 | return getResult(); 33 | } 34 | 35 | /** {@inheritDoc} */ 36 | @Override 37 | public String getName() { 38 | return "Custom command " + getCommandPID(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/ObdResetCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | /** 16 | * Reset the OBD connection. 17 | * 18 | */ 19 | public class ObdResetCommand extends ObdProtocolCommand { 20 | 21 | /** 22 | *Constructor for ObdResetCommand.
23 | */ 24 | public ObdResetCommand() { 25 | super("AT Z"); 26 | } 27 | 28 | /** 29 | *Constructor for ObdResetCommand.
30 | * 31 | * @param other a {@link com.github.pires.obd.commands.protocol.ObdResetCommand} object. 32 | */ 33 | public ObdResetCommand(ObdResetCommand other) { 34 | super(other); 35 | } 36 | 37 | /** {@inheritDoc} */ 38 | @Override 39 | public String getFormattedResult() { 40 | return getResult(); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | public String getName() { 46 | return "Reset OBD"; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/ObdWarmstartCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | /** 16 | * Warm-start the OBD connection. 17 | * 18 | */ 19 | public class ObdWarmstartCommand extends ObdProtocolCommand { 20 | 21 | /** 22 | *Constructor for ObdWarmstartCommand.
23 | */ 24 | public ObdWarmstartCommand() { 25 | super("AT WS"); 26 | } 27 | 28 | /** 29 | *Constructor for ObdWarmstartCommand.
30 | * 31 | * @param other a {@link com.github.pires.obd.commands.protocol.ObdWarmstartCommand} object. 32 | */ 33 | public ObdWarmstartCommand(ObdWarmstartCommand other) { 34 | super(other); 35 | } 36 | 37 | /** {@inheritDoc} */ 38 | @Override 39 | public String getFormattedResult() { 40 | return getResult(); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | public String getName() { 46 | return "Warmstart OBD"; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/ResetTroubleCodesCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | 17 | /** 18 | * Reset trouble codes. 19 | * 20 | */ 21 | public class ResetTroubleCodesCommand extends ObdCommand { 22 | 23 | /** 24 | *Constructor for ResetTroubleCodesCommand.
25 | */ 26 | public ResetTroubleCodesCommand() { 27 | super("04"); 28 | } 29 | 30 | /** {@inheritDoc} */ 31 | @Override 32 | protected void performCalculations() { 33 | 34 | } 35 | 36 | /** {@inheritDoc} */ 37 | @Override 38 | public String getFormattedResult() { 39 | return getResult(); 40 | } 41 | 42 | /** {@inheritDoc} */ 43 | @Override 44 | public String getCalculatedResult() { 45 | return getResult(); 46 | } 47 | 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public String getName() { 52 | return getResult(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/SelectProtocolCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | import com.github.pires.obd.enums.ObdProtocols; 16 | 17 | /** 18 | * Select the protocol to use. 19 | * 20 | */ 21 | public class SelectProtocolCommand extends ObdProtocolCommand { 22 | 23 | private final ObdProtocols protocol; 24 | 25 | /** 26 | *Constructor for SelectProtocolCommand.
27 | * 28 | * @param protocol a {@link com.github.pires.obd.enums.ObdProtocols} object. 29 | */ 30 | public SelectProtocolCommand(final ObdProtocols protocol) { 31 | super("AT SP " + protocol.getValue()); 32 | this.protocol = protocol; 33 | } 34 | 35 | /** {@inheritDoc} */ 36 | @Override 37 | public String getFormattedResult() { 38 | return getResult(); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | public String getName() { 44 | return "Select Protocol " + protocol.name(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/SpacesOffCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | 16 | /** 17 | * Turn-off spaces. 18 | */ 19 | public class SpacesOffCommand extends ObdProtocolCommand { 20 | 21 | public SpacesOffCommand() { 22 | super("ATS0"); 23 | } 24 | 25 | /** 26 | *Constructor for SpacesOffCommand.
27 | * 28 | * @param other a {@link SpacesOffCommand} object. 29 | */ 30 | public SpacesOffCommand(SpacesOffCommand other) { 31 | super(other); 32 | } 33 | 34 | @Override 35 | public String getFormattedResult() { 36 | return getResult(); 37 | } 38 | 39 | @Override 40 | public String getName() { 41 | return "Spaces Off"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/protocol/TimeoutCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.protocol; 14 | 15 | /** 16 | * This will set the value of time in milliseconds (ms) that the OBD interface 17 | * will wait for a response from the ECU. If exceeds, the response is "NO DATA". 18 | * 19 | */ 20 | public class TimeoutCommand extends ObdProtocolCommand { 21 | 22 | /** 23 | *Constructor for TimeoutCommand.
24 | * 25 | * @param timeout value between 0 and 255 that multiplied by 4 results in the 26 | * desired timeout in milliseconds (ms). 27 | */ 28 | public TimeoutCommand(int timeout) { 29 | super("AT ST " + Integer.toHexString(0xFF & timeout)); 30 | } 31 | 32 | /** 33 | *Constructor for TimeoutCommand.
34 | * 35 | * @param other a {@link com.github.pires.obd.commands.protocol.TimeoutCommand} object. 36 | */ 37 | public TimeoutCommand(TimeoutCommand other) { 38 | super(other); 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | public String getFormattedResult() { 44 | return getResult(); 45 | } 46 | 47 | /** {@inheritDoc} */ 48 | @Override 49 | public String getName() { 50 | return "Timeout"; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/temperature/AirIntakeTemperatureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.temperature; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Temperature of intake air. 19 | * 20 | */ 21 | public class AirIntakeTemperatureCommand extends TemperatureCommand { 22 | 23 | /** 24 | *Constructor for AirIntakeTemperatureCommand.
25 | */ 26 | public AirIntakeTemperatureCommand() { 27 | super("01 0F"); 28 | } 29 | 30 | /** 31 | *Constructor for AirIntakeTemperatureCommand.
32 | * 33 | * @param other a {@link com.github.pires.obd.commands.temperature.AirIntakeTemperatureCommand} object. 34 | */ 35 | public AirIntakeTemperatureCommand(AirIntakeTemperatureCommand other) { 36 | super(other); 37 | } 38 | 39 | /** {@inheritDoc} */ 40 | @Override 41 | public String getName() { 42 | return AvailableCommandNames.AIR_INTAKE_TEMP.getValue(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/temperature/AmbientAirTemperatureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.temperature; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Ambient Air Temperature. 19 | * 20 | */ 21 | public class AmbientAirTemperatureCommand extends TemperatureCommand { 22 | 23 | /** 24 | *Constructor for AmbientAirTemperatureCommand.
25 | */ 26 | public AmbientAirTemperatureCommand() { 27 | super("01 46"); 28 | } 29 | 30 | /** 31 | *Constructor for AmbientAirTemperatureCommand.
32 | * 33 | * @param other a {@link com.github.pires.obd.commands.temperature.TemperatureCommand} object. 34 | */ 35 | public AmbientAirTemperatureCommand(TemperatureCommand other) { 36 | super(other); 37 | } 38 | 39 | /** {@inheritDoc} */ 40 | @Override 41 | public String getName() { 42 | return AvailableCommandNames.AMBIENT_AIR_TEMP.getValue(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/temperature/EngineCoolantTemperatureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.temperature; 14 | 15 | import com.github.pires.obd.enums.AvailableCommandNames; 16 | 17 | /** 18 | * Engine Coolant Temperature. 19 | * 20 | */ 21 | public class EngineCoolantTemperatureCommand extends TemperatureCommand { 22 | 23 | /** 24 | *Constructor for EngineCoolantTemperatureCommand.
25 | */ 26 | public EngineCoolantTemperatureCommand() { 27 | super("01 05"); 28 | } 29 | 30 | /** 31 | *Constructor for EngineCoolantTemperatureCommand.
32 | * 33 | * @param other a {@link com.github.pires.obd.commands.temperature.TemperatureCommand} object. 34 | */ 35 | public EngineCoolantTemperatureCommand(TemperatureCommand other) { 36 | super(other); 37 | } 38 | 39 | /** {@inheritDoc} */ 40 | @Override 41 | public String getName() { 42 | return AvailableCommandNames.ENGINE_COOLANT_TEMP.getValue(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/commands/temperature/TemperatureCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands.temperature; 14 | 15 | import com.github.pires.obd.commands.ObdCommand; 16 | import com.github.pires.obd.commands.SystemOfUnits; 17 | 18 | /** 19 | * Abstract temperature command. 20 | * 21 | */ 22 | public abstract class TemperatureCommand extends ObdCommand implements 23 | SystemOfUnits { 24 | 25 | private float temperature = 0.0f; 26 | 27 | /** 28 | * Default ctor. 29 | * 30 | * @param cmd a {@link java.lang.String} object. 31 | */ 32 | public TemperatureCommand(String cmd) { 33 | super(cmd); 34 | } 35 | 36 | /** 37 | * Copy ctor. 38 | * 39 | * @param other a {@link com.github.pires.obd.commands.temperature.TemperatureCommand} object. 40 | */ 41 | public TemperatureCommand(TemperatureCommand other) { 42 | super(other); 43 | } 44 | 45 | /** {@inheritDoc} */ 46 | @Override 47 | protected void performCalculations() { 48 | // ignore first two bytes [hh hh] of the response 49 | temperature = buffer.get(2) - 40; 50 | } 51 | 52 | 53 | /** 54 | * {@inheritDoc} 55 | *56 | * Get values from 'buff', since we can't rely on char/string for 57 | * calculations. 58 | */ 59 | @Override 60 | public String getFormattedResult() { 61 | return useImperialUnits ? String.format("%.1f%s", getImperialUnit(), getResultUnit()) 62 | : String.format("%.0f%s", temperature, getResultUnit()); 63 | } 64 | 65 | /** {@inheritDoc} */ 66 | @Override 67 | public String getCalculatedResult() { 68 | return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(temperature); 69 | } 70 | 71 | /** {@inheritDoc} */ 72 | @Override 73 | public String getResultUnit() { 74 | return useImperialUnits ? "F" : "C"; 75 | } 76 | 77 | /** 78 | *
Getter for the field temperature
.
getImperialUnit.
88 | * 89 | * @return the temperature in Fahrenheit. 90 | */ 91 | public float getImperialUnit() { 92 | return temperature * 1.8f + 32; 93 | } 94 | 95 | /** 96 | *getKelvin.
97 | * 98 | * @return the temperature in Kelvin. 99 | */ 100 | public float getKelvin() { 101 | return temperature + 273.15f; 102 | } 103 | 104 | /** 105 | *getName.
106 | * 107 | * @return the OBD command name. 108 | */ 109 | public abstract String getName(); 110 | 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/enums/AvailableCommandNames.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.enums; 14 | 15 | /** 16 | * Names of all available commands. 17 | * 18 | */ 19 | public enum AvailableCommandNames { 20 | 21 | AIR_INTAKE_TEMP("Air Intake Temperature"), 22 | AMBIENT_AIR_TEMP("Ambient Air Temperature"), 23 | ENGINE_COOLANT_TEMP("Engine Coolant Temperature"), 24 | BAROMETRIC_PRESSURE("Barometric Pressure"), 25 | FUEL_PRESSURE("Fuel Pressure"), 26 | INTAKE_MANIFOLD_PRESSURE("Intake Manifold Pressure"), 27 | ENGINE_LOAD("Engine Load"), 28 | ENGINE_RUNTIME("Engine Runtime"), 29 | ENGINE_RPM("Engine RPM"), 30 | SPEED("Vehicle Speed"), 31 | MAF("Mass Air Flow"), 32 | THROTTLE_POS("Throttle Position"), 33 | TROUBLE_CODES("Trouble Codes"), 34 | PENDING_TROUBLE_CODES("Pending Trouble Codes"), 35 | PERMANENT_TROUBLE_CODES("Permanent Trouble Codes"), 36 | FUEL_LEVEL("Fuel Level"), 37 | FUEL_TYPE("Fuel Type"), 38 | FUEL_CONSUMPTION_RATE("Fuel Consumption Rate"), 39 | TIMING_ADVANCE("Timing Advance"), 40 | DTC_NUMBER("Diagnostic Trouble Codes"), 41 | EQUIV_RATIO("Command Equivalence Ratio"), 42 | DISTANCE_TRAVELED_AFTER_CODES_CLEARED("Distance since codes cleared"), 43 | CONTROL_MODULE_VOLTAGE("Control Module Power Supply "), 44 | ENGINE_FUEL_RATE("Engine Fuel Rate"), 45 | FUEL_RAIL_PRESSURE("Fuel Rail Pressure"), 46 | VIN("Vehicle Identification Number (VIN)"), 47 | DISTANCE_TRAVELED_MIL_ON("Distance traveled with MIL on"), 48 | TIME_TRAVELED_MIL_ON("Time run with MIL on"), 49 | TIME_SINCE_TC_CLEARED("Time since trouble codes cleared"), 50 | REL_THROTTLE_POS("Relative throttle position"), 51 | PIDS_01_20("Available PIDs 01-20"), 52 | PIDS_21_40("Available PIDs 21-40"), 53 | PIDS_41_60("Available PIDs 41-60"), 54 | ABS_LOAD("Absolute load"), 55 | ENGINE_OIL_TEMP("Engine oil temperature"), 56 | AIR_FUEL_RATIO("Air/Fuel Ratio"), 57 | WIDEBAND_AIR_FUEL_RATIO("Wideband Air/Fuel Ratio"), 58 | DESCRIBE_PROTOCOL("Describe protocol"), 59 | DESCRIBE_PROTOCOL_NUMBER("Describe protocol number"), 60 | IGNITION_MONITOR("Ignition monitor") 61 | ; 62 | 63 | private final String value; 64 | 65 | /** 66 | * @param value Command description 67 | */ 68 | AvailableCommandNames(String value) { 69 | this.value = value; 70 | } 71 | 72 | /** 73 | *Getter for the field value
.
map
*/
30 | private static MapfromValue.
47 | * 48 | * @param value a int. 49 | * @return a {@link com.github.pires.obd.enums.FuelTrim} object. 50 | */ 51 | public static FuelTrim fromValue(final int value) { 52 | return map.get(value); 53 | } 54 | 55 | /** 56 | *Getter for the field value
.
Getter for the field bank
.
buildObdCommand.
75 | * 76 | * @return a {@link java.lang.String} object. 77 | */ 78 | public final String buildObdCommand() { 79 | return new String("01 0" + value); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/enums/FuelType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.enums; 14 | 15 | import java.util.HashMap; 16 | import java.util.Map; 17 | 18 | /** 19 | * MODE 1 PID 0x51 will return one of the following values to identify the fuel 20 | * type of the vehicle. 21 | * 22 | */ 23 | public enum FuelType { 24 | GASOLINE(0x01, "Gasoline"), 25 | METHANOL(0x02, "Methanol"), 26 | ETHANOL(0x03, "Ethanol"), 27 | DIESEL(0x04, "Diesel"), 28 | LPG(0x05, "GPL/LGP"), 29 | CNG(0x06, "Natural Gas"), 30 | PROPANE(0x07, "Propane"), 31 | ELECTRIC(0x08, "Electric"), 32 | BIFUEL_GASOLINE(0x09, "Biodiesel + Gasoline"), 33 | BIFUEL_METHANOL(0x0A, "Biodiesel + Methanol"), 34 | BIFUEL_ETHANOL(0x0B, "Biodiesel + Ethanol"), 35 | BIFUEL_LPG(0x0C, "Biodiesel + GPL/LGP"), 36 | BIFUEL_CNG(0x0D, "Biodiesel + Natural Gas"), 37 | BIFUEL_PROPANE(0x0E, "Biodiesel + Propane"), 38 | BIFUEL_ELECTRIC(0x0F, "Biodiesel + Electric"), 39 | BIFUEL_GASOLINE_ELECTRIC(0x10, "Biodiesel + Gasoline/Electric"), 40 | HYBRID_GASOLINE(0x11, "Hybrid Gasoline"), 41 | HYBRID_ETHANOL(0x12, "Hybrid Ethanol"), 42 | HYBRID_DIESEL(0x13, "Hybrid Diesel"), 43 | HYBRID_ELECTRIC(0x14, "Hybrid Electric"), 44 | HYBRID_MIXED(0x15, "Hybrid Mixed"), 45 | HYBRID_REGENERATIVE(0x16, "Hybrid Regenerative"); 46 | 47 | /** Constantmap
*/
48 | private static MapfromValue.
65 | * 66 | * @param value a int. 67 | * @return a {@link com.github.pires.obd.enums.FuelType} object. 68 | */ 69 | public static FuelType fromValue(final int value) { 70 | return map.get(value); 71 | } 72 | 73 | /** 74 | *Getter for the field value
.
Getter for the field description
.
Getter for the field value
.
Constructor for BusInitException.
23 | */ 24 | public BusInitException() { 25 | super("BUS INIT... ERROR"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/exceptions/MisunderstoodCommandException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.exceptions; 14 | 15 | /** 16 | * Thrown when there is a "?" message. 17 | * 18 | */ 19 | public class MisunderstoodCommandException extends ResponseException { 20 | 21 | /** 22 | *Constructor for MisunderstoodCommandException.
23 | */ 24 | public MisunderstoodCommandException() { 25 | super("?"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/exceptions/NoDataException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.exceptions; 14 | 15 | /** 16 | * Thrown when there is a "NO DATA" message. 17 | * 18 | */ 19 | public class NoDataException extends ResponseException { 20 | 21 | /** 22 | *Constructor for NoDataException.
23 | */ 24 | public NoDataException() { 25 | super("NO DATA"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/exceptions/NonNumericResponseException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.exceptions; 14 | 15 | /** 16 | * Thrown when there are no numbers in the response and they are expected 17 | * 18 | */ 19 | public class NonNumericResponseException extends RuntimeException { 20 | 21 | /** 22 | *Constructor for NonNumericResponseException.
23 | * 24 | * @param message a {@link java.lang.String} object. 25 | */ 26 | public NonNumericResponseException(String message) { 27 | super("Error reading response: " + message); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/exceptions/ResponseException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.exceptions; 14 | 15 | /** 16 | * Generic message error 17 | * 18 | */ 19 | public class ResponseException extends RuntimeException { 20 | 21 | private String message; 22 | 23 | private String response; 24 | 25 | private String command; 26 | 27 | private boolean matchRegex; 28 | 29 | /** 30 | *Constructor for ResponseException.
31 | * 32 | * @param message a {@link java.lang.String} object. 33 | */ 34 | protected ResponseException(String message) { 35 | this.message = message; 36 | } 37 | 38 | /** 39 | *Constructor for ResponseException.
40 | * 41 | * @param message a {@link java.lang.String} object. 42 | * @param matchRegex a boolean. 43 | */ 44 | protected ResponseException(String message, boolean matchRegex) { 45 | this.message = message; 46 | this.matchRegex = matchRegex; 47 | } 48 | 49 | private static String clean(String s) { 50 | return s == null ? "" : s.replaceAll("\\s", "").toUpperCase(); 51 | } 52 | 53 | /** 54 | *isError.
55 | * 56 | * @param response a {@link java.lang.String} object. 57 | * @return a boolean. 58 | */ 59 | public boolean isError(String response) { 60 | this.response = response; 61 | if (matchRegex) { 62 | return clean(response).matches(clean(message)); 63 | } else { 64 | return clean(response).contains(clean(message)); 65 | } 66 | } 67 | 68 | /** 69 | *Setter for the field command
.
Constructor for StoppedException.
23 | */ 24 | public StoppedException() { 25 | super("STOPPED"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/exceptions/UnableToConnectException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.exceptions; 14 | 15 | /** 16 | * Thrown when there is a "UNABLE TO CONNECT" message. 17 | * 18 | */ 19 | public class UnableToConnectException extends ResponseException { 20 | 21 | /** 22 | *Constructor for UnableToConnectException.
23 | */ 24 | public UnableToConnectException() { 25 | super("UNABLE TO CONNECT"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/exceptions/UnknownErrorException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.exceptions; 14 | 15 | /** 16 | * Thrown when there is "ERROR" in the response 17 | * 18 | */ 19 | public class UnknownErrorException extends ResponseException { 20 | 21 | /** 22 | *Constructor for UnknownErrorException.
23 | */ 24 | public UnknownErrorException() { 25 | super("ERROR"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/github/pires/obd/exceptions/UnsupportedCommandException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.exceptions; 14 | 15 | /** 16 | * Thrown when there is a "?" message. 17 | * 18 | */ 19 | public class UnsupportedCommandException extends ResponseException { 20 | 21 | /** 22 | *Constructor for UnsupportedCommandException.
23 | */ 24 | public UnsupportedCommandException() { 25 | super("7F 0[0-A] 1[1-2]", true); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/github/pires/obd/commands/AirIntakeTempCommandTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import com.github.pires.obd.commands.temperature.AirIntakeTemperatureCommand; 16 | import org.powermock.core.classloader.annotations.PrepareForTest; 17 | import org.testng.annotations.AfterClass; 18 | import org.testng.annotations.BeforeMethod; 19 | import org.testng.annotations.Test; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | import static org.powermock.api.easymock.PowerMock.*; 25 | import static org.testng.Assert.assertEquals; 26 | 27 | /** 28 | * Tests for TemperatureCommand sub-classes. 29 | */ 30 | @PrepareForTest(InputStream.class) 31 | public class AirIntakeTempCommandTest { 32 | 33 | private AirIntakeTemperatureCommand command = null; 34 | private InputStream mockIn = null; 35 | 36 | /** 37 | * @throws Exception 38 | */ 39 | @BeforeMethod 40 | public void setUp() throws Exception { 41 | command = new AirIntakeTemperatureCommand(); 42 | } 43 | 44 | /** 45 | * Test for valid InputStream read, 24ºC 46 | * 47 | * @throws IOException 48 | */ 49 | @Test 50 | public void testValidTemperatureCelsius() throws IOException { 51 | // mock InputStream read 52 | mockIn = createMock(InputStream.class); 53 | mockIn.read(); 54 | expectLastCall().andReturn((byte) '4'); 55 | expectLastCall().andReturn((byte) '1'); 56 | expectLastCall().andReturn((byte) ' '); 57 | expectLastCall().andReturn((byte) '0'); 58 | expectLastCall().andReturn((byte) 'F'); 59 | expectLastCall().andReturn((byte) ' '); 60 | expectLastCall().andReturn((byte) '4'); 61 | expectLastCall().andReturn((byte) '0'); 62 | expectLastCall().andReturn((byte) '>'); 63 | 64 | replayAll(); 65 | 66 | // call the method to test 67 | command.readResult(mockIn); 68 | assertEquals(command.getTemperature(), 24f); 69 | 70 | verifyAll(); 71 | } 72 | 73 | /** 74 | * Test for valid InputStream read, 75.2F 75 | * 76 | * @throws IOException 77 | */ 78 | @Test 79 | public void testValidTemperatureFahrenheit() throws IOException { 80 | // mock InputStream read 81 | mockIn = createMock(InputStream.class); 82 | mockIn.read(); 83 | expectLastCall().andReturn((byte) '4'); 84 | expectLastCall().andReturn((byte) '1'); 85 | expectLastCall().andReturn((byte) ' '); 86 | expectLastCall().andReturn((byte) '0'); 87 | expectLastCall().andReturn((byte) 'F'); 88 | expectLastCall().andReturn((byte) ' '); 89 | expectLastCall().andReturn((byte) '4'); 90 | expectLastCall().andReturn((byte) '5'); 91 | expectLastCall().andReturn((byte) '>'); 92 | 93 | replayAll(); 94 | 95 | // call the method to test 96 | command.readResult(mockIn); 97 | command.useImperialUnits = true; 98 | assertEquals(command.getImperialUnit(), 84.2f); 99 | 100 | verifyAll(); 101 | } 102 | 103 | /** 104 | * Test for valid InputStream read, 0ºC 105 | * 106 | * @throws IOException 107 | */ 108 | @Test 109 | public void testValidTemperatureZeroCelsius() throws IOException { 110 | // mock InputStream read 111 | mockIn = createMock(InputStream.class); 112 | mockIn.read(); 113 | expectLastCall().andReturn((byte) '4'); 114 | expectLastCall().andReturn((byte) '1'); 115 | expectLastCall().andReturn((byte) ' '); 116 | expectLastCall().andReturn((byte) '0'); 117 | expectLastCall().andReturn((byte) 'F'); 118 | expectLastCall().andReturn((byte) ' '); 119 | expectLastCall().andReturn((byte) '2'); 120 | expectLastCall().andReturn((byte) '8'); 121 | expectLastCall().andReturn((byte) '>'); 122 | 123 | replayAll(); 124 | 125 | // call the method to test 126 | command.readResult(mockIn); 127 | assertEquals(command.getTemperature(), 0f); 128 | 129 | verifyAll(); 130 | } 131 | 132 | /** 133 | * Clear resources. 134 | */ 135 | @AfterClass 136 | public void tearDown() { 137 | command = null; 138 | mockIn = null; 139 | } 140 | 141 | } 142 | -------------------------------------------------------------------------------- /src/test/java/com/github/pires/obd/commands/DescribeProtocolNumberCommandTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import com.github.pires.obd.commands.protocol.DescribeProtocolNumberCommand; 16 | import com.github.pires.obd.enums.ObdProtocols; 17 | import org.testng.annotations.AfterClass; 18 | import org.testng.annotations.BeforeMethod; 19 | import org.testng.annotations.Test; 20 | 21 | import java.io.InputStream; 22 | 23 | import static org.powermock.api.easymock.PowerMock.*; 24 | import static org.testng.Assert.assertEquals; 25 | 26 | /** 27 | * Tests for DescribeProtocolNumberCommand class. 28 | */ 29 | public class DescribeProtocolNumberCommandTest { 30 | 31 | private DescribeProtocolNumberCommand command; 32 | private InputStream mockIn; 33 | 34 | @BeforeMethod 35 | public void setUp() throws Exception { 36 | command = new DescribeProtocolNumberCommand(); 37 | } 38 | 39 | @Test 40 | public void testGetCalculatedResult() throws Exception { 41 | mockIn = createMock(InputStream.class); 42 | mockIn.read(); 43 | expectLastCall().andReturn((byte) 'A'); 44 | expectLastCall().andReturn((byte) '3'); 45 | expectLastCall().andReturn((byte) '>'); 46 | expectLastCall().andReturn((byte) '2'); 47 | expectLastCall().andReturn((byte) '>'); 48 | 49 | replayAll(); 50 | 51 | // call the method to test 52 | command.readResult(mockIn); 53 | assertEquals(command.getCalculatedResult(), ObdProtocols.ISO_9141_2.name());//AUTO ISO_9141_2 54 | 55 | // call the method to test 56 | command.readResult(mockIn); 57 | assertEquals(command.getCalculatedResult(), ObdProtocols.SAE_J1850_VPW.name());//SAE_J1850_VPW 58 | 59 | verifyAll(); 60 | } 61 | 62 | @Test 63 | public void testGetProtocol() throws Exception { 64 | mockIn = createMock(InputStream.class); 65 | mockIn.read(); 66 | expectLastCall().andReturn((byte) 'A'); 67 | expectLastCall().andReturn((byte) '6'); 68 | expectLastCall().andReturn((byte) '>'); 69 | expectLastCall().andReturn((byte) '7'); 70 | expectLastCall().andReturn((byte) '>'); 71 | 72 | replayAll(); 73 | 74 | // call the method to test 75 | command.readResult(mockIn); 76 | assertEquals(command.getObdProtocol(), ObdProtocols.ISO_15765_4_CAN);//AUTO ISO_15765_4_CAN 77 | 78 | // call the method to test 79 | command.readResult(mockIn); 80 | assertEquals(command.getObdProtocol(), ObdProtocols.ISO_15765_4_CAN_B);//ISO_15765_4_CAN_B 81 | 82 | verifyAll(); 83 | } 84 | 85 | /** 86 | * Clear resources. 87 | */ 88 | @AfterClass 89 | public void tearDown() { 90 | command = null; 91 | mockIn = null; 92 | } 93 | } -------------------------------------------------------------------------------- /src/test/java/com/github/pires/obd/commands/DistanceSinceCCCommandTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import com.github.pires.obd.commands.control.DistanceSinceCCCommand; 16 | import org.powermock.core.classloader.annotations.PrepareForTest; 17 | import org.testng.annotations.AfterClass; 18 | import org.testng.annotations.BeforeMethod; 19 | import org.testng.annotations.Test; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | import static org.powermock.api.easymock.PowerMock.*; 25 | import static org.testng.Assert.assertEquals; 26 | 27 | /** 28 | * Runtime since engine start in seconds, with a maximum value of 65535. 29 | */ 30 | @PrepareForTest(InputStream.class) 31 | public class DistanceSinceCCCommandTest { 32 | 33 | private DistanceSinceCCCommand command; 34 | private InputStream mockIn; 35 | 36 | /** 37 | * @throws Exception 38 | */ 39 | @BeforeMethod 40 | public void setUp() throws Exception { 41 | command = new DistanceSinceCCCommand(); 42 | } 43 | 44 | /** 45 | * Test for valid InputStream read, 65535 km. 46 | * 47 | * @throws IOException 48 | */ 49 | @Test 50 | public void testMaxDistanceValue() throws IOException { 51 | // mock InputStream read 52 | mockIn = createMock(InputStream.class); 53 | mockIn.read(); 54 | expectLastCall().andReturn((byte) '4'); 55 | expectLastCall().andReturn((byte) '1'); 56 | expectLastCall().andReturn((byte) ' '); 57 | expectLastCall().andReturn((byte) '3'); 58 | expectLastCall().andReturn((byte) '1'); 59 | expectLastCall().andReturn((byte) ' '); 60 | expectLastCall().andReturn((byte) 'F'); 61 | expectLastCall().andReturn((byte) 'F'); 62 | expectLastCall().andReturn((byte) ' '); 63 | expectLastCall().andReturn((byte) 'F'); 64 | expectLastCall().andReturn((byte) 'F'); 65 | expectLastCall().andReturn((byte) '>'); 66 | 67 | replayAll(); 68 | 69 | // call the method to test 70 | command.readResult(mockIn); 71 | assertEquals(command.getKm(), 65535); 72 | 73 | verifyAll(); 74 | } 75 | 76 | /** 77 | * Test for valid InputStream read, 17731 kms 78 | * 79 | * @throws IOException 80 | */ 81 | @Test 82 | public void testSomeRuntimeValue() throws IOException { 83 | // mock InputStream read 84 | mockIn = createMock(InputStream.class); 85 | mockIn.read(); 86 | expectLastCall().andReturn((byte) '4'); 87 | expectLastCall().andReturn((byte) '1'); 88 | expectLastCall().andReturn((byte) ' '); 89 | expectLastCall().andReturn((byte) '3'); 90 | expectLastCall().andReturn((byte) '1'); 91 | expectLastCall().andReturn((byte) ' '); 92 | expectLastCall().andReturn((byte) '4'); 93 | expectLastCall().andReturn((byte) '5'); 94 | expectLastCall().andReturn((byte) ' '); 95 | expectLastCall().andReturn((byte) '4'); 96 | expectLastCall().andReturn((byte) '3'); 97 | expectLastCall().andReturn((byte) '>'); 98 | 99 | replayAll(); 100 | 101 | // call the method to test 102 | command.readResult(mockIn); 103 | assertEquals(command.getKm(), 17731); 104 | 105 | verifyAll(); 106 | } 107 | 108 | /** 109 | * Test for valid InputStream read, 0 km. 110 | * 111 | * @throws IOException 112 | */ 113 | @Test 114 | public void testMinRuntimeValue() throws IOException { 115 | // mock InputStream read 116 | mockIn = createMock(InputStream.class); 117 | mockIn.read(); 118 | expectLastCall().andReturn((byte) '4'); 119 | expectLastCall().andReturn((byte) '1'); 120 | expectLastCall().andReturn((byte) ' '); 121 | expectLastCall().andReturn((byte) '3'); 122 | expectLastCall().andReturn((byte) '1'); 123 | expectLastCall().andReturn((byte) ' '); 124 | expectLastCall().andReturn((byte) '0'); 125 | expectLastCall().andReturn((byte) '0'); 126 | expectLastCall().andReturn((byte) ' '); 127 | expectLastCall().andReturn((byte) '0'); 128 | expectLastCall().andReturn((byte) '0'); 129 | expectLastCall().andReturn((byte) '>'); 130 | 131 | replayAll(); 132 | 133 | // call the method to test 134 | command.readResult(mockIn); 135 | assertEquals(command.getKm(), 0); 136 | 137 | verifyAll(); 138 | } 139 | 140 | /** 141 | * Clear resources. 142 | */ 143 | @AfterClass 144 | public void tearDown() { 145 | command = null; 146 | mockIn = null; 147 | } 148 | 149 | } 150 | -------------------------------------------------------------------------------- /src/test/java/com/github/pires/obd/commands/DtcNumberCommandTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import com.github.pires.obd.commands.control.DtcNumberCommand; 16 | import org.powermock.core.classloader.annotations.PrepareForTest; 17 | import org.testng.annotations.AfterClass; 18 | import org.testng.annotations.BeforeMethod; 19 | import org.testng.annotations.Test; 20 | 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | import static org.powermock.api.easymock.PowerMock.*; 25 | import static org.testng.Assert.*; 26 | 27 | /** 28 | * Tests for DtcNumberCommand class. 29 | */ 30 | @PrepareForTest(InputStream.class) 31 | public class DtcNumberCommandTest { 32 | 33 | private DtcNumberCommand command; 34 | private InputStream mockIn; 35 | 36 | /** 37 | * @throws Exception 38 | */ 39 | @BeforeMethod 40 | public void setUp() throws Exception { 41 | command = new DtcNumberCommand(); 42 | } 43 | 44 | /** 45 | * Test for valid InputStream read, MIL on. 46 | * 47 | * @throws IOException 48 | */ 49 | @Test 50 | public void testMILOn() throws IOException { 51 | // mock InputStream read 52 | mockIn = createMock(InputStream.class); 53 | mockIn.read(); 54 | expectLastCall().andReturn((byte) '4'); 55 | expectLastCall().andReturn((byte) '1'); 56 | expectLastCall().andReturn((byte) ' '); 57 | expectLastCall().andReturn((byte) '0'); 58 | expectLastCall().andReturn((byte) '1'); 59 | expectLastCall().andReturn((byte) ' '); 60 | expectLastCall().andReturn((byte) '9'); 61 | expectLastCall().andReturn((byte) 'F'); 62 | expectLastCall().andReturn((byte) '>'); 63 | 64 | replayAll(); 65 | 66 | // call the method to test 67 | command.readResult(mockIn); 68 | command.getFormattedResult(); 69 | 70 | assertTrue(command.getMilOn()); 71 | assertEquals(command.getTotalAvailableCodes(), 31); 72 | 73 | verifyAll(); 74 | } 75 | 76 | /** 77 | * Test for valid InputStream read, MIL off. 78 | * 79 | * @throws IOException 80 | */ 81 | @Test 82 | public void testMILOff() throws IOException { 83 | // mock InputStream read 84 | mockIn = createMock(InputStream.class); 85 | mockIn.read(); 86 | expectLastCall().andReturn((byte) '4'); 87 | expectLastCall().andReturn((byte) '1'); 88 | expectLastCall().andReturn((byte) ' '); 89 | expectLastCall().andReturn((byte) '0'); 90 | expectLastCall().andReturn((byte) '1'); 91 | expectLastCall().andReturn((byte) ' '); 92 | expectLastCall().andReturn((byte) '0'); 93 | expectLastCall().andReturn((byte) 'F'); 94 | expectLastCall().andReturn((byte) '>'); 95 | 96 | replayAll(); 97 | 98 | // call the method to test 99 | command.readResult(mockIn); 100 | command.getFormattedResult(); 101 | 102 | assertFalse(command.getMilOn()); 103 | assertEquals(command.getTotalAvailableCodes(), 15); 104 | 105 | verifyAll(); 106 | } 107 | 108 | /** 109 | * Clear resources. 110 | */ 111 | @AfterClass 112 | public void tearDown() { 113 | command = null; 114 | mockIn = null; 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/test/java/com/github/pires/obd/commands/ExtraMessagesTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import org.powermock.core.classloader.annotations.PrepareForTest; 16 | import org.testng.annotations.BeforeMethod; 17 | import org.testng.annotations.Test; 18 | 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | 22 | import static org.powermock.api.easymock.PowerMock.*; 23 | import static org.testng.Assert.assertEquals; 24 | 25 | /** 26 | * Test results with echo on and off. 27 | */ 28 | @PrepareForTest(InputStream.class) 29 | public class ExtraMessagesTest { 30 | 31 | private SpeedCommand command; 32 | private InputStream mockIn; 33 | 34 | /** 35 | * @throws Exception 36 | */ 37 | @BeforeMethod 38 | public void setUp() throws Exception { 39 | command = new SpeedCommand(); 40 | } 41 | 42 | /** 43 | * Test for valid InputStream read with echo 44 | * 45 | * @throws java.io.IOException 46 | */ 47 | @Test 48 | public void testValidSpeedMetricWithMessage() throws IOException { 49 | // mock InputStream read 50 | mockIn = createMock(InputStream.class); 51 | mockIn.read(); 52 | expectLastCall().andReturn((byte) 'B'); 53 | expectLastCall().andReturn((byte) 'U'); 54 | expectLastCall().andReturn((byte) 'S'); 55 | expectLastCall().andReturn((byte) ' '); 56 | expectLastCall().andReturn((byte) 'I'); 57 | expectLastCall().andReturn((byte) 'N'); 58 | expectLastCall().andReturn((byte) 'I'); 59 | expectLastCall().andReturn((byte) 'T'); 60 | expectLastCall().andReturn((byte) '.'); 61 | expectLastCall().andReturn((byte) '.'); 62 | expectLastCall().andReturn((byte) '.'); 63 | expectLastCall().andReturn((byte) 13); 64 | expectLastCall().andReturn((byte) '4'); 65 | expectLastCall().andReturn((byte) '1'); 66 | expectLastCall().andReturn((byte) ' '); 67 | expectLastCall().andReturn((byte) '0'); 68 | expectLastCall().andReturn((byte) 'D'); 69 | expectLastCall().andReturn((byte) ' '); 70 | expectLastCall().andReturn((byte) '4'); 71 | expectLastCall().andReturn((byte) '0'); 72 | expectLastCall().andReturn((byte) '>'); 73 | 74 | replayAll(); 75 | 76 | // call the method to test 77 | command.readResult(mockIn); 78 | command.getFormattedResult(); 79 | assertEquals(command.getMetricSpeed(), 64); 80 | 81 | verifyAll(); 82 | } 83 | 84 | /** 85 | * Test for valid InputStream read with echo 86 | * 87 | * @throws java.io.IOException 88 | */ 89 | @Test 90 | public void testValidSpeedMetricWithoutMessage() throws IOException { 91 | // mock InputStream read 92 | mockIn = createMock(InputStream.class); 93 | mockIn.read(); 94 | expectLastCall().andReturn((byte) '4'); 95 | expectLastCall().andReturn((byte) '1'); 96 | expectLastCall().andReturn((byte) ' '); 97 | expectLastCall().andReturn((byte) '0'); 98 | expectLastCall().andReturn((byte) 'D'); 99 | expectLastCall().andReturn((byte) ' '); 100 | expectLastCall().andReturn((byte) '4'); 101 | expectLastCall().andReturn((byte) '0'); 102 | expectLastCall().andReturn((byte) '>'); 103 | 104 | replayAll(); 105 | 106 | // call the method to test 107 | command.readResult(mockIn); 108 | command.getFormattedResult(); 109 | assertEquals(command.getMetricSpeed(), 64); 110 | 111 | verifyAll(); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/test/java/com/github/pires/obd/commands/FindFuelTypeCommandTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import com.github.pires.obd.commands.fuel.FindFuelTypeCommand; 16 | import org.testng.annotations.AfterClass; 17 | import org.testng.annotations.BeforeMethod; 18 | import org.testng.annotations.Test; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | 23 | import static org.powermock.api.easymock.PowerMock.*; 24 | import static org.testng.Assert.assertEquals; 25 | 26 | /** 27 | * Tests for FindFuelTypeCommand class. 28 | */ 29 | public class FindFuelTypeCommandTest { 30 | 31 | private FindFuelTypeCommand command; 32 | private InputStream mockIn; 33 | 34 | /** 35 | * @throws Exception 36 | */ 37 | @BeforeMethod 38 | public void setUp() throws Exception { 39 | command = new FindFuelTypeCommand(); 40 | } 41 | 42 | /** 43 | * Test for valid InputStream read, Gasoline 44 | * 45 | * @throws IOException 46 | */ 47 | @Test 48 | public void testFindGasoline() throws IOException { 49 | // mock InputStream read 50 | mockIn = createMock(InputStream.class); 51 | mockIn.read(); 52 | expectLastCall().andReturn((byte) '4'); 53 | expectLastCall().andReturn((byte) '1'); 54 | expectLastCall().andReturn((byte) ' '); 55 | expectLastCall().andReturn((byte) '5'); 56 | expectLastCall().andReturn((byte) '1'); 57 | expectLastCall().andReturn((byte) ' '); 58 | expectLastCall().andReturn((byte) '0'); 59 | expectLastCall().andReturn((byte) '1'); 60 | expectLastCall().andReturn((byte) '>'); 61 | 62 | replayAll(); 63 | 64 | // call the method to test 65 | command.readResult(mockIn); 66 | assertEquals(command.getFormattedResult(), "Gasoline"); 67 | 68 | verifyAll(); 69 | } 70 | 71 | /** 72 | * Test for valid InputStream read, Diesel 73 | * 74 | * @throws IOException 75 | */ 76 | @Test 77 | public void testDiesel() throws IOException { 78 | // mock InputStream read 79 | mockIn = createMock(InputStream.class); 80 | mockIn.read(); 81 | expectLastCall().andReturn((byte) '4'); 82 | expectLastCall().andReturn((byte) '1'); 83 | expectLastCall().andReturn((byte) ' '); 84 | expectLastCall().andReturn((byte) '5'); 85 | expectLastCall().andReturn((byte) '1'); 86 | expectLastCall().andReturn((byte) ' '); 87 | expectLastCall().andReturn((byte) '0'); 88 | expectLastCall().andReturn((byte) '4'); 89 | expectLastCall().andReturn((byte) '>'); 90 | 91 | replayAll(); 92 | 93 | // call the method to test 94 | command.readResult(mockIn); 95 | assertEquals(command.getFormattedResult(), "Diesel"); 96 | 97 | verifyAll(); 98 | } 99 | 100 | /** 101 | * Test for valid InputStream read, Ethanol 102 | * 103 | * @throws IOException 104 | */ 105 | @Test 106 | public void testHybridEthanol() throws IOException { 107 | // mock InputStream read 108 | mockIn = createMock(InputStream.class); 109 | mockIn.read(); 110 | expectLastCall().andReturn((byte) '4'); 111 | expectLastCall().andReturn((byte) '1'); 112 | expectLastCall().andReturn((byte) ' '); 113 | expectLastCall().andReturn((byte) '5'); 114 | expectLastCall().andReturn((byte) '1'); 115 | expectLastCall().andReturn((byte) ' '); 116 | expectLastCall().andReturn((byte) '1'); 117 | expectLastCall().andReturn((byte) '2'); 118 | expectLastCall().andReturn((byte) '>'); 119 | 120 | replayAll(); 121 | 122 | // call the method to test 123 | command.readResult(mockIn); 124 | assertEquals(command.getFormattedResult(), "Hybrid Ethanol"); 125 | 126 | verifyAll(); 127 | } 128 | 129 | /** 130 | * Clear resources. 131 | */ 132 | @AfterClass 133 | public void tearDown() { 134 | command = null; 135 | mockIn = null; 136 | } 137 | 138 | } 139 | -------------------------------------------------------------------------------- /src/test/java/com/github/pires/obd/commands/FuelLevelCommandTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import com.github.pires.obd.commands.fuel.FuelLevelCommand; 16 | import org.testng.annotations.AfterClass; 17 | import org.testng.annotations.BeforeMethod; 18 | import org.testng.annotations.Test; 19 | 20 | import java.io.IOException; 21 | import java.io.InputStream; 22 | 23 | import static org.powermock.api.easymock.PowerMock.*; 24 | import static org.testng.Assert.assertEquals; 25 | 26 | /** 27 | * Tests for FuelLevelCommand class. 28 | */ 29 | public class FuelLevelCommandTest { 30 | private FuelLevelCommand command = null; 31 | private InputStream mockIn = null; 32 | 33 | /** 34 | * @throws Exception 35 | */ 36 | @BeforeMethod 37 | public void setUp() throws Exception { 38 | command = new FuelLevelCommand(); 39 | } 40 | 41 | /** 42 | * Test for valid InputStream read, full tank 43 | * 44 | * @throws IOException 45 | */ 46 | @Test 47 | public void testFullTank() throws IOException { 48 | // mock InputStream read 49 | mockIn = createMock(InputStream.class); 50 | mockIn.read(); 51 | expectLastCall().andReturn((byte) '4'); 52 | expectLastCall().andReturn((byte) '1'); 53 | expectLastCall().andReturn((byte) ' '); 54 | expectLastCall().andReturn((byte) '2'); 55 | expectLastCall().andReturn((byte) 'F'); 56 | expectLastCall().andReturn((byte) ' '); 57 | expectLastCall().andReturn((byte) 'F'); 58 | expectLastCall().andReturn((byte) 'F'); 59 | expectLastCall().andReturn((byte) '>'); 60 | 61 | replayAll(); 62 | 63 | // call the method to test 64 | command.readResult(mockIn); 65 | assertEquals(command.getFuelLevel(), 100f); 66 | 67 | verifyAll(); 68 | } 69 | 70 | /** 71 | * Test for valid InputStream read. 78.4% 72 | * 73 | * @throws IOException 74 | */ 75 | @Test 76 | public void testSomeValue() throws IOException { 77 | // mock InputStream read 78 | mockIn = createMock(InputStream.class); 79 | mockIn.read(); 80 | expectLastCall().andReturn((byte) '4'); 81 | expectLastCall().andReturn((byte) '1'); 82 | expectLastCall().andReturn((byte) ' '); 83 | expectLastCall().andReturn((byte) '2'); 84 | expectLastCall().andReturn((byte) 'F'); 85 | expectLastCall().andReturn((byte) ' '); 86 | expectLastCall().andReturn((byte) 'C'); 87 | expectLastCall().andReturn((byte) '8'); 88 | expectLastCall().andReturn((byte) '>'); 89 | 90 | replayAll(); 91 | 92 | // call the method to test 93 | command.readResult(mockIn); 94 | assertEquals(command.getFuelLevel(), 78.43137f); 95 | 96 | verifyAll(); 97 | } 98 | 99 | /** 100 | * Test for valid InputStream read, empty tank 101 | * 102 | * @throws IOException 103 | */ 104 | @Test 105 | public void testEmptyTank() throws IOException { 106 | // mock InputStream read 107 | mockIn = createMock(InputStream.class); 108 | mockIn.read(); 109 | expectLastCall().andReturn((byte) '4'); 110 | expectLastCall().andReturn((byte) '1'); 111 | expectLastCall().andReturn((byte) ' '); 112 | expectLastCall().andReturn((byte) '2'); 113 | expectLastCall().andReturn((byte) 'F'); 114 | expectLastCall().andReturn((byte) ' '); 115 | expectLastCall().andReturn((byte) '0'); 116 | expectLastCall().andReturn((byte) '0'); 117 | expectLastCall().andReturn((byte) '>'); 118 | 119 | replayAll(); 120 | 121 | // call the method to test 122 | command.readResult(mockIn); 123 | assertEquals(command.getFuelLevel(), 0f); 124 | 125 | verifyAll(); 126 | } 127 | 128 | /** 129 | * Clear resources. 130 | */ 131 | @AfterClass 132 | public void tearDown() { 133 | command = null; 134 | mockIn = null; 135 | } 136 | 137 | } 138 | -------------------------------------------------------------------------------- /src/test/java/com/github/pires/obd/commands/FuelTrimCommandTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at 5 | * http://www.apache.org/licenses/LICENSE-2.0 6 | * 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 | * License for the specific language governing permissions and limitations under 11 | * the License. 12 | */ 13 | package com.github.pires.obd.commands; 14 | 15 | import com.github.pires.obd.commands.fuel.FuelTrimCommand; 16 | import com.github.pires.obd.enums.FuelTrim; 17 | import org.powermock.core.classloader.annotations.PrepareForTest; 18 | import org.testng.annotations.AfterClass; 19 | import org.testng.annotations.BeforeMethod; 20 | import org.testng.annotations.Test; 21 | 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | 25 | import static org.powermock.api.easymock.PowerMock.*; 26 | import static org.testng.Assert.assertEquals; 27 | 28 | /** 29 | * Tests for FuelTrimCommand class. 30 | *
31 | * TODO replace integer values in expected values with strings, like in other
32 | * tests.
33 | */
34 | @PrepareForTest(InputStream.class)
35 | public class FuelTrimCommandTest {
36 |
37 | private FuelTrimCommand command;
38 | private InputStream mockIn;
39 | private FuelTrim fuelTrimBank = FuelTrim.LONG_TERM_BANK_1;
40 |
41 | /**
42 | * @throws Exception
43 | */
44 | @BeforeMethod
45 | public void setUp() throws Exception {
46 | command = new FuelTrimCommand(FuelTrim.LONG_TERM_BANK_1);
47 | }
48 |
49 | /**
50 | * Test for valid InputStream read, 99.22%
51 | *
52 | * @throws IOException
53 | */
54 | @Test(enabled = false)
55 | public void testMaxFuelTrimValue() throws IOException {
56 | // mock InputStream read
57 | mockIn = createMock(InputStream.class);
58 | mockIn.read();
59 | expectLastCall().andReturn(0x41);
60 | expectLastCall().andReturn(fuelTrimBank.getValue());
61 | expectLastCall().andReturn(0xFF);
62 | expectLastCall().andReturn(0x3E); // '>'
63 |
64 | replayAll();
65 |
66 | // call the method to test
67 | command.readResult(mockIn);
68 | assertEquals(command.getValue(), 99.22f);
69 |
70 | verifyAll();
71 | }
72 |
73 | /**
74 | * Test for valid InputStream read, 56.25%
75 | *
76 | * @throws IOException
77 | */
78 | @Test(enabled = false)
79 | public void testSomeValue() throws IOException {
80 | // mock InputStream read
81 | mockIn = createMock(InputStream.class);
82 | mockIn.read();
83 | expectLastCall().andReturn(0x41);
84 | expectLastCall().andReturn(0x20);
85 | expectLastCall().andReturn(fuelTrimBank.getValue());
86 | expectLastCall().andReturn(0x20);
87 | expectLastCall().andReturn(0xC8);
88 | expectLastCall().andReturn(0x20);
89 | expectLastCall().andReturn(0x3E); // '>'
90 |
91 | replayAll();
92 |
93 | // call the method to test
94 | command.readResult(mockIn);
95 | assertEquals(command.getValue(), 56.25f);
96 |
97 | verifyAll();
98 | }
99 |
100 | /**
101 | * Test for valid InputStream read, -100.00%
102 | *
103 | * @throws IOException
104 | */
105 | @Test(enabled = false)
106 | public void testMinFuelTrimValue() throws IOException {
107 | // mock InputStream read
108 | mockIn = createMock(InputStream.class);
109 | mockIn.read();
110 | expectLastCall().andReturn(0x41);
111 | expectLastCall().andReturn(0x20);
112 | expectLastCall().andReturn(fuelTrimBank.getValue());
113 | expectLastCall().andReturn(0x20);
114 | expectLastCall().andReturn(0x00);
115 | expectLastCall().andReturn(0x20);
116 | expectLastCall().andReturn(0x3E); // '>'
117 |
118 | replayAll();
119 |
120 | // call the method to test
121 | command.readResult(mockIn);
122 | assertEquals(command.getValue(), -100f);
123 |
124 | verifyAll();
125 | }
126 |
127 | /**
128 | * Clear resources.
129 | */
130 | @AfterClass
131 | public void tearDown() {
132 | command = null;
133 | mockIn = null;
134 | }
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/commands/IntakeManifoldPressureCommandTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.commands;
14 |
15 | import com.github.pires.obd.commands.pressure.IntakeManifoldPressureCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.AfterClass;
18 | import org.testng.annotations.BeforeMethod;
19 | import org.testng.annotations.Test;
20 |
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 | import static org.testng.Assert.assertEquals;
26 |
27 | /**
28 | * Tests for PressureCommand sub-classes.
29 | */
30 | @PrepareForTest(InputStream.class)
31 | public class IntakeManifoldPressureCommandTest {
32 |
33 | private IntakeManifoldPressureCommand command;
34 | private InputStream mockIn;
35 |
36 | /**
37 | * @throws Exception
38 | */
39 | @BeforeMethod
40 | public void setUp() throws Exception {
41 | command = new IntakeManifoldPressureCommand();
42 | }
43 |
44 | /**
45 | * Test for valid InputStream read, 100kPa
46 | *
47 | * @throws IOException
48 | */
49 | @Test
50 | public void testValidPressureMetric() throws IOException {
51 | // mock InputStream read
52 | mockIn = createMock(InputStream.class);
53 | mockIn.read();
54 | expectLastCall().andReturn((byte) '4');
55 | expectLastCall().andReturn((byte) '1');
56 | expectLastCall().andReturn((byte) ' ');
57 | expectLastCall().andReturn((byte) '0');
58 | expectLastCall().andReturn((byte) 'B');
59 | expectLastCall().andReturn((byte) ' ');
60 | expectLastCall().andReturn((byte) '6');
61 | expectLastCall().andReturn((byte) '4');
62 | expectLastCall().andReturn((byte) '>');
63 |
64 | replayAll();
65 |
66 | // call the method to test
67 | command.readResult(mockIn);
68 | command.useImperialUnits(false);
69 | assertEquals(command.getMetricUnit(), 100);
70 |
71 | verifyAll();
72 | }
73 |
74 | /**
75 | * Test for valid InputStream read, 14.50psi
76 | *
77 | * @throws IOException
78 | */
79 | @Test
80 | public void testValidPressureImperial() throws IOException {
81 | // mock InputStream read
82 | mockIn = createMock(InputStream.class);
83 | mockIn.read();
84 | expectLastCall().andReturn((byte) '4');
85 | expectLastCall().andReturn((byte) '1');
86 | expectLastCall().andReturn((byte) ' ');
87 | expectLastCall().andReturn((byte) '0');
88 | expectLastCall().andReturn((byte) 'B');
89 | expectLastCall().andReturn((byte) ' ');
90 | expectLastCall().andReturn((byte) '6');
91 | expectLastCall().andReturn((byte) '4');
92 | expectLastCall().andReturn((byte) '>');
93 |
94 | replayAll();
95 |
96 | // call the method to test
97 | command.readResult(mockIn);
98 | command.useImperialUnits(true);
99 | assertEquals(command.getImperialUnit(), 14.503774f);
100 |
101 | verifyAll();
102 | }
103 |
104 | /**
105 | * Clear resources.
106 | */
107 | @AfterClass
108 | public void tearDown() {
109 | command = null;
110 | mockIn = null;
111 | }
112 |
113 | }
114 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/commands/MassAirFlowCommandTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.commands;
14 |
15 | import com.github.pires.obd.commands.engine.MassAirFlowCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.AfterClass;
18 | import org.testng.annotations.BeforeMethod;
19 | import org.testng.annotations.Test;
20 |
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 | import static org.testng.Assert.assertEquals;
26 |
27 | /**
28 | * Tests for MassAirFlowCommand class.
29 | */
30 | @PrepareForTest(InputStream.class)
31 | public class MassAirFlowCommandTest {
32 | private MassAirFlowCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new MassAirFlowCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read, maximum value of 655.35g/s
45 | *
46 | * @throws IOException
47 | */
48 | @Test
49 | public void testMaxMAFValue() throws IOException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) '4');
54 | expectLastCall().andReturn((byte) '1');
55 | expectLastCall().andReturn((byte) ' ');
56 | expectLastCall().andReturn((byte) '1');
57 | expectLastCall().andReturn((byte) '0');
58 | expectLastCall().andReturn((byte) ' ');
59 | expectLastCall().andReturn((byte) 'F');
60 | expectLastCall().andReturn((byte) 'F');
61 | expectLastCall().andReturn((byte) ' ');
62 | expectLastCall().andReturn((byte) 'F');
63 | expectLastCall().andReturn((byte) 'F');
64 | expectLastCall().andReturn((byte) '>');
65 |
66 | replayAll();
67 |
68 | // call the method to test
69 | command.readResult(mockIn);
70 | assertEquals(command.getMAF(), 655.3499755859375d);
71 |
72 | verifyAll();
73 | }
74 |
75 | /**
76 | * Test for valid InputStream read, 381.61g/s
77 | *
78 | * @throws IOException
79 | */
80 | @Test
81 | public void testSomeMAFValue() throws IOException {
82 | // mock InputStream read
83 | mockIn = createMock(InputStream.class);
84 | mockIn.read();
85 | expectLastCall().andReturn((byte) '4');
86 | expectLastCall().andReturn((byte) '1');
87 | expectLastCall().andReturn((byte) ' ');
88 | expectLastCall().andReturn((byte) '1');
89 | expectLastCall().andReturn((byte) '0');
90 | expectLastCall().andReturn((byte) ' ');
91 | expectLastCall().andReturn((byte) '9');
92 | expectLastCall().andReturn((byte) '5');
93 | expectLastCall().andReturn((byte) ' ');
94 | expectLastCall().andReturn((byte) '1');
95 | expectLastCall().andReturn((byte) '1');
96 | expectLastCall().andReturn((byte) '>');
97 |
98 | replayAll();
99 |
100 | // call the method to test
101 | command.readResult(mockIn);
102 | assertEquals(command.getMAF(), 381.6099853515625d);
103 |
104 | verifyAll();
105 | }
106 |
107 | /**
108 | * Test for valid InputStream read, minimum value 0g/s
109 | *
110 | * @throws IOException
111 | */
112 | @Test
113 | public void testMinMAFValue() throws IOException {
114 | // mock InputStream read
115 | mockIn = createMock(InputStream.class);
116 | mockIn.read();
117 | expectLastCall().andReturn((byte) '4');
118 | expectLastCall().andReturn((byte) '1');
119 | expectLastCall().andReturn((byte) ' ');
120 | expectLastCall().andReturn((byte) '1');
121 | expectLastCall().andReturn((byte) '0');
122 | expectLastCall().andReturn((byte) ' ');
123 | expectLastCall().andReturn((byte) '0');
124 | expectLastCall().andReturn((byte) '0');
125 | expectLastCall().andReturn((byte) ' ');
126 | expectLastCall().andReturn((byte) '0');
127 | expectLastCall().andReturn((byte) '0');
128 | expectLastCall().andReturn((byte) '>');
129 |
130 | replayAll();
131 |
132 | // call the method to test
133 | command.readResult(mockIn);
134 | assertEquals(command.getMAF(), 0d);
135 |
136 | verifyAll();
137 | }
138 |
139 | /**
140 | * Clear resources.
141 | */
142 | @AfterClass
143 | public void tearDown() {
144 | command = null;
145 | mockIn = null;
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/commands/RPMCommandTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.commands;
14 |
15 | import com.github.pires.obd.commands.engine.RPMCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.AfterClass;
18 | import org.testng.annotations.BeforeMethod;
19 | import org.testng.annotations.Test;
20 |
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 | import static org.testng.Assert.assertEquals;
26 |
27 | /**
28 | * Tests for RPMCommand class.
29 | */
30 | @PrepareForTest(InputStream.class)
31 | public class RPMCommandTest {
32 |
33 | private RPMCommand command = null;
34 | private InputStream mockIn = null;
35 |
36 | /**
37 | * @throws Exception
38 | */
39 | @BeforeMethod
40 | public void setUp() throws Exception {
41 | command = new RPMCommand();
42 | }
43 |
44 | /**
45 | * Test for valid InputStream read, max RPM
46 | *
47 | * @throws IOException
48 | */
49 | @Test
50 | public void testMaximumRPMValue() throws IOException {
51 | // mock InputStream read
52 | mockIn = createMock(InputStream.class);
53 | mockIn.read();
54 | expectLastCall().andReturn((byte) '4');
55 | expectLastCall().andReturn((byte) '1');
56 | expectLastCall().andReturn((byte) ' ');
57 | expectLastCall().andReturn((byte) '0');
58 | expectLastCall().andReturn((byte) 'C');
59 | expectLastCall().andReturn((byte) ' ');
60 | expectLastCall().andReturn((byte) 'F');
61 | expectLastCall().andReturn((byte) 'F');
62 | expectLastCall().andReturn((byte) ' ');
63 | expectLastCall().andReturn((byte) 'F');
64 | expectLastCall().andReturn((byte) 'F');
65 | expectLastCall().andReturn((byte) '>');
66 |
67 | replayAll();
68 |
69 | // call the method to test
70 | command.readResult(mockIn);
71 | assertEquals(command.getRPM(), 16383);
72 |
73 | verifyAll();
74 | }
75 |
76 | /**
77 | * Test for valid InputStream read
78 | *
79 | * @throws IOException
80 | */
81 | @Test
82 | public void testHighRPM() throws IOException {
83 | // mock InputStream read
84 | mockIn = createMock(InputStream.class);
85 | mockIn.read();
86 | expectLastCall().andReturn((byte) '4');
87 | expectLastCall().andReturn((byte) '1');
88 | expectLastCall().andReturn((byte) ' ');
89 | expectLastCall().andReturn((byte) '0');
90 | expectLastCall().andReturn((byte) 'C');
91 | expectLastCall().andReturn((byte) ' ');
92 | expectLastCall().andReturn((byte) '2');
93 | expectLastCall().andReturn((byte) '8');
94 | expectLastCall().andReturn((byte) ' ');
95 | expectLastCall().andReturn((byte) '3');
96 | expectLastCall().andReturn((byte) 'C');
97 | expectLastCall().andReturn((byte) '>');
98 |
99 | replayAll();
100 |
101 | // call the method to test
102 | command.readResult(mockIn);
103 | assertEquals(command.getRPM(), 2575);
104 |
105 | verifyAll();
106 | }
107 |
108 | /**
109 | * Test for valid InputStream read
110 | *
111 | * @throws IOException
112 | */
113 | @Test
114 | public void testLowRPM() throws IOException {
115 | // mock InputStream read
116 | mockIn = createMock(InputStream.class);
117 | mockIn.read();
118 | expectLastCall().andReturn((byte) '4');
119 | expectLastCall().andReturn((byte) '1');
120 | expectLastCall().andReturn((byte) ' ');
121 | expectLastCall().andReturn((byte) '0');
122 | expectLastCall().andReturn((byte) 'C');
123 | expectLastCall().andReturn((byte) ' ');
124 | expectLastCall().andReturn((byte) '0');
125 | expectLastCall().andReturn((byte) 'A');
126 | expectLastCall().andReturn((byte) ' ');
127 | expectLastCall().andReturn((byte) '0');
128 | expectLastCall().andReturn((byte) '0');
129 | expectLastCall().andReturn((byte) '>');
130 |
131 | replayAll();
132 |
133 | // call the method to test
134 | command.readResult(mockIn);
135 | assertEquals(command.getRPM(), 640);
136 |
137 | verifyAll();
138 | }
139 |
140 | /**
141 | * Clear resources.
142 | */
143 | @AfterClass
144 | public void tearDown() {
145 | command = null;
146 | mockIn = null;
147 | }
148 |
149 | }
150 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/commands/SpeedCommandTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.commands;
14 |
15 | import org.powermock.core.classloader.annotations.PrepareForTest;
16 | import org.testng.annotations.AfterClass;
17 | import org.testng.annotations.BeforeMethod;
18 | import org.testng.annotations.Test;
19 |
20 | import java.io.IOException;
21 | import java.io.InputStream;
22 |
23 | import static org.powermock.api.easymock.PowerMock.*;
24 | import static org.testng.Assert.assertEquals;
25 |
26 | /**
27 | * Tests for ObdSpeedCommand class.
28 | */
29 | @PrepareForTest(InputStream.class)
30 | public class SpeedCommandTest {
31 |
32 | private SpeedCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new SpeedCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read, 64km/h
45 | *
46 | * @throws IOException
47 | */
48 | @Test
49 | public void testValidSpeedMetric() throws IOException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) '4');
54 | expectLastCall().andReturn((byte) '1');
55 | expectLastCall().andReturn((byte) ' ');
56 | expectLastCall().andReturn((byte) '0');
57 | expectLastCall().andReturn((byte) 'D');
58 | expectLastCall().andReturn((byte) ' ');
59 | expectLastCall().andReturn((byte) '4');
60 | expectLastCall().andReturn((byte) '0');
61 | expectLastCall().andReturn((byte) '>');
62 |
63 | replayAll();
64 |
65 | // call the method to test
66 | command.readResult(mockIn);
67 | command.getFormattedResult();
68 | assertEquals(command.getMetricSpeed(), 64);
69 |
70 | verifyAll();
71 | }
72 |
73 | /**
74 | * Test for valid InputStream read, 42.87mph
75 | *
76 | * @throws IOException
77 | */
78 | @Test
79 | public void testValidSpeedImperial() throws IOException {
80 | // mock InputStream read
81 | mockIn = createMock(InputStream.class);
82 | mockIn.read();
83 | expectLastCall().andReturn((byte) '4');
84 | expectLastCall().andReturn((byte) '1');
85 | expectLastCall().andReturn((byte) ' ');
86 | expectLastCall().andReturn((byte) '0');
87 | expectLastCall().andReturn((byte) 'D');
88 | expectLastCall().andReturn((byte) ' ');
89 | expectLastCall().andReturn((byte) '4');
90 | expectLastCall().andReturn((byte) '5');
91 | expectLastCall().andReturn((byte) '>');
92 |
93 | replayAll();
94 |
95 | // call the method to test
96 | command.readResult(mockIn);
97 | command.useImperialUnits = true;
98 | command.getFormattedResult();
99 | assertEquals(command.getImperialSpeed(), 42.874615f);
100 |
101 | verifyAll();
102 | }
103 |
104 | /**
105 | * Test for valid InputStream read, 0km/h
106 | *
107 | * @throws IOException
108 | */
109 | @Test
110 | public void testZeroSpeedMetric() throws IOException {
111 | // mock InputStream read
112 | mockIn = createMock(InputStream.class);
113 | mockIn.read();
114 | expectLastCall().andReturn((byte) '4');
115 | expectLastCall().andReturn((byte) '1');
116 | expectLastCall().andReturn((byte) ' ');
117 | expectLastCall().andReturn((byte) '0');
118 | expectLastCall().andReturn((byte) 'D');
119 | expectLastCall().andReturn((byte) ' ');
120 | expectLastCall().andReturn((byte) '0');
121 | expectLastCall().andReturn((byte) '0');
122 | expectLastCall().andReturn((byte) '>');
123 |
124 | replayAll();
125 |
126 | // call the method to test
127 | command.readResult(mockIn);
128 | command.getFormattedResult();
129 | assertEquals(command.getMetricSpeed(), 0);
130 |
131 | verifyAll();
132 | }
133 |
134 | /**
135 | * Clear resources.
136 | */
137 | @AfterClass
138 | public void tearDown() {
139 | command = null;
140 | mockIn = null;
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/commands/ThrottleCommandTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.commands;
14 |
15 | import com.github.pires.obd.commands.engine.ThrottlePositionCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.AfterClass;
18 | import org.testng.annotations.BeforeMethod;
19 | import org.testng.annotations.Test;
20 |
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 | import static org.testng.Assert.assertEquals;
26 |
27 | /**
28 | * Tests for ThrottlePositionCommand class.
29 | */
30 | @PrepareForTest(InputStream.class)
31 | public class ThrottleCommandTest {
32 | private ThrottlePositionCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new ThrottlePositionCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read, maximum value of 100%
45 | *
46 | * @throws IOException
47 | */
48 | @Test
49 | public void testMaxThrottlePositionValue() throws IOException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) '4');
54 | expectLastCall().andReturn((byte) '1');
55 | expectLastCall().andReturn((byte) ' ');
56 | expectLastCall().andReturn((byte) '1');
57 | expectLastCall().andReturn((byte) '1');
58 | expectLastCall().andReturn((byte) ' ');
59 | expectLastCall().andReturn((byte) 'F');
60 | expectLastCall().andReturn((byte) 'F');
61 | expectLastCall().andReturn((byte) '>');
62 |
63 | replayAll();
64 |
65 | // call the method to test
66 | command.readResult(mockIn);
67 | assertEquals(command.getPercentage(), 100f);
68 |
69 | verifyAll();
70 | }
71 |
72 | /**
73 | * Test for valid InputStream read, 58.4%
74 | *
75 | * @throws IOException
76 | */
77 | @Test
78 | public void testSomeThrottlePositionValue() throws IOException {
79 | // mock InputStream read
80 | mockIn = createMock(InputStream.class);
81 | mockIn.read();
82 | expectLastCall().andReturn((byte) '4');
83 | expectLastCall().andReturn((byte) '1');
84 | expectLastCall().andReturn((byte) ' ');
85 | expectLastCall().andReturn((byte) '1');
86 | expectLastCall().andReturn((byte) '1');
87 | expectLastCall().andReturn((byte) ' ');
88 | expectLastCall().andReturn((byte) '9');
89 | expectLastCall().andReturn((byte) '5');
90 | expectLastCall().andReturn((byte) '>');
91 |
92 | replayAll();
93 |
94 | // call the method to test
95 | command.readResult(mockIn);
96 | assertEquals(command.getPercentage(), 58.431374f);
97 |
98 | verifyAll();
99 | }
100 |
101 | /**
102 | * Test for valid InputStream read, minimum value 0%
103 | *
104 | * @throws IOException
105 | */
106 | @Test
107 | public void testMinThrottlePositionValue() throws IOException {
108 | // mock InputStream read
109 | mockIn = createMock(InputStream.class);
110 | mockIn.read();
111 | expectLastCall().andReturn((byte) '4');
112 | expectLastCall().andReturn((byte) '1');
113 | expectLastCall().andReturn((byte) ' ');
114 | expectLastCall().andReturn((byte) '1');
115 | expectLastCall().andReturn((byte) '1');
116 | expectLastCall().andReturn((byte) ' ');
117 | expectLastCall().andReturn((byte) '0');
118 | expectLastCall().andReturn((byte) '0');
119 | expectLastCall().andReturn((byte) '>');
120 |
121 | replayAll();
122 |
123 | // call the method to test
124 | command.readResult(mockIn);
125 | assertEquals(command.getPercentage(), 0f);
126 |
127 | verifyAll();
128 | }
129 |
130 | /**
131 | * Clear resources.
132 | */
133 | @AfterClass
134 | public void tearDown() {
135 | command = null;
136 | mockIn = null;
137 | }
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/commands/VinCommandTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.commands;
14 |
15 | import com.github.pires.obd.commands.control.VinCommand;
16 | import org.testng.annotations.AfterClass;
17 | import org.testng.annotations.BeforeMethod;
18 | import org.testng.annotations.Test;
19 |
20 | import java.io.IOException;
21 | import java.io.InputStream;
22 |
23 | import static org.powermock.api.easymock.PowerMock.*;
24 | import static org.testng.Assert.assertEquals;
25 |
26 | /**
27 | * Tests for VinCommand class.
28 | */
29 | public class VinCommandTest {
30 | private VinCommand command;
31 | private InputStream mockIn;
32 |
33 | /**
34 | * @throws Exception
35 | */
36 | @BeforeMethod
37 | public void setUp() throws Exception {
38 | command = new VinCommand();
39 | // mock InputStream read
40 | mockIn = createMock(InputStream.class);
41 | mockIn.read();
42 | }
43 |
44 | /**
45 | * Clear resources.
46 | */
47 | @AfterClass
48 | public void tearDown() {
49 | command = null;
50 | mockIn = null;
51 | }
52 |
53 | /**
54 | * Test VIN CAN (ISO-15765) format
55 | *
56 | * @throws IOException
57 | */
58 | @Test
59 | public void vinCAN() throws IOException {
60 | byte[] v = new byte[]{
61 | '0', '1', '4', '\n',
62 | '0', ':', ' ', '4', '9', ' ', '0', '2', ' ', '0', '1', ' ', '5', '7', ' ', '5', '0', ' ', '3', '0', '\n',
63 | '1', ':', ' ', '5', 'A', ' ', '5', 'A', ' ', '5', 'A', ' ', '3', '9', ' ', '3', '9', ' ', '5', 'A', ' ', '5', '4', '\n',
64 | '2', ':', ' ', '5', '3', ' ', '3', '3', ' ', '3', '9', ' ', '3', '2', ' ', '3', '1', ' ', '3', '2', ' ', '3', '4', '>'
65 | };
66 | for (byte b : v) {
67 | expectLastCall().andReturn(b);
68 | }
69 | replayAll();
70 | String res = "WP0ZZZ99ZTS392124";
71 |
72 | // call the method to test
73 | command.readResult(mockIn);
74 |
75 | assertEquals(command.getFormattedResult(), res);
76 |
77 | verifyAll();
78 | }
79 |
80 | /**
81 | * Test VIN ISO9141-2, KWP2000 Fast and KWP2000 5Kbps (ISO15031) format
82 | *
83 | * @throws IOException
84 | */
85 | @Test
86 | public void vin() throws IOException {
87 | byte[] v = new byte[]{
88 | '4', '9', ' ', '0', '2', ' ', '0', '1', ' ', '0', '0', ' ', '0', '0', ' ', '0', '0', ' ', '5', '7', '\n',
89 | '4', '9', ' ', '0', '2', ' ', '0', '2', ' ', '5', '0', ' ', '3', '0', ' ', '5', 'A', ' ', '5', 'A', '\n',
90 | '4', '9', ' ', '0', '2', ' ', '0', '3', ' ', '5', 'A', ' ', '3', '9', ' ', '3', '9', ' ', '5', 'A', '\n',
91 | '4', '9', ' ', '0', '2', ' ', '0', '4', ' ', '5', '4', ' ', '5', '3', ' ', '3', '3', ' ', '3', '9', '\n',
92 | '4', '9', ' ', '0', '2', ' ', '0', '5', ' ', '3', '2', ' ', '3', '1', ' ', '3', '2', ' ', '3', '4', '>'
93 | };
94 | for (byte b : v) {
95 | expectLastCall().andReturn(b);
96 | }
97 |
98 | replayAll();
99 | String res = "WP0ZZZ99ZTS392124";
100 |
101 | // call the method to test
102 | command.readResult(mockIn);
103 |
104 | assertEquals(command.getFormattedResult(), res);
105 |
106 | verifyAll();
107 | }
108 | }
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/exceptions/BusInitExceptionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.exceptions;
14 |
15 | import com.github.pires.obd.commands.SpeedCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.BeforeMethod;
18 | import org.testng.annotations.Test;
19 |
20 | import java.io.ByteArrayOutputStream;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 |
26 | /**
27 | * Test results with echo on and off.
28 | */
29 | @PrepareForTest(InputStream.class)
30 | public class BusInitExceptionTest {
31 |
32 | private SpeedCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new SpeedCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read with echo
45 | *
46 | * @throws java.io.IOException, java.lang.InterruptedException
47 | */
48 | @Test(expectedExceptions = BusInitException.class)
49 | public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) 'B');
54 | expectLastCall().andReturn((byte) 'U');
55 | expectLastCall().andReturn((byte) 'S');
56 | expectLastCall().andReturn((byte) ' ');
57 | expectLastCall().andReturn((byte) 'I');
58 | expectLastCall().andReturn((byte) 'N');
59 | expectLastCall().andReturn((byte) 'I');
60 | expectLastCall().andReturn((byte) 'T');
61 | expectLastCall().andReturn((byte) '.');
62 | expectLastCall().andReturn((byte) '.');
63 | expectLastCall().andReturn((byte) '.');
64 | expectLastCall().andReturn((byte) ' ');
65 | expectLastCall().andReturn((byte) 'E');
66 | expectLastCall().andReturn((byte) 'R');
67 | expectLastCall().andReturn((byte) 'R');
68 | expectLastCall().andReturn((byte) 'O');
69 | expectLastCall().andReturn((byte) 'R');
70 | expectLastCall().andReturn((byte) '>');
71 |
72 | replayAll();
73 |
74 | // call the method to test
75 | command.run(mockIn, new ByteArrayOutputStream());
76 |
77 | verifyAll();
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/exceptions/NoDataExceptionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.exceptions;
14 |
15 | import com.github.pires.obd.commands.SpeedCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.BeforeMethod;
18 | import org.testng.annotations.Test;
19 |
20 | import java.io.ByteArrayOutputStream;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 |
26 | /**
27 | * Test results with echo on and off.
28 | */
29 | @PrepareForTest(InputStream.class)
30 | public class NoDataExceptionTest {
31 |
32 | private SpeedCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new SpeedCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read with echo
45 | *
46 | * @throws java.io.IOException, java.lang.InterruptedException
47 | */
48 | @Test(expectedExceptions = NoDataException.class)
49 | public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) 'N');
54 | expectLastCall().andReturn((byte) 'O');
55 | expectLastCall().andReturn((byte) ' ');
56 | expectLastCall().andReturn((byte) 'D');
57 | expectLastCall().andReturn((byte) 'A');
58 | expectLastCall().andReturn((byte) 'T');
59 | expectLastCall().andReturn((byte) 'A');
60 | expectLastCall().andReturn((byte) '>');
61 |
62 | replayAll();
63 |
64 | // call the method to test
65 | command.run(mockIn, new ByteArrayOutputStream());
66 |
67 | verifyAll();
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/exceptions/NonNumericResponseExceptionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.exceptions;
14 |
15 | import com.github.pires.obd.commands.SpeedCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.BeforeMethod;
18 | import org.testng.annotations.Test;
19 |
20 | import java.io.ByteArrayOutputStream;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 |
26 | /**
27 | * Test results with echo on and off.
28 | */
29 | @PrepareForTest(InputStream.class)
30 | public class NonNumericResponseExceptionTest {
31 |
32 | private SpeedCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new SpeedCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read with echo
45 | *
46 | * @throws java.io.IOException, java.lang.InterruptedException
47 | */
48 | @Test(expectedExceptions = NonNumericResponseException.class)
49 | public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) 'O');
54 | expectLastCall().andReturn((byte) 'K');
55 | expectLastCall().andReturn((byte) '>');
56 |
57 | replayAll();
58 |
59 | // call the method to test
60 | command.run(mockIn, new ByteArrayOutputStream());
61 |
62 | verifyAll();
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/exceptions/StoppedExceptionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.exceptions;
14 |
15 | import com.github.pires.obd.commands.SpeedCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.BeforeMethod;
18 | import org.testng.annotations.Test;
19 |
20 | import java.io.ByteArrayOutputStream;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 |
26 | /**
27 | * Test results with echo on and off.
28 | */
29 | @PrepareForTest(InputStream.class)
30 | public class StoppedExceptionTest {
31 |
32 | private SpeedCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new SpeedCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read with echo
45 | *
46 | * @throws java.io.IOException, java.lang.InterruptedException
47 | */
48 | @Test(expectedExceptions = StoppedException.class)
49 | public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) 'S');
54 | expectLastCall().andReturn((byte) 'T');
55 | expectLastCall().andReturn((byte) 'O');
56 | expectLastCall().andReturn((byte) 'P');
57 | expectLastCall().andReturn((byte) 'P');
58 | expectLastCall().andReturn((byte) 'E');
59 | expectLastCall().andReturn((byte) 'D');
60 | expectLastCall().andReturn((byte) '>');
61 |
62 | replayAll();
63 |
64 | // call the method to test
65 | command.run(mockIn, new ByteArrayOutputStream());
66 |
67 | verifyAll();
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/exceptions/UnknownErrorExceptionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.exceptions;
14 |
15 | import com.github.pires.obd.commands.SpeedCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.BeforeMethod;
18 | import org.testng.annotations.Test;
19 |
20 | import java.io.ByteArrayOutputStream;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 |
26 | /**
27 | * Test results with echo on and off.
28 | */
29 | @PrepareForTest(InputStream.class)
30 | public class UnknownErrorExceptionTest {
31 |
32 | private SpeedCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new SpeedCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read with echo
45 | *
46 | * @throws java.io.IOException, java.lang.InterruptedException
47 | */
48 | @Test(expectedExceptions = UnknownErrorException.class)
49 | public void testValidSpeedMetricWithMessage() throws IOException, InterruptedException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) 'S');
54 | expectLastCall().andReturn((byte) 'E');
55 | expectLastCall().andReturn((byte) 'A');
56 | expectLastCall().andReturn((byte) 'R');
57 | expectLastCall().andReturn((byte) 'C');
58 | expectLastCall().andReturn((byte) 'H');
59 | expectLastCall().andReturn((byte) 'I');
60 | expectLastCall().andReturn((byte) 'N');
61 | expectLastCall().andReturn((byte) 'G');
62 | expectLastCall().andReturn((byte) '.');
63 | expectLastCall().andReturn((byte) '.');
64 | expectLastCall().andReturn((byte) '.');
65 | expectLastCall().andReturn((byte) ' ');
66 | expectLastCall().andReturn((byte) 'E');
67 | expectLastCall().andReturn((byte) 'R');
68 | expectLastCall().andReturn((byte) 'R');
69 | expectLastCall().andReturn((byte) 'O');
70 | expectLastCall().andReturn((byte) 'R');
71 | expectLastCall().andReturn((byte) '>');
72 |
73 | replayAll();
74 |
75 | // call the method to test
76 | command.run(mockIn, new ByteArrayOutputStream());
77 |
78 | verifyAll();
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/exceptions/UnsupportedCommandExceptionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.exceptions;
14 |
15 | import com.github.pires.obd.commands.SpeedCommand;
16 | import org.powermock.core.classloader.annotations.PrepareForTest;
17 | import org.testng.annotations.BeforeMethod;
18 | import org.testng.annotations.Test;
19 |
20 | import java.io.ByteArrayOutputStream;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | import static org.powermock.api.easymock.PowerMock.*;
25 |
26 | /**
27 | * Test results with echo on and off.
28 | */
29 | @PrepareForTest(InputStream.class)
30 | public class UnsupportedCommandExceptionTest {
31 |
32 | private SpeedCommand command;
33 | private InputStream mockIn;
34 |
35 | /**
36 | * @throws Exception
37 | */
38 | @BeforeMethod
39 | public void setUp() throws Exception {
40 | command = new SpeedCommand();
41 | }
42 |
43 | /**
44 | * Test for valid InputStream read with echo
45 | *
46 | * @throws java.io.IOException, java.lang.InterruptedException
47 | */
48 | @Test(expectedExceptions = UnsupportedCommandException.class)
49 | public void testUnsupportedVin() throws IOException, InterruptedException {
50 | // mock InputStream read
51 | mockIn = createMock(InputStream.class);
52 | mockIn.read();
53 | expectLastCall().andReturn((byte) '7');
54 | expectLastCall().andReturn((byte) 'F');
55 | expectLastCall().andReturn((byte) '0');
56 | expectLastCall().andReturn((byte) '9');
57 | expectLastCall().andReturn((byte) '1');
58 | expectLastCall().andReturn((byte) '2');
59 | expectLastCall().andReturn((byte) '>');
60 |
61 | replayAll();
62 |
63 | // call the method to test
64 | command.run(mockIn, new ByteArrayOutputStream());
65 |
66 | verifyAll();
67 | }
68 |
69 | /**
70 | * Test for valid InputStream read with echo
71 | *
72 | * @throws java.io.IOException, java.lang.InterruptedException
73 | */
74 | @Test(expectedExceptions = UnsupportedCommandException.class)
75 | public void testUnsupportedSpeed() throws IOException, InterruptedException {
76 | // mock InputStream read
77 | mockIn = createMock(InputStream.class);
78 | mockIn.read();
79 | expectLastCall().andReturn((byte) '7');
80 | expectLastCall().andReturn((byte) 'F');
81 | expectLastCall().andReturn((byte) '0');
82 | expectLastCall().andReturn((byte) '1');
83 | expectLastCall().andReturn((byte) '1');
84 | expectLastCall().andReturn((byte) '2');
85 | expectLastCall().andReturn((byte) '>');
86 |
87 | replayAll();
88 |
89 | // call the method to test
90 | command.run(mockIn, new ByteArrayOutputStream());
91 |
92 | verifyAll();
93 | }
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/src/test/java/com/github/pires/obd/utils/CommandAvailabilityHelperTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3 | * use this file except in compliance with the License. You may obtain a copy of
4 | * the License at
5 | * http://www.apache.org/licenses/LICENSE-2.0
6 | *
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations under
11 | * the License.
12 | */
13 | package com.github.pires.obd.utils;
14 |
15 | import com.github.pires.obd.commands.engine.ThrottlePositionCommand;
16 | import org.testng.Assert;
17 | import org.testng.annotations.Test;
18 |
19 | public class CommandAvailabilityHelperTest {
20 |
21 | @Test
22 | public void testDigestAvailabilityString() throws Exception {
23 | int[] expected = new int[]{Integer.parseInt("10111110", 2), Integer.parseInt("00011111", 2),
24 | Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2)};
25 | int[] result = CommandAvailabilityHelper.digestAvailabilityString("BE1FA813");
26 | Assert.assertEquals(expected, result);
27 |
28 | //Now with 16 characters
29 | expected = new int[]{Integer.parseInt("10111110", 2), Integer.parseInt("00011111", 2),
30 | Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2),
31 | Integer.parseInt("10111110", 2), Integer.parseInt("00011111", 2),
32 | Integer.parseInt("10101000", 2), Integer.parseInt("00010011", 2)};
33 |
34 | result = CommandAvailabilityHelper.digestAvailabilityString("BE1FA813BE1FA813");
35 | Assert.assertEquals(expected, result);
36 | }
37 |
38 | @Test
39 | public void testIsAvailable() throws Exception {
40 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("02", "BE1FA813"), false);
41 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("07", "BE1FA813"), true);
42 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable(new ThrottlePositionCommand().getCommandPID() /*11*/, "BE1FA813"), true);
43 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1A", "BE1FA813"), false);
44 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1D", "BE1FA813"), false);
45 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("1F", "BE1FA813"), true);
46 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("22", "BE1FA813BE1FA813"), false);
47 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("27", "BE1FA813BE1FA813"), true);
48 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3A", "BE1FA813BE1FA813"), false);
49 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3D", "BE1FA813BE1FA813"), false);
50 | Assert.assertEquals(CommandAvailabilityHelper.isAvailable("3F", "BE1FA813BE1FA813"), true);
51 | }
52 |
53 | @Test(expectedExceptions = IllegalArgumentException.class)
54 | public void testFail() throws Exception {
55 | CommandAvailabilityHelper.digestAvailabilityString("AAA");
56 | CommandAvailabilityHelper.digestAvailabilityString("AAAAAAAR");
57 | CommandAvailabilityHelper.isAvailable("2F", "BE1FA813");
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/src/test/resources/testng.xml:
--------------------------------------------------------------------------------
1 |
15 |
16 |
17 |