Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2046 Fixes crash when data is loaded which has been written on… #2094

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/java/io/pslab/DataFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class DataFormatter {
public static final char decSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator();

public static String formatDouble(double value, String format) {
return String.format(Locale.getDefault(), format, value);
return String.format(Locale.ROOT, format, value);
}
}
8 changes: 4 additions & 4 deletions app/src/main/java/io/pslab/activity/DataLoggerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
getFileData(file);
} else
CustomSnackBar.showSnackBar(findViewById(android.R.id.content),
getString(R.string.no_file_selected),null,null, Snackbar.LENGTH_SHORT);
getString(R.string.no_file_selected), null, null, Snackbar.LENGTH_SHORT);
}
}

Expand All @@ -236,12 +236,12 @@ private void getFileData(File file) {
realm.copyToRealm(object);
} else {
CustomSnackBar.showSnackBar(findViewById(android.R.id.content),
getString(R.string.incorrect_import_format),null,null, Snackbar.LENGTH_SHORT);
getString(R.string.incorrect_import_format), null, null, Snackbar.LENGTH_SHORT);
}
realm.commitTransaction();
} catch (Exception e) {
CustomSnackBar.showSnackBar(findViewById(android.R.id.content),
getString(R.string.incorrect_import_format),null,null, Snackbar.LENGTH_SHORT);
getString(R.string.incorrect_import_format), null, null, Snackbar.LENGTH_SHORT);
}
} else if (i == 0) {
block = System.currentTimeMillis();
Expand Down Expand Up @@ -277,7 +277,7 @@ private RealmObject getObject(String objectType, String[] data, long time, long
returnObject = new GyroData(time, block, Float.valueOf(data[2]), Float.valueOf(data[3]), Float.valueOf(data[4]), Double.valueOf(data[5]), Double.valueOf(data[6]));
break;
case "Compass":
returnObject = new CompassData(time, block, data[2].equals("null") ? "0" : data[2], data[3].equals("null") ? "0" : data[3], data[4].equals("null") ? "0" : data[4], data[5], Double.valueOf(data[6]), Double.valueOf(data[7]));
returnObject = new CompassData(time, block, data[2].equals("null") ? 0f : Float.valueOf(data[2]), data[3].equals("null") ? 0f : Float.valueOf(data[3]), data[4].equals("null") ? 0f : Float.valueOf(data[4]), data[5], Double.valueOf(data[6]), Double.valueOf(data[7]));
break;
case "Thermometer":
returnObject = new ThermometerData(time, block, Float.valueOf(data[2]), Double.valueOf(data[5]), Double.valueOf(data[6]));
Expand Down
26 changes: 20 additions & 6 deletions app/src/main/java/io/pslab/activity/MultimeterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import io.pslab.fragment.MultimeterSettingsFragment;
import io.pslab.models.MultimeterData;
import io.pslab.models.SensorDataBlock;
import io.pslab.others.CSVDataLine;
import io.pslab.others.CSVLogger;
import io.pslab.others.CustomSnackBar;
import io.pslab.others.GPSLogger;
Expand All @@ -67,6 +68,14 @@ public class MultimeterActivity extends AppCompatActivity {
public static final String PREFS_NAME = "customDialogPreference";
public static final String NAME = "savingData";
private static final int MY_PERMISSIONS_REQUEST_STORAGE_FOR_DATA = 101;
private static final CSVDataLine CSV_HEADER =
new CSVDataLine()
.add("Timestamp")
.add("DateTime")
.add("Data")
.add("Value")
.add("Latitude")
.add("Longitude");
private final String KEY_LOG = "has_log";
private final String DATA_BLOCK = "data_block";
public boolean recordData = false;
Expand Down Expand Up @@ -105,7 +114,7 @@ public class MultimeterActivity extends AppCompatActivity {
SharedPreferences multimeter_data;
private ScienceLab scienceLab;
private int knobState;
private String dataRecorded;
private CSVDataLine dataRecorded;
private String defaultValue;
private Menu menu;
private Boolean switchIsChecked;
Expand All @@ -116,7 +125,6 @@ public class MultimeterActivity extends AppCompatActivity {
private boolean locationEnabled = true;
private long recordPeriod;
private double lat = 0, lon = 0;
private String multimeterCSVheader = "Timestamp,DateTime,Data,Value,Latitude,Longitude";
private GPSLogger gpsLogger;
private Realm realm;
private long block;
Expand Down Expand Up @@ -153,7 +161,7 @@ public void onClick(View v) {
});

multimeter_data = this.getSharedPreferences(NAME, MODE_PRIVATE);
dataRecorded = multimeterCSVheader;
dataRecorded = CSV_HEADER;
knobState = multimeter_data.getInt("KnobState", 2);
switchIsChecked = multimeter_data.getBoolean("SwitchState", false);
aSwitch.setChecked(switchIsChecked);
Expand Down Expand Up @@ -445,7 +453,13 @@ private void record(String data, String value) {
lon = 0.0;
}
long timestamp = System.currentTimeMillis();
dataRecorded = timestamp + "," + CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp)) + "," + data + "," + value + "," + lat + "," + lon;
dataRecorded = new CSVDataLine()
.add(timestamp)
.add(CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp)))
.add(data)
.add(value)
.add(lat)
.add(lon);
multimeterLogger.writeCSVFile(dataRecorded);
recordSensorData(new MultimeterData(timestamp, block, data, value, lat, lon));
}
Expand Down Expand Up @@ -491,7 +505,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (isDataRecorded) {
MenuItem item1 = menu.findItem(R.id.record_pause_data);
item1.setIcon(R.drawable.ic_record_white);
dataRecorded = multimeterCSVheader;
dataRecorded = CSV_HEADER;
// Export Data
CustomSnackBar.showSnackBar(coordinatorLayout,
getString(R.string.csv_store_text) + " " + multimeterLogger.getCurrentFilePath()
Expand All @@ -513,7 +527,7 @@ public void onClick(View view) {
multimeterLogger = new CSVLogger(getString(R.string.multimeter));
multimeterLogger.prepareLogFile();
multimeterLogger.writeMetaData(getResources().getString(R.string.multimeter));
multimeterLogger.writeCSVFile(multimeterCSVheader);
multimeterLogger.writeCSVFile(CSV_HEADER);
block = System.currentTimeMillis();
recordSensorDataBlockID(new SensorDataBlock(block, getResources().getString(R.string.multimeter)));
isRecordingStarted = true;
Expand Down
41 changes: 30 additions & 11 deletions app/src/main/java/io/pslab/activity/OscilloscopeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import io.pslab.models.OscilloscopeData;
import io.pslab.models.SensorDataBlock;
import io.pslab.others.AudioJack;
import io.pslab.others.CSVDataLine;
import io.pslab.others.CSVLogger;
import io.pslab.others.CustomSnackBar;
import io.pslab.others.GPSLogger;
Expand All @@ -89,6 +90,16 @@
public class OscilloscopeActivity extends AppCompatActivity implements View.OnClickListener {

private static final String PREF_NAME = "OscilloscopeActivity";
private static final CSVDataLine CSV_HEADER = new CSVDataLine()
.add("Timestamp")
.add("DateTime")
.add("Mode")
.add("Channel")
.add("xData")
.add("yData")
.add("Timebase")
.add("lat")
.add("lon");
private final Object lock = new Object();
@BindView(R.id.chart_os)
public LineChart mChart;
Expand Down Expand Up @@ -189,7 +200,6 @@ public class OscilloscopeActivity extends AppCompatActivity implements View.OnCl
private long block;
private Timer recordTimer;
private long recordPeriod = 100;
private String oscilloscopeCSVHeader = "Timestamp,DateTime,Mode,Channel,xData,yData,Timebase,lat,lon";
private String loggingXdata = "";
private final String KEY_LOG = "has_log";
private final String DATA_BLOCK = "data_block";
Expand Down Expand Up @@ -539,7 +549,7 @@ public void onClick(View view) {
csvLogger = new CSVLogger(getResources().getString(R.string.oscilloscope));
csvLogger.prepareLogFile();
csvLogger.writeMetaData(getResources().getString(R.string.oscilloscope));
csvLogger.writeCSVFile(oscilloscopeCSVHeader);
csvLogger.writeCSVFile(CSV_HEADER);
recordSensorDataBlockID(new SensorDataBlock(block, getResources().getString(R.string.oscilloscope)));
CustomSnackBar.showSnackBar(mainLayout, getString(R.string.data_recording_start), null, null, Snackbar.LENGTH_SHORT);
}
Expand Down Expand Up @@ -679,12 +689,21 @@ public void pauseData() {
private void logChannelData(String[] channels) {
long timestamp = System.currentTimeMillis();
int noOfChannels = channels.length;
String timeData = timestamp + "," + CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp));
String locationData = lat + "," + lon;
String dateTime = CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp));
for (int i = 0; i < noOfChannels; i++) {
recordSensorData(new OscilloscopeData(timestamp + i, block, noOfChannels, channels[i], loggingXdata, loggingYdata[i], xAxisScale, lat, lon));
String data = timeData + "," + noOfChannels + "," + channels[i] + "," + loggingXdata + "," + loggingYdata[i] + "," + xAxisScale + "," + locationData;
csvLogger.writeCSVFile(data);
csvLogger.writeCSVFile(
new CSVDataLine()
.add(timestamp)
.add(dateTime)
.add(noOfChannels)
.add(channels[i])
.add(loggingXdata)
.add(loggingYdata[i])
.add(xAxisScale)
.add(lat)
.add(lon)
);
}
}

Expand Down Expand Up @@ -978,7 +997,7 @@ protected Void doInBackground(String... channels) {
String[] xDataString = null;
maxAmp = 0;
scienceLab.captureTraces(4, samples, timeGap, channel, isTriggerSelected, null);
Thread.sleep((long)(samples*timeGap*1e-3));
Thread.sleep((long) (samples * timeGap * 1e-3));
for (int i = 0; i < noOfChannels; i++) {
entries.add(new ArrayList<>());
channel = channels[i];
Expand Down Expand Up @@ -1052,12 +1071,12 @@ protected Void doInBackground(String... channels) {
double max = xData[xData.length - 1];
for (int j = 0; j < 500; j++) {
double x = j * max / 500;
double t = 2*Math.PI*freq*(x - phase);
double t = 2 * Math.PI * freq * (x - phase);
double y;
if (t%(2*Math.PI) < 2*Math.PI*dc) {
if (t % (2 * Math.PI) < 2 * Math.PI * dc) {
y = offset + amp;
} else {
y = offset - 2*amp;
y = offset - 2 * amp;
}
curveFitEntries.get(curveFitEntries.size() - 1).add(new Entry((float) x, (float) y));
}
Expand Down Expand Up @@ -1097,7 +1116,7 @@ protected Void doInBackground(String... channels) {
float audioValue = (float) map(buffer[i], -32768, 32767, -3, 3);
if (!isFourierTransformSelected) {
if (noOfChannels == 1) {
xDataString[i] = String.valueOf(2.0*i);
xDataString[i] = String.valueOf(2.0 * i);
}
entries.get(entries.size() - 1).add(new Entry(i, audioValue));
} else {
Expand Down
45 changes: 32 additions & 13 deletions app/src/main/java/io/pslab/activity/PowerSourceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.pslab.items.SquareImageButton;
import io.pslab.models.PowerSourceData;
import io.pslab.models.SensorDataBlock;
import io.pslab.others.CSVDataLine;
import io.pslab.others.CSVLogger;
import io.pslab.others.CustomSnackBar;
import io.pslab.others.GPSLogger;
Expand All @@ -58,6 +59,15 @@
public class PowerSourceActivity extends AppCompatActivity {

public static final String POWER_PREFERENCES = "Power_Preferences";
private static final CSVDataLine CSV_HEADER = new CSVDataLine()
.add("Timestamp")
.add("DateTime")
.add("PV1")
.add("PV2")
.add("PV3")
.add("PCS")
.add("Latitude")
.add("Longitude");

private final int CONTROLLER_MIN = 1;
private final int PV1_CONTROLLER_MAX = 1001;
Expand Down Expand Up @@ -210,11 +220,11 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
float voltage = Float.parseFloat(voltageValue);
if (voltage < -5.00f) {
voltage = -5.00f;
displayPV1.setText(String.valueOf(voltage) + " V");
displayPV1.setText(voltage + " V");
}
if (voltage > 5.00f) {
voltage = 5.00f;
displayPV1.setText(String.valueOf(voltage) + " V");
displayPV1.setText(voltage + " V");
}
controllerPV1.setProgress(mapPowerToProgress(voltage, PV1_CONTROLLER_MAX,
5.00f, -5.00f));
Expand All @@ -232,11 +242,11 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
float voltage = Float.parseFloat(voltageValue);
if (voltage < -3.30f) {
voltage = -3.30f;
displayPV2.setText(String.valueOf(voltage) + " V");
displayPV2.setText(voltage + " V");
}
if (voltage > 3.30f) {
voltage = 3.30f;
displayPV2.setText(String.valueOf(voltage) + " V");
displayPV2.setText(voltage + " V");
}
controllerPV2.setProgress(mapPowerToProgress(voltage, PV2_CONTROLLER_MAX,
3.30f, -3.30f));
Expand All @@ -254,11 +264,11 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
float voltage = Float.parseFloat(voltageValue);
if (voltage < 0.00f) {
voltage = 0.00f;
displayPV3.setText(String.valueOf(voltage) + " V");
displayPV3.setText(voltage + " V");
}
if (voltage > 3.30f) {
voltage = 3.30f;
displayPV3.setText(String.valueOf(voltage) + " V");
displayPV3.setText(voltage + " V");
}
controllerPV3.setProgress(mapPowerToProgress(voltage, PV3_CONTROLLER_MAX,
3.30f, 0.00f));
Expand All @@ -276,11 +286,11 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
float current = Float.parseFloat(currentValue);
if (current < 0.00f) {
current = 0.00f;
displayPCS.setText(String.valueOf(current) + " mA");
displayPCS.setText(current + " mA");
}
if (current > 3.30f) {
current = 3.30f;
displayPCS.setText(String.valueOf(current) + " mA");
displayPCS.setText(current + " mA");
}
controllerPCS.setProgress(mapPowerToProgress(current, PCS_CONTROLLER_MAX,
3.30f, 0.00f));
Expand Down Expand Up @@ -571,6 +581,7 @@ private void removeCursor() {
displayPV3.setCursorVisible(false);
displayPCS.setCursorVisible(false);
}

/**
* Click listeners to increment and decrement buttons
*
Expand Down Expand Up @@ -909,7 +920,7 @@ private int mapPowerToProgress(float power, int CONTROLLER_MAX, float max, float
*/
private float limitDigits(float number) {
try {
return Float.valueOf(String.format(Locale.US, "%.2f", number));
return Float.valueOf(String.format(Locale.ROOT, "%.2f", number));
} catch (NumberFormatException e) {
return 0.00f;
}
Expand Down Expand Up @@ -957,7 +968,7 @@ private void recordData() {
powerSourceLogger = new CSVLogger(getString(R.string.power_source));
powerSourceLogger.prepareLogFile();
powerSourceLogger.writeMetaData(getString(R.string.power_source));
powerSourceLogger.writeCSVFile("Timestamp,DateTime,PV1,PV2,PV2,PCS,Latitude,Longitude");
powerSourceLogger.writeCSVFile(CSV_HEADER);
writeHeaderToFile = !writeHeaderToFile;
recordSensorDataBlockID(new SensorDataBlock(block, getResources().getString(R.string.power_source)));
}
Expand All @@ -975,9 +986,17 @@ private void recordData() {
lon = 0.0;
}
timestamp = System.currentTimeMillis();
String dateTime = CSVLogger.FILE_NAME_FORMAT.format(new Date(System.currentTimeMillis()));
powerSourceLogger.writeCSVFile(System.currentTimeMillis() + "," + dateTime + "," + String.valueOf(voltagePV1)
+ "," + String.valueOf(voltagePV2) + "," + String.valueOf(voltagePV3) + "," + String.valueOf(currentPCS) + "," + lat + "," + lon);
powerSourceLogger.writeCSVFile(
new CSVDataLine()
.add(System.currentTimeMillis())
.add(CSVLogger.FILE_NAME_FORMAT.format(new Date(System.currentTimeMillis())))
.add(voltagePV1)
.add(voltagePV2)
.add(voltagePV3)
.add(currentPCS)
.add(lat)
.add(lon)
);
recordSensorData(new PowerSourceData(timestamp, block, voltagePV1, voltagePV2, voltagePV3, currentPCS, lat, lon));
}

Expand Down
Loading