///
/// Copyright (c) 2013-2020 Sensus Slovensko a.s.
///
using System;
using log4net;
namespace TBF.Rig.MettlerToledo.Standard
{
public class ZeroOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(ZeroOp));
public override string ToString() { return string.Format("ZeroOp(.,.)"); }
bool done;
/// Set by the constructor
BalanceDev scale;
bool activityStarted;
///
/// Events: BalanceDone, Error
///
/// Balance device instance
/// Reference to variable for 'tara' in kg
public ZeroOp(BalanceDev scale)
{
if (scale == null) throw new ArgumentNullException("balanceDev");
this.scale = scale;
log.Debug(this.ToString());
}
/// Start this operation
public void Start()
{
log.DebugFormat("{0} - ZeroOp - Start()", scale.Name);
done = false;
if (scale.Activity == Activity.Idle)
{
scale.SendZeroWhenStableCmd();
activityStarted = true;
scale.Activity = Activity.RunningOperation;
}
else
{
activityStarted = false;
}
}
/// Run this operation
///
/// Event.None
/// Event.BalanceDone
/// Event.Error . . . . . Current.Tick == null
///
public Event Run()
{
if (!activityStarted && scale.Activity == Activity.Idle)
{
scale.SendZeroWhenStableCmd();
activityStarted = true;
scale.Activity = Activity.RunningOperation;
return Event.Busy;
}
if (done) return Event.ScaleDone; /// Zeroing has been done already
if (scale.MsrmntState == MsrmntState.Busy) return Event.Busy;
if (scale.MsrmntState == MsrmntState.Overload)
{
done = true;
return Event.ScaleOverload;
}
if (scale.MsrmntState == MsrmntState.Valid)
{
done = true;
return Event.ScaleDone; /// Zeroing completed
}
scale.SendZeroWhenStableCmd(); /// Failed -> repeat zeroing
return Event.Busy;
}
/// Stop this operation
public void Stop()
{
if (scale.MsrmntState == MsrmntState.Busy) scale.MsrmntState = MsrmntState.Failed;
scale.Activity = Activity.Idle;
log.DebugFormat("{0} - ZeroOp - Stop()", scale.Name);
}
}
}