///
/// Copyright (c) 2015 Sensus Metering Systems
/// Author: Milan Hanajik
///
using System;
using log4net;
using TBF.Boxes;
namespace TBF.BenchControl.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 balanceDev;
FloatBox result;
///
/// Events: BalanceDone, Error
///
/// Balance device instance
/// Required mass readings count (>= 3)
/// Reference to the measured mass in kg
public ReadMassOp(BalanceDev balanceDev, ref FloatBox result)
{
if (balanceDev == null) throw new ArgumentNullException("balanceDev");
this.balanceDev = balanceDev;
this.result = result;
log.Debug(this.ToString());
}
/// Start this operation
public void Start()
{
done = false;
started = BalanceDev.GetImmediateMassMeasurement(balanceDev.IdNr, balanceDev.Name);
}
/// Run this operation
///
/// Event.None
/// Event.BalanceDone
/// Event.Error . . . . . Current.Tick == null
///
public Event Run()
{
if (!started)
{
started = BalanceDev.GetImmediateMassMeasurement(balanceDev.IdNr, balanceDev.Name);
if (!done) return Event.Busy;
else return Event.BalanceDone;
}
else if (started && balanceDev.MsrmntState == MsrmntState.Valid)
{
result.Val = balanceDev.Mass;
log.InfoFormat("ReadMassOp.Run() ... mass={0} ... returning Event.BalanceDone", balanceDev.Mass);
done = true;
started = false;
return Event.BalanceDone; /// Mass read OK
}
else if (done)
{
return Event.BalanceDone; /// Mass has already been read (at least once)
}
else
{
return Event.Busy;
}
}
/// Stop this operation
public void Stop()
{
}
}
}