diff --git a/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/ExportService.java b/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/ExportService.java index 511daeb..c76cc88 100644 --- a/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/ExportService.java +++ b/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/ExportService.java @@ -1,142 +1,149 @@ package com.monstarmike.telemetry.plugins.tlmDecoder; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONException; +import org.json.JSONObject; + +import com.monstarmike.tlmreader.Flight; +import com.monstarmike.tlmreader.IFlight; +import com.monstarmike.tlmreader.TLMReader; + import android.app.IntentService; import android.content.Intent; import android.net.Uri; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; -import com.google.common.io.ByteStreams; -import com.monstarmike.tlmreader.TLMReader; - -import org.json.JSONObject; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; - /** * Created by mgardner on 6/5/15. */ public class ExportService extends IntentService { - private static final String TAG = "ExportService"; - - public ExportService() { - super(TAG); - } - - @Override - protected void onHandleIntent(Intent intent) { - String action = intent.getAction(); - - Log.d(TAG, "Export Service action: " + action); - if (action.equalsIgnoreCase(Constants.ExportServiceActions.READ_FLIGHT)) { - Uri fileUri = ServiceDataTransfer.getInstance().get_fileUri(); - JSONObject flightJO = ServiceDataTransfer.getInstance().get_flight(); - - if (fileUri == null) { - Log.w(TAG, "The file Uri pulled from the ServiceDataTransfer singleton was null!"); - } - if (flightJO == null) { - Log.w(TAG, "The flight object pulled from the ServiceDataTransfer singleton was null!"); - } - - JSONObject decodedFlight = null; - try { - decodedFlight = this.buildFlight(fileUri, flightJO); - } catch (Exception e) { - Log.e(TAG, "Error occurred decoding the flight!", e); - } - - if (decodedFlight == null) { - Log.w(TAG, "The decoded flight was null. This could be seriously not good!"); - } - - ServiceDataTransfer.getInstance().set_flight(decodedFlight); - - Intent localIntent = new Intent(Constants.READ_FLIGHT_BROADCAST_ACTION); - LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); - } else if (action.equalsIgnoreCase(Constants.ExportServiceActions.READ_FILE)) { - Uri uri = intent.getData(); - - Log.d(TAG, "Reading file: " + uri.toString()); - - JSONObject file = null; - - try { - file = this.parseFile(uri); - } catch (IOException e) { - Log.e(TAG, "IO error when trying to get and parse the TLM data file.", e); - } - - if (file == null) { - Log.w(TAG, "This is interesting. The file JSONObject from the parse function is null!"); - } - - Log.d(TAG, "Done with the file. Broadcasting a local intent."); - ServiceDataTransfer.getInstance().set_file(file); - Intent localIntent = new Intent(Constants.READ_FILE_BROADCAST_ACTION); - LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); - } - } - - private JSONObject buildFlight(Uri uri, JSONObject flightJO) { - - if (uri == null) { - Log.w(TAG, "Returning the default flight object that was passed in."); - return flightJO; - } else { - Log.d(TAG, "URI: " + uri.toString()); - } - - InputStream inputStream = null; - try { - inputStream = this.getContentResolver().openInputStream(uri); - } catch (FileNotFoundException e) { - Log.w(TAG, "Cannot find the file at " + uri.toString(), e); - } - if (inputStream == null) { - Log.w(TAG, "Returning the default flight object that was passed in."); - return flightJO; - } - - byte[] bytes = new byte[0]; - try { - bytes = ByteStreams.toByteArray(inputStream); - } catch (IOException e) { - Log.w(TAG, "Unable to read the input stream to a byte array.", e); - } - - if (bytes.length == 0) { - Log.w(TAG, "Returning the default flight object that was passed in."); - return flightJO; - } - - TLMReader reader = new TLMReader(); - - reader.Read(bytes); - - Exporter exporter = new Exporter(uri, reader, this.getApplicationContext()); - return exporter.exportFlightData(flightJO); - } - - private JSONObject parseFile(Uri uri) throws IOException { - InputStream inputStream = this.getApplicationContext().getContentResolver().openInputStream(uri); - Log.d(TAG, "Have an input stream."); - byte[] bytes = ByteStreams.toByteArray(inputStream); - Log.d(TAG, "Read bytes: " + bytes.length); - - TLMReader reader = new TLMReader(); - - reader.Read(bytes); - - Log.d(TAG, "The reader has read all the bytes. Going to export."); - - Exporter exporter = new Exporter(uri, reader, this.getApplicationContext()); - - JSONObject file = exporter.exportFlights(); - - - return file; - } + private static final String TAG = "ExportService"; + + public ExportService() { + super(TAG); + } + + @Override + protected void onHandleIntent(Intent intent) { + String action = intent.getAction(); + + Log.d(TAG, "Export Service action: " + action); + if (action.equalsIgnoreCase(Constants.ExportServiceActions.READ_FLIGHT)) { + Uri fileUri = ServiceDataTransfer.getInstance().get_fileUri(); + JSONObject flightJO = ServiceDataTransfer.getInstance().get_flight(); + + if (fileUri == null) { + Log.w(TAG, "The file Uri pulled from the ServiceDataTransfer singleton was null!"); + } + if (flightJO == null) { + Log.w(TAG, "The flight object pulled from the ServiceDataTransfer singleton was null!"); + } + + JSONObject decodedFlight = null; + try { + decodedFlight = this.decodeFlight(fileUri, flightJO); + } catch (Exception e) { + Log.e(TAG, "Error occurred decoding the flight!", e); + } + + if (decodedFlight == null) { + Log.w(TAG, "The decoded flight was null. This could be seriously not good!"); + } + + ServiceDataTransfer.getInstance().set_flight(decodedFlight); + + Intent localIntent = new Intent(Constants.READ_FLIGHT_BROADCAST_ACTION); + LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); + } else if (action.equalsIgnoreCase(Constants.ExportServiceActions.READ_FILE)) { + Uri uri = intent.getData(); + + Log.d(TAG, "Reading file: " + uri.toString()); + + JSONObject file = null; + + try { + file = this.decodeFlightDefinitions(uri); + } catch (IOException e) { + Log.e(TAG, "IO error when trying to get and parse the TLM data file.", e); + } + + if (file == null) { + Log.w(TAG, "This is interesting. The file JSONObject from the parse function is null!"); + } + + Log.d(TAG, "Done with the file. Broadcasting a local intent."); + ServiceDataTransfer.getInstance().set_file(file); + Intent localIntent = new Intent(Constants.READ_FILE_BROADCAST_ACTION); + LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); + } + } + + private JSONObject decodeFlight(Uri uri, JSONObject flightJO) throws IOException { + + if (uri == null) { + Log.w(TAG, "Returning the default flight object that was passed in."); + return flightJO; + } else { + Log.d(TAG, "URI: " + uri.toString()); + } + + InputStream inputStream = null; + try { + inputStream = this.getContentResolver().openInputStream(uri); + } catch (FileNotFoundException e) { + Log.w(TAG, "Cannot find the file at " + uri.toString(), e); + } + if (inputStream == null) { + Log.w(TAG, "Returning the default flight object that was passed in."); + return flightJO; + } + + int joId = 0; + try { + joId = flightJO.getInt("_id"); + Log.d(TAG, "FlightId: " + joId); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + final TLMReader reader = new TLMReader(); + final Flight flight = reader.parseFlight(inputStream, joId); + + Exporter exporter = new Exporter(uri, this.getApplicationContext()); + Log.d(TAG, "Found flight. Going to do a full decode."); + JSONObject exportFlightData = exporter.exportFlightData(flight); + Log.d(TAG, "Full decode done"); + if (exportFlightData != null) { + try { + exportFlightData.put("_id", joId); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return exportFlightData; + + } + + private JSONObject decodeFlightDefinitions(Uri uri) throws IOException { + InputStream inputStream = this.getApplicationContext().getContentResolver().openInputStream(uri); + Log.d(TAG, "Instantiate TLMReader."); + List flights = new ArrayList(); + final TLMReader reader = new TLMReader(); + try { + flights = reader.parseFlightDefinitions(inputStream); + } catch (final IOException e) { + Log.w(TAG, "Unable to read the input stream.", e); + } + Log.d(TAG, "Read bytes: " + reader.getNumberOfBytesRead()); + Exporter exporter = new Exporter(uri, this.getApplicationContext()); + JSONObject file = exporter.exportFlightDefintions(flights); + return file; + } } diff --git a/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/Exporter.java b/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/Exporter.java index 12e7807..177c54e 100644 --- a/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/Exporter.java +++ b/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/Exporter.java @@ -3,7 +3,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.util.Iterator; +import java.util.List; import org.joda.time.Duration; import org.joda.time.Period; @@ -14,6 +14,7 @@ import org.json.JSONObject; import com.monstarmike.tlmreader.Flight; +import com.monstarmike.tlmreader.IFlight; import com.monstarmike.tlmreader.datablock.AirspeedBlock; import com.monstarmike.tlmreader.datablock.AltitudeBlock; import com.monstarmike.tlmreader.datablock.CurrentBlock; @@ -32,336 +33,293 @@ import android.util.Log; public class Exporter { - Iterable flights; - Uri uri; - Context context; - - private static final String TAG = "TLMDecoder"; - - public Exporter(Uri uri, Iterable flights, Context context) { - this.flights = flights; - this.uri = uri; - this.context = context; - } - - public JSONObject exportFlights() { - JSONObject file = new JSONObject(); - - JSONArray flightsArray = new JSONArray(); - try { - file.put("uri", uri); - file.put("flights", flightsArray); - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - int index = 0; - for (Flight flight : this.flights) { - JSONObject flightJO = this.buildFlight(false, flight); - try { - flightJO.put("_id", index++); - } catch (JSONException e) { - e.printStackTrace(); - } - flightsArray.put(flightJO); - } - - return file; - } - - public JSONObject exportFlightData(JSONObject flightJO) { - JSONObject newFlightJO = null; - int index = 0; - for (Flight flight : this.flights) { - int joId = 0; - try { - joId = flightJO.getInt("_id"); - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - if (index != joId) { - Log.d(TAG, "Flight index: " + index + " != passed in value: " + joId); - index++; - continue; - } - - Log.d(TAG, "Found flight. Going to do a full decode."); - newFlightJO = this.buildFlight(true, flight); - if (newFlightJO != null) { - try { - newFlightJO.put("_id", joId); - } catch (JSONException e) { - e.printStackTrace(); - } - } - break; - } - - return newFlightJO; - } - - private JSONObject buildFlight(boolean includeBlockData, Flight flight) { - JSONObject flightJO = new JSONObject(); - try { - flightJO.put("_id", flight.hashCode()); - - Duration flightDuration = flight.get_duration(); - Period period = flightDuration.toPeriod(); - PeriodFormatter hms = new PeriodFormatterBuilder() - .appendHours() - .appendSeparator(":") - .printZeroAlways() - .appendMinutes() - .appendSeparator(":") - .appendSecondsWithMillis() - .toFormatter(); - - flightJO.put("duration", hms.print(period)); - - Iterator iterator = flight.get_headerBlocks(); - while (iterator.hasNext()) { - HeaderBlock headerBlock = iterator.next(); - if (headerBlock instanceof HeaderNameBlock) { - HeaderNameBlock nameBlock = (HeaderNameBlock) headerBlock; - flightJO.put("name", nameBlock.get_modelName()); - flightJO.put("modelNumber", nameBlock.get_modelNumber()); - flightJO.put("bindInfo", nameBlock.get_bindInfo()); - flightJO.put("modelType", nameBlock.get_modelType()); - } - } - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - if (includeBlockData) { - JSONObject flightData = null; - try { - flightData = this.loadChartDataTemplate(); - } catch (JSONException e) { - e.printStackTrace(); - } - try { - flightJO.put("flightData", flightData); - } catch (JSONException e) { - e.printStackTrace(); - } - - Iterator headerBlockIterator = flight.get_headerBlocks(); - while (headerBlockIterator.hasNext()) { - HeaderBlock headerBlock = headerBlockIterator.next(); - try { - handleHeaderBlock(flightJO, headerBlock); - } catch (JSONException e) { - e.printStackTrace(); - } - } - Iterator dataBlockIterator = flight.get_dataBlocks(); - while (dataBlockIterator.hasNext()) { - DataBlock dataBlock = dataBlockIterator.next(); - try { - handleDataBlock(flightData, dataBlock); - } catch (JSONException e) { - Log.w(TAG, "JSON error when working with data block", e); - } - } - } - - return flightJO; - } - - private JSONObject loadChartDataTemplate() throws JSONException { - BufferedReader reader = null; - String jsonTemplate = null; - try { - reader = new BufferedReader(new InputStreamReader(context.getAssets().open("json/chartTemplate.json"))); - String line = reader.readLine(); - StringBuilder stringBuilder = new StringBuilder(); - while (line != null) { - stringBuilder.append(line); - line = reader.readLine(); - } - jsonTemplate = stringBuilder.toString(); - } catch (IOException e) { - Log.e(TAG, "Error while reading JSON template.", e); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - Log.e(TAG, "Error while trying to close JSON template reader", e); - } - } - } - - if (jsonTemplate == null || jsonTemplate.equalsIgnoreCase("")) { - Log.e(TAG, "The template read from assets is null!"); - } - - return new JSONObject(jsonTemplate); - } - - private JSONArray findDataPointsArray(JSONObject flightData, String sensorName, - int seriesPosition, int dataSetPosition) throws JSONException { - return flightData.getJSONObject(sensorName) - .getJSONArray("chartSeriesTypes") - .getJSONObject(seriesPosition) - .getJSONArray("data") - .getJSONObject(dataSetPosition) - .getJSONArray("dataPoints"); - } - - void handleHeaderBlock(JSONObject jsonFlight, HeaderBlock headerBlock) throws JSONException { - JSONObject jsonHeaderBlock = null; - - if (headerBlock instanceof HeaderNameBlock) { - HeaderNameBlock nameBlock = (HeaderNameBlock) headerBlock; - jsonHeaderBlock = new JSONObject(); - jsonHeaderBlock.put("modelNumber", nameBlock.get_modelNumber()); - jsonHeaderBlock.put("bindInfo", nameBlock.get_bindInfo()); - jsonHeaderBlock.put("modelName", nameBlock.get_modelName()); - jsonHeaderBlock.put("modelType", nameBlock.get_modelType()); - } - - if (jsonHeaderBlock == null) return; - - if (jsonFlight.has("headers")) { - jsonFlight.getJSONArray("headers").put(jsonHeaderBlock); - } else { - JSONArray headersArray = new JSONArray(); - headersArray.put(jsonHeaderBlock); - jsonFlight.put("headers", headersArray); - } - } - - void handleDataBlock(JSONObject flightData, DataBlock dataBlock) throws JSONException { - if (dataBlock instanceof AirspeedBlock) { - - } else if (dataBlock instanceof AltitudeBlock) { - JSONObject jsonBlock = new JSONObject(); - jsonBlock.put("x", dataBlock.get_timestamp()); - jsonBlock.put("y", - ((AltitudeBlock) dataBlock).get_altitudeInTenthsOfAMeter()); - - this.findDataPointsArray(flightData, "altitude", 0, 0).put(jsonBlock); - } else if (dataBlock instanceof CurrentBlock) { - JSONObject jsonBlock = new JSONObject(); - jsonBlock.put("x", dataBlock.get_timestamp()); - - CurrentBlock currentBlock = (CurrentBlock) dataBlock; - jsonBlock.put("y", currentBlock.get_Current()); - - this.findDataPointsArray(flightData, "current", 0, 0).put(jsonBlock); - } else if (dataBlock instanceof GForceBlock) { - JSONObject jsonBlock = new JSONObject(); - jsonBlock.put("x", dataBlock.get_timestamp()); - - GForceBlock gfBlock = (GForceBlock) dataBlock; - jsonBlock.put("maxX", gfBlock.get_maxX()); - jsonBlock.put("maxY", gfBlock.get_maxY()); - jsonBlock.put("maxZ", gfBlock.get_maxZ()); - jsonBlock.put("x", gfBlock.get_x()); - jsonBlock.put("y", gfBlock.get_y()); - jsonBlock.put("z", gfBlock.get_z()); - jsonBlock.put("minZ", gfBlock.get_minZ()); - - } else if (dataBlock instanceof PowerboxBlock) { - JSONObject v1Block = new JSONObject(), - v2Block = new JSONObject(), - cap1Block = new JSONObject(), - cap2Block = new JSONObject(); - - v1Block.put("x", dataBlock.get_timestamp()); - v2Block.put("x", dataBlock.get_timestamp()); - cap1Block.put("x", dataBlock.get_timestamp()); - cap2Block.put("x", dataBlock.get_timestamp()); - - PowerboxBlock pbBlock = (PowerboxBlock) dataBlock; - cap1Block.put("y", pbBlock.get_capacityOne()); - cap2Block.put("y", pbBlock.get_capacityTwo()); - v1Block.put("y", pbBlock.get_voltageOne()); - v2Block.put("y", pbBlock.get_voltageTwo()); - - this.findDataPointsArray(flightData, "powerbox", 0, 0).put(v1Block); - this.findDataPointsArray(flightData, "powerbox", 0, 1).put(v2Block); - this.findDataPointsArray(flightData, "powerbox", 1, 0).put(cap1Block); - this.findDataPointsArray(flightData, "powerbox", 1, 1).put(cap2Block); - } else if (dataBlock instanceof RXBlock) { - RXBlock rxBlock = (RXBlock) dataBlock; - - JSONObject aBlock = new JSONObject(), - bBlock = new JSONObject(), - lBlock = new JSONObject(), - rBlock = new JSONObject(), - frameLossBlock = new JSONObject(), - holdsBlock = new JSONObject(), - voltsBlock = new JSONObject(); - - aBlock.put("x", dataBlock.get_timestamp()); - bBlock.put("x", dataBlock.get_timestamp()); - lBlock.put("x", dataBlock.get_timestamp()); - rBlock.put("x", dataBlock.get_timestamp()); - frameLossBlock.put("x", dataBlock.get_timestamp()); - holdsBlock.put("x", dataBlock.get_timestamp()); - voltsBlock.put("x", dataBlock.get_timestamp()); - - aBlock.put("y", rxBlock.get_a()); - bBlock.put("y", rxBlock.get_b()); - lBlock.put("y", rxBlock.get_l()); - rBlock.put("y", rxBlock.get_r()); - - frameLossBlock.put("y", rxBlock.get_frameLoss()); - holdsBlock.put("y", rxBlock.get_holds()); - - voltsBlock.put("y", rxBlock.get_volts()); - - this.findDataPointsArray(flightData, "rx", 0, 0).put(aBlock); - this.findDataPointsArray(flightData, "rx", 0, 1).put(bBlock); - this.findDataPointsArray(flightData, "rx", 0, 2).put(lBlock); - this.findDataPointsArray(flightData, "rx", 0, 3).put(rBlock); - - this.findDataPointsArray(flightData, "rx", 1, 0).put(frameLossBlock); - this.findDataPointsArray(flightData, "rx", 1, 1).put(holdsBlock); - - this.findDataPointsArray(flightData, "rx", 2, 0).put(voltsBlock); - } else if (dataBlock instanceof StandardBlock) { - StandardBlock standard = (StandardBlock) dataBlock; - - JSONObject rpmBlock = new JSONObject(), - tempBlock = new JSONObject(), - voltBlock = new JSONObject(); - - rpmBlock.put("x", dataBlock.get_timestamp()); - tempBlock.put("x", dataBlock.get_timestamp()); - voltBlock.put("x", dataBlock.get_timestamp()); - - rpmBlock.put("y", standard.get_rpm()); - tempBlock.put("y", standard.get_temperature()); - voltBlock.put("y", standard.get_volt()); - - this.findDataPointsArray(flightData, "standard", 0, 0).put(rpmBlock); - this.findDataPointsArray(flightData, "standard", 1, 0).put(tempBlock); - this.findDataPointsArray(flightData, "standard", 2, 0).put(voltBlock); - - } else if (dataBlock instanceof VarioBlock) { -// VarioBlock varioBlock = (VarioBlock) dataBlock; -/* - jsonBlock.put("delta1000ms", varioBlock.get_1000ms()); - jsonBlock.put("delta2000ms", varioBlock.get_2000ms()); - jsonBlock.put("delta250ms", varioBlock.get_250ms()); - jsonBlock.put("delta3000ms", varioBlock.get_3000ms()); - jsonBlock.put("delta500ms", varioBlock.get_500ms()); - jsonBlock.put("altitude", varioBlock.get_altitude());*/ - - } else if (dataBlock instanceof VoltageBlock) { -// VoltageBlock voltageBlock = (VoltageBlock) dataBlock; - -// jsonBlock.put("delta3000ms", voltageBlock.g); -// jsonBlock.put("delta500ms", varioBlock.get_500ms()); -// jsonBlock.put("altitude", varioBlock.get_altitude()); - } - } + Uri uri; + Context context; + + private static final String TAG = "TLMDecoder"; + + public Exporter(Uri uri, Context context) { + this.uri = uri; + this.context = context; + } + + public JSONObject exportFlightDefintions(List flightDefinitions) { + JSONObject file = new JSONObject(); + + JSONArray flightsArray = new JSONArray(); + try { + file.put("uri", uri); + file.put("flights", flightsArray); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + int index = 0; + for (IFlight flight : flightDefinitions) { + JSONObject flightJO = this.buildFlightDefinition(flight); + try { + flightJO.put("_id", index++); + } catch (JSONException e) { + e.printStackTrace(); + } + flightsArray.put(flightJO); + } + + return file; + } + + public JSONObject exportFlightData(Flight flight) { + JSONObject newFlightJO = this.buildFlightDefinition(flight); + JSONObject flightData = null; + try { + flightData = this.loadChartDataTemplate(); + } catch (JSONException e) { + e.printStackTrace(); + } + try { + newFlightJO.put("flightData", flightData); + } catch (JSONException e) { + e.printStackTrace(); + } + + for (HeaderBlock headerBlock : flight.getHeaderBlocks()) { + try { + this.handleHeaderBlock(newFlightJO, headerBlock); + } catch (JSONException e) { + e.printStackTrace(); + } + } + int i = 0; + for (DataBlock dataBlock : flight.getDataBlocks()) { + if (i % 100 == 0) { + Log.d(TAG, "DataBlock: " + i); + } + i++; + try { + this.handleDataBlock(flightData, dataBlock, flight); + } catch (JSONException e) { + Log.w(TAG, "JSON error when working with data block", e); + } + } + return newFlightJO; + } + + private JSONObject buildFlightDefinition(IFlight flight) { + JSONObject flightJO = new JSONObject(); + try { + Duration flightDuration = flight.getDuration(); + Period period = flightDuration.toPeriod(); + PeriodFormatter hms = new PeriodFormatterBuilder().appendHours().appendSeparator(":").printZeroAlways() + .appendMinutes().appendSeparator(":").appendSecondsWithMillis().toFormatter(); + + flightJO.put("duration", hms.print(period)); + + for (HeaderBlock headerBlock : flight.getHeaderBlocks()) { + if (headerBlock instanceof HeaderNameBlock) { + HeaderNameBlock nameBlock = (HeaderNameBlock) headerBlock; + flightJO.put("name", nameBlock.getModelName()); + flightJO.put("modelNumber", nameBlock.getModelNumber()); + flightJO.put("bindInfo", nameBlock.getBindInfo()); + flightJO.put("modelType", nameBlock.getModelType()); + } + } + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return flightJO; + } + + private JSONObject loadChartDataTemplate() throws JSONException { + BufferedReader reader = null; + String jsonTemplate = null; + try { + reader = new BufferedReader(new InputStreamReader(context.getAssets().open("json/chartTemplate.json"))); + String line = reader.readLine(); + StringBuilder stringBuilder = new StringBuilder(); + while (line != null) { + stringBuilder.append(line); + line = reader.readLine(); + } + jsonTemplate = stringBuilder.toString(); + } catch (IOException e) { + Log.e(TAG, "Error while reading JSON template.", e); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + Log.e(TAG, "Error while trying to close JSON template reader", e); + } + } + } + + if (jsonTemplate == null || jsonTemplate.equalsIgnoreCase("")) { + Log.e(TAG, "The template read from assets is null!"); + } + + return new JSONObject(jsonTemplate); + } + + private JSONArray findDataPointsArray(JSONObject flightData, String sensorName, int seriesPosition, + int dataSetPosition) throws JSONException { + return flightData.getJSONObject(sensorName).getJSONArray("chartSeriesTypes").getJSONObject(seriesPosition) + .getJSONArray("data").getJSONObject(dataSetPosition).getJSONArray("dataPoints"); + } + + void handleHeaderBlock(JSONObject jsonFlight, HeaderBlock headerBlock) throws JSONException { + JSONObject jsonHeaderBlock = null; + + if (headerBlock instanceof HeaderNameBlock) { + HeaderNameBlock nameBlock = (HeaderNameBlock) headerBlock; + jsonHeaderBlock = new JSONObject(); + jsonHeaderBlock.put("modelNumber", nameBlock.getModelNumber()); + jsonHeaderBlock.put("bindInfo", nameBlock.getBindInfo()); + jsonHeaderBlock.put("modelName", nameBlock.getModelName()); + jsonHeaderBlock.put("modelType", nameBlock.getModelType()); + } else { + return; + } + + if (!jsonFlight.has("headers")) { + jsonFlight.put("headers", new JSONArray()); + } + jsonFlight.getJSONArray("headers").put(jsonHeaderBlock); + } + + void handleDataBlock(JSONObject flightData, DataBlock dataBlock, Flight flight) throws JSONException { + if (dataBlock instanceof AirspeedBlock) { + } else if (dataBlock instanceof AltitudeBlock) { + JSONObject jsonBlock = new JSONObject(); + jsonBlock.put("x", dataBlock.getTimestamp()); + jsonBlock.put("y", ((AltitudeBlock) dataBlock).getAltitudeInTenthOfMeter()); + + this.findDataPointsArray(flightData, "altitude", 0, 0).put(jsonBlock); + } else if (dataBlock instanceof CurrentBlock) { + JSONObject jsonBlock = new JSONObject(); + jsonBlock.put("x", dataBlock.getTimestamp()); + + CurrentBlock currentBlock = (CurrentBlock) dataBlock; + jsonBlock.put("y", currentBlock.getCurrent()); + + this.findDataPointsArray(flightData, "current", 0, 0).put(jsonBlock); + } else if (dataBlock instanceof GForceBlock) { + JSONObject jsonBlock = new JSONObject(); + jsonBlock.put("x", dataBlock.getTimestamp()); + + GForceBlock gfBlock = (GForceBlock) dataBlock; + jsonBlock.put("maxX", gfBlock.getMaxXInHunderthOfG()); + jsonBlock.put("maxY", gfBlock.getMaxYInHunderthOfG()); + jsonBlock.put("maxZ", gfBlock.getMaxZInHunderthOfG()); + jsonBlock.put("x", gfBlock.getXInHunderthOfG()); + jsonBlock.put("y", gfBlock.getYInHunderthOfG()); + jsonBlock.put("z", gfBlock.getZInHunderthOfG()); + jsonBlock.put("minZ", gfBlock.getMinZInHunderthOfG()); + + } else if (dataBlock instanceof PowerboxBlock) { + JSONObject v1Block = new JSONObject(), v2Block = new JSONObject(), cap1Block = new JSONObject(), + cap2Block = new JSONObject(); + + v1Block.put("x", dataBlock.getTimestamp()); + v2Block.put("x", dataBlock.getTimestamp()); + cap1Block.put("x", dataBlock.getTimestamp()); + cap2Block.put("x", dataBlock.getTimestamp()); + + PowerboxBlock pbBlock = (PowerboxBlock) dataBlock; + cap1Block.put("y", pbBlock.getCapacityOneInmAh()); + cap2Block.put("y", pbBlock.getCapacityTwoInmAh()); + v1Block.put("y", pbBlock.getVoltageOneInHunderthOfVolts()); + v2Block.put("y", pbBlock.getVoltageTwoInHunderthOfVolts()); + + this.findDataPointsArray(flightData, "powerbox", 0, 0).put(v1Block); + this.findDataPointsArray(flightData, "powerbox", 0, 1).put(v2Block); + this.findDataPointsArray(flightData, "powerbox", 1, 0).put(cap1Block); + this.findDataPointsArray(flightData, "powerbox", 1, 1).put(cap2Block); + } else if (dataBlock instanceof RXBlock) { + RXBlock rxBlock = (RXBlock) dataBlock; + + if (rxBlock.hasValidDataLostPacketsReceiverA()) { + JSONObject aBlock = new JSONObject(); + aBlock.put("x", dataBlock.getTimestamp()); + aBlock.put("y", rxBlock.getLostPacketsReceiverA()); + this.findDataPointsArray(flightData, "rx", 0, 0).put(aBlock); + } + if (rxBlock.hasValidDataLostPacketsReceiverB()) { + JSONObject bBlock = new JSONObject(); + bBlock.put("x", dataBlock.getTimestamp()); + bBlock.put("y", rxBlock.getLostPacketsReceiverB()); + this.findDataPointsArray(flightData, "rx", 0, 1).put(bBlock); + } + if (rxBlock.hasValidDataLostPacketsReceiverL()) { + JSONObject lBlock = new JSONObject(); + lBlock.put("x", dataBlock.getTimestamp()); + lBlock.put("y", rxBlock.getLostPacketsReceiverL()); + this.findDataPointsArray(flightData, "rx", 0, 2).put(lBlock); + } + if (rxBlock.hasValidDataLostPacketsReceiverR()) { + JSONObject rBlock = new JSONObject(); + rBlock.put("x", dataBlock.getTimestamp()); + rBlock.put("y", rxBlock.getLostPacketsReceiverR()); + this.findDataPointsArray(flightData, "rx", 0, 3).put(rBlock); + } + + if (rxBlock.hasValidFrameLosssData()) { + JSONObject frameLossBlock = new JSONObject(); + frameLossBlock.put("x", dataBlock.getTimestamp()); + frameLossBlock.put("y", rxBlock.getFrameLoss()); + this.findDataPointsArray(flightData, "rx", 1, 0).put(frameLossBlock); + } + if (rxBlock.hasValidHoldsData()) { + JSONObject holdsBlock = new JSONObject(); + holdsBlock.put("x", dataBlock.getTimestamp()); + holdsBlock.put("y", rxBlock.getHolds()); + this.findDataPointsArray(flightData, "rx", 1, 1).put(holdsBlock); + } + + JSONObject voltsBlock = new JSONObject(); + voltsBlock.put("x", dataBlock.getTimestamp()); + voltsBlock.put("y", rxBlock.getVoltageInHunderthOfVolts()); + this.findDataPointsArray(flightData, "rx", 2, 0).put(voltsBlock); + } else if (dataBlock instanceof StandardBlock) { + StandardBlock standard = (StandardBlock) dataBlock; + + if (flight.hasRpmHeader() && standard.hasValidRpmData()) { + JSONObject rpmBlock = new JSONObject(); + rpmBlock.put("x", dataBlock.getTimestamp()); + rpmBlock.put("y", standard.getRpm()); + this.findDataPointsArray(flightData, "standard", 0, 0).put(rpmBlock); + } + if (standard.hasValidTemperatureData()) { + JSONObject tempBlock = new JSONObject(); + tempBlock.put("x", dataBlock.getTimestamp()); + this.findDataPointsArray(flightData, "standard", 1, 0).put(tempBlock); + tempBlock.put("y", standard.getTemperatureInGradFahrenheit()); + } + if (standard.hasValidVoltageData()) { + JSONObject voltBlock = new JSONObject(); + voltBlock.put("y", standard.getVoltageInHunderthOfVolts()); + voltBlock.put("x", dataBlock.getTimestamp()); + this.findDataPointsArray(flightData, "standard", 2, 0).put(voltBlock); + } + + } else if (dataBlock instanceof VarioBlock) { + // VarioBlock varioBlock = (VarioBlock) dataBlock; + /* + * jsonBlock.put("delta1000ms", varioBlock.get_1000ms()); + * jsonBlock.put("delta2000ms", varioBlock.get_2000ms()); + * jsonBlock.put("delta250ms", varioBlock.get_250ms()); + * jsonBlock.put("delta3000ms", varioBlock.get_3000ms()); + * jsonBlock.put("delta500ms", varioBlock.get_500ms()); + * jsonBlock.put("altitude", varioBlock.get_altitude()); + */ + + } else if (dataBlock instanceof VoltageBlock) { + // VoltageBlock voltageBlock = (VoltageBlock) dataBlock; + + // jsonBlock.put("delta3000ms", voltageBlock.g); + // jsonBlock.put("delta500ms", varioBlock.get_500ms()); + // jsonBlock.put("altitude", varioBlock.get_altitude()); + } + } } diff --git a/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/libs/tlmreader.jar b/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/libs/tlmreader.jar index f4d4d1e..258680a 100644 Binary files a/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/libs/tlmreader.jar and b/plugins/com.monstarmike.telemetry.plugins.tlmDecoder/src/android/libs/tlmreader.jar differ diff --git a/telemetryReaderForAndroid/package.json b/telemetryReaderForAndroid/package.json index 013f766..95ad410 100644 --- a/telemetryReaderForAndroid/package.json +++ b/telemetryReaderForAndroid/package.json @@ -6,7 +6,7 @@ "author": "Michael Gardner (http://blog.itsnotfound.com/)", "contributors": [ "Michael Gardner (http://blog.itsnotfound.com/)", - "David Leuenberger" + "David Leuenberger " ], "repository": { "type": "git", diff --git a/telemetryReaderForAndroid/www/css/style.css b/telemetryReaderForAndroid/www/css/style.css index bdbb0da..5c7b326 100644 --- a/telemetryReaderForAndroid/www/css/style.css +++ b/telemetryReaderForAndroid/www/css/style.css @@ -6,8 +6,8 @@ .grid-container { width: 100%; height: 100%; - padding: 20px; - padding-bottom: 100px; + padding: 10px; + padding-bottom: 120px; } .grid-container figure { diff --git a/telemetryReaderForAndroid/www/js/chartDefinitionService.js b/telemetryReaderForAndroid/www/js/chartDefinitionService.js index 2476ba8..e7a0725 100644 --- a/telemetryReaderForAndroid/www/js/chartDefinitionService.js +++ b/telemetryReaderForAndroid/www/js/chartDefinitionService.js @@ -367,11 +367,11 @@ angular.module('telemetryReaderForAndroid.services') { "selected": true, "axis": { - "title": "Temperature", + "title": "Temperature in °F", "labelFormatter": function (e) { return e.value; }, - "minimum": -100 + "minimum": 23 }, "tooltip": { "contentFormatter": function (e) { diff --git a/telemetryReaderForAndroid/www/js/controllers.js b/telemetryReaderForAndroid/www/js/controllers.js index 56def2b..c803226 100644 --- a/telemetryReaderForAndroid/www/js/controllers.js +++ b/telemetryReaderForAndroid/www/js/controllers.js @@ -102,10 +102,12 @@ angular.module('telemetryReaderForAndroid.controllers', ['telemetryReaderForAndr if (chartSeriesTypes[0].tooltip) { canvasJSChartOptions['toolTip'] = chartSeriesTypes[0].tooltip; } - canvasJSChartOptions['data'] = chartSeriesTypes[0].data; - _.forEach(canvasJSChartOptions['data'], function (dataSet) { + canvasJSChartOptions['data'] = []; + _.forEach(chartSeriesTypes[0].data, function (dataSet) { + console.log('data set series 1', dataSet); dataSet['axisYType'] = 'primary'; - }) + canvasJSChartOptions['data'].push(dataSet); + }); console.log('canvasJSChartOptions - 0', canvasJSChartOptions);