123 lines
4.3 KiB
C#
123 lines
4.3 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 void CloseAllConnections()
|
|
{
|
|
if (isConnectedArray != null)
|
|
{
|
|
for (int ix = 0; ix < isConnectedArray.Length; ix++)
|
|
{
|
|
if (isConnectedArray[ix])
|
|
{
|
|
if (DataStreamIFace.CloseConnection(ix))
|
|
{
|
|
isConnectedArray[ix] = false; /// reset IsConnected flag
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|