use of com.kresshy.weatherstation.weather.Measurement in project Weather-Station by Kresshy.
the class GraphViewFragment method handleIncomingMeasurement.
private void handleIncomingMeasurement(Measurement measurement) {
// prevent adding false zero temperature measurements
Timber.d("Filtering wrong measurements and load previous values");
for (int j = 0; j < measurement.getNumberOfNodes(); j++) {
if (previousMeasurement.hasNodeId(j) && measurement.hasNodeId(j)) {
if (measurement.getWeatherDataForNode(j).getTemperature() == 0.0 || (measurement.getWeatherDataForNode(j).getTemperature() - previousMeasurement.getWeatherDataForNode(j).getTemperature() > 3.0)) {
measurement.getWeatherDataForNode(j).setTemperature(previousMeasurement.getWeatherDataForNode(j).getTemperature());
}
}
}
previousMeasurement = measurement;
// maintaining bucket for average calculation
if (lastMeasurementsList.size() == averageBucketSize + 1) {
lastMeasurementsList.remove(0);
}
lastMeasurementsList.add(measurement);
for (int i = 0; i < measurement.getNumberOfNodes(); i++) {
double sumWindSpeed = 0;
double sumTemperature = 0;
int missingMeasurements = 0;
for (Measurement m : lastMeasurementsList) {
if (!(m.getNumberOfNodes() < i)) {
// if measurement has missing data of nodes
if (m.hasNodeId(i)) {
// ensuring node exists
if (i > 0) {
sumWindSpeed += (m.getWeatherDataForNode(i).getWindSpeed() + correctionWind);
sumTemperature += (m.getWeatherDataForNode(i).getTemperature() + correctionTemp);
} else {
sumWindSpeed += m.getWeatherDataForNode(i).getWindSpeed();
sumTemperature += m.getWeatherDataForNode(i).getTemperature();
}
} else {
// else doesn't count in the average calc
missingMeasurements++;
}
}
}
double averageWindSpeed = sumWindSpeed / (lastMeasurementsList.size() - missingMeasurements);
double averageTemperature = sumTemperature / (lastMeasurementsList.size() - missingMeasurements);
Timber.d("SumWindSpeed: " + sumWindSpeed + ", sumTemp: " + sumTemperature);
Timber.d("Missing measurements: " + missingMeasurements);
Timber.d("LastMeasurementList: " + lastMeasurementsList.toString());
Timber.d("AverageWindSpeed: " + averageWindSpeed + ", AverageTemp: " + averageTemperature);
Timber.d("Adding data in Series for nodeId: " + i);
Timber.d("Number of samples: " + numberOfSamples);
try {
windSpeedSeriesList.get(i).appendData(new GraphViewData(measurementCount, averageWindSpeed), true, numberOfSamples);
} catch (IndexOutOfBoundsException e) {
Timber.d("Cannot find windSpeedSeries for nodeId: " + i + " creating new GraphViewSeries");
GraphViewData[] windSpeedData = new GraphViewData[1];
windSpeedData[0] = new GraphViewData(measurementCount, measurement.getWeatherDataForNode(i).getWindSpeed());
windSpeedDataList.add(i, windSpeedData);
GraphViewSeries windSpeedSeries = new GraphViewSeries("Wind Speed", new GraphViewSeries.GraphViewSeriesStyle(getColorForWindSpeedByNode(i), getLineWidthByNode(i)), windSpeedDataList.get(i));
windSpeedSeriesList.add(i, windSpeedSeries);
windSpeedSeriesList.get(i).resetData(windSpeedDataList.get(i));
windSpeedGraph.addSeries(windSpeedSeriesList.get(i));
}
try {
temperatureSeriesList.get(i).appendData(new GraphViewData(measurementCount, averageTemperature), true, numberOfSamples);
} catch (IndexOutOfBoundsException e) {
Timber.d("Cannot find windSpeedSeries for nodeId: " + i + " creating new GraphViewSeries");
GraphViewData[] temperatureData = new GraphViewData[1];
temperatureData[0] = new GraphViewData(measurementCount, measurement.getWeatherDataForNode(i).getTemperature());
temperatureDataList.add(i, temperatureData);
GraphViewSeries temperatureSeries = new GraphViewSeries("Temperature", new GraphViewSeries.GraphViewSeriesStyle(getColorForTemperatureByNode(i), getLineWidthByNode(i)), temperatureDataList.get(i));
temperatureSeriesList.add(i, temperatureSeries);
temperatureSeriesList.get(i).resetData(temperatureDataList.get(i));
temperatureGraph.addSeries(temperatureSeriesList.get(i));
}
}
measurementCount++;
}
Aggregations