public void UpdateLiveGraph()
{
HandleDaqTemperatureSourceSet(_deviceConnection.UsingDaqTemp);
// current live state
var now = DateTime.Now;
var sensors = _deviceConnection.Where(s => s.IsValid).ToList();
// get readings
var readings = new Dictionary<ISensor, IReading>();
foreach (var sensor in sensors) {
var reading = sensor.GetCurrentReading();
if (reading.IsValid) {
readings.Add(sensor, reading);
}
}
// save to memory
foreach (var reading in readings) {
if (!_memoryDataStore.GetAllSensorInfos().Any(si => si.Name.Equals(reading.Key.Name))) {
_memoryDataStore.AddSensor(reading.Key);
}
_memoryDataStore.Push(reading.Key.Name, new[]{reading.Value});
}
// the current sensor views
var sensorViews = _sensorViewPanelControler.Views.ToList();
// determine which sensors are enabled
var enabledSensors = new List<ISensor>();
for (int i = 0; i < sensors.Count && i < sensorViews.Count; i++) {
if (sensorViews[i].IsSelected) {
enabledSensors.Add(sensors[i]);
}
}
var liveDataTimeSpan = liveAtmosphericHeader.TimeRange.SelectedSpan;
TimeSpan liveUpdateInterval;
if(liveDataTimeSpan > new TimeSpan(1,0,0))
liveUpdateInterval = new TimeSpan(0,1,0);
else if(liveDataTimeSpan >= new TimeSpan(1,0,0))
liveUpdateInterval = new TimeSpan(0,0,15);
else
liveUpdateInterval = new TimeSpan(0,0,0,0,500);
var liveDataEnabled =
(_lastLiveUpdateInterval != liveUpdateInterval)
|| (now - liveUpdateInterval > _lastLiveUpdate);
_lastLiveUpdateInterval = liveUpdateInterval;
// pass it off to the live data graphs/tables
if (liveDataEnabled) {
_lastLiveUpdate = now;
TimeUnit meanLiveUnit;
if(liveDataTimeSpan >= new TimeSpan(1,0,0))
meanLiveUnit = TimeUnit.Minute;
else
meanLiveUnit = TimeUnit.Second;
// gather the data for each selected sensor
var enabledSensorsLiveMeans = new List<List<ReadingAggregate>>(enabledSensors.Count);
foreach (var sensor in enabledSensors) {
// get the recent readings
var recentReadings = _memoryDataStore.GetReadings(sensor.Name, now, TimeSpan.Zero.Subtract(liveDataTimeSpan));
// calculate the mean data
var means = StatsUtil.AggregateMean(recentReadings, meanLiveUnit).ToList();
// convert the units for display
var converter = ConverterCacheReadingAggregate.Get(
sensor.TemperatureUnit, TemperatureUnit,
sensor.SpeedUnit, SpeedUnit,
sensor.PressureUnit, PressureUnit
);
converter.ConvertInline(means);
// add it to the presentation list
enabledSensorsLiveMeans.Add(means);
}
// compile it all together into one set
var enabledSensorsCompiledMeans = StatsUtil.JoinParallelMeanReadings(enabledSensorsLiveMeans);
// present the data set
liveAtmosphericGraph.Invoke(new Action(() => {
liveAtmosphericGraph.TemperatureUnit = TemperatureUnit;
liveAtmosphericGraph.PressureUnit = PressureUnit;
liveAtmosphericGraph.SpeedUnit = SpeedUnit;
liveAtmosphericGraph.FormatTimeAxis(liveDataTimeSpan);
liveAtmosphericGraph.SetLatest(enabledSensorsCompiledMeans.LastOrDefault());
liveAtmosphericGraph.State = AppContext.PersistentState;
liveAtmosphericGraph.SetDataSource(enabledSensorsCompiledMeans);
// update the sensor controls
}));
}
Invoke(new Action(() => { _sensorViewPanelControler.UpdateView(sensors, AppContext.PersistentState); }));
}