public FitImporter(String fileName) { FileInputStream fitFile = null; try { Decode decode = new Decode(); MesgBroadcaster broadcaster = new MesgBroadcaster(decode); decode.addListener((MesgDefinitionListener) this); decode.addListener((MesgListener) this); fitFile = new FileInputStream(fileName); broadcaster.run(fitFile); // calculate powers here??? if (!isPower) { for (Telemetry point : data) { int p = (int) Power.getPower(userPrefs.getTotalWeight(), point.getGradient(), point.getSpeed()); if (p > userPrefs.getMaxPower() && (p > (last.getPower() * 2.0))) { // We are above FTP and power has doubled, remove power // spikes p = (int) (last.getPower() * 1.05); } if (p > (userPrefs.getMaxPower() * 4)) { // power is 4 x FTP, this is a spike p = last.getPower(); } if (p > 0) { point.setPower(p); } else { point.setPower(0); } } // for } } catch (Exception fex) { try { fitFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } fex.printStackTrace(); } finally { if (fitFile != null) { try { fitFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
private static void dumpFitFile(String fitFilename) { try { Decode decode = new Decode(); MesgBroadcaster broadcaster = new MesgBroadcaster(decode); MesgSimpleTextWriter mesgWriter = new MesgSimpleTextWriter(System.out); decode.addListener((MesgDefinitionListener) mesgWriter); decode.addListener((MesgListener) mesgWriter); broadcaster.run((InputStream) new FileInputStream(fitFilename)); mesgWriter.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } }
public static void main(String args[]) { final int DATA_OR_DEFINITION_SEARCH_COUNT = 2; String in = ""; String out = ""; ArrayList<String> mesgDefinitionsToOutput = new ArrayList<String>(); ArrayList<String> dataMessagesToOutput = new ArrayList<String>(); boolean fitToCsv = false; boolean csvToFit = false; boolean test = false; boolean checkIntegrity = false; boolean showInvalidValues = false; boolean invalidsToEmpty = false; boolean hideUnknownData = false; int nextArgumentDefinition = 0; int nextArgumentData = 0; int numUnknownFields = 0; int numUnknownMesgs = 0; int arg = 0; System.out.printf( "FIT CSV Tool - Protocol %d.%d Profile %.2f %s\n", Fit.PROTOCOL_VERSION_MAJOR, Fit.PROTOCOL_VERSION_MINOR, Fit.PROFILE_VERSION / 100.0, Fit.PROFILE_TYPE); while (arg < args.length) { if (args[arg].equals("-b")) { if ((args.length - arg) < 3) { printUsage(); return; } fitToCsv = true; in = args[arg + 1]; out = args[arg + 2]; arg += 2; } else if (args[arg].equals("-c")) { if ((args.length - arg) < 3) { printUsage(); return; } csvToFit = true; in = args[arg + 1]; out = args[arg + 2]; arg += 2; } else if (args[arg].equals("-t")) { test = true; } else if (args[arg].equals("-d")) { Fit.debug = true; test = true; } else if (args[arg].equals("-i")) { checkIntegrity = true; } else if (args[arg].equals("--defn")) { nextArgumentDefinition = DATA_OR_DEFINITION_SEARCH_COUNT; } else if (args[arg].equals("--data")) { nextArgumentData = DATA_OR_DEFINITION_SEARCH_COUNT; } else if (args[arg].charAt(0) != '-') { if (nextArgumentDefinition > 0) { mesgDefinitionsToOutput = new ArrayList<String>(Arrays.asList(args[arg].toLowerCase().split(","))); } else if (nextArgumentData > 0) { dataMessagesToOutput = new ArrayList<String>(Arrays.asList(args[arg].toLowerCase().split(","))); } else { in = args[arg]; if (in.endsWith(".fit")) { fitToCsv = true; out = in.substring(0, in.length() - 4) + ".csv"; } else if (in.endsWith(".csv")) { csvToFit = true; out = in.substring(0, in.length() - 4) + ".fit"; } } } else if (args[arg].equals("-s")) { showInvalidValues = true; } else if (args[arg].equals("-se")) { showInvalidValues = true; invalidsToEmpty = true; } else if (args[arg].equals("-u")) { hideUnknownData = true; } if (nextArgumentDefinition > 0) { nextArgumentDefinition--; if ((nextArgumentDefinition == 0) && (mesgDefinitionsToOutput.isEmpty())) { System.out.println( "No mesg definitions defined for --defn option. Use 'none' if no definitions are desired."); return; } } if (nextArgumentData > 0) { nextArgumentData--; if ((nextArgumentData == 0) && (dataMessagesToOutput.isEmpty())) { System.out.println( "No data messages defined for --data option. Use 'none' if no data is desired."); return; } } arg++; } if (fitToCsv) { if ((out.length() >= 4) && (out.substring(out.length() - 4, out.length()).compareTo(".csv") == 0)) out = out.substring(0, out.length() - 4); // Remove .csv extension. if (checkIntegrity) { try { if (!Decode.checkIntegrity((InputStream) new FileInputStream(in))) { if (!Decode.getInvalidDataSize()) throw new RuntimeException("FIT file integrity failure."); else { System.out.println("FIT file integrity failure. Invalid file size in header."); System.out.println("Trying to continue..."); } } } catch (java.io.IOException e) { throw new RuntimeException(e); } } if (test) { Tests tests = new Tests(); System.out.println("Running FIT verification tests..."); if (tests.run(in)) System.out.println("Passed FIT verification."); else System.out.println("Failed FIT verification."); } try { Decode decode = new Decode(); MesgCSVWriter mesgWriter = new MesgCSVWriter(out + ".csv"); FileInputStream fileInputStream = new FileInputStream(in); if (showInvalidValues == true) decode.showInvalidValues(); MesgFilter mesgFilter = new MesgFilter(); mesgFilter.setMesgDefinitionsToOutput(mesgDefinitionsToOutput); mesgFilter.setDataMessagesToOutput(dataMessagesToOutput); MesgDataCSVWriter dataMesgWriter = new MesgDataCSVWriter(out + "_data.csv"); if (invalidsToEmpty) { mesgWriter.showInvalidsAsEmptyCells(); dataMesgWriter.showInvalidsAsEmptyCells(); } if (hideUnknownData) { mesgWriter.hideUnknownData(); dataMesgWriter.hideUnknownData(); } mesgFilter.addListener((MesgDefinitionListener) mesgWriter); mesgFilter.addListener((MesgListener) mesgWriter); mesgFilter.addListener((MesgListener) dataMesgWriter); decode.addListener((MesgDefinitionListener) mesgFilter); decode.addListener((MesgListener) mesgFilter); while (fileInputStream.available() > 0) { // Try to read a file while more data is available. try { decode.read((InputStream) fileInputStream); decode.nextFile(); // Initialize to read next file (if any). } catch (FitRuntimeException e) { if (decode.getInvalidDataSize()) continue; } } mesgWriter.close(); dataMesgWriter.close(); numUnknownFields = mesgWriter.getNumUnknownFields(); numUnknownMesgs = mesgWriter.getNumUnknownMesgs(); } catch (java.io.IOException e) { throw new RuntimeException(e); } if (hideUnknownData) System.out.printf( "Hid %d unknown field(s) and %d unknown message(s).\n", numUnknownFields, numUnknownMesgs); System.out.printf("FIT binary file %s decoded to %s*.csv files.\n", in, out); } else if (csvToFit) { try { FileEncoder encoder = new FileEncoder(new File(out)); if (!CSVReader.read((InputStream) new FileInputStream(in), encoder, encoder)) throw new RuntimeException("FIT encoding error."); encoder.close(); System.out.printf("%s encoded into FIT binary file %s.\n", in, out); } catch (java.io.IOException e) { throw new RuntimeException(e); } } else { printUsage(); } }