/// /// Copyright (c) 2021 Sensus Slovensko a.s. /// using System; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using log4net; using Common; using DataStreamInterface; namespace TBF.Rig.RegisterReaders.DataStream.MefImport { /// /// MEF interface to an external data stream reader component. /// public class MefImport : ComponentBase { private static readonly ILog log = LogManager.GetLogger(typeof(MefImport)); public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); } public Int64 MinSamplesCount { get { return 10; } } [Import(typeof(IDataStreamMeter))] public IDataStreamMeter DataStreamIFace; readonly MefImportCfg myCfg; public string CatalogDir { get { return myCfg.CatalogDir; } } /// /// Size of this array is DataStreamIFace.GetMetersCount() /// 'true' in this array represents an open connection to the water meter with the same index /// bool[] isConnectedArray; public MefImport() { } public MefImport(Generic.IComponentCfg cfg) : base(cfg) { myCfg = cfg as MefImportCfg; } public override void Initialize() { if (DebugLevel == DebugMode.Normal) { var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(typeof(DataStreamInterface.IDataStreamMeter).Assembly)); catalog.Catalogs.Add(new DirectoryCatalog(CatalogDir)); (new CompositionContainer(catalog)).ComposeParts(this); isConnectedArray = new bool[DataStreamIFace.GetMetersCount()]; log.FatalFormat("{0} initialized: {1}", Name, this); log.FatalFormat(" Meters count = {0}", DataStreamIFace.GetMetersCount()); log.FatalFormat(" Time unit = {0}", DataStreamIFace.GetTimeUnits().ToString()); log.FatalFormat(" Volume unit = {0}", DataStreamIFace.GetVolumeUnits().ToString()); for (int i = 0; i < DataStreamIFace.GetQuantitiesCount(); i++) { log.FatalFormat(" Quantity_{0} = {1}, unit = {2}", i + 1, DataStreamIFace.GetQuantityCaption(i), DataStreamIFace.GetQuantityUnits(i).ToString()); } } else { log.FatalFormat("{0} simulated: {1}", Name, this); } } public bool IsConnected(int ix) { return (isConnectedArray != null && ix >= 0 && ix < isConnectedArray.Length) ? isConnectedArray[ix] : false; } /// /// Wrapper for DataStreamIFace.OpenConnection() that updates bool[] isConnected array. /// /// 0-based water meter index /// Connection parameters /// Water meter ID (serial number) /// true = Successfully connected public bool OpenConnection(int ix, string connectionParameters, out string meterId) { if (isConnectedArray != null && ix >= 0 && ix < isConnectedArray.Length) { isConnectedArray[ix] = DataStreamIFace.OpenConnection(ix, connectionParameters, out meterId); return isConnectedArray[ix]; } else { meterId = string.Empty; return false; } } /// /// Wrapper for DataStreamIFace.CloseConnection() called in a loop for all connected water meters /// public bool CloseConnectionAll() { if (isConnectedArray != null) { for (int ix = 0; ix < isConnectedArray.Length; ix++) { if (isConnectedArray[ix]) { DataStreamIFace.CloseConnection(ix); isConnectedArray[ix] = false; /// reset IsConnected flag regardless of returned value } } } return true; } /// /// Wrapper for DataStreamIFace.StartMeasurement(), starts only connected water meters /// public bool StartMeasurement(int ix) { if (isConnectedArray != null && ix >= 0 && ix < isConnectedArray.Length && isConnectedArray[ix]) { return DataStreamIFace.StartMeasurement(ix); } return false; } /// /// Wrapper that bypasses DataStreamIFace.StartMeasurementAll(), starts only connected water meters /// public bool StartMeasurementAll() { if (isConnectedArray != null) { for (int ix = 0; ix < isConnectedArray.Length; ix++) { if (isConnectedArray[ix]) DataStreamIFace.StartMeasurement(ix); } return true; } return false; } /// /// Wrapper for DataStreamIFace.StopMeasurement(), stops only connected water meters /// public bool StopMeasurement(int ix, out Int64 storedFramesCount) { if (isConnectedArray != null && ix >= 0 && ix < isConnectedArray.Length && isConnectedArray[ix]) { return DataStreamIFace.StopMeasurement(ix, out storedFramesCount); } storedFramesCount = 0; return false; } /// /// Wrapper that bypasses DataStreamIFace.StopMeasurementAll(), stops only connected water meters /// public bool StopMeasurementAll(out Int64[] storedFramesCounts) { if (isConnectedArray != null) { storedFramesCounts = new Int64[isConnectedArray.Length]; for (int ix = 0; ix < isConnectedArray.Length; ix++) { if (isConnectedArray[ix]) DataStreamIFace.StopMeasurement(ix, out storedFramesCounts[ix]); else storedFramesCounts[ix] = 0; } return true; } storedFramesCounts = null; return false; } } }