/** * <p>getImperialSpeed.</p> * * @return the speed in imperial units. */ public float getImperialSpeed() { return getImperialUnit(); }
/** * Iterate all commands, send them and read response. * * @param in a {@link java.io.InputStream} object. * @param out a {@link java.io.OutputStream} object. * @throws java.io.IOException if any. * @throws java.lang.InterruptedException if any. */ public void sendCommands(InputStream in, OutputStream out) throws IOException, InterruptedException { for (ObdCommand command : commands) command.run(in, out); }
/** * Reads the OBD-II response. * <p> * This method may be overriden in subclasses, such as ObdMultiCommand. * * @param in a {@link java.io.InputStream} object. * @throws java.io.IOException if any. */ protected void readResult(InputStream in) throws IOException { readRawData(in); checkForErrors(); fillBuffer(); performCalculations(); }
/** * <p>getFormattedResult.</p> * * @return a {@link java.lang.String} object. */ public String getFormattedResult() { return useImperialUnits ? String.format("%.2f%s", getImperialUnit(), getResultUnit()) : String.format("%d%s", getMetricSpeed(), getResultUnit()); }
private void updateTripStatistic(final ObdCommandJob job, final String cmdID) { if (currentTrip != null) { if (cmdID.equals(AvailableCommandNames.SPEED.toString())) { SpeedCommand command = (SpeedCommand) job.getCommand(); currentTrip.setSpeedMax(command.getMetricSpeed()); } else if (cmdID.equals(AvailableCommandNames.ENGINE_RPM.toString())) { RPMCommand command = (RPMCommand) job.getCommand(); currentTrip.setEngineRpmMax(command.getRPM()); } else if (cmdID.endsWith(AvailableCommandNames.ENGINE_RUNTIME.toString())) { RuntimeCommand command = (RuntimeCommand) job.getCommand(); currentTrip.setEngineRuntime(command.getFormattedResult()); } } }
public void stateUpdate(final ObdCommandJob job) { final String cmdName = job.getCommand().getName(); String cmdResult = ""; final String cmdID = LookUpCommand(cmdName); if (job.getState().equals(ObdCommandJob.ObdCommandJobState.EXECUTION_ERROR)) { cmdResult = job.getCommand().getResult(); if (cmdResult != null && isServiceBound) { obdStatusTextView.setText(cmdResult.toLowerCase()); } } else if (job.getState().equals(ObdCommandJob.ObdCommandJobState.BROKEN_PIPE)) { if (isServiceBound) stopLiveData(); } else if (job.getState().equals(ObdCommandJob.ObdCommandJobState.NOT_SUPPORTED)) { cmdResult = getString(R.string.status_obd_no_support); } else { cmdResult = job.getCommand().getFormattedResult(); if(isServiceBound) obdStatusTextView.setText(getString(R.string.status_obd_data)); } if (vv.findViewWithTag(cmdID) != null) { TextView existingTV = (TextView) vv.findViewWithTag(cmdID); existingTV.setText(cmdResult); } else addTableRow(cmdID, cmdName, cmdResult); commandResult.put(cmdID, cmdResult); updateTripStatistic(job, cmdID); }
/** {@inheritDoc} */ @Override public String getCalculatedResult() { return useImperialUnits ? String.valueOf(getImperialUnit()) : String.valueOf(getMetricSpeed()); }
/** {@inheritDoc} */ @Override public void run(InputStream in, OutputStream out) throws IOException, InterruptedException { String key = getClass().getSimpleName(); if (knownValues.containsKey(key)) { rawData = knownValues.get(key); buffer = knownBuffers.get(key); performCalculations(); } else { super.run(in, out); } } }
Log.d(TAG, "Job state is NEW. Run it.."); job.setState(ObdCommandJobState.RUNNING); Log.d(TAG, job.getCommand().getName()); job.getCommand().run(new ByteArrayInputStream("41 00 00 00>41 00 00 00>41 00 00 00>".getBytes()), new ByteArrayOutputStream()); } else { Log.e(TAG, "Job state was not new, so it shouldn't be in queue. BUG ALERT!");
/** * Sends the OBD-II request and deals with the response. * <p> * This method CAN be overriden in fake commands. * * @param in a {@link java.io.InputStream} object. * @param out a {@link java.io.OutputStream} object. * @throws java.io.IOException if any. * @throws java.lang.InterruptedException if any. */ public void run(InputStream in, OutputStream out) throws IOException, InterruptedException { synchronized (ObdCommand.class) {//Only one command can write and read a data in one time. start = System.currentTimeMillis(); sendCommand(out); readResult(in); end = System.currentTimeMillis(); } }
/** * <p>getFormattedResult.</p> * * @return a {@link java.lang.String} object. */ public String getFormattedResult() { StringBuilder res = new StringBuilder(); for (ObdCommand command : commands) res.append(command.getFormattedResult()).append(","); return res.toString(); }
/** * @param prefs * @return */ public static ArrayList<ObdCommand> getObdCommands(SharedPreferences prefs) { ArrayList<ObdCommand> cmds = ObdConfig.getCommands(); ArrayList<ObdCommand> ucmds = new ArrayList<>(); for (int i = 0; i < cmds.size(); i++) { ObdCommand cmd = cmds.get(i); boolean selected = prefs.getBoolean(cmd.getName(), true); if (selected) ucmds.add(cmd); } return ucmds; }
/** {@inheritDoc} */ @Override public String getFormattedResult() { return String.format("%.1f%s", percentage, getResultUnit()); }
/** {@inheritDoc} */ @Override protected void readResult(InputStream in) throws IOException { super.readResult(in); String key = getClass().getSimpleName(); knownValues.put(key, rawData); knownBuffers.put(key, new ArrayList<>(buffer)); }
/** * This method will add a job to the queue while setting its ID to the * internal queue counter. * * @param job the job to queue. */ @Override public void queueJob(ObdCommandJob job) { // This is a good place to enforce the imperial units option job.getCommand().useImperialUnits(prefs.getBoolean(ConfigActivity.IMPERIAL_UNITS_KEY, false)); // Now we can pass it along super.queueJob(job); }
job.setState(ObdCommandJobState.RUNNING); if (sock.isConnected()) { job.getCommand().run(sock.getInputStream(), sock.getOutputStream()); } else { job.setState(ObdCommandJobState.EXECUTION_ERROR);
/** * */ private void queueCommands() { if (isServiceBound) { for (ObdCommand Command : ObdConfig.getCommands()) { if (prefs.getBoolean(Command.getName(), true)) service.queueJob(new ObdCommandJob(Command)); } } }
/** * <p>fillBuffer.</p> */ protected void fillBuffer() { rawData = removeAll(WHITESPACE_PATTERN, rawData); //removes all [ \t\n\x0B\f\r] rawData = removeAll(BUSINIT_PATTERN, rawData); if (!DIGITS_LETTERS_PATTERN.matcher(rawData).matches()) { throw new NonNumericResponseException(rawData); } // read string each two chars buffer.clear(); int begin = 0; int end = 2; while (end <= rawData.length()) { buffer.add(Integer.decode("0x" + rawData.substring(begin, end))); begin = end; end += 2; } }