/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System.Collections.Generic; using TBF.BenchControl.Generic; namespace TBF.BenchControl { public static class TbfComponents { public readonly static IList Factories; static TbfComponents() { Factories = new List(); Factories.Add(new TestMethods.Adjustment.TestMethodFactory()); Factories.Add(new ResultsPrinters.Basic.PrinterFactory()); Factories.Add(new ResultsWriters.Basic.WriterFactory()); Factories.Add(new BenchId.ComponentFactory()); Factories.Add(new Cameras.CameraRoi.CameraRoiFactory()); Factories.Add(new TestMethods.CombinedMeters.TestMethodFactory()); Factories.Add(new TestMethods.CombinedWithDetection.TestMethodFactory()); Factories.Add(new Elde.ControlBoardFactory()); Factories.Add(new DataEntry.Combined.EntryFormFactory()); Factories.Add(new DataEntry.Munich3.EntryFormFactory()); Factories.Add(new DataEntry.Munich.EntryFormFactory()); Factories.Add(new DataEntry.Standard.EntryFormFactory()); Factories.Add(new DataEntry.Standard24.EntryFormFactory()); Factories.Add(new Elde.Diverter.DiverterFactory()); Factories.Add(new TestMethods.FixedStartMassCollection.TestMethodFactory()); Factories.Add(new Elde.FixedStartRegisterReader.RegisterReaderFactory()); Factories.Add(new Elde.FlowMeter.FlowMeterFactory()); Factories.Add(new Elde.FlowMeterTwins.FlowMeterFactory()); Factories.Add(new TestMethods.FlyingStart.TestMethodFactory()); Factories.Add(new TestMethods.FlyingStartCollectionMethod.TestMethodFactory()); Factories.Add(new DataEntry.WMStates.EntryFormFactory()); Factories.Add(new Ambient.Comet.Factory()); Factories.Add(new Ambient.Greco.Factory()); Factories.Add(new Cameras.IdcCamera.IdcCameraFactory()); Factories.Add(new TestMethods.LeakTest.TestMethodFactory()); Factories.Add(new MettlerToledo.Standard.BalanceFactory()); Factories.Add(new MettlerToledo.Standard.BalanceNewFactory()); Factories.Add(new MettlerToledo.Multi.BalanceFactory()); Factories.Add(new TestMethods.PMaxTest.TestMethodFactory()); Factories.Add(new Elde.PressureMeter.PressureMeterFactory()); Factories.Add(new Elde.PressureMeterInternal.PressureMeterFactory()); Factories.Add(new Elde.Pump.PumpFactory()); Factories.Add(new Danfoss.VLT2800.PumpFactory()); Factories.Add(new Elde.PumpWithFM.PumpFactory()); Factories.Add(new Elde.PumpTandem.PumpFactory()); Factories.Add(new TestMethods.ReferenceFlowmeterCalibration.TestMethodFactory()); Factories.Add(new Elde.RegisterReader.RegisterReaderFactory()); Factories.Add(new Elde.RegulValve.RegulValveFactory()); Factories.Add(new Elde.RegulValveTandem.RegulValveTandemFactory()); Factories.Add(new ResultsPrinters.Munich.PrinterFactory()); Factories.Add(new TestMethods.CameraRoiDetection.RoiDetectionFactory()); Factories.Add(new Elde.TempMeter.TempMeterFactory()); Factories.Add(new Elde.TempMeterInternal.TempMeterFactory()); Factories.Add(new Elde.Valve.ValveFactory()); Factories.Add(new WaterMeter.WaterMeterFactory()); } /// /// Get a component factory given the component name /// /// Component class name /// Component factory instance public static IComponentFactory CmpntFactoryFromClassName(string className) { foreach (var factory in Factories) { if (factory.ClassName.Equals(className)) return factory; } return null; } /// /// Get a component configuration given a component DB entity /// /// DB entity /// An appropriate component configuration or null if factory was not found public static IComponentCfg CmpntCfgFromCmpntEntity(Entities.Component entity) { IComponentFactory cmpntFactory = CmpntFactoryFromClassName(entity.ClassName); return (cmpntFactory != null) ? cmpntFactory.CmpntCfgFromCmpntEntity(entity) : null; } /// /// Load component entities from the database and convert them to TBF components /// derived from IComponent with mutual references. /// /// NHibernate DB session /// A list of TBF Components public static IList LoadComponentsFromDB(NHibernate.ISession session) { foreach (var factory in Factories) factory.ResetStaticProperties(); /// Load the list of components from the database IList cmptnEntities = session .CreateQuery("FROM Component ORDER BY ItemNr") .List(); /// Convert database entities to TBF components IList components = new List(); foreach (var entity in cmptnEntities) { IComponentFactory cmpntFactory = BenchControl.TbfComponents.CmpntFactoryFromClassName(entity.ClassName); if (cmpntFactory == null) continue; IComponent cmpnt = cmpntFactory.CmpntCfgFromCmpntEntity(entity).GetComponent(components); /// /// Inherit DebugMode from the parent (if any) /// if ((cmpnt.Cfg.DebugLevel == Entities.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 = Entities.DebugMode.Off; } } if (cmpnt.Cfg.DebugLevel != Entities.DebugMode.Off) components.Add(cmpnt); } return components; } /// /// Get a reference to a component with a given name from a list of components. /// /// Component name /// A list of components /// Matching component or null public static IComponent FindComponent(string name, IList components) { if (string.IsNullOrEmpty(name)) return null; foreach (var cmpnt in components) { if (cmpnt.Cfg.Name.Equals(name)) return cmpnt; } return null; } public static IComponent FindComponent(string name) { return FindComponent(name, StateMachine.Components); } } }