// Copyright (C) Stichting Deltares 2018. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero 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.Linq; namespace Deltares.DamEngine.Data.General.Sensors { /// /// Class for sensor group /// public class SensorGroup { /// /// Holds a reference to the set of selected sensors /// private readonly HashSet sensors; private List pickSensors; private int id; /// /// Holds a reference to a set of relative loctions along the profile /// private readonly IDictionary relativeLocationAlongProfileDictionary; /// /// Initializes a new instance of the class. /// public SensorGroup() { sensors = new HashSet(); relativeLocationAlongProfileDictionary = new Dictionary(); ID = -1; } /// /// Gets or sets the ID. /// /// /// The ID value should be unique. /// public int ID { get { return id; } set { id = value; } } /// /// Gets the selected sensors. /// public IEnumerable Selection { get { return sensors; } } /// /// Gets or sets the selection as list for UI (table) purposes. /// /// /// The selection as list. /// public string SelectionAsString { get { string res = ""; foreach (var sensor in sensors) { res = res + sensor.ID + "; "; } return res; } set { ClearSelection(); var locSensors = ParseStringToSensorIDs(value); if (pickSensors != null) { foreach (var sensorID in locSensors) { var sensor = PickSensors.Find(x => x.ID == sensorID); if (sensor != null) { Add(sensor); } } } } } private List ParseStringToSensorIDs(string value) { value = value.Trim(); var ids = new List(); var idsarr = value.Split(new Char [] {';'}, StringSplitOptions.RemoveEmptyEntries); foreach (var s in idsarr) { try { var val = Int32.Parse(s); ids.Add(val); } catch (Exception) { // surpress errors, just do not use value } } return ids; } /// /// Persistable sensor array. Only for internal use /// public Sensor[] SensorArray { get { return sensors.ToArray(); } set { ClearSelection(); foreach (var sensor in value) { Add(sensor); } } } /// /// Gets the sensor count. /// public int SensorCount { get { return sensors.Count; } } /// /// Gets the sensor relative locations. /// /// /// The sensor relative locations. /// public IEnumerable> SensorRelativeLocations { get { return relativeLocationAlongProfileDictionary; } } /// /// Gets or sets the pick sensors. /// /// /// The pick sensors. /// 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; } /// /// Adds the specified sensor to the selected sensor list for this sensorGroup. /// /// 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 sensorGroup. /// /// 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(); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return ID.ToString(); } } }