tbf/TBF/Rig/TbfComponents.cs

426 lines
22 KiB
C#

///
/// Copyright (c) 2013-2023 Sensus Slovensko a.s.
///
using System.Collections.Generic;
using System.Linq;
using Common;
using Config.Entities;
using log4net;
using log4net.Repository.Hierarchy;
using TBF.Rig.Generic;
using TBF.Rig.Sequences;
namespace TBF.Rig
{
public static class TbfComponents
{
private static readonly ILog log = LogManager.GetLogger(typeof(TbfComponents));
public readonly static IList<IComponentFactory> Factories;
static TbfComponents()
{
Factories = new List<IComponentFactory>()
{
new Ambient.Comet.Factory(),
new Ambient.Greco.Factory(),
new BridgeComponents.GciBridge.Factory(),
new ControlBoard.Papouch.Factory(),
new ControlBoard.Uni.Factory(),
new DataContainer.BackupAndSecurityOptions.Factory(),
new DataContainer.BenchInfo.Factory(),
new DataContainer.Buoyancy.Factory(),
new DataContainer.Density.Factory(),
new DataContainer.Evaporation.Factory(),
new DataContainer.iPerlBenchInfo.Factory(),
new DataEntry.Combined.EntryFormFactory(),
new DataEntry.HeatMeters6.Factory(),
new DataEntry.HeatMeters12.Factory(),
new DataEntry.iPerl.Factory(),
new DataEntry.PoseidonCmd.Factory(),
new DataEntry.S620.Factory(),
new DataEntry.Single.Factory(),
new DataEntry.Single.FactoryNoStartEnd(),
new DataEntry.Standard6.Factory(),
new DataEntry.Standard6.FactoryNoStartEnd(),
new DataEntry.Standard12.Factory(),
new DataEntry.Standard12.FactoryNoStartEnd(),
new DataEntry.Standard24.Factory(),
new DataEntry.Standard24.FactoryNoStartEnd(),
new DataEntry.Standard48.Factory(),
new DataEntry.Standard48.FactoryNoStartEnd(),
new DataEntry.StandardCamera.Factory(),
new DataEntry.StandartCameraPurchaseOrder.Factory(),
new DataEntry.Uni.Factory(),
new DataEntry.Uni.FactoryNoStartEnd(),
new RegisterReaders.KPackE.DataEntryForRadio.Factory(), /// DataEntry for KPackE radio
new Uni.Diverter.Factory(),
new Dummy.Balance.Factory(),
new Dummy.Diverter.Factory(),
new Dummy.FlowMeter.Factory(),
new Dummy.RegValve.Factory(),
new Dummy.TempControl.Factory(),
new Dummy.Valve.Factory(),
new Uni.FlowMeter.Factory(),
new Uni.FlowMetersInParallel.Factory(),
new Hart.Common.Factory(),
new Hart.Nivotrack.Factory(),
new Keithley.Multimeter_2010_RS232.Factory(), /// Keithley.Multimeter_2010_RS232
new Keithley.TempMeter.Factory(), /// Keithley.TempMeter
new Modbus.Common.Factory(), /// Modbus
new Modbus.Ambient.Comet.Factory(), /// Comet Ambient temp./pressure/humidity measurement via a modbus on a PC
new Modbus.PressureMeter.Meret.Factory(), /// Meret pressure meter connected via a modbus on a PC
new Modbus.Meret.AdjustableScale.Factory(), /// Adjustable meter connected via a modbus on a PC
new Modbus.PumpFM.DanfossVLT.Factory(), /// Pump controlled by Danfoss VLT connected via Modbus
new Modbus.PumpFM.Grundfoss.Factory(), /// Pump controlled by Grundfoss VLT connected via Modbus
new Modbus.QuidoRS.Factory(), /// Modbus.QuidoRS
new Modbus.TankSelector.Factory(), /// Modbus.TankSelector
new Modbus.TempControl.Easytherm.Factory(),
new Modbus.TempControl.Novus.Factory(),
new Modbus.TempMeter.Groch.Factory(), /// Groch temperature meter connected via a modbus on a PC
new Modbus.TempMeter.Meret.Factory(), /// Meret temperature meter connected via a modbus on a PC
new Modbus.UltrasoundLevelMeter.Factory(),
new Modbus.WaterAnalyzer.Factory(),
new Modbus.WaterAnalyzerUni.Factory(),
new Modbus.ConductivityMeter.Factory(),
new Network.Adapter.Factory(),
new Network.AdapterFTP.Factory(),
new Network.AdapterJMS.Factory(),
new Network.Camera.CJMS11.Factory(),
new Network.Camera.CLP1611.Factory(),
new Network.Camera.KeyenceIV3G120.Factory(),
new Network.Camera.Display.Factory(),
new Network.Camera.Roi.Factory(),
new Network.Camera.RoiForFixedStart.Factory(),
new Network.Camera.RoiForFixedStartCJMS11.Factory(),
new Network.Camera.RoiForFixedStartKeyence.Factory(),
new Network.Comet.Ambient.Factory(),
new Network.RestAPI.Factory(),
new Input.DataStorage.UniDataStorageReader.Factory(),
new Output.DataStorage.UniDataStorageWriter.Factory(),
new Output.DB.DatabaseWriter.Factory(),
new Output.DB.ProductionTracing.Factory(),
new Output.DB.ResultsWriter.Factory(),
new Output.DB.SaveDiverterCorrections.Factory(),
new Output.DB.SaveFlowmeterCorrections.Factory(),
new Output.DB.SensusOracle.Factory(),
new Output.EventTriggers.Iperl.Factory(),
new Output.EventTriggers.Standard.Factory(),
new Output.FileWriters.Basic.FactorySingle(),
new Output.FileWriters.Basic.FactoryCompound(),
new Output.FileWriters.Basic.FactoryHeatMeters(),
new Output.FileWriters.Elde.FactorySingle(),
new Output.FileWriters.Enhanced.FactorySingle(),
new Output.FileWriters.Enhanced.FactoryCompound(),
new Output.FileWriters.Enhanced.FactoryHeatMeters(),
new Output.FileWriters.ImageArchiver.Factory(),
new Output.FileWriters.IperlLogger.Factory(),
new Output.FileWriters.OneFilePerMeter.FactorySingle(),
new Output.FileWriters.OneFilePerMeter.FactoryCompound(),
new Output.FileWriters.OneFilePerMeter.FactoryHeatMeters(),
new Output.FileWriters.Xml.FactorySingle(),
new Output.FileWriters.Xml.FactoryCompound(),
new Output.FileWriters.Xml.FactoryHeatMeters(),
new Output.FileWriters.XmlBatch.FactorySingle(),
new Output.Printers.Enhanced.FactorySingle(),
new Output.Printers.Enhanced.FactoryCompound(),
new Output.Printers.Enhanced.FactoryHeatMeters(),
new Output.Printers.GroupPrinting.Single.Factory(), //new Group Printer
new Output.Printers.Label.Factory(),
new Output.Printers.MultiLabel.Factory(),
new Output.Printers.OnePerBatch.Factory(),
new Output.Printers.OnePerMeter.FactorySingle(),
new Output.Printers.OnePerMeter.FactoryCompound(),
new Output.Printers.OnePerMeter.FactoryHeatMeters(),
new Output.Printers.Zapiska.FactorySingle(),
new Output.Printers.Zapiska.FactoryCompound(),
new Output.Printers.Zapiska.FactoryHeatMeters(),
new BuiltIn.Pump.PumpFactory(),
new Danfoss.VLT2800.PumpFactory(),
new BuiltIn.PumpTandem.PumpFactory(),
new RegisterReaders.DataStream.MefImport.Factory(), /// 'Interface for data stream stream via MEF'
new RegisterReaders.DataStream.Reader.Factory(), /// 'RegisterReader for data stream stream via MEF'
new RegisterReaders.FrequencyMeterFromUniCB.Factory(), ///
//new RegisterReaders.iPerlReaderUNI.Factory(), /// 'UNI RegisterReader for Smart Meters'
new RegisterReaders.PoseidonReader.Factory(),
new RegisterReaders.IPerlReader.Factory(), /// the same functionality as TestMethods.iPerlCommunication.iPerlHead.Factory(),
new RegisterReaders.iPerlASICReader.Factory(), /// ASIC IPerl, C4 communication
new RegisterReaders.AllyReader.Factory(), // ALLY IPerl, communication
new RegisterReaders.GenesisRegReader.Factory(), /// Genesis RegisterReader - dirrect communication with the head
new RegisterReaders.PulsesFromUniCB.Factory(), /// 'RegisterReader'
new RegisterReaders.StandingStartStop.Factory(), /// 'RegisterReader for standing start/stop'
new TestMethods.iPerlCommunication.iPerlHead.Factory(), /// 'RegisterReader for iPerl'
new RegisterReaders.S640Stream.Factory(), /// 'RegisterReader for S640'
new RegisterReaders.SerialStream.Factory(), /// 'RegisterReader for generic serial stream'
new RegisterReaders.PoseidonCmdStartStop.Factory(), /// 'RegisterReader for poseidon start/stop'
new RegisterReaders.KPackE.RegisterReader.Factory(), /// KPackE register reader
new RegisterReaders.KPackE.Radio.Factory(), /// Radio for KPackE register readers
new Uni.RegValve.Factory(),
new Uni.RegValveAnalog.Factory(),
new Uni.RegValveLowRegulTimeSaturation.Factory(),
new Scales.MettlerToledo.Factory(),
new TestMethods.Adjustment.Factory(),
new TestMethods.ChangeFlowDirection.Factory(),
new TestMethods.CombinedWithDetection.TestMethodFactory(),
new TestMethods.Counter.Factory(),
new TestMethods.DiverterTest.Factory(),
new TestMethods.Dummy.Factory(),
new TestMethods.Endurance.Factory(),
new TestMethods.Evacuation.Factory(),
new TestMethods.FixedStart.Single.Factory(),
new TestMethods.FixedStart.Compound.Factory(),
new TestMethods.FixedStart.HeatMeters.Factory(),
new TestMethods.FixedStartAdvanced.Single.Factory(),
new TestMethods.FixedStartDeferredEval.Single.Factory(),
new TestMethods.FixedStartDeferredEval.Compound.Factory(),
new TestMethods.FixedStartDeferredEval.HeatMeters.Factory(),
new TestMethods.FixedStartMassCollAdvanced.Single.Factory(),
new TestMethods.FixedStartMassCollAdvanced.Compound.Factory(),
new TestMethods.FixedStartMassCollAdvanced.HeatMeters.Factory(),
new TestMethods.FixedStartMassCollDeferredEval.Single.Factory(),
new TestMethods.FixedStartMassCollDeferredEval.Compound.Factory(),
new TestMethods.FixedStartMassCollDeferredEval.HeatMeters.Factory(),
new TestMethods.FixedStartTankCollection.Single.Factory(),
new TestMethods.FixedStartTankCollection.Compound.Factory(),
new TestMethods.FlowAdjustment.Factory(),
new TestMethods.FlyingStart.Single.Factory(),
new TestMethods.FlyingStart.Compound.Factory(),
new TestMethods.FlyingStart.HeatMeters.Factory(),
new TestMethods.FlyingStartMassCollection.Single.Factory(),
new TestMethods.FlyingStartMassCollection.Compound.Factory(),
new TestMethods.FlyingStartMassCollection.HeatMeters.Factory(),
new TestMethods.FlyingStartMassCollComparative.Single.Factory(),
new TestMethods.FlyingStartMassCollComparative.Compound.Factory(),
new TestMethods.FlyingStartMassCollComparative.HeatMeters.Factory(),
new TestMethods.FlyingStartMassCollProlonged.Single.Factory(),
new TestMethods.FlyingStartMassCollProlonged.Compound.Factory(),
new TestMethods.FlyingStartMassCollProlonged.HeatMeters.Factory(),
new TestMethods.FlyingStartFirstRepetWithMassColl.Single.Factory(),
new TestMethods.FlyingStartFirstRepetWithMassColl.Compound.Factory(),
new TestMethods.FlyingStartFirstRepetWithMassColl.HeatMeters.Factory(),
new TestMethods.FlyingStartTankCollection.Single.Factory(),
new TestMethods.FlyingStartTankCollection.Compound.Factory(),
//new TestMethods.GenesisCommunication.GenesisHead.Factory(),
new TestMethods.GrabImage.Factory(),
new TestMethods.iPerlCommunication.TestMethodFactory(), /// iPerlCommunication
new TestMethods.AllyCalibration.Factory(), // Ally meter test
new TestMethods.LeakTest.Factory(),
new TestMethods.LiveStream.Factory(),
new TestMethods.ManualEntry.Factory(),
new TestMethods.OuterLoop.Start.Factory(),
new TestMethods.OuterLoop.End.Factory(),
new TestMethods.PMaxTest.Factory(),
new TestMethods.PulsesTest.Factory(),
new TestMethods.PulsesTestManual.Factory(),
new TestMethods.Q2CorrectionFromHistory.Factory(),
new TestMethods.RoiDetection.RoiDetectionFactory(),
new TestMethods.S640Communication.S640StartFactory(),
new TestMethods.S640Communication.S640EndFactory(),
new TestMethods.SensitivityTest.Factory(),
new TestMethods.SmartMeterFlyingStartMassCollection.Factory(),
new TestMethods.StandingStart.Single.Factory(),
new TestMethods.StandingStart.Compound.Factory(),
new TestMethods.StandingStart.HeatMeters.Factory(),
new TestMethods.StandingStartMassCollection.Single.Factory(),
new TestMethods.StandingStartMassCollection.Compound.Factory(),
new TestMethods.StandingStartMassCollection.HeatMeters.Factory(),
new TestMethods.StandingStartMassCollectionPoseidon.Single.Factory(),
new TestMethods.StandingStartMassCollectionAdvance.Single.Factory(),
new TestMethods.StandingStartMassCollectionAdvance.Compound.Factory(),
new TestMethods.StandingStartMassCollectionAdvance.HeatMeters.Factory(),
new TestMethods.StandingStartMassCollectionAdvanceCollect.Single.Factory(),
new TestMethods.StandingStartMassCollWODiv.Factory(),
new BuiltIn.Valve.ValveFactory(),
new BuiltIn.ValveEx.ValveFactory(),
new Various.Clamping.Factory(),
new Various.CoverTest.Factory(),
new Various.Drawing.Junction.Factory(),
new Various.Drawing.Knee.Factory(),
new Various.Drawing.Pipes.Factory(),
new Various.Drawing.Tank.Factory(),
new Various.Drawing.Vacuum.Factory(),
new Various.EmergencyStop.Factory(),
new Various.ErrorFlags.Factory(),
new Various.StatisticsMonitoring.Factory(),
new Various.TankWithLevelMsrmnt.Factory(),
new WaterMeters.WaterMeter.WaterMeterFactory(), /// 'WaterMeter'
#if OBSOLETE
/// Obsolete components
new MettlerToledo.Standard.BalanceFactory(), /// MettlerToledo Balance
new MettlerToledo.Standard.BalanceOldFactory(), /// MettlerToledoBalanceOld
new MettlerToledo.Standard.BalanceNewFactory(), /// MettlerToledoBalanceSN
#endif
};
}
/// <summary>
/// Get a component factory given the component name
/// </summary>
/// <param name="className">Component class name</param>
/// <returns>Component factory instance</returns>
public static IComponentFactory CmpntFactoryFromClassName(string className)
{
foreach (var factory in Factories)
{
if (factory.ClassName.Equals(className)) return factory;
}
return null;
}
/// <summary>
/// Get a component configuration given a component DB entity
/// </summary>
/// <param name="entity">DB entity</param>
/// <returns>An appropriate component configuration or null if factory was not found</returns>
public static IComponentCfg CmpntCfgFromCmpntEntity(Component entity)
{
IComponentFactory cmpntFactory = CmpntFactoryFromClassName(entity.ClassName);
if (cmpntFactory == null) return null;
try { return cmpntFactory.CmpntCfgFromCmpntEntity(entity); }
catch { return null; }
}
public static string CurrentlyLoadedComponentName;
/// <summary>
/// Load component entities from the database and convert them to TBF components
/// derived from IComponent with mutual references.
/// </summary>
/// <param name="session">NHibernate DB session</param>
/// <returns>A list of TBF Components</returns>
public static IList<IComponent> LoadComponentsFromDB(NHibernate.ISession session)
{
/// Load the list of components from the database
var cmptnEntities = session.QueryOver<Config.Entities.Component>()
.OrderBy(x => x.ItemNr).Asc
.List();
return LoadComponentsFromDB(cmptnEntities);
}
/// <summary>
/// Load component entities from the database and convert them to TBF components
/// derived from IComponent with mutual references.
/// </summary>
/// <param name="session">NHibernate DB session</param>
/// <returns>A list of TBF Components</returns>
public static IList<IComponent> LoadComponentsFromDB(IList<Config.Entities.Component> cmptnEntities)
{
CurrentlyLoadedComponentName = string.Empty;
/// Convert database entities to TBF components
IList<IComponent> components = new List<Rig.Generic.IComponent>();
foreach (var entity in cmptnEntities)
{
CurrentlyLoadedComponentName = entity.Name;
IComponentFactory cmpntFactory = Rig.TbfComponents.CmpntFactoryFromClassName(entity.ClassName);
if (cmpntFactory == null) continue;
IComponent cmpnt = cmpntFactory.GetComponent(cmpntFactory.CmpntCfgFromCmpntEntity(entity), components);
if (cmpnt == null)
{
log.Error("Failed loading component " + entity.Name + " of class " + entity.ClassName);
continue;
}
///
/// Inherit DebugMode from the parent (if any)
///
if ((cmpnt.Cfg.DebugLevel == DebugMode.Inherit) &&
(cmpnt.Cfg is IChildComponentCfg) &&
!string.IsNullOrEmpty(cmpnt.Cfg.ParentName))
{
bool inherited = false;
foreach (var par in components)
{
if (par.Cfg.Name.Equals(cmpnt.Cfg.ParentName))
{
/// Parent found, inherit the debug mode
cmpnt.Cfg.DebugLevel = par.Cfg.DebugLevel;
inherited = true;
break;
}
}
if (!inherited)
{
/// Applied when the parent is (most likely) in DebugMode.Off
cmpnt.Cfg.DebugLevel = DebugMode.Off;
}
}
if (cmpnt.Cfg.DebugLevel != DebugMode.Off) components.Add(cmpnt);
}
CurrentlyLoadedComponentName = string.Empty;
return components;
}
/// <summary>
/// Get a reference to a component with a given name from a list of components.
/// </summary>
/// <param name="name">Component name</param>
/// <param name="components">A list of components</param>
/// <returns>Matching component or null</returns>
public static IComponent FindComponent(string name, IList<IComponent> components)
{
if (string.IsNullOrEmpty(name) || components == null) return null;
return components.FirstOrDefault(x => x.Name == name);
}
public static IComponent FindComponent(string name)
{
return FindComponent(name, StateMachine.Components);
}
public static IComponent FindFirstSequenceComponentImpl<T>(IList<IComponent> components) where T : class, ISequence
{
foreach (var component in components)
{
if (component is T)
{
return component;
}
}
return null;
}
public static IComponent FindFirstSequenceComponentImpl<T>() where T : class, ISequence
{
return FindFirstSequenceComponentImpl<T>(StateMachine.Components);
}
public static IComponent FindFirstComponentImpl<T>(IList<IComponent> components) where T : class, IComponent
{
foreach (var component in components)
{
if (component is T)
{
return component;
}
}
return null;
}
public static IComponent FindFirstComponentImpl<T>() where T : class, IComponent
{
return FindFirstComponentImpl<T>(StateMachine.Components);
}
public static IComponent FindFirstSequenceComponentImpl()
{
foreach (var component in StateMachine.Components)
{
if (component is IProcedureCameraCollect)
{
return component;
}
}
return null;
}
}
}