-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DataProcess class and moved some functions
Co-authored-by: Jamie Grenier <[email protected]> Co-authored-by: Ajay Ramachandran <[email protected]>
- Loading branch information
1 parent
3d6fdce
commit f9e301d
Showing
4 changed files
with
129 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/uorocketry/basestation/data/DataProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package uorocketry.basestation.data; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import uorocketry.basestation.connections.DataReceiver; | ||
import uorocketry.basestation.connections.DeviceConnection; | ||
|
||
public class DataProcessor implements DataReceiver { | ||
|
||
private List<List<DataHolder>> allData = new ArrayList<>(2); | ||
private JSONObject config; | ||
|
||
public DataProcessor(JSONObject config, int dataSourceCount) { | ||
this.config = config; | ||
|
||
allData = new ArrayList<>(dataSourceCount); | ||
for (int i = 0; i < dataSourceCount; i++) { | ||
allData.add(new ArrayList<>()); | ||
} | ||
} | ||
|
||
/** Separator for the data */ | ||
public static final String SEPARATOR = ","; | ||
|
||
@Override | ||
public void receivedData(DeviceConnection deviceConnection, byte[] data) { | ||
|
||
} | ||
|
||
public DataHolder parseData(String data, int tableIndex) { | ||
DataHolder dataHolder = new DataHolder(tableIndex, config.getJSONArray("datasets").getJSONObject(tableIndex)); | ||
|
||
// Clear out the b' ' stuff added that is only meant for the radio to see | ||
data = data.replaceAll("b'|\\\\r\\\\n'", ""); | ||
if (data.endsWith(",")) data = data.substring(0, data.length() - 1); | ||
|
||
// Semi-colon separated | ||
String[] splitData = data.split(SEPARATOR); | ||
if (splitData.length != dataHolder.data.length) { | ||
//invalid data | ||
System.err.println("Line with invalid data (Not the correct amount of data). It was " + splitData.length); | ||
|
||
return null; | ||
} | ||
|
||
// TODO: Potentially remove and test this | ||
// Ensure that the timestamp has not gone back in time | ||
try { | ||
DataHolder lastDataPointDataHolder = findLastValidDataPoint(allData.get(tableIndex)); | ||
|
||
int timestampIndex = config.getJSONArray("datasets").getJSONObject(tableIndex).getInt("timestampIndex"); | ||
if (lastDataPointDataHolder != null) { | ||
Float value = lastDataPointDataHolder.data[timestampIndex].getDecimalValue(); | ||
if (value != null && Float.parseFloat(splitData[timestampIndex]) < value) { | ||
System.err.println("Timestamp just went backwards"); | ||
|
||
// Treat as invalid data | ||
return null; | ||
} | ||
} | ||
} catch (NumberFormatException | JSONException e) {} | ||
|
||
for (int i = 0; i < splitData.length; i++) { | ||
if (!dataHolder.set(i, splitData[i])) { | ||
System.err.println("Failed to set data handler"); | ||
|
||
// Parsing failed | ||
return null; | ||
} | ||
} | ||
|
||
return dataHolder; | ||
} | ||
|
||
/** | ||
* Find last non null data point | ||
* | ||
* @param currentTableData | ||
* @return DataHolder if found, null otherwise | ||
*/ | ||
private DataHolder findLastValidDataPoint(List<DataHolder> currentTableData) { | ||
for (int i = currentTableData.size() - 1; i >= 0; i--) { | ||
if (currentTableData.get(i) != null) { | ||
return currentTableData.get(i); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public List<List<DataHolder>> getAllData() { | ||
return allData; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.