///
/// Copyright (c) 2013-2017 Sensus Metering Systems
///
using System;
using log4net;
using TBF.Boxes;
namespace TBF.BenchControl.MettlerToledo.Standard
{
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("balanceDev");
this.scale = scale;
this.result = result;
log.Debug(this.ToString());
}
/// Start this operation
public void Start()
{
scale.GetImmediateMassMeasurement();
started = true;
done = false;
}
/// Run this operation
///
/// Event.None
/// Event.BalanceDone
/// Event.Error . . . . . Current.Tick == null
///
public Event Run()
{
scale.MsrmntTime++;
if (!started)
{
scale.GetImmediateMassMeasurement();
started = true;
done = false;
return Event.Busy;
}
else if (started && scale.MsrmntState == MsrmntState.Busy)
{
/// Measurement is in progress
if (scale.MsrmntTime >= 2)
{
/// Timeout - no response from the scale within 2 seconds - send command again
scale.GetImmediateMassMeasurement();
started = true;
done = false;
}
return Event.Busy;
}
else
{
/// Measurement completed or failed: (started == true) && (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.BalanceDone; /// Mass read OK
}
}
/// Stop this operation
public void Stop()
{
}
}
}