tbf/TestBenchFramework/BenchControl/Operations/SetAllRegulValvesOp.cs
2015-04-15 20:32:56 +02:00

80 lines
2.7 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()"); }
IList<IRegulValve> regulValves;
int rvCount;
IOperation[] setRVPosOps;
public SetAllRegulValvesOp(IList<IRegulValve> regulValves, string encodedPositions, int timeout)
{
if (regulValves == null) throw new ArgumentNullException("regulValves");
this.regulValves = regulValves;
rvCount = regulValves.Count;
// Parse the positions
float[] positions = new float[rvCount];
string[] rvPosStr = (encodedPositions != null) ? encodedPositions.Split(new char[] { ';' }) : new string[0];
for (int i = 0; i < Math.Min(rvCount, rvPosStr.Length); i++)
{
float posPct;
if (float.TryParse(rvPosStr[i], out posPct) && posPct >= 0 && posPct <= 100.0f)
{
positions[i] = posPct;
}
}
setRVPosOps = new IOperation[rvCount];
for (int i = 0; i < rvCount; i++)
{
float posLo = Math.Max(positions[i] - 5.0f, 0);
float posHi = Math.Min(positions[i] + 5.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.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();
}
}
}