tbf/TestBenchFramework/BenchControl/MettlerToledo/Multi/ReadMassOp.cs

82 lines
2.0 KiB
C#

///
/// Copyright (c) 2015-2016 Sensus Metering Systems
///
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 scale;
DoubleBox result;
/// <summary>
/// Events: BalanceDone, Error
/// </summary>
/// <param name="scale">Balance device instance</param>
/// <param name="requiredReadingsCount">Required mass readings count (>= 3)</param>
/// <param name="result">Reference to the measured mass in kg</param>
public ReadMassOp(BalanceDev scale, ref DoubleBox result)
{
if (scale == null) throw new ArgumentNullException("scale");
this.scale = scale;
this.result = result;
log.Debug(this.ToString());
}
/// <summary>Start this operation</summary>
public void Start()
{
done = false;
started = BalanceDev.GetImmediateMassMeasurement(scale.IdNr, scale.Name);
}
/// <summary>Run this operation</summary>
/// <returns>
/// Event.None
/// Event.BalanceDone
/// Event.Error . . . . . Current.Tick == null
/// </returns>
public Event Run()
{
if (!started)
{
started = BalanceDev.GetImmediateMassMeasurement(scale.IdNr, scale.Name);
if (!done)
return Event.Busy;
else
return Event.BalanceDone;
}
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.BalanceDone; /// Mass read OK
}
else
{
return Event.Busy;
}
}
/// <summary>Stop this operation</summary>
public void Stop()
{
}
}
}