///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl.GenericDevices;
namespace TBF.BenchControl.Dummy.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);
}
const int CoaxValveNr = 7; /// Coax. valve has number 7
///
/// Internal states of this operation
///
enum OpState
{
Idle = 0,
MoveToPosition, /// Send commend to move to position
CheckState, /// Check RV state by reading statos word
MovingToPosition, /// Command passed OK, RV is moving to a position
}
OpState opState;
/// Set by the constructor
readonly RegulValve regulValve;
readonly float posLoPct;
readonly float posHiPct;
readonly int timeout;
/// Process values
bool firstRun;
int expireTime;
bool positionReached;
///
/// Set required water flow.
/// Events: FlowSet, FlowTimeOut
///
/// Control board device
/// Regulation valve component
/// Lower limit of the position to be achieved
/// Upper limit of the position to be achieved
/// Timeout in sec. for setting the flow
/// Only Elde.Valve flow are used, other flow on the lists are ignored
public SetRegulValvePositionOp(RegulValve regulValve, float posLoPct, float posHiPct, int timeout)
{
this.regulValve = regulValve as RegulValve;
if (regulValve == null) throw new ArgumentNullException("regValve is null or not Elde");
this.posLoPct = posLoPct;
this.posHiPct = posHiPct;
this.timeout = timeout;
log.Debug(this.ToString());
}
///
/// Set required water flow - no timeout.
/// Events: FlowSet
///
public SetRegulValvePositionOp(RegulValve regValve, float posLoPct, float posHiPct)
: this(regValve, posLoPct, posHiPct, int.MaxValue)
{
}
/// Start this operation
public void Start()
{
firstRun = true;
positionReached = false;
expireTime = StateMachine.Time + timeout;
log.InfoFormat("Start(): RV#={0}, reqPosLo={1}%, reqPosHi={2}%", posLoPct.ToString("F1"), posHiPct.ToString("F1"));
opState = OpState.MoveToPosition;
}
/// Run this operation
///
/// Event.None . . . . . . . busy adjusting position
/// Event.PositionReached . . position reached
/// Event.OpArgumentError . . invalid required position
///
public Event Run()
{
if (opState == OpState.MoveToPosition)
{
opState = OpState.CheckState;
return Event.None;
}
if (positionReached) return Event.PositionReached;
if (opState == OpState.CheckState)
{
opState = OpState.MovingToPosition;
return Event.None;
}
else if (opState == OpState.MovingToPosition)
{
positionReached = true;
return Event.PositionReached;
}
return Event.None;
}
/// Stop this operation
public void Stop()
{
}
}
}