tbf/TBF/Rig/RegisterReaders/KPackE/RegisterReader/RegisterReader.cs

143 lines
4.0 KiB
C#

///
/// Copyright (c) 2018-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using Common;
using TBF.Boxes;
namespace TBF.Rig.RegisterReaders.KPackE.RegisterReader
{
public class RegisterReader : ComponentBase, Generic.IDevice, GenericDevices.IRegReader, IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(RegisterReader));
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
readonly RRCfg rrCfg;
readonly Radio.Radio radio;
readonly TBF.Rig.ControlBoard.Uni.UniCB uniCB;
public int Position
{
get
{
int firstDigitPos = Name.IndexOfAny(new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' });
int position;
return (firstDigitPos < 0) ? 0 : (int.TryParse(Name.Substring(firstDigitPos), out position) ? position : 0);
}
}
public RegisterReaderType RegisterReaderType { get { return RegisterReaderType.Manual; } }
public double PulsesPerLtr {
get { return rrCfg.ProcParams.PulsesPerLtr; }
set { rrCfg.ProcParams.PulsesPerLtr = value; }
}
public double LtrsPerPulse { get { return (PulsesPerLtr <= float.Epsilon) ? 1.0 : (1 / PulsesPerLtr); } }
public string QuantityUnits { get; set; }
/// <summary>
/// Writen to by DataEntryForRadio component
/// </summary>
public string SerialNr; /// Serial number of the respective water meter
public bool IsEndOfTest; /// false at the beginning oftest, true at the end of test
/// <summary>
/// Called by Radio when data for SerialNr were received
/// </summary>
/// <param name="wmState"></param>
public void WMStateReceived(int wmState)
{
WMPulses = wmState;
if (IsEndOfTest)
{
EndWMState = wmState;
}
else
{
BeginWMState = wmState;
}
}
public int WMPulses { get; set; }
public int WMRefPulses { get; set; }
public double WMVolume { get { return WMPulses * LtrsPerPulse; } }
public double BeginWMState { get; set; }
public double EndWMState { get; set; }
public RegisterReader() { }
public RegisterReader(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
rrCfg = cfg as RRCfg;
/// Control board is used to read reference flowmeter pulses
radio = (Radio.Radio)TbfComponents.FindComponent(cfg.ParentName, components);
if (radio == null) throw new Exception(string.Format("Cannot find a parent of {0}", Name));
uniCB = StateMachine.ControlBoardMain as TBF.Rig.ControlBoard.Uni.UniCB;
if (uniCB == null) throw new Exception("Missing ELDE control board");
log.Warn(this.ToString());
}
public override void Initialize()
{
radio.Register(this);
Clear();
}
public void Clear()
{
BeginWMState = 0;
EndWMState = 0;
WMRefPulses = 0;
}
public void StopDevice()
{
radio.Unregister(this);
}
public void RunDeviceBefore() { }
public void RunDeviceAfter() { }
public void StopDevice2() { }
/// <summary>
/// Events: Event.ReadRegisterDone
/// </summary>
/// <returns>ReadWaterMeter instance reference casted to IOperaton</returns>
public IOperation ReadRegisterOp()
{
return this;
}
private void ReadPulses()
{
WMRefPulses = (uniCB != null) ? uniCB.RefPulses : 0;
}
/// <summary>Start this operation</summary>
public void Start()
{
ReadPulses();
}
/// <summary>Run this operation</summary>
/// <returns>eventDone</returns>
public Event Run()
{
ReadPulses();
return Event.ReadRegisterDone;
}
/// <summary>Stop this operation</summary>
public void Stop() { }
}
}