// Copyright (C) Stichting Deltares 2025. 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 Deltares.DamEngine.Data.Standard;
namespace Deltares.DamEngine.Data.General.Sensors;
///
/// Class holding the repository for the sensors
///
public class SensorRepository
{
private readonly HashSet names;
private readonly HashSet ids;
private readonly SensorLocation sensorLocation;
///
/// Initializes a new instance of the class.
///
/// The location.
public SensorRepository(Location location) : this()
{
if (location.SensorLocation == null)
{
location.AddSensorLocation();
}
sensorLocation = location.SensorLocation;
if (sensorLocation.SensorGroup == null)
{
sensorLocation.SensorGroup = new SensorGroup();
}
}
private SensorRepository()
{
names = new HashSet();
ids = new HashSet();
}
///
/// Gets the sensors.
///
///
/// The sensors.
///
public IEnumerable Sensors
{
get
{
return SensorGroup.Selection;
}
}
///
/// Adds the collection of sensors to the sensorGroup.
///
/// The sensor needs to meet the business rules before it can be added
/// The sensor collection to add to the sensorGroup of this location.
///
///
public void Add(IEnumerable sensors)
{
if (sensors == null)
{
throw new ArgumentNullException("sensors");
}
foreach (Sensor sensor in sensors)
{
Add(sensor);
}
}
///
/// Adds the specified sensor.
///
/// The sensor needs to meet the business rules before it can be added
/// The sensor to add.
///
///
public void Add(Sensor sensor)
{
if (sensor == null)
{
throw new ArgumentNullException("sensor");
}
if (string.IsNullOrWhiteSpace(sensor.Name))
{
throw new ArgumentException("Cant add this sensor because its name is empty");
}
if (sensor.IsTransient())
{
throw new ArgumentException("Cant add this sensor because its ID is not valid. The ID must be greater then -1");
}
if (ids.Contains(sensor.ID))
{
throw new ArgumentException("Cant add this sensor becuase its ID is already added");
}
if (names.Contains(sensor.Name))
{
throw new ArgumentException("Cant add this sensor because its name is already used");
}
AddSensor(sensor);
}
///
/// Gets the sensor group.
///
///
/// The sensor group.
///
internal SensorGroup SensorGroup
{
get
{
return sensorLocation.SensorGroup;
}
}
///
/// Gets the unique sensor identifier.
///
///
internal int GetUniqueSensorId()
{
return SensorGroup.Selection.GetUniqueID(x => x.ID);
}
///
/// Adds the sensor unconditionally
///
/// The sensor.
private void AddSensor(Sensor sensor)
{
ids.Add(sensor.ID);
names.Add(sensor.Name);
sensorLocation.SensorGroup.Add(sensor);
}
}