diff --git a/can4eve/src/main/java/com/bitplan/can4eve/CANValue.java b/can4eve/src/main/java/com/bitplan/can4eve/CANValue.java index bb07efd6..d971a4f2 100644 --- a/can4eve/src/main/java/com/bitplan/can4eve/CANValue.java +++ b/can4eve/src/main/java/com/bitplan/can4eve/CANValue.java @@ -27,6 +27,8 @@ import org.apache.commons.collections4.queue.CircularFifoQueue; +import com.bitplan.csv.CSVUtil; + /** * base class for all PidValues @@ -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 { public IntegerValue(CANInfo canInfo) { diff --git a/elm327/src/main/java/com/bitplan/csv/CSVUtil.java b/elm327/src/main/java/com/bitplan/csv/CSVUtil.java new file mode 100644 index 00000000..4344b6e2 --- /dev/null +++ b/elm327/src/main/java/com/bitplan/csv/CSVUtil.java @@ -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 ""; + } +} diff --git a/elm327/src/main/java/com/bitplan/elm327/ELM327Impl.java b/elm327/src/main/java/com/bitplan/elm327/ELM327Impl.java index 483ec16e..55451348 100644 --- a/elm327/src/main/java/com/bitplan/elm327/ELM327Impl.java +++ b/elm327/src/main/java/com/bitplan/elm327/ELM327Impl.java @@ -20,6 +20,8 @@ */ package com.bitplan.elm327; +import com.bitplan.csv.CSVUtil; + /** * Created by wf on 03.06.17. * @@ -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); diff --git a/obdii/.classpath b/obdii/.classpath index d4978540..37d06d00 100644 --- a/obdii/.classpath +++ b/obdii/.classpath @@ -17,15 +17,11 @@ - - - - - + diff --git a/obdii/src/main/java/com/bitplan/obdii/OBDHandler.java b/obdii/src/main/java/com/bitplan/obdii/OBDHandler.java index 5f839f72..acfb83cd 100644 --- a/obdii/src/main/java/com/bitplan/obdii/OBDHandler.java +++ b/obdii/src/main/java/com/bitplan/obdii/OBDHandler.java @@ -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; diff --git a/obdii/src/main/java/com/bitplan/obdii/OBDMain.java b/obdii/src/main/java/com/bitplan/obdii/OBDMain.java index 32cecff9..b6639916 100644 --- a/obdii/src/main/java/com/bitplan/obdii/OBDMain.java +++ b/obdii/src/main/java/com/bitplan/obdii/OBDMain.java @@ -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") @@ -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); diff --git a/obdii/src/main/java/com/bitplan/obdii/elm327/ELM327.java b/obdii/src/main/java/com/bitplan/obdii/elm327/ELM327.java index fff203c0..8aa35f21 100644 --- a/obdii/src/main/java/com/bitplan/obdii/elm327/ELM327.java +++ b/obdii/src/main/java/com/bitplan/obdii/elm327/ELM327.java @@ -84,4 +84,6 @@ public void run() { } }); } + + } \ No newline at end of file diff --git a/obdii/src/main/java/com/bitplan/triplet/Climate.java b/obdii/src/main/java/com/bitplan/triplet/Climate.java index b691e3cd..e130b2ad 100644 --- a/obdii/src/main/java/com/bitplan/triplet/Climate.java +++ b/obdii/src/main/java/com/bitplan/triplet/Climate.java @@ -20,6 +20,11 @@ */ package com.bitplan.triplet; +/** + * Climate description + * @author wf + * + */ public class Climate { int ventLevel; String climateLevel; diff --git a/obdii/src/main/java/com/bitplan/triplet/OBDTriplet.java b/obdii/src/main/java/com/bitplan/triplet/OBDTriplet.java index eb058006..34d63ceb 100644 --- a/obdii/src/main/java/com/bitplan/triplet/OBDTriplet.java +++ b/obdii/src/main/java/com/bitplan/triplet/OBDTriplet.java @@ -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; @@ -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; @@ -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; @@ -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; } /** @@ -457,22 +474,11 @@ public void handleResponse(PIDResponse pr) { */ public List> 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>(Arrays.asList(top)); // TODO check handling of raw values // create a map of these already added values - Map> canValueMap = new HashMap>(); + HashMap> canValueMap = new HashMap>(); for (CANValue canValue : canValues) { CANInfo canInfo = canValue.canInfo; Pid pid = canInfo.getPid(); @@ -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(); + } } diff --git a/obdii/src/test/java/com/bitplan/obdii/TestELM327.java b/obdii/src/test/java/com/bitplan/obdii/TestELM327.java index 4c5643b4..f2cbb300 100644 --- a/obdii/src/test/java/com/bitplan/obdii/TestELM327.java +++ b/obdii/src/test/java/com/bitplan/obdii/TestELM327.java @@ -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 lines = FileUtils.readLines(reportFile, "UTF-8"); + debug=true; + if (debug) { + for (String line:lines) { + System.out.println(line); + } + } + assertEquals(27,lines.size()); + reportFile.delete(); + } }