// Copyright (C) Stichting Deltares 2025. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Xml.Serialization; using Deltares.Dam.Data.Specifications; using Deltares.Standard.Attributes; using Deltares.Standard.EventPublisher; using Deltares.Standard.IO.Xml; using Deltares.Standard.Specifications; using XmlSerializer = Deltares.Standard.IO.Xml.XmlSerializer; namespace Deltares.Dam.Data.Sensors; [Serializable] public class Group { /// /// Holds a reference to the set of selected sensors /// private readonly HashSet sensors; /// /// Holds a reference to a set of relative loctions along the profile /// private readonly IDictionary relativeLocationAlongProfileDictionary; private List pickSensors; private int id; public Group() { sensors = new HashSet(); relativeLocationAlongProfileDictionary = new Dictionary(); ID = -1; DataEventPublisher.AfterChange(this); } /// /// Gets or sets the ID. /// /// /// The ID value should be unique. /// [PropertyOrder(1, 1)] public int ID { get { return id; } set { id = value; DataEventPublisher.AfterChange(this, g => g.ID); } } /// /// Gets the selected sensors. /// [XmlIgnore] [Browsable(false)] public IEnumerable Selection { get { return sensors; } } /// /// Gets or sets the selection as list for UI (table) purposes. /// /// /// The selection as list. /// [XmlIgnore] [PropertyOrder(1, 2)] public string SelectionAsString { get { var res = ""; foreach (Sensor sensor in sensors) { res = res + sensor.ID + "; "; } return res; } set { ClearSelection(); List locSensors = ParseStringToSensorIDs(value); if (pickSensors != null) { foreach (int sensorID in locSensors) { Sensor sensor = PickSensors.Find(x => x.ID == sensorID); if (sensor != null) { Add(sensor); } } } } } /// /// Persistable sensor array. Only for internal use /// [Browsable(false)] public Sensor[] SensorArray { get { return sensors.ToArray(); } set { ClearSelection(); foreach (Sensor sensor in value) { Add(sensor); } } } /// /// Gets the sensor count. /// [Browsable(false)] public int SensorCount { get { return sensors.Count; } } [XmlIgnore] [Browsable(false)] public IEnumerable> SensorRelativeLocations { get { return relativeLocationAlongProfileDictionary; } } [XmlIgnore] [ReadOnly(true)] public List PickSensors { get { return pickSensors; } set { pickSensors = value; } } /// /// Determines whether this instance is valid. /// /// public bool IsValid() { IEnumerable validationResults = Validator.Validate(this); return !validationResults.Any(); } /// /// Determines whether this instance is transient (associated with an invalid ID). /// /// /// true if this instance is transient; otherwise, false. /// public bool IsTransient() { return ID <= 0; } /// /// Sets the relative location. /// /// The sensor. /// The value. public void SetRelativeLocation(Sensor sensor, double value) { relativeLocationAlongProfileDictionary[sensor] = value; } /// /// Adds the specified sensor to the selected sensor list for this group. /// /// The sensor to add. public void Add(Sensor sensor) { sensors.Add(sensor); if (!relativeLocationAlongProfileDictionary.ContainsKey(sensor)) { relativeLocationAlongProfileDictionary.Add(sensor, 0); } } /// /// Removes the specified sensor from the selected sensor list from this group. /// /// The sensor to remove. public void Remove(Sensor sensor) { sensors.Remove(sensor); if (relativeLocationAlongProfileDictionary.ContainsKey(sensor)) { relativeLocationAlongProfileDictionary.Remove(sensor); } } /// /// Clears the sensor selection /// public void ClearSelection() { sensors.Clear(); relativeLocationAlongProfileDictionary.Clear(); } public string Serialize() { return new XmlSerializer().SerializeToString(this); } public static Group Deserialize(string xml) { return (Group) new XmlDeserializer().XmlDeserializeFromString(xml, typeof(Group)); } public override string ToString() { return ID.ToString(); } private List ParseStringToSensorIDs(string value) { value = value.Trim(); var ids = new List(); string[] idsarr = value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in idsarr) { try { int val = Int32.Parse(s); ids.Add(val); } catch (Exception) { // surpress errors, just do not use value } } return ids; } }