///
/// Copyright (c) 2015-2016 Sensus Metering Systems
///
using System;
using log4net;
using TBF.Boxes;
namespace TBF.Rig.MettlerToledo.Multi
{
public class ReadMassOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(ReadMassOp));
public override string ToString() { return string.Format("ReadMassOp(.,.)"); }
bool started;
bool done;
/// Set by the constructor
BalanceDev scale;
DoubleBox result;
///
/// Events: BalanceDone, Error
///
/// Balance device instance
/// Required mass readings count (>= 3)
/// Reference to the measured mass in kg
public ReadMassOp(BalanceDev scale, ref DoubleBox result)
{
if (scale == null) throw new ArgumentNullException("scale");
this.scale = scale;
this.result = result;
log.Debug(this.ToString());
}
/// Start this operation
public void Start()
{
done = false;
started = BalanceDev.GetImmediateMassMeasurement(scale.IdNr, scale.Name);
}
/// Run this operation
///
/// Event.None
/// Event.BalanceDone
/// Event.Error . . . . . Current.Tick == null
///
public Event Run()
{
if (!started)
{
started = BalanceDev.GetImmediateMassMeasurement(scale.IdNr, scale.Name);
if (!done)
return Event.Busy;
else
return Event.ScaleDone;
}
else if (started && scale.MsrmntState != MsrmntState.Busy)
{
if (scale.MsrmntState == MsrmntState.Valid) result.Val = scale.Mass;
log.InfoFormat("ReadMassOp.Run() ... state={0}, mass={1} ... returning Event.BalanceDone",
scale.MsrmntState, scale.Mass);
done = true;
started = false;
return Event.ScaleDone; /// Mass read OK
}
else
{
return Event.Busy;
}
}
/// Stop this operation
public void Stop()
{
}
}
}