///
/// 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;
///
/// Starts the measurement.
/// Events: MeasurementStarted
///
/// Control board component reference
/// Specifies used components
/// Test method
/// Test duration in number of reference flowmeter pulses
/// Specifies filter for watermeter pulses
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());
}
/// Start this operation
public void Start()
{
controlBoard.SyncRoute();
controlBoard.SendCommand(commandOnStartOp, 0, controlBoard.Route, 0, 0, testMethod, 0, 20, 0, StopDevs.None);
opState = OpState.StartingCommand;
}
/// Run this operation
///
/// Event.FlowInDone or Event.FlowOutDone
///
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;
}
/// Stop this operation
public void Stop()
{
if (commandOnStopOp != Command.None)
{
controlBoard.SyncRoute();
controlBoard.SendCommand(commandOnStopOp, 0, controlBoard.Route, 0, 0, testMethod, 0, 20, 0, StopDevs.None);
}
}
}
}