tbf/TestBenchFramework/BenchControl/Elde/RegulValve/SetRegulValvePositionOp.cs
Milan Hanajik fcfdfeca41 - COmponent properties 'Position' --> 'Idx0' or 'Idx1'
- process data logging, RegulVal position logging
- changes for I11+I12, NOK yet
2014-09-10 12:05:49 +02:00

132 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl.GenericDevices;
namespace TBF.BenchControl.Elde.RegulValve
{
public class SetRegulValvePositionOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(SetRegulValvePositionOp));
public override string ToString()
{
return string.Format("SetRegulValvePositionOp({0},{1},{2})", regulValve.Name, posLoPct, posHiPct);
}
/// Set by the constructor
readonly ControlBoardDev controlBoard;
readonly RegulValve regulValve;
readonly int regulValveNr;
readonly float posLoPct;
readonly float 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 SetRegulValvePositionOp(ControlBoardDev controlBoard, RegulValve regulValve, float posLoPct, float posHiPct, int timeout)
{
if (controlBoard == null) throw new ArgumentNullException("ctrlBoard");
this.controlBoard = controlBoard;
this.regulValve = regulValve as RegulValve;
if (regulValve == null) throw new ArgumentNullException("regValve is null or not Elde");
this.regulValveNr = this.regulValve.Idx1;
this.posLoPct = posLoPct;
this.posHiPct = posHiPct;
this.timeout = timeout;
log.Debug(this.ToString());
}
/// <summary>
/// Set required water flow - no timeout.
/// Events: FlowSet
/// </summary>
public SetRegulValvePositionOp(ControlBoardDev controlBoard, RegulValve 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}%", regulValveNr, 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;
controlBoard.ValveMove(regulValveNr,
RegulValveMode.TargetPosition,
new float[2] { posLoPct, posHiPct },
regulValve.StableTime);
return Event.None;
}
if (positionReached) return Event.PositionReached;
float positionPct = controlBoard.RValvePosition(regulValveNr);
log.WarnFormat("Run(): RV#={0}, actPos={1}%", regulValveNr, 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()
{
controlBoard.ValveMove(regulValveNr,
RegulValveMode.Stop,
new float[2] { posLoPct, posHiPct },
regulValve.StableTime);
}
}
}