using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using Deltares.Standard.EventPublisher; namespace Deltares.Dam.Data.Sensors { /// /// Class to hold the collected sensor data /// public class SensorData : IDisposable { private List sensors = new List(); private List sensorGroups = new List(); private List sensorLocations = new List(); public SensorData() { DataEventPublisher.OnAfterChange += DataEventPublisher_OnAfterChange; DataEventPublisher.OnDataListModified += DataEventPublisher_OnDataListModified; } private void DataEventPublisher_OnDataListModified(object sender, PublishEventArgs e) { if (sender is List) { var dma = (DataListModifiedArgs)e; if (dma.Action == ListModifyAction.Delete) { UpdatePickSensorsForGroups(); foreach (var sensorGroup in sensorGroups) { sensorGroup.Remove((Sensor)e.Objects[0]); // Next lines just to force an update of the group table content var ms = sensorGroup.SelectionAsString; sensorGroup.SelectionAsString = ms; } } } if (sender is List) { var dma = (DataListModifiedArgs)e; if (dma.Action == ListModifyAction.Delete) { var delList = new List(); foreach (var sensorLocation in sensorLocations) { if (sensorLocation.Group == (Group) e.Objects[0]) { delList.Add(sensorLocation); } } foreach (var delLocation in delList) { sensorLocations.Remove(delLocation); } DataEventPublisher.DataListModified(sensorLocations,null); } } } private void DataEventPublisher_OnAfterChange(object sender, PublishEventArgs e) { if (sender is Sensor || sender is Group) { UpdatePickSensorsForGroups(); } } /// /// Updates the pick sensors for groups. /// public void UpdatePickSensorsForGroups() { foreach (var sensorGroup in sensorGroups) { sensorGroup.PickSensors = sensors; } } /// /// Gets or sets the sensors. /// /// /// The sensors. /// public List Sensors { get { return sensors; } set { sensors = value; } } /// /// Gets or sets the sensor groups. /// /// /// The sensor groups. /// public List SensorGroups { get { return sensorGroups; } set { sensorGroups = value; } } /// /// Gets or sets the sensor locations. /// /// /// The sensor locations. /// [Browsable(false)] [ReadOnly(true)] public List SensorLocations { get { return sensorLocations; } } public Sensor GetSensorById(int id) { return sensors.FirstOrDefault(sensor => sensor.ID == id); } public Group GetGroupById(int id) { return sensorGroups.FirstOrDefault(sensorGroup => sensorGroup.ID == id); } public SensorLocation GetSensorLocationByLocationName(string name) { return sensorLocations.FirstOrDefault(sensorLocation => sensorLocation.LocationName == name); } /// /// Gets the groups for use in selection lists in de UI. /// /// The sensoLocation. /// public IEnumerable GetGroups(SensorLocation sensorLocation) { var list = new List(); list.AddRange(sensorGroups); return list; } public void Dispose() { DataEventPublisher.OnAfterChange -= DataEventPublisher_OnAfterChange; } } }