tbf/TBF/BenchControl/Elde/ExecuteCommandOp.cs

105 lines
2.9 KiB
C#

///
/// Copyright (c) 2016 Sensus Metering Systems
///
using System;
using log4net;
namespace TBF.BenchControl.Elde
{
public class ExecuteCommandOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(ExecuteCommandOp));
public override string ToString() { return string.Format("ExecuteCommandOp( command={0}, testMethod={1} )", commandOnStartOp, testMethod); }
/// Set by the constructor
readonly ControlBoardDev controlBoard;
readonly Command commandOnStartOp;
readonly Command commandOnStopOp;
readonly TestMethods testMethod;
bool started = false;
///
/// Internal states of this operation
///
enum OpState
{
StartingCommand,
CommandStarted,
OpCompleted,
}
OpState opState;
/// <summary>
/// Starts the measurement.
/// Events: MeasurementStarted
/// </summary>
/// <param name="controlBoard">Control board component reference</param>
/// <param name="outputPath">Specifies used components</param>
/// <param name="testMethod">Test method</param>
/// <param name="refPulses">Test duration in number of reference flowmeter pulses</param>
/// <param name="filterConstant">Specifies filter for watermeter pulses</param>
public ExecuteCommandOp(ControlBoardDev controlBoard, Command commandOnStartOp, Command commandOnStopOp, TestMethods testMethod)
{
if (controlBoard == null) throw new ArgumentNullException("controlBoardDev");
this.controlBoard = controlBoard;
this.commandOnStartOp = commandOnStartOp;
this.commandOnStopOp = commandOnStopOp;
this.testMethod = testMethod;
log.Debug(this.ToString());
}
/// <summary>Start this operation</summary>
public void Start()
{
controlBoard.SendCommand(commandOnStartOp, 0, 0, 0, testMethod, StopDevs.None);
opState = OpState.StartingCommand;
}
/// <summary>Run this operation</summary>
/// <returns>
/// Event.FlowInDone or Event.FlowOutDone
/// </returns>
public Event Run()
{
if (opState == OpState.StartingCommand)
{
StatusP statusP = controlBoard.StatusP;
log.DebugFormat("statusP = {0} {1} {2} {3}",
(((ulong)statusP >> 48) & 0xFFFF).ToString("X4"),
(((ulong)statusP >> 32) & 0xFFFF).ToString("X4"),
(((ulong)statusP >> 16) & 0xFFFF).ToString("X4"),
((ulong)statusP & 0xFFFF).ToString("X4"));
log.Info("Run() - StartingCommand");
opState = OpState.CommandStarted;
return Event.None;
}
else if (opState == OpState.CommandStarted)
{
log.InfoFormat("Run() - CommandStarted");
opState = OpState.OpCompleted;
return Event.None;
}
else if (opState == OpState.OpCompleted)
{
return Event.CommandCompleted;
}
return Event.None;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
if (commandOnStopOp != Command.None)
{
controlBoard.SendCommand(commandOnStopOp, 0, 0, 0, testMethod, StopDevs.None);
}
}
}
}