New RR.DataStream.MefImport and RR.DataStream.Reader components.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
///
|
||||
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
||||
///
|
||||
using System.Collections.Generic;
|
||||
using TBF.Rig.Generic;
|
||||
|
||||
namespace TBF.Rig.RegisterReaders.DataStream.MefImport
|
||||
{
|
||||
public class Factory : IComponentFactory
|
||||
{
|
||||
public string ClassName { get { return GetType().Namespace.Substring(8); } }
|
||||
public override string ToString() { return ClassName; }
|
||||
|
||||
public IComponent DummyComponent() { return new MefImport(); }
|
||||
|
||||
public IComponent GetComponent(IComponentCfg cfg, IList<IComponent> components) { return new MefImport(cfg); }
|
||||
|
||||
public IComponentCfg DefaultConfig() { return new MefImportCfg(this); }
|
||||
|
||||
public IComponentCfg CmpntCfgFromCmpntEntity(Config.Entities.Component component)
|
||||
{
|
||||
return ComponentCfgBase.CreateFromDbEntity(MefImportCfg.Serializer, component, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
///
|
||||
/// 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
|
||||
{
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
///
|
||||
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml.Serialization;
|
||||
using Common;
|
||||
using Config.Entities;
|
||||
using TBF.Rig.Generic;
|
||||
|
||||
namespace TBF.Rig.RegisterReaders.DataStream.MefImport
|
||||
{
|
||||
/// <summary>
|
||||
/// Holds information identifying the test bench - serializable configuration.
|
||||
/// </summary>
|
||||
public class MefImportCfg : ComponentCfgBase, Generic.IComponentCfg, IParamsProvider
|
||||
{
|
||||
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(MefImportCfg) })[0];
|
||||
public override XmlSerializer GetSerializer() { return Serializer; }
|
||||
|
||||
public IComponentCfgCtrl GetControl(IList<Config.Entities.Component> cmpntEntities)
|
||||
{
|
||||
return new Configs.ParamsProvider.ComponentCfgCtrl(this, null);
|
||||
}
|
||||
|
||||
///
|
||||
/// Serialized parameters
|
||||
///
|
||||
public string CatalogDir;
|
||||
|
||||
|
||||
/// Private parameterless constructor invoked by all other (public) constructors
|
||||
MefImportCfg() {}
|
||||
|
||||
public MefImportCfg(IComponentFactory factory)
|
||||
: this()
|
||||
{
|
||||
Factory = factory;
|
||||
Name = "MefImport";
|
||||
ParentName = string.Empty;
|
||||
InitializeAll();
|
||||
}
|
||||
|
||||
public string ComponentName { get { return Name; } }
|
||||
|
||||
public void InitializeAll()
|
||||
{
|
||||
CatalogDir = "..\\..\\..\\DataStreamMeter\\bin";
|
||||
}
|
||||
|
||||
string[] paramNames = new string[]
|
||||
{
|
||||
"Catalog directory",
|
||||
};
|
||||
public string ParamName(int i) { return paramNames[i]; }
|
||||
public int ParamsCount() { return paramNames.Length; }
|
||||
|
||||
public ICollection<string> ParamValues(int i)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
return null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string ToString(int i)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
return CatalogDir;
|
||||
default:
|
||||
return string.Format("{0}: CatalogDir = {1}", Name, CatalogDir);
|
||||
}
|
||||
}
|
||||
|
||||
public CfgUpdateFlags UpdateParam(int i, string strValue)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
CatalogDir = strValue;
|
||||
return CfgUpdateFlags.RestartRqrd;
|
||||
default:
|
||||
return CfgUpdateFlags.None;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ValidateParam(int i, string strValue, out string message)
|
||||
{
|
||||
message = string.Empty;
|
||||
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
return true;
|
||||
default:
|
||||
message = "Invalid index";
|
||||
return false;
|
||||
}
|
||||
|
||||
message = ParamName(i) + " is invalid";
|
||||
return false;
|
||||
}
|
||||
|
||||
void CopyContentTo(MefImportCfg prms)
|
||||
{
|
||||
prms.CatalogDir = this.CatalogDir;
|
||||
}
|
||||
|
||||
public IParamsProvider Clone()
|
||||
{
|
||||
MefImportCfg pars = new MefImportCfg();
|
||||
CopyContentTo(pars);
|
||||
return pars;
|
||||
}
|
||||
|
||||
public bool UpdateEmbeddedDbEntity()
|
||||
{
|
||||
return true; /// =OK, do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user