///
/// Copyright (c) 2013-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
namespace TBF.Rig.MettlerToledo.Standard
{
public class SetUnitsOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(SetUnitsOp));
public override string ToString() { return string.Format("SetUnitsOp({0})", units); }
/// Set by the constructor
BalanceDev scale;
Common.Unit units;
bool activityStarted;
///
/// Events: BalanceDone, Error
///
/// Balance device instance
/// Reference to the serialNumber
public SetUnitsOp(BalanceDev scale, Common.Unit units)
{
if (scale == null) throw new ArgumentNullException("balanceDev");
this.scale = scale;
this.units = units;
log.Debug(this.ToString());
}
/// Start this operation
public void Start()
{
log.DebugFormat("{0} - SetUnitsOp - Start()", scale.Name);
if (scale.Activity == Activity.Idle)
{
scale.SendSetUnitsCmd(units);
activityStarted = true;
scale.Activity = Activity.RunningOperation;
}
else
{
activityStarted = false;
}
}
/// Run this operation
///
/// Event.None
/// Event.BalanceDone
///
public Event Run()
{
if (!activityStarted && scale.Activity == Activity.Idle)
{
scale.SendSetUnitsCmd(units);
activityStarted = true;
scale.Activity = Activity.RunningOperation;
return Event.None;
}
if (scale.MsrmntState == MsrmntState.Valid) return Event.BalanceDone; /// Units set
if (scale.MsrmntState == MsrmntState.Failed) return Event.Error; /// Failed
return Event.None; /// No result yet
}
/// Stop this operation
public void Stop()
{
if (scale.MsrmntState == MsrmntState.Busy) scale.MsrmntState = MsrmntState.Failed;
scale.Activity = Activity.Idle;
log.DebugFormat("{0} - SetUnitsOp - Stop()", scale.Name);
}
}
}