189 lines
6.5 KiB
C#
189 lines
6.5 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.ComponentModel.Composition;
|
|
using System.ComponentModel.Composition.Hosting;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using DataStreamInterface;
|
|
|
|
namespace TBF.BenchControl.RegisterReaders.DataStream.MefImport
|
|
{
|
|
/// <summary>
|
|
/// MEF interface to an external data stream reader component.
|
|
/// </summary>
|
|
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; } }
|
|
|
|
|
|
/// <summary>
|
|
/// Size of this array is DataStreamIFace.GetMetersCount()
|
|
/// 'true' in this array represents an open connection to the water meter with the same index
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wrapper for DataStreamIFace.OpenConnection() that updates bool[] isConnected array.
|
|
/// </summary>
|
|
/// <param name="ix">0-based water meter index</param>
|
|
/// <param name="connectionParameters">Connection parameters</param>
|
|
/// <param name="meterId">Water meter ID (serial number)</param>
|
|
/// <returns>true = Successfully connected</returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wrapper for DataStreamIFace.CloseConnection() called in a loop for all connected water meters
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wrapper for DataStreamIFace.StartMeasurement(), starts only connected water meters
|
|
/// </summary>
|
|
public bool StartMeasurement(int ix)
|
|
{
|
|
if (isConnectedArray != null && ix >= 0 && ix < isConnectedArray.Length && isConnectedArray[ix])
|
|
{
|
|
return DataStreamIFace.StartMeasurement(ix);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wrapper that bypasses DataStreamIFace.StartMeasurementAll(), starts only connected water meters
|
|
/// </summary>
|
|
public bool StartMeasurementAll()
|
|
{
|
|
if (isConnectedArray != null)
|
|
{
|
|
for (int ix = 0; ix < isConnectedArray.Length; ix++)
|
|
{
|
|
if (isConnectedArray[ix]) DataStreamIFace.StartMeasurement(ix);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wrapper for DataStreamIFace.StopMeasurement(), stops only connected water meters
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wrapper that bypasses DataStreamIFace.StopMeasurementAll(), stops only connected water meters
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
}
|