127 lines
4.1 KiB
C#
127 lines
4.1 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using TBF.Rig.GenericDevices;
|
|
using TBF.Rig.Uni;
|
|
|
|
namespace TBF.Rig.Uni.RegValveTandem
|
|
{
|
|
public class SetRegValvePositionOp : IOperation
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(SetRegValvePositionOp));
|
|
public override string ToString()
|
|
{
|
|
return string.Format("SetRegValvePositionOp({0},{1},{2})", regValve.Name, posLoPct, posHiPct);
|
|
}
|
|
|
|
/// Set by the constructor
|
|
readonly TBF.Rig.ControlBoard.Uni.UniCB uniCB;
|
|
readonly RegValve.RegValve regValve;
|
|
readonly double posLoPct;
|
|
readonly double posHiPct;
|
|
readonly int timeout;
|
|
|
|
/// Process values
|
|
bool firstRun;
|
|
int expireTime;
|
|
bool positionReached;
|
|
|
|
/// <summary>
|
|
/// Set required water flow.
|
|
/// Events: FlowSet, FlowTimeOut
|
|
/// </summary>
|
|
/// <param name="ControlBoard">Control board device</param>
|
|
/// <param name="_regulValve">Regulation valve component</param>
|
|
/// <param name="posLoPct">Lower limit of the position to be achieved</param>
|
|
/// <param name="posHiPct">Upper limit of the position to be achieved</param>
|
|
/// <param name="timeout">Timeout in sec. for setting the flow</param>
|
|
/// <remarks>Only Elde.Valve flow are used, other flow on the lists are ignored</remarks>
|
|
public SetRegValvePositionOp(TBF.Rig.ControlBoard.IControlBoard cb, IRegValve regV, double posLoPct, double posHiPct, int timeout)
|
|
{
|
|
uniCB = cb as TBF.Rig.ControlBoard.Uni.UniCB;
|
|
if (uniCB == null) throw new ArgumentNullException("ctrlBoard");
|
|
|
|
regValve = regV as RegValve.RegValve;
|
|
if (regValve == null) throw new ArgumentNullException("regValve is null or not Uni");
|
|
|
|
this.posLoPct = posLoPct;
|
|
this.posHiPct = posHiPct;
|
|
this.timeout = timeout;
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set required water flow - no timeout.
|
|
/// Events: FlowSet
|
|
/// </summary>
|
|
public SetRegValvePositionOp(TBF.Rig.ControlBoard.IControlBoard controlBoard, RegValveTandem regValve, float posLoPct, float posHiPct)
|
|
: this(controlBoard, regValve, posLoPct, posHiPct, int.MaxValue)
|
|
{
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Start()
|
|
{
|
|
firstRun = true;
|
|
positionReached = false;
|
|
expireTime = StateMachine.Time + timeout;
|
|
log.InfoFormat("Start(): RV#={0}, reqPosLo={1}%, reqPosHi={2}%", regValve.Idx1, posLoPct.ToString("F1"), posHiPct.ToString("F1"));
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>
|
|
/// Event.None . . . . . . . busy adjusting position
|
|
/// Event.PositionReached . . position reached
|
|
/// Event.OpArgumentError . . invalid required position
|
|
/// </returns>
|
|
public Event Run()
|
|
{
|
|
if (firstRun)
|
|
{
|
|
if (posLoPct >= posHiPct || posLoPct >= 100.0f || posHiPct <= 0)
|
|
{
|
|
return Event.OpArgumentError;
|
|
}
|
|
|
|
firstRun = false;
|
|
|
|
uniCB.RegVlvMoveToPos(false, regValve.Idx1, posLoPct, posHiPct);
|
|
return Event.None;
|
|
}
|
|
|
|
if (positionReached) return Event.PositionReached;
|
|
|
|
double positionPct = regValve.Position;
|
|
log.WarnFormat("Run(): RV#={0}, actPos={1}%", regValve.Idx1, positionPct.ToString("F1"));
|
|
|
|
if (posLoPct >= posHiPct || posLoPct >= 100.0f || posHiPct <= 0)
|
|
{
|
|
return Event.OpArgumentError;
|
|
}
|
|
else if (posLoPct <= positionPct && positionPct <= posHiPct)
|
|
{
|
|
positionReached = true;
|
|
return Event.PositionReached;
|
|
}
|
|
else if (StateMachine.Time > expireTime)
|
|
{
|
|
return Event.RegulValveTimeOut;
|
|
}
|
|
else
|
|
{
|
|
return Event.None;
|
|
}
|
|
}
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
public void Stop()
|
|
{
|
|
uniCB.StopAll(false);
|
|
}
|
|
}
|
|
}
|