/// /// 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 regulValves, IList 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); } } } /// Start this operation public void Start() { for (int i = 0; i < rvCount; i++) setRVPosOps[i].Start(); } /// Run this operation 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; } /// Stop this operation public void Stop() { for (int i = 0; i < rvCount; i++) setRVPosOps[i].Stop(); } } }