Skip to content

Commit

Permalink
Merge pull request #302 from enviroCar/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
dewall committed Jun 8, 2016
2 parents 384ddbb + 0e3e7c1 commit d12efca
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ private void onClickAddFueling() {

Double cost = null, milage = null, volume = null;
try {
cost = getEditTextDoubleValue(addFuelingTotalCostText.toString());
milage = getEditTextDoubleValue(addFuelingMilageText.toString());
volume = getEditTextDoubleValue(addFuelingVolumeText.toString());
cost = getEditTextDoubleValue(addFuelingTotalCostText.getText().toString());
milage = getEditTextDoubleValue(addFuelingMilageText.getText().toString());
volume = getEditTextDoubleValue(addFuelingVolumeText.getText().toString());
} catch (ParseException e) {
formError = true;
if (cost == null) {
Expand Down Expand Up @@ -528,8 +528,19 @@ private double getEditTextDoubleValue(EditText input) throws ParseException {
}

private double getEditTextDoubleValue(String input) throws ParseException {
String numberValue = input.split(" ")[0].replaceAll(",", ".");
return Double.parseDouble(numberValue);
String[] yea = input.split(" ");
if(yea.length == 0){
return 0.0;
}

String toParse = yea[0].replaceAll(",", ".");
try{
return Double.parseDouble(toParse);
} catch (NumberFormatException e){
LOG.error(String.format("Error while parsing double [%s]", toParse), e);
throw new ParseException(e.getMessage(), 0);
}

}

private void showSnackbarInfo(int resourceID) {
Expand Down Expand Up @@ -570,6 +581,9 @@ public CharSequence filter(CharSequence source, int start, int end, Spanned dest

// The string value contains a unit. Therefore split the value and show an error
// if the splitted value does not match.
if(source.toString().split(" ").length == 0)
return "";

Matcher matcher = pattern.matcher(source.toString().split(" ")[0]);
if (!matcher.matches()) {
addFuelingPricePerLitreText.setError(getString(R.string.logbook_invalid_input));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import org.envirocar.obd.commands.request.BasicCommand;
import org.envirocar.obd.commands.request.PIDCommand;
import org.envirocar.obd.exception.AdapterFailedException;
import org.envirocar.obd.exception.InvalidCommandResponseException;
import org.envirocar.obd.exception.NoDataReceivedException;

import java.util.ArrayDeque;
import java.util.Arrays;
Expand All @@ -32,9 +30,9 @@ public class CarTrendAdapter extends SyncAdapter {
protected BasicCommand pollNextInitializationCommand() {
if (this.initializeRing == null) {
this.initializeRing = new ArrayDeque<>();
// this.initializeRing.add(new EmptyCommand());
this.initializeRing.add(new EmptyCommand());
this.initializeRing.add(new IdentifyCommand());
// this.initializeRing.add(new EmptyCommand());
this.initializeRing.add(new EmptyCommand());
this.initializeRing.add(new ProtocolCommand("S"));
this.initializeRing.add(new ProtocolCommand("1"));
this.initializeRing.add(new ProtocolCommand("2"));
Expand Down Expand Up @@ -209,6 +207,11 @@ public byte[] getOutputBytes() {
public boolean awaitsResults() {
return true;
}

@Override
public String toString() {
return this.name;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ private byte[] readResponseLine() throws IOException, StreamFinishedException {
byteArray = baos.toByteArray();
}

if (byteArray.length == 0){
LOGGER.info("Unexpected empty line anonmaly detected. Try to read next line.");
baos.reset();
readUntilLineEnd(baos);
byteArray = baos.toByteArray();
}

if (byteArray.length > 0 && LOGGER.isEnabled(Logger.DEBUG)) {
LOGGER.debug("Received bytes: " + Base64.encodeToString(byteArray, Base64.DEFAULT));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void call(Subscriber<? super Boolean> subscriber) {
}

subscriber.onNext(true);
subscriber.unsubscribe();
subscriber.onCompleted();
}
else {
BasicCommand cc = pollNextInitializationCommand();
Expand All @@ -104,13 +104,22 @@ public void call(Subscriber<? super Boolean> subscriber) {
subscriber.unsubscribe();
}

LOGGER.info("Sending command in initial phase: "+cc.toString());
//push the command to the output stream
commandExecutor.execute(cc);

//check if the command needs a response (most likely)
if (cc.awaitsResults()) {
byte[] resp = commandExecutor.retrieveLatestResponse();
analyzedSuccessfully = analyzeMetadataResponse(resp, cc);
try {
Thread.sleep(1000);

byte[] resp = commandExecutor.retrieveLatestResponse();
LOGGER.info("Retrieved initial phase response: "+Base64.encodeToString(resp, Base64.DEFAULT));
analyzedSuccessfully = analyzedSuccessfully | analyzeMetadataResponse(resp, cc);
} catch (InterruptedException e) {
LOGGER.warn(e.getMessage());
}

}
}
}
Expand Down

0 comments on commit d12efca

Please sign in to comment.