Skip to content

Commit

Permalink
starts #4
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Jun 21, 2017
1 parent c091ca6 commit 749c429
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 20 deletions.
11 changes: 11 additions & 0 deletions can4eve/src/main/java/com/bitplan/can4eve/CANValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import org.apache.commons.collections4.queue.CircularFifoQueue;

import com.bitplan.csv.CSVUtil;


/**
* base class for all PidValues
Expand Down Expand Up @@ -303,6 +305,15 @@ public String asString() {
else
return "-";
}

public String asCSV() {
if (this.valueItem.available) {
String csv=CSVUtil.csv(this.canInfo.title, asString());
return csv;
}
else
return "";
}

public static class IntegerValue extends CANValue<Integer> {
public IntegerValue(CANInfo canInfo) {
Expand Down
36 changes: 36 additions & 0 deletions elm327/src/main/java/com/bitplan/csv/CSVUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
*
* This file is part of the https://github.com/BITPlan/can4eve open source project
*
* Copyright 2017 BITPlan GmbH https://github.com/BITPlan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http:www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bitplan.csv;

public class CSVUtil {
/**
* create a csv entry for the given name value
* @param name
* @param value
* @return the csv entry
*/
public static String csv(String name, String value) {
if (value!=null)
return name+";"+value+"\n";
else
return "";
}
}
12 changes: 12 additions & 0 deletions elm327/src/main/java/com/bitplan/elm327/ELM327Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package com.bitplan.elm327;

import com.bitplan.csv.CSVUtil;

/**
* Created by wf on 03.06.17.
*
Expand Down Expand Up @@ -308,6 +310,16 @@ public String getInfo() {
}
return info;
}

public String asCSV() {
String csv="";
csv+=CSVUtil.csv("id",this.getId());
csv+=CSVUtil.csv("description",this.getDescription());
csv+=CSVUtil.csv("firmwareId",this.getFirmwareId());
csv+=CSVUtil.csv("hardwareId", this.getHardwareId());
return csv;
}


public void initOBD2() throws Exception {
initOBD2(INIT_TIMEOUT);
Expand Down
6 changes: 1 addition & 5 deletions obdii/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 0 additions & 1 deletion obdii/src/main/java/com/bitplan/obdii/OBDHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.bitplan.can4eve.Pid;
import com.bitplan.can4eve.VehicleGroup;
import com.bitplan.elm327.Connection;
import com.bitplan.elm327.ConnectionImpl;
import com.bitplan.elm327.Packet;
import com.bitplan.elm327.ResponseHandler;
import com.bitplan.obdii.elm327.ELM327;
Expand Down
8 changes: 7 additions & 1 deletion obdii/src/main/java/com/bitplan/obdii/OBDMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ enum DisplayChoice {
@Option(name = "-p", aliases = {
"--pid" }, usage = "pid to monitor\nthe pid to monitor")
String pid;

@Option(name = "-r", aliases = {
"--report" }, usage = "create a report of all pids for this vehicle group\n")
String reportFileName;

@Option(name = "-t", aliases = {
"--timeout" }, usage = "timeout in msecs\nthe timeout for elm327 communication")
Expand Down Expand Up @@ -132,7 +136,9 @@ public void doMonitorOBD() throws Exception {
if (this.logFileName != null) {
obdTriplet.logResponses(new File(logFileName), "Triplet");
}
if (pid != null)
if (this.reportFileName!=null) {
obdTriplet.report(display,reportFileName);
} else if (pid != null)
obdTriplet.checkPid(display, pid, frameLimit);
else {
obdTriplet.STMMonitor(display, obdTriplet.getCANValues(), frameLimit);
Expand Down
2 changes: 2 additions & 0 deletions obdii/src/main/java/com/bitplan/obdii/elm327/ELM327.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ public void run() {
}
});
}


}
5 changes: 5 additions & 0 deletions obdii/src/main/java/com/bitplan/triplet/Climate.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
package com.bitplan.triplet;

/**
* Climate description
* @author wf
*
*/
public class Climate {
int ventLevel;
String climateLevel;
Expand Down
68 changes: 56 additions & 12 deletions obdii/src/main/java/com/bitplan/triplet/OBDTriplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
package com.bitplan.triplet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -46,8 +48,10 @@
import com.bitplan.can4eve.VehicleGroup;
import com.bitplan.elm327.Connection;
import com.bitplan.can4eve.CANValue.*;
import com.bitplan.csv.CSVUtil;
import com.bitplan.obdii.CANValueDisplay;
import com.bitplan.obdii.CANValueHistoryPlot;
import com.bitplan.obdii.Display;
import com.bitplan.obdii.OBDHandler;
import com.bitplan.obdii.PIDResponse;
import com.bitplan.obdii.TripletDisplay;
Expand Down Expand Up @@ -101,6 +105,7 @@ public class OBDTriplet extends OBDHandler {
ShifterPositionValue shifterPositionValue;
// meter per Round // FIXME - is vehicle dependen and needs to be configured
private double kmPerRound = 0.261 / 1000.0;
private CANValue<?>[] top;

public boolean isWithHistory() {
return withHistory;
Expand Down Expand Up @@ -214,6 +219,18 @@ public void initCanValues() {
getCanInfo("SteeringWheelMovement"));
shifterPositionValue = new ShifterPositionValue(
getCanInfo("ShifterPosition"));
// the top list as requested
CANValue<?>[] topArray = { this.VIN, this.cellCount, this.batteryCapacity,
this.key, this.odometer, this.tripOdo, this.tripRounds, this.speed,
this.rpm, this.rpmSpeed, this.range, this.SOC, this.climateValue,
this.ventDirection, this.acamps, this.acvolts, this.dcamps,
this.dcvolts, this.motortemp, this.chargertemp,
this.shifterPositionValue, this.steeringWheelPosition,
this.steeringWheelMovement, this.accelerator, this.breakPressed,
this.breakPedal, this.blinkerLeft, this.blinkerRight, this.doorOpen,
this.parkingLight, this.headLight, this.highBeam,
this.cellTemperature, this.cellVoltage };
top=topArray;
}

/**
Expand Down Expand Up @@ -457,22 +474,11 @@ public void handleResponse(PIDResponse pr) {
*/
public List<CANValue<?>> getCANValues() {
if (canValues == null) {
// the top list as requested
CANValue<?>[] top = { this.VIN, this.cellCount, this.batteryCapacity,
this.key, this.odometer, this.tripOdo, this.tripRounds, this.speed,
this.rpm, this.rpmSpeed, this.range, this.SOC, this.climateValue,
this.ventDirection, this.acamps, this.acvolts, this.dcamps,
this.dcvolts, this.motortemp, this.chargertemp,
this.shifterPositionValue, this.steeringWheelPosition,
this.steeringWheelMovement, this.accelerator, this.breakPressed,
this.breakPedal, this.blinkerLeft, this.blinkerRight, this.doorOpen,
this.parkingLight, this.headLight, this.highBeam,
this.cellTemperature, this.cellVoltage };
// start the canValues with the top list
canValues = new ArrayList<CANValue<?>>(Arrays.asList(top));
// TODO check handling of raw values
// create a map of these already added values
Map<Pid, CANValue<?>> canValueMap = new HashMap<Pid, CANValue<?>>();
HashMap<Pid, CANValue<?>> canValueMap = new HashMap<Pid, CANValue<?>>();
for (CANValue<?> canValue : canValues) {
CANInfo canInfo = canValue.canInfo;
Pid pid = canInfo.getPid();
Expand Down Expand Up @@ -693,5 +699,43 @@ private void stopDisplay() {
public void initOBD() throws Exception {
this.getElm327().initOBD2();
}

/**
* get the PID with the given PID id
* @param pidId
* @return
* @throws Exception
*/
public Pid pidByName(String pidId) throws Exception {
Pid pid=getElm327().getVehicleGroup().getPidByName(pidId);
return pid;
}

/**
* create a csv report according to https://github.com/BITPlan/can4eve/issues/4
* and put the result into the given CSV file
* @param display
* @param reportFileName - the filename to use
* @throws Exception
*/
public void report(CANValueDisplay display, String reportFileName) throws Exception {
File reportFile=new File(reportFileName);
PrintWriter printWriter=new PrintWriter(reportFile);
this.getElm327().identify();
String isoDate=isoDateFormatter.format(new Date());
printWriter.write(CSVUtil.csv("date", isoDate));
String elmCSV=this.getElm327().asCSV();
printWriter.write(elmCSV);
printWriter.flush();
readPid(display,pidByName("BatteryCapacity"));
printWriter.write(this.batteryCapacity.asCSV());
this.getCANValues(); // side effec: creates map;
for (CANValue<?> canValue:top) {
CANInfo canInfo = canValue.canInfo;
monitorPid(display,canInfo.getPid().getPid(), canInfo.getMaxIndex()+3);
printWriter.write(canValue.asCSV());
}
printWriter.close();
}

}
24 changes: 23 additions & 1 deletion obdii/src/test/java/com/bitplan/obdii/TestELM327.java
Original file line number Diff line number Diff line change
Expand Up @@ -622,5 +622,27 @@ public void testIntegrate() throws Exception {
tripRounds.integrate(rpm1, date1, rpm2, date2, 1 / 60000.0);
assertEquals(50.0, tripRounds.getValueItem().getValue(), 0.1);
}


/**
* test creating a report according to
* https://github.com/BITPlan/can4eve/issues/4
* @throws Exception
*/
@Test
public void testReport() throws Exception {
this.prepareOBDTriplet(simulated, debug);
obdTriplet.initOBD();
File reportFile = File.createTempFile("report", ".csv");
obdTriplet.report(display,reportFile.getAbsolutePath());
assertTrue(reportFile.exists());
List<String> lines = FileUtils.readLines(reportFile, "UTF-8");
debug=true;
if (debug) {
for (String line:lines) {
System.out.println(line);
}
}
assertEquals(27,lines.size());
reportFile.delete();
}
}

0 comments on commit 749c429

Please sign in to comment.