Skip to content

Internal Data Handling

vstarlinger edited this page Aug 29, 2017 · 2 revisions

The internal data structure is similar to the structure of the protocol xml. Once data is received it is sent to the MessageController where it gets parsed. The parsing happens accoring to the protocol file that is read by the XMLReader.

Data gets stored in HashMap<Long, Object> objects in the Var class. The map key is always a Long, typically refering to the time in milliseconds corresponding to the value of that specific variable. The other fields correspond to the ones described in the Protocol section of this wiki.

Example

Following is an example of how the data structure can be used to display sensor data to the user. The code dispalyed is taken from the updateData method of the MainPanelController class.

@Override
    public void updateData(MessageController mc, USOCEvent e) {
        //in case this is an error event, ignore it
        if (e instanceof ErrorEvent) {
            return;
        }

        //go through the data and update the charts
        for (Sensor sensor : mc.getData().getSensors()) {
            //adjust values for the sensor specific to the current experiment
            Sensor adjusted = DataModification.adjustSensorData(sensor);

            //search for pressure sensors
            if (adjusted.getSensorName().contains("Pressure")) {
                //pressure sensors have only one variable with the current
                //data scheme and it uses the sensor name as variable name 
                for (Var var : adjusted.getVars()) {
                    addVarToChart(var, lineChart2);
                }
            }
        }
    }

    private void addVarToChart(Var var, LineChart<Number, Number> chart) {
        XYChart.Series series = getSeriesForChart(var, chart);
        for (Long time : var.getValues().keySet()) {
            series.getData().add(new XYChart.Data<>(time, var.getValues().get(time)));
        }
    }

    private XYChart.Series getSeriesForChart(Var var, LineChart<Number, Number> chart) {
        for (XYChart.Series<Number, Number> series : chart.getData()) {
            if (series.getName().equals(var.getDataName())) {
                return series;
            }
        }
        XYChart.Series series = new XYChart.Series<>();
        series.setName(var.getDataName());
        chart.getData().add(series);
        return series;
    }

Clone this wiki locally