MefImport : IsConnected(), OpenConnection() and CloseAllConnections() wrappers, DataStream.EntryForm calls CloseAllConnections().

This commit is contained in:
Milan Hanajik 2021-07-16 16:56:04 +02:00
parent c4afaf739a
commit bb4b1a1998
2 changed files with 61 additions and 5 deletions

View File

@ -88,7 +88,8 @@ namespace TBF.BenchControl.DataEntry.DataStream
public IOperation ShowCycleBeginFormOp()
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.ShowFormAtCycleBeginning;
mefImport.CloseAllConnections();
currentOp = CurrentOp.ShowFormAtCycleBeginning;
this.waterMeters = TBF.BenchControl.Sequences.ProcessData.BatchRslts.Batch.WaterMeters;
return this;
}
@ -97,10 +98,9 @@ namespace TBF.BenchControl.DataEntry.DataStream
public IOperation ShowCycleEndFormOp()
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.ShowFormAtCycleEnd;
this.waterMeters = TBF.BenchControl.Sequences.ProcessData.BatchRslts.Batch.WaterMeters;
return this;
}
mefImport.CloseAllConnections();
return null;
}
/// <returns>Reference to the operation</returns>
public IOperation ShowTestStartFormOp(IRegReader[] regReaders)

View File

@ -29,6 +29,13 @@ namespace TBF.BenchControl.RegisterReaders.DataStream.MefImport
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)
@ -46,6 +53,8 @@ namespace TBF.BenchControl.RegisterReaders.DataStream.MefImport
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());
@ -62,5 +71,52 @@ namespace TBF.BenchControl.RegisterReaders.DataStream.MefImport
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
}
}
}
}
}
}
}