tbf/TestBenchFramework/BenchControl/Operations/SetAllRegulValvesOp.cs

75 lines
2.4 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl.GenericDevices;
namespace TBF.BenchControl.Operations
{
public class SetAllRegulValvesOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(SetAllRegulValvesOp));
public override string ToString() { return string.Format("SetAllRegulValvesOp()"); }
int rvCount;
IOperation[] setRVPosOps;
public SetAllRegulValvesOp(IList<IRegulValve> regulValves, IList<float> positions, int timeout)
{
if (regulValves == null || regulValves.Count == 0)
{
rvCount = 0;
}
else
{
rvCount = regulValves.Count;
if (positions == null || positions.Count != rvCount) throw new ArgumentNullException("positions");
setRVPosOps = new IOperation[rvCount];
for (int i = 0; i < rvCount; i++)
{
float posLo = Math.Max(positions[i] - 3.0f, 0);
float posHi = Math.Min(positions[i] + 3.0f, 100.0f);
setRVPosOps[i] = regulValves[i].SetRegulValvePositionOp(posLo, posHi, timeout);
}
}
}
/// <summary>Start this operation</summary>
public void Start()
{
for (int i = 0; i < rvCount; i++) setRVPosOps[i].Start();
}
/// <summary>Run this operation</summary>
public Event Run()
{
bool allDone = true;
bool anyError = false;
bool anyTimeout = false;
for (int i = 0; i < rvCount; i++)
{
Event evnt = setRVPosOps[i].Run();
if (evnt == Event.Error) anyError = true;
else if (evnt == Event.OpArgumentError) anyTimeout = true;
else if (evnt == Event.RegulValveTimeOut) anyTimeout = true;
else if (evnt != Event.PositionReached) allDone = false;
}
if (anyError) return Event.Error;
if (anyTimeout) return Event.Error;
if (allDone) return Event.AllPositionsReached;
return Event.None;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
for (int i = 0; i < rvCount; i++) setRVPosOps[i].Stop();
}
}
}