tbf/TBF/Rig/Elde/StopPreviousOp.cs
2021-10-26 19:38:09 +02:00

107 lines
2.7 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using log4net;
namespace TBF.Rig.Elde
{
public class StopPreviousOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(StopPreviousOp));
public override string ToString() { return string.Format("StopPreviousOp(.)"); }
/// Set by the constructor
readonly ControlBoardDev controlBoard;
///
/// Internal states of this operation
///
enum OpState
{
Idle = 0,
CheckIfStopped,
ResendCommand,
Stopped,
}
OpState opState;
/// <summary>
/// Stop the previous control board operation (diverter, gate, etc.)
/// Events: None or PreviousStopped
/// </summary>
/// <param name="controlBoardDev">Control board component reference</param>
public StopPreviousOp(ControlBoardDev controlBoardDev)
{
if (controlBoardDev == null) throw new ArgumentNullException("controlBoardDev");
this.controlBoard = controlBoardDev;
log.Debug(this.ToString());
}
void SendStopCommand()
{
controlBoard.SendCommand(Command.Stop, 0, 0, 0, TestMethods.Diverter,
StopDevs.Diverter | StopDevs.GatePulse | StopDevs.Reference | StopDevs.RegValveRegulation);
}
/// <summary>Start this operation</summary>
public void Start()
{
SendStopCommand();
opState = OpState.CheckIfStopped;
}
/// <summary>Run this operation</summary>
/// <returns>
/// Event.None or Event.PreviousStopped
/// </returns>
public Event Run()
{
if (opState == OpState.CheckIfStopped)
{
if ((controlBoard.StatusP & (StatusP.TestCompleted | StatusP.TestInProgress)) == 0)
{
opState = OpState.Stopped;
return Event.PreviousStopped;
}
else
{
opState = OpState.ResendCommand;
return Event.None;
}
}
else if (opState == OpState.ResendCommand)
{
if ((controlBoard.StatusP & (StatusP.TestCompleted | StatusP.TestInProgress)) == 0)
{
opState = OpState.Stopped;
return Event.PreviousStopped;
}
else
{
SendStopCommand();
opState = OpState.CheckIfStopped;
return Event.None;
}
}
else if (opState == OpState.Stopped)
{
return Event.PreviousStopped;
}
return Event.None;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
opState = OpState.Idle;
}
}
}