216 lines
8.4 KiB
C#
216 lines
8.4 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using Dirichlet.Numerics;
|
|
using TBF.BenchControl.ControlBoard.Uni;
|
|
using TBF.BenchControl.Generic;
|
|
using TBF.Boxes;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.BenchControl.Uni.Diverter
|
|
{
|
|
public class Diverter : ComponentBase, IDevice, GenericDevices.IDiverter, GenericDevices.ISequenceCondition, GenericDevices.IHasCalendarEvents, SchematicDrawing.IDrawingItCmpnt
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Diverter));
|
|
public override string ToString() { return string.Format("Diverter({0})", Cfg.ToString(1)); }
|
|
|
|
|
|
/// <summary>
|
|
/// Enumeration of balances via static fields and methods
|
|
/// </summary>
|
|
static int nextIdx = 0;
|
|
public static new void ResetStaticProperties()
|
|
{
|
|
nextIdx = 0;
|
|
}
|
|
public static int DivertersCount { get { return nextIdx; } }
|
|
public static GenericDevices.IDiverter[] Diverters;
|
|
|
|
readonly DiverterCfg diverterCfg;
|
|
public SchematicDrawing.IDrawingItem DrawingItem { get { return diverterCfg as SchematicDrawing.IDrawingItem; } }
|
|
|
|
readonly UniCB cBoard;
|
|
readonly UInt128 mask; /// derived from bitPosition in the constructor
|
|
|
|
public int DiverterNr { get { return diverterCfg.DivNr1; } } /// 1-based public diverter number
|
|
public int AdcNr { get { return diverterCfg.AdcNr; } }
|
|
|
|
readonly IOperation diverterToTankOp;
|
|
readonly IOperation diverterToSinkOp;
|
|
|
|
|
|
public bool State { get { return (cBoard.Route & mask) != 0; } }
|
|
public int ThresholdGate { get { return diverterCfg.StartingLevel; } }
|
|
public int ThresholdLo { get { return diverterCfg.ThresholdLo; } }
|
|
public int ThresholdHi { get { return diverterCfg.ThresholdHi; } }
|
|
|
|
FloatBox switchTimeStart;
|
|
FloatBox switchTimeEnd;
|
|
public FloatBox SwitchTimeStart { get { return switchTimeStart; } }
|
|
public FloatBox SwitchTimeEnd { get { return switchTimeEnd; } }
|
|
|
|
/// <summary>
|
|
/// For a given flow in [m3/h] returns a test time correction in [s]
|
|
/// </summary>
|
|
public double TestTimeCorrection(double flow)
|
|
{
|
|
return Config.Formulas.GetCorrection(flow, Corrections);
|
|
}
|
|
|
|
|
|
public Diverter()
|
|
{
|
|
}
|
|
|
|
public Diverter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
diverterCfg = cfg as DiverterCfg;
|
|
|
|
cBoard = TbfComponents.FindComponent(cfg.ParentName, components) as UniCB;
|
|
if (cBoard == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
if (cfg.DebugLevel != Config.Entities.DebugMode.Off)
|
|
{
|
|
if (diverterCfg.BitNr < 0 || diverterCfg.BitNr >= 64) throw new ArgumentOutOfRangeException("position");
|
|
this.mask = (((UInt128)1) << diverterCfg.BitNr);
|
|
|
|
switchTimeStart = new FloatBox(diverterCfg.DfltSwithTimeStartMs / 1000.0f);
|
|
switchTimeEnd = new FloatBox(diverterCfg.DfltSwithTimeEndMs / 1000.0f);
|
|
|
|
///// Prepare arguments of SendCalibData()
|
|
//if (diverterCfg.AdcNr >= 0 && diverterCfg.AdcNr < cBoard.RegValveCalib.GetLength(0))
|
|
//{
|
|
// cBoard.RegValveCalib[diverterCfg.AdcNr, 0] = (uint)diverterCfg.AdcValueToSink;
|
|
// cBoard.RegValveCalib[diverterCfg.AdcNr, 1] = (uint)diverterCfg.AdcValueToTank;
|
|
//}
|
|
//else
|
|
//{
|
|
// log.ErrorFormat("Unable to set {0} ADC levels: RegValveCalib array size={1}, diverter AdcNr={2}",
|
|
// Name, cBoard.RegValveCalib.GetLength(0), diverterCfg.AdcNr);
|
|
//}
|
|
|
|
diverterToTankOp = new SwitchDiverterOp(cBoard, true, DiverterNr);
|
|
diverterToSinkOp = new SwitchDiverterOp(cBoard, false, DiverterNr);
|
|
|
|
log.Warn(this.ToString());
|
|
}
|
|
}
|
|
|
|
///
|
|
/// Calendar support
|
|
///
|
|
public IList<Config.CalendarEvent.ICalendarEvent> GetCalendarEvents()
|
|
{
|
|
DateTime calibrationDue = diverterCfg.CalibValidDate;
|
|
IList<Config.CalendarEvent.ICalendarEvent> calendarEvents = new List<Config.CalendarEvent.ICalendarEvent>();
|
|
|
|
if (calibrationDue > TBF.UI.Constants.MinDate)
|
|
{
|
|
/// Calibration due date calendar event
|
|
calendarEvents.Add(new TBF.UI.Calendar.CalibrationDueDateEvent(calibrationDue.Date, Name,
|
|
string.Format(Strings.Calibration_due_date_is_0,
|
|
calibrationDue.Date.ToString(TBF.UI.Constants.DateFormat))));
|
|
if (DateTime.Now.Date <= calibrationDue.AddDays(-7))
|
|
{
|
|
/// Weekly reminders (last 5 weeks)
|
|
calendarEvents.Add(new TBF.UI.Calendar.CalibrationReminderEvent(calibrationDue.Date.AddDays(-35), Name,
|
|
string.Format(Strings.Calibration_due_date_is_0,
|
|
calibrationDue.Date.ToString(TBF.UI.Constants.DateFormat)),
|
|
false));
|
|
}
|
|
if (DateTime.Now.Date <= calibrationDue.Date)
|
|
{
|
|
/// Daily reminders (last 5 days)
|
|
calendarEvents.Add(new TBF.UI.Calendar.CalibrationReminderEvent(calibrationDue.AddDays(-5), Name,
|
|
string.Format(Strings.Calibration_due_date_is_0,
|
|
calibrationDue.Date.ToString(TBF.UI.Constants.DateFormat)),
|
|
true));
|
|
}
|
|
}
|
|
return calendarEvents;
|
|
}
|
|
|
|
|
|
///
|
|
/// IDevice interface implementation
|
|
///
|
|
public void Initialize() { }
|
|
public void RunDeviceBefore()
|
|
{
|
|
Int16 adcValue = cBoard.Data.AnalogInput[diverterCfg.AdcNr];
|
|
Handlers.OnAdcChanged(this, new CmdResponseArgs(CfgChangeCmd.GetAdc, diverterCfg.BitNr, adcValue));
|
|
}
|
|
public void RunDeviceAfter() { }
|
|
public void StopDevice() { }
|
|
public void StopDevice2() { }
|
|
|
|
|
|
///
|
|
/// ISequenceCondition interface implementation
|
|
///
|
|
public int ConditionsCount { get { return 2; } }
|
|
|
|
/// Returned strings are added to the combo-box
|
|
public string ConditionName(int i)
|
|
{
|
|
if (i == 0) return string.Format("{0} {1}", Name, Strings.Start);
|
|
else return string.Format("{0} {1}", Name, Strings.End);
|
|
}
|
|
|
|
public IOperation ConditionOp(int i)
|
|
{
|
|
if (i == 0) return diverterToTankOp;
|
|
else return diverterToSinkOp;
|
|
}
|
|
|
|
|
|
#region Configuration Change Handling
|
|
|
|
public static void OnCfgChange(object sender, CfgChangeArgs args)
|
|
{
|
|
if (CfgChangeHandler == null) return;
|
|
try { CfgChangeHandler(sender, args); }
|
|
catch (Exception e) { log.Error("CfgChangeHandler(...) failed", e); }
|
|
}
|
|
|
|
public static event EventHandler<CfgChangeArgs> CfgChangeHandler;
|
|
|
|
public override void StartChangeHandler()
|
|
{
|
|
CfgChangeHandler += delegate(object sender, CfgChangeArgs args)
|
|
{
|
|
DiverterCfg tmpcfg = args.Cfg as DiverterCfg;
|
|
if (tmpcfg != null && tmpcfg.Name.Equals(Name))
|
|
{
|
|
if (args.Command == CfgChangeCmd.CfgChange)
|
|
{
|
|
}
|
|
else if (args.Command == CfgChangeCmd.GetAdc1 || args.Command == CfgChangeCmd.GetAdc2)
|
|
{
|
|
Int16 adcValue = cBoard.Data.AnalogInput[diverterCfg.AdcNr];
|
|
DiverterCfgCtrl.OnCmdResponse(this, new CmdResponseArgs(args.Command, diverterCfg.BitNr, adcValue));
|
|
}
|
|
else if (args.Command == CfgChangeCmd.DivToTank)
|
|
{
|
|
cBoard.DiverterToTank(diverterCfg.DivNr1);
|
|
}
|
|
else if (args.Command == CfgChangeCmd.DivToSink)
|
|
{
|
|
cBoard.DiverterToSink(diverterCfg.DivNr1);
|
|
}
|
|
else if (args.Command == CfgChangeCmd.ReadDivTransition)
|
|
{
|
|
cBoard.ReadDiverterTransition(diverterCfg.DivNr1);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#endregion Configuration Change Handling
|
|
}
|
|
}
|