tbf/TestBenchFramework/BenchControl/Operations/TimerOp.cs
2015-01-12 07:43:54 +01:00

96 lines
2.6 KiB
C#

using log4net;
namespace TBF.BenchControl.Operations
{
/// <summary>
/// Implements timer function
///
/// Constructors:
/// Timer(timerTimeMs) ................ use Event.TimerExpired as eventExpired
/// Timer(timerTimeMs, eventExpired) .. use constructor argument as eventExpired
///
/// Start argument:
///
/// Events:
/// Event.None ........ operation is in progress
/// eventExpired ...... timer expired
/// </summary>
public class TimerOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(TimerOp));
public override string ToString() { return string.Format("TimerOp({0}s, Event.{1})", timerTime, eventExpired); }
/// Private fields
bool timeExpired = false;
int expireTime;
/// Set by constructors
int timerTime;
Event eventExpired;
TBF.Boxes.IntBox remainingTimeSec;
/// <summary>
/// Constructors, optional argument is a timer name (you can create more then one timer)
/// </summary>
public TimerOp(int timerTimeSec, Event eventExpired, TBF.Boxes.IntBox remainingTimeSec)
{
this.timerTime = timerTimeSec;
this.eventExpired = eventExpired;
this.remainingTimeSec = remainingTimeSec;
}
public TimerOp(int timerTimeSec, Event eventExpired)
: this(timerTimeSec, eventExpired, null)
{
}
public TimerOp(int timerTime, TBF.Boxes.IntBox remainingTimeSec)
: this(timerTime, Event.TimerExpired, remainingTimeSec)
{
}
public TimerOp(int timerTime)
: this(timerTime, Event.TimerExpired, null)
{
}
void UpdateRemainingTime()
{
if (remainingTimeSec != null)
{
remainingTimeSec.Val = System.Math.Max(expireTime - StateMachine.Time, 0);
}
}
/// <param name="obj">Time period (TimeSpan)</param>
public void Start()
{
timeExpired = false;
expireTime = StateMachine.Time + timerTime;
UpdateRemainingTime();
}
public Event Run()
{
UpdateRemainingTime();
if (timeExpired)
{
return eventExpired;
}
else if (StateMachine.Time >= expireTime)
{
timeExpired = true;
return eventExpired;
}
else
{
return Event.TimerBusy;
}
}
public void Stop()
{
timeExpired = false;
}
}
}