/// /// Copyright (c) 2013-2018 Sensus Slovensko a.s. /// using System; using System.Text; using System.Threading; using System.Collections.Generic; using log4net; using NHibernate; using Config.Entities; using TBF.BenchControl.Generic; using TBF.BenchControl.GenericDevices; using TBF.BenchControl.Sequences; using Dirichlet.Numerics; using TBF.Resources; namespace TBF.BenchControl { public class QuitStateMachineException : Exception { } /// /// This class controls real test bench behavior. /// It is based on a state machine /// public static class StateMachine { private static readonly ILog log = LogManager.GetLogger(typeof(StateMachine)); private static readonly ILog wlog = LogManager.GetLogger(typeof(StateMachine)); /// Private devices and components static IList components; /// list of all components static IList devices; /// list of devices /// /// Bench paths static IList feedingPaths; static IList benchPaths; static IList outputPaths; static IList metersPaths; static IList heatMetersPaths; public static IList TransitionSequences; public static IList TransitionSteps; /// Public components public static Elde.ControlBoardDev ControlBoard; public static GenericDevices.IAmbient Ambient; public static GenericDevices.IScale Scale1; public static GenericDevices.IScale Scale2; public static GenericDevices.IScale Scale3; public static GenericDevices.IValve DrainValve1; public static GenericDevices.IValve DrainValve2; public static GenericDevices.IValve DrainValve3; public static IList MasterValves; /// list of directly controlled valves public static IList CoupledValves; /// list of coupled valves public static IList ExtendedValves; /// list of extended valves /// Time and synchronization #if TURA_IPERL public const int Period = 2; /// State machine period in sec. #else public const int Period = 1; /// State machine period in sec. #endif static DateTime startDateTime; /// DateTime of time instance when the state machine worker thread starts static int currentTimeSec; /// Time from the start of the state machine in seconds static bool quitStateMachine; /// flag to stop the worker thread /// true when the state machine is running static bool stateMachineRunning; public static bool Running { get { return stateMachineRunning; } } /// Worker thread and database session static Thread workerThread; static IList states; /// list of states public static DateTime CycleStartTimeStamp; public static IList DefaultValvesOpen { get { return GenericDevices.ValveBase.Merge( Utils.ValvesOpen((feedingPaths != null && feedingPaths.Count > 0) ? feedingPaths[0] : null), Utils.ValvesOpen((benchPaths != null && benchPaths.Count > 0) ? benchPaths[0] : null), Utils.ValvesOpen((outputPaths != null && outputPaths.Count > 0) ? outputPaths[0] : null) ); } } public static IList DefaultValvesClose { get { return GenericDevices.ValveBase.Merge( Utils.ValvesClose((feedingPaths != null && feedingPaths.Count > 0) ? feedingPaths[0] : null), Utils.ValvesClose((benchPaths != null && benchPaths.Count > 0) ? benchPaths[0] : null), Utils.ValvesClose((outputPaths != null && outputPaths.Count > 0) ? outputPaths[0] : null) ); } } /// /// Loaded by LoadProcedure() or IOperation LoadProcedureOp(...) /// public static Procedure Procedure; /// Procedure public static IList Tests; /// Tests /// Hardware devices connected to the PC controlling the bench. public static IList Components { get { return components; } } public static IList Devices { get { return devices; } } /// /// DateTime of time instance when the state machine worker thread starts /// public static DateTime StartDateTime { get { return startDateTime; } } /// /// Current state name /// public static int Time { get { return currentTimeSec; } } /// /// Constructor /// static StateMachine() { devices = new List(); states = new List(); currentTimeSec = 0; quitStateMachine = false; } /// /// Add a device to the state machine. /// /// Device public static void AddDevice(IDevice device) { if (device != null && !devices.Contains(device)) devices.Add(device); } /// /// Add a state to the state machine. /// In this way a sequence can be created programtically. /// /// State public static void AddState(State state) { if (state != null && !states.Contains(state)) states.Add(state); } /// /// Remove the state from the state machine. /// /// State public static void RemoveState(State state) { if (state != null && state != State.CurrentState && states.Contains(state)) states.Remove(state); } /// /// Get a state from a label /// /// /// The matching state or null static State GetStateFromLabel(string label) { if (label == null) return null; foreach (State state in states) { if (state.Label != null && state.Label.Equals(label)) return state; } return null; } /// /// Start the state machine in the state 'label' in a desired mode of operation. /// This method is called in the UI thread and creates a new state machine thread. /// This method call should be embedded in: try { StateMachine.Start(...); } catch { } /// to handle configuration problems. Calls CreateDevices(mode) and CreateStates(). /// /// Mode of operation /// A copy of bench data used by the state machine /// Identifies the initial state #if DN100 public static void InitializeBoardEtc(ControlCom2VB.ControlCom2panel ctrlBrdComponent) #elif MUNICH || FUZHOU150 public static void InitializeBoardEtc(ControlComponent3Munich.UserControl1 ctrlBrdComponent) #elif FUZHOU300 public static void InitializeBoardEtc(ControlComponent3F300.UserControl1 ctrlBrdComponent) #elif PT200IL || PUCHONG_PT200 || GENESIS || BERLIN || SLM_150 || PETERSBURG_200 || CEVAK_200 public static void InitializeBoardEtc(ControlComponent_Izrael2014.UserControl1 ctrlBrdComponent) #else /// all newer benches public static void InitializeBoardEtc(ControlComponent_Torino2015.UserControl1 ctrlBrdComponent) #endif { /// Load the list of components (entities) from the database. /// Then create the components (derived from IComponent). components = BenchControl.TbfComponents.LoadComponentsFromDB(Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config)); MasterValves = GenericDevices.ValveBase.MasterValves(components); CoupledValves = GenericDevices.ValveBase.CoupledValves(components); ExtendedValves = GenericDevices.ValveBase.ExtendedValves(components); /// Find all balances (to initialize tank capacities in the control board) /// Find the control board IList balances = new List(); SequenceBase.FlowMeters = new List(); SequenceBase.RegulValves = new List(); SequenceBase.PumpsWithFM = new List(); SequenceBase.WaterMeters = new List(); SequenceBase.Cameras = new List(); UInt128 valvesToInvert = 0; foreach (var cmpnt in components) { if (cmpnt is Elde.ControlBoardDev) ControlBoard = cmpnt as Elde.ControlBoardDev; if (cmpnt is IBenchInfo) ProcessData.BenchInfo = cmpnt as IBenchInfo; if (cmpnt is IErrorFlags) ProcessData.ErrorFlagsComp = cmpnt as IErrorFlags; if (cmpnt is IFlowMeter) SequenceBase.FlowMeters.Add(cmpnt as IFlowMeter); if ((cmpnt is IRegulValve) && !(cmpnt is BenchControl.Elde.RegulValveTandem.RegulValveTandem)) { SequenceBase.RegulValves.Add(cmpnt as IRegulValve); } if (cmpnt is IPumpFM && !(cmpnt is Elde.PumpTandem.Pump)) { SequenceBase.PumpsWithFM.Add(cmpnt as IPumpFM); } if (cmpnt is IWaterMeter) SequenceBase.WaterMeters.Add(cmpnt as IWaterMeter); if (cmpnt is ICamera) SequenceBase.Cameras.Add(cmpnt as ICamera); if (cmpnt is GenericDevices.IAmbient) Ambient = cmpnt as GenericDevices.IAmbient; if (cmpnt is IScale) { IScale scale = cmpnt as IScale; balances.Add(scale); if (scale.ScaleNr == 0) Scale1 = scale; else if (scale.ScaleNr == 1) Scale2 = scale; else if (scale.ScaleNr == 2) Scale3 = scale; } Elde.Valve.Valve eldeValve = (cmpnt as Elde.Valve.Valve); if ((eldeValve != null) && eldeValve.Inverted) valvesToInvert |= eldeValve.Mask; cmpnt.StartChangeHandler(); /// Start handling parameter change events } /// Create an array with tank capacities double[] tankCapacities = new double[balances.Count]; for (int i = 0; i < tankCapacities.Length; i++) tankCapacities[i] = balances[i].Capacity; /// Pre-initialize the control board (= buffer the arguments ctrlBrdComponent, tankCapacities) if (ControlBoard != null) { ControlBoard.InitializeComponent(ctrlBrdComponent, valvesToInvert, tankCapacities); } else { throw new Exception("Control board component is missing"); } } public static string CurrentlyInitializedDeviceName; /// public static string InitializeDevices() { CurrentlyInitializedDeviceName = "-"; bool anyComponentIsInSimulMode = false; StringBuilder inSimulMode = new StringBuilder(); /// Add all devices to the state machine and initialize them foreach (var cmpnt in components) { if (cmpnt.DebugLevel == DebugMode.Simulate) { inSimulMode.AppendFormat("{0}{1}", anyComponentIsInSimulMode ? ", " : "", cmpnt.Name); anyComponentIsInSimulMode = true; } IDevice device = cmpnt as IDevice; if (device != null) { CurrentlyInitializedDeviceName = device.Name; UiBridge.Bridge.OnActivity(null, device.Name); /// Info device.Initialize(); AddDevice(device); /// Only components that were initialized are added } } CurrentlyInitializedDeviceName = "---"; /// /// (1) Propagate debug levels from parents to children when necessary /// (2) Set drain valves DrainValve1, DrainValve2 and DrainValve3 /// foreach (var cmpnt in components) { if (cmpnt.Cfg is IChildComponentCfg && !string.IsNullOrEmpty(cmpnt.Cfg.ParentName) && (cmpnt.Cfg.DebugLevel == DebugMode.Inherit || cmpnt.Cfg.DebugLevel == DebugMode.AutoDetect)) { foreach (var par in components) { if (par.Cfg.Name.Equals(cmpnt.Cfg.ParentName)) { cmpnt.Cfg.DebugLevel = par.Cfg.DebugLevel; break; } } } if (cmpnt is IScale) { IScale scale = cmpnt as IScale; if (scale.ScaleNr == 0) { DrainValve1 = scale.DrainValve; log.WarnFormat("InitializeBoardEtc() ... Scale1={0} Draining valve1={1}", scale.Name, (DrainValve1 != null) ? DrainValve1.Name : "---"); } else if (scale.ScaleNr == 1) { DrainValve2 = scale.DrainValve; log.WarnFormat("InitializeBoardEtc() ... Scale2={0} Draining valve2={1}", scale.Name, (DrainValve2 != null) ? DrainValve2.Name : "---"); } else if (scale.ScaleNr == 2) { DrainValve3 = scale.DrainValve; log.WarnFormat("InitializeBoardEtc() ... Scale3={0} Draining valve3={1}", scale.Name, (DrainValve3 != null) ? DrainValve3.Name : "---"); } } } if (anyComponentIsInSimulMode) return inSimulMode.ToString(); else return null; } /// /// Stop devices that were started by InitializeDevices(). /// Works correctly also after exeption from InitializeDevices() as only ... /// ... correctly started devices were added in 'devices' list. /// public static void StopDevices() { foreach (var dev in devices) { UiBridge.Bridge.OnActivity(null, string.Format("1. {0}", dev.Name)); /// Info dev.StopDevice(); } } /// /// Stop devices that were started by InitializeDevices(). /// Works correctly also after exeption from InitializeDevices() as only ... /// ... correctly started devices were added in 'devices' list. /// public static void StopDevices2() { foreach (var dev in devices) { UiBridge.Bridge.OnActivity(null, string.Format("2. {0}", dev.Name)); /// Info dev.StopDevice2(); } } /// /// Start the state machine /// public static void Start() { if (stateMachineRunning) return; workerThread = new Thread(Worker); workerThread.CurrentCulture = Thread.CurrentThread.CurrentCulture; workerThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture; workerThread.Start(); stateMachineRunning = true; } /// /// Loads all paths and transitions from the DB. /// Updates StatMachine.feedingPaths ... StatMachine.meterPaths, StatMachine.TransitionSequences /// public static void LoadPathsAndTransitions(ISession session) { feedingPaths = session.QueryOver().OrderBy(x => x.ItemNr).Asc.List(); benchPaths = session.QueryOver().OrderBy(x => x.ItemNr).Asc.List(); outputPaths = session.QueryOver().OrderBy(x => x.ItemNr).Asc.List(); metersPaths = session.QueryOver().OrderBy(x => x.ItemNr).Asc.List(); TransitionSequences = session.QueryOver().OrderBy(x => x.ItemNr).Asc.List(); TransitionSteps = session.QueryOver().OrderBy(x => x.ItemNr).Asc.List(); #if HEAT_METERS heatMetersPaths = session.QueryOver().OrderBy(x => x.ItemNr).Asc.List(); #endif } /// /// Loads selected procedure from the DB. /// Updates StateMachine.Procedure and StateMachine.Tests /// public static bool LoadProcedure(ISession session, string procedureName) { Procedure = null; IList selectedProcs = session.QueryOver() .Where(x => (x.ProcedureState == ProcedureState.Active)) .And(x => (x.Name == procedureName)) .List(); if (selectedProcs.Count != 1) return false; Procedure = selectedProcs[0]; Tests = selectedProcs[0].Tests; return true; } public static void LoadProcedureParams(Procedure procedure) { foreach (var cmpnt in components) { cmpnt.Cfg.LoadProcedureParamsFromDB(procedure); } } public static void LoadTestParams(Test test) { foreach (var cmpnt in components) { cmpnt.Cfg.LoadTestParamsFromDB(test); } } /// /// Processes the selection done by the bench control panel in the main sequence. /// /// Selection.Q1, .Q2, .Q3 or .Test /// The selected test or null public static Config.Entities.Test GetTest(string selectedTestName, out int repetNr) { repetNr = 1; foreach (var test in Tests) { if (test.Name.Equals(selectedTestName)) return test; /// Test name specified, keep repetNr = 1 for (int i = 1; i <= test.Repeats; i++) { if (Utils.TestTitle(test, i).Equals(selectedTestName)) { repetNr = i; return test; } } } return null; } /// /// Called from the sequence to update paths based on the selected test /// /// Selected test /// /// /// /// /// Transition sequence entity /// Transition sequence entity /// Error message in case of incorrect configuration /// true when loaded configuration is correct (all four paths are defined !=null, etc.) public static bool GetPaths(Test test, bool heatMetersPathRequired, out FeedingPath pfeed, out BenchPath pben, out OutputPath pout, out MetersPath pmtrs, out HeatMetersPath phmtrs, out TransitionSequence transitionBefore, out TransitionSequence transitionAfter, out string errorMsg) { pfeed = null; pben = null; pout = null; phmtrs = null; transitionBefore = null; transitionAfter = null; foreach (var path in feedingPaths) { if (test.FeedingPath == path.Name) { pfeed = new FeedingPath(path, components); break; } } foreach (var path in benchPaths) { if (test.BenchPath == path.Name) { pben = new BenchPath(path, components); break; } } foreach (var path in outputPaths) { if (test.OutputPath == path.Name) { pout = new OutputPath(path, components); break; } } pmtrs = GetMetersPath(test); if (pfeed == null) errorMsg = Strings.Cannot_load_feeding_path; else if (pben == null) errorMsg = Strings.Cannot_load_bench_path; else if (pout == null) errorMsg = Strings.Cannot_load_output_path; else if (pmtrs == null) errorMsg = Strings.Cannot_load_sensor_path; else errorMsg = string.Empty; if ((pfeed == null) || (pben == null) || (pout == null) || (pmtrs == null)) { return false; } #if HEAT_METERS foreach (var path in heatMetersPaths) { if (test.HeatMetersPath == path.Name) { phmtrs = new HeatMetersPath(path, components); break; } } #endif if (heatMetersPathRequired && phmtrs == null) { errorMsg = "Cannot load heat meters sensors"; return false; } foreach (var tr in TransitionSequences) { if (tr.Name == test.RelTransBefore) transitionBefore = tr; if (tr.Name == test.TransitionAfter) transitionAfter = tr; } if (pout.Scale == null) { errorMsg = string.Format("No balance specified in path {0}", test.OutputPath); return false; } if (Program.LocalSettings.RealDensity < 500.0f || Program.LocalSettings.RealDensity > 2000.0f) { errorMsg = string.Format("Density was not specified"); return false; } errorMsg = string.Empty; return true; } /// /// Updates paths based on the selected test /// public static MetersPath GetMetersPath(Test test) { MetersPath pmtrs = null; foreach (var path in metersPaths) { if (test.MetersPath == path.Name) { pmtrs = new MetersPath(path, components); break; } } if (pmtrs != null) { int count = Math.Min(Config.Data.WMsCount, pmtrs.RegisterReaders.Length); for (int i = 0; i < count; i++) { if ((pmtrs.RegisterReaders[i] != null) && (pmtrs.RegisterReaders[i].Cfg.DebugLevel == DebugMode.DetectedOff)) { pmtrs.RegisterReaders[i] = null; } } } return pmtrs; } /// /// Stops the state machine (and the worker thread) /// public static void Stop() { if (stateMachineRunning) quitStateMachine = true; } /* * This is and example sequence of RunDeviceBefore() / RunOperations() / RunDeviceAfter() calls * as they are executed during normal run from the progran start to the end. * foreach (var device in devices) device.RunDeviceBefore(); . . . . . . in StateMachine.Worker() State.Create(...).AddOperation(...).AddOperation(...).EnterState() . . in the sequence in Execute(...) foreach (var device in devices) device.RunDeviceAfter(); . . . . . . . in WaitRunDevsRunOps() WaitNextTick() (may throw QuitStateMachineException) . . . . . . . . in WaitRunDevsRunOps() foreach (var device in devices) device.RunDeviceBefore(); . . . . . . in WaitRunDevsRunOps() IList events = State.RunOperations(); . . . . . . . . . . . . . in WaitRunDevsRunOps() foreach (var device in devices) device.RunDeviceAfter(); . . . . . . . in WaitRunDevsRunOps() WaitNextTick() (may throw QuitStateMachineException) . . . . . . . . in WaitRunDevsRunOps() foreach (var device in devices) device.RunDeviceBefore(); . . . . . . in WaitRunDevsRunOps() IList events = State.RunOperations(); . . . . . . . . . . . . . in WaitRunDevsRunOps() State.Create(...).AddOperation(...).AddOperation(...).EnterState() . . in the sequence in Execute(...) foreach (var device in devices) device.RunDeviceAfter(); . . . . . . . in WaitRunDevsRunOps() WaitNextTick() (may throw QuitStateMachineException) . . . . . . . . in WaitRunDevsRunOps() foreach (var device in devices) device.RunDeviceBefore(); . . . . . . in WaitRunDevsRunOps() IList events = State.RunOperations(); . . . . . . . . . . . . . in WaitRunDevsRunOps() foreach (var device in devices) device.RunDeviceAfter(); . . . . . . . in WaitRunDevsRunOps() WaitNextTick() (assume QuitStateMachineException thrown) . . . . . . in WaitRunDevsRunOps() State.StopOperations(); . . . . . . . . . . . . . . . . . . . . . . . in StateMachine.Worker() catch() foreach (var device in devices) device.StopDevice(); . . . . . . . . . in StateMachine.Worker() catch() */ /// /// Worker thread: calls Start(), Run() and Stop() methods of operations. /// It uses 'currentState', 'nextState' and 'quitStateMachine' static fields. /// static void Worker() { startDateTime = DateTime.Now; CycleStartTimeStamp = startDateTime; /// To prevent it is undefined wlog.InfoFormat(" currentTime = {0}s startDateTime = {1}", currentTimeSec.ToString(), startDateTime.ToString()); /// Run all devices for the first time foreach (var device in devices) device.RunDeviceBefore(); try { SequenceBase.ReferenceFlowmetersCount = SequenceBase.FlowMeters.Count; SequenceBase.CalibratedLtrPerRefPulse = new float[SequenceBase.ReferenceFlowmetersCount]; foreach (var flowmtr in SequenceBase.FlowMeters) { int ix = flowmtr.Idx1; if (ix > 0 && ix <= SequenceBase.ReferenceFlowmetersCount) { SequenceBase.CalibratedLtrPerRefPulse[ix - 1] = flowmtr.NominalFlow / 7200.0f; } } (new Sequences.MainSeq()).Execute(null); } catch (QuitStateMachineException) { } } /// /// Do stuff that is repeated in the state execution loops most often /// /// List of Event-s returned from the state operations, null == quit public static IList WaitRunDevsRunOps() { foreach (var device in devices) device.RunDeviceAfter(); if (WaitNextTick()) { /// /// Executed when the state machine is stopped /// wlog.Fatal("quitStateMachine == true ... The last StopOperaions() start now"); State.StopOperations(); stateMachineRunning = false; wlog.Fatal("StopOperaions() completed ... stateMachineRunning = false)"); throw new QuitStateMachineException(); } foreach (var device in devices) device.RunDeviceBefore(); IList events = State.RunOperations(); return events; /// /// This is followed by a state change in the sequence /// } /// /// Wait time period - synchronize /// /// true when interrupted by 'quitStateMachine', otherwise false public static bool WaitNextTick() { currentTimeSec += Period; TimeSpan timeFromStart = TimeSpan.FromSeconds(currentTimeSec); DateTime nextLoopDateTime = startDateTime + timeFromStart; while (DateTime.Now < nextLoopDateTime) { Thread.Sleep(100); if (quitStateMachine) return true; } return false; } } }