/// /// 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 regulValves; int rvCount; IOperation[] setRVPosOps; public SetAllRegulValvesOp(IList 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); } } /// 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.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(); } } }