//----------------------------------------------------------------------- // // Copyright (c) 2012 Deltares. All rights reserved. // // B. Faassen // barry.faassen@deltares.nl // 1-8-2012 // n.a. //----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using Deltares.DamEngine.Data.Standard; namespace Deltares.DamEngine.Data.General.Sensors { /// /// This class represents a sensor or monitoring point used in dikes. /// This entity is created for Dam Live /// public class Sensor : IName { #region Business Rules /// /// Specication to test if the value of the candidate is valid /// internal class ContiansAtLeastOneItem : PredicateSpecification> { public ContiansAtLeastOneItem() : base(x => x.Any()) { Description = "The sensor should contain at least one PL Line mapping."; } } #endregion private readonly IGeometry geometry; private double relativeLocation; private readonly HashSet plLineTypes; private int id; public Sensor() { plLineTypes = new HashSet(); ID = -1; geometry = new Point(0,0,0); } public Sensor(Point point) : this() { if (point == null) throw new ArgumentNullException("point"); this.geometry = point; } public Sensor(double x, double y, double z) : this(new Point(x, y, z)) { } /// /// Gets or sets the ID. /// /// /// The ID should be unique. /// public int ID { get { return id; } set { id = value; } } /// /// Gets or sets the name of this sensor. /// /// /// The name string value should not be empty and unique. /// [Specification(typeof(NotEmptySpecification))] public string Name { get; set; } /// /// Gets or sets the depth. /// /// /// The depth. /// public double Depth { get { return ZRd; } set { ZRd = value; } } /// /// Gets or sets the relative location along the profile. /// /// /// The relative location in meter. /// public double RelativeLocation { get { return relativeLocation; } set { relativeLocation = value; RelativeLocationSpecified = true; } } /// /// Gets or sets the X rd. /// /// /// The X rd. /// public double XRd { get { return geometry.Coordinate.X; } set { geometry.Coordinate.X = value; } } /// /// Gets or sets the Y rd. /// /// /// The Y rd. /// public double YRd { get { return geometry.Coordinate.Y; } set { geometry.Coordinate.Y = value; } } /// /// Gets or sets the Z rd. Same as Depth?? /// /// /// The Z rd. /// public double ZRd { get { return geometry.Coordinate.Z; } set { geometry.Coordinate.Z = value; } } /// /// Gets a value indicating whether the relative location is specified. /// /// /// true if the relative location is specified; otherwise, false. /// public bool RelativeLocationSpecified { get; private set; } private SensorType type; /// /// Gets or sets the type of this sensor. /// /// /// The type. Default value is PiezoMetricHead. /// public SensorType Type { get { return type; } set { type = value; } } /// /// Gets or sets the PL line array. Used for serialization only. /// /// /// The PL line array. /// [Specification(typeof(NotNullSpecification))] [Specification(typeof(ContiansAtLeastOneItem))] public PLLineType[] PLLineMappings { get { return plLineTypes.ToArray(); } set { ClearPLLines(); foreach (var lineType in value) { Add(lineType); } } } public string PLLineMappingsAsString { get { string res = ""; foreach (var plLineType in plLineTypes) { switch (plLineType) { case PLLineType.PL1: res = res + "1; "; break; case PLLineType.PL2: res = res + "2; "; break; case PLLineType.PL3: res = res + "3; "; break; case PLLineType.PL4: res = res + "4; "; break; } } return res; } set { ClearPLLines(); var locPlLineTypes = ParseStringToPlLineTypes(value); foreach (var plLineType in locPlLineTypes) { switch (plLineType) { case 1: plLineTypes.Add(PLLineType.PL1); break; case 2: plLineTypes.Add(PLLineType.PL2); break; case 3: plLineTypes.Add(PLLineType.PL3); break; case 4: plLineTypes.Add(PLLineType.PL4); break; } } } } private List ParseStringToPlLineTypes(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; } /// /// Adds the specified PL line. /// /// The PL line. public void Add(PLLineType plLine) { plLineTypes.Add(plLine); } /// /// Removes the specified PL line. /// /// The PL line. public void Remove(PLLineType plLine) { plLineTypes.Remove(plLine); } /// /// Clears the PL lines list. /// public void ClearPLLines() { plLineTypes.Clear(); } /// /// Determines whether this instance is valid. /// /// /// true if this instance is valid; otherwise, false. /// public bool IsValid() { return !Validator.Validate(this).Any(); } /// /// Determines whether this instance is transient (associated with a correct ID in the context of the repository). /// /// /// true if this instance is transient; otherwise, false. /// public bool IsTransient() { return ID < 0; } public override string ToString() { var name = string.IsNullOrWhiteSpace(Name) ? "name_not_set" : Name; return string.Format("[ID: {0}, Name: {1}, Depth: {2}, Type: {3}, RelativeLocation: {4}]", ID, name, Depth, Type, RelativeLocation); } public bool IsVisible(string property) { switch (property) { case "XRd": return false; case "YRd": return false; case "ZRd": return false; default: return true; } } public bool IsEnabled(string property) { return true; } } }