165 lines
5.8 KiB
C#
165 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using CtrEditor.ObjetosSim;
|
|
|
|
namespace CtrEditor.HydraulicSimulator.TSNet.Components
|
|
{
|
|
/// <summary>
|
|
/// Adaptador simplificado para osHydTank - SOLO lectura de configuración y almacenamiento de resultados
|
|
/// Los valores se capturan al INICIAR la simulación y no se modifican durante la ejecución
|
|
/// </summary>
|
|
public class TSNetTankAdapter
|
|
{
|
|
private readonly osHydTank tank;
|
|
private string tankId;
|
|
|
|
// Configuración capturada al inicio de simulación (INMUTABLE durante simulación)
|
|
public TankConfiguration Configuration { get; private set; }
|
|
|
|
// Resultados calculados por TSNet (SOLO escritura desde TSNet)
|
|
public TSNetTankResults Results { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Acceso al objeto tanque hidráulico subyacente
|
|
/// </summary>
|
|
public osHydTank Tank => tank;
|
|
|
|
public TSNetTankAdapter(osHydTank tank)
|
|
{
|
|
this.tank = tank ?? throw new ArgumentNullException(nameof(tank));
|
|
|
|
// Validación defensiva para Id
|
|
if (tank.Id == null)
|
|
{
|
|
throw new InvalidOperationException($"El tanque '{tank.Nombre ?? "sin nombre"}' no tiene un Id válido");
|
|
}
|
|
|
|
this.tankId = $"TANK_{tank.Id.Value}";
|
|
this.Results = new TSNetTankResults { TankId = tankId };
|
|
}
|
|
|
|
/// <summary>
|
|
/// ID único del tanque para TSNet
|
|
/// </summary>
|
|
public string TankId => tankId;
|
|
|
|
/// <summary>
|
|
/// Captura la configuración actual del tanque al INICIO de la simulación
|
|
/// Esta configuración queda CONGELADA hasta que se detenga y reinicie la simulación
|
|
/// </summary>
|
|
public void CaptureConfigurationForSimulation()
|
|
{
|
|
Configuration = new TankConfiguration
|
|
{
|
|
TankId = tankId,
|
|
MinLevelM = tank.MinLevelM,
|
|
MaxLevelM = tank.MaxLevelM,
|
|
InitialLevelM = tank.CurrentLevelM, // Usar CurrentLevelM en lugar de InitialLevelM
|
|
DiameterM = 1.0, // Valor por defecto - no existe DiameterM en osHydTank
|
|
TankPressure = tank.TankPressure,
|
|
IsFixedPressure = tank.IsFixedPressure,
|
|
Position = new TankPosition { X = tank.Left, Y = tank.Top },
|
|
CapturedAt = DateTime.Now
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resetea los resultados (para nueva simulación)
|
|
/// </summary>
|
|
public void ResetCalculatedValues()
|
|
{
|
|
Results = new TSNetTankResults { TankId = tankId };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validación de configuración capturada
|
|
/// </summary>
|
|
public List<string> ValidateConfiguration()
|
|
{
|
|
var errors = new List<string>();
|
|
|
|
if (Configuration == null)
|
|
{
|
|
errors.Add($"Tanque {TankId}: Configuración no capturada");
|
|
return errors;
|
|
}
|
|
|
|
if (Configuration.MinLevelM < 0)
|
|
errors.Add($"Tanque {TankId}: MinLevelM no puede ser negativo");
|
|
|
|
if (Configuration.MaxLevelM <= Configuration.MinLevelM)
|
|
errors.Add($"Tanque {TankId}: MaxLevelM debe ser mayor que MinLevelM");
|
|
|
|
if (Configuration.InitialLevelM < Configuration.MinLevelM || Configuration.InitialLevelM > Configuration.MaxLevelM)
|
|
errors.Add($"Tanque {TankId}: InitialLevelM debe estar entre MinLevelM y MaxLevelM");
|
|
|
|
if (Configuration.DiameterM <= 0)
|
|
errors.Add($"Tanque {TankId}: DiameterM debe ser mayor que 0");
|
|
|
|
return errors;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
var configStatus = Configuration != null ? "Configured" : "Not Configured";
|
|
var resultStatus = Results?.Status ?? "No Results";
|
|
return $"TSNetTankAdapter[{TankId}] - Config: {configStatus}, Results: {resultStatus}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aplica los resultados de TSNet al tanque
|
|
/// </summary>
|
|
public void ApplyTSNetResults(Dictionary<string, double> flows, Dictionary<string, double> pressures)
|
|
{
|
|
if (Results == null) return;
|
|
|
|
try
|
|
{
|
|
// Actualizar resultados desde los diccionarios de TSNet
|
|
if (flows.ContainsKey(TankId))
|
|
{
|
|
Results.NetFlowM3s = flows[TankId];
|
|
}
|
|
|
|
if (pressures.ContainsKey(TankId))
|
|
{
|
|
Results.CalculatedPressureBar = pressures[TankId] / 100000.0; // Pa a bar
|
|
}
|
|
|
|
Results.Timestamp = DateTime.Now;
|
|
Results.Status = "Updated from TSNet";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Results.Status = $"Error: {ex.Message}";
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configuración inmutable del tanque capturada al inicio de simulación
|
|
/// </summary>
|
|
public class TankConfiguration
|
|
{
|
|
public string TankId { get; set; }
|
|
public DateTime CapturedAt { get; set; }
|
|
|
|
// Propiedades físicas del tanque
|
|
public double MinLevelM { get; set; }
|
|
public double MaxLevelM { get; set; }
|
|
public double InitialLevelM { get; set; }
|
|
public double DiameterM { get; set; }
|
|
public double TankPressure { get; set; }
|
|
public bool IsFixedPressure { get; set; }
|
|
public TankPosition Position { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Posición del tanque en el canvas
|
|
/// </summary>
|
|
public class TankPosition
|
|
{
|
|
public double X { get; set; }
|
|
public double Y { get; set; }
|
|
}
|
|
} |