/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using log4net; namespace TBF.UiBridge { /// /// Provides separation of the UI and the state machine. /// static class Bridge { static readonly ILog log = LogManager.GetLogger(typeof(Bridge)); public static readonly Queue Ui2MachineQueue; public static string SelectedProcedureName { get { return Program.MainWnd.ProcedureName; } } public static string SelectedTestName { get { return Program.MainWnd.BenchControlPanel.TestName; } } static Bridge() { Ui2MachineQueue = new Queue(); } /// /// This function should be called from the UI /// /// public static void Ui2Bench(UI2BenchCmd cmd) { lock (Ui2MachineQueue) { Ui2MachineQueue.Enqueue(cmd); } } /// /// This function should be called from state machine operations /// public static void Bench2UI(ButtonsEtc flags) { if (ButtonsEtcHandler == null) return; try { ButtonsEtcHandler(null, new ButtonsEtcEventArgs(flags)); } catch (Exception e) { log.Error("UiEventHandler(...) failed", e); } } public static event EventHandler ButtonsEtcHandler; /// /// Called from the state machine when a procedure is selected and UI needs to be updated. /// public static void OnProcedureSelected(object sender, ProcedureSelectedEventArgs data) { if (ProcedureSelectedHandler == null) return; try { ProcedureSelectedHandler(sender, data); } catch (Exception e) { log.Error("ProcedureSelectedHandler(...) failed", e); } } public static event EventHandler ProcedureSelectedHandler; /// /// Called from the state machine when a test is selected and UI needs to be updated. /// public static void OnTestSelected(object sender, TestSelectedEventArgs data) { if (TestSelectedHandler == null) return; try { TestSelectedHandler(sender, data); } catch (Exception e) { log.Error("TestSelectedHandler(...) failed", e); } } public static event EventHandler TestSelectedHandler; /// /// Called from the state machine when process data changed and UI needs to be updated. /// public static void OnProcessData(object sender, ProcessDataEventArgs data) { if (ProcessDataHandler == null) return; try { ProcessDataHandler(sender, data); } catch (Exception e) { log.Error("ProcessDataHandler(...) failed", e); } } public static event EventHandler ProcessDataHandler; /// /// Called from the state machine when progress bars need to be updated. /// public static void OnTestProgress(object sender, TestProgressEventArgs data) { if (TestProgressHandler == null) return; try { TestProgressHandler(sender, data); } catch (Exception e) { log.Error("TestProgressHandler(...) failed", e); } } public static event EventHandler TestProgressHandler; /// /// Called from the state machine when one test of the adjustment process was completed and UI needs to be updated. /// public static void OnAdjustmentInProgress(object sender, AdjustmentInProgressEventArgs data) { if (AdjustmentInProgressHandler == null) return; try { AdjustmentInProgressHandler(sender, data); } catch (Exception e) { log.Error("AdjustmentInProgressHandler(...) failed", e); } } public static event EventHandler AdjustmentInProgressHandler; /// /// Called from the state machine when the test is completed and UI needs to be updated. /// public static void OnTestCompleted(object sender, TestCompletedEventArgs data) { if (TestCompletedHandler == null) return; try { TestCompletedHandler(sender, data); } catch (Exception e) { log.Error("TestCompletedHandler(...) failed", e); } } public static event EventHandler TestCompletedHandler; /// /// Called from the state machine when the procedure is completed and UI needs to be updated. /// public static void OnProcedureCompleted(object sender, ProcedureCompletedEventArgs data) { if (ProcedureCompletedHandler == null) return; try { ProcedureCompletedHandler(sender, data); } catch (Exception e) { log.Error("ProcedureCompletedHandler(...) failed", e); } } public static event EventHandler ProcedureCompletedHandler; /// /// Called from the state machine when the procedure is completed and UI needs to be updated. /// public static void OnCameraStateChanged(object sender, CameraStateChangedEventArgs data) { if (CameraStateChangedHandler == null) return; try { CameraStateChangedHandler(sender, data); } catch (Exception e) { log.Error("CameraStateChangedHandler(...) failed", e); } } public static event EventHandler CameraStateChangedHandler; /// /// Called from the state machine when the procedure is completed and UI needs to be updated. /// public static void OnImageUpdated(object sender, ImageUpdatedEventArgs data) { if (ImageUpdatedHandler == null) return; try { ImageUpdatedHandler(sender, data); } catch (Exception e) { log.Error("ImageUpdatedHandler(...) failed", e); } } public static event EventHandler ImageUpdatedHandler; /// /// Called from the state machine when there is and information to be logged. /// public static void OnLog(object sender, string message) { if (LogHandler == null) return; try { LogHandler(sender, new LogEventArgs(message)); } catch (Exception e) { log.Error("LogHandler(...) failed", e); } } public static event EventHandler LogHandler; /// /// Called from the state machine when 'Activity' information changes. /// public static void OnActivity(object sender, string activity) { if (ActivityHandler == null) return; try { ActivityHandler(sender, new ActivityEventArgs(ActivityEventArgs.Update.Activity, activity, null, false)); } catch (Exception e) { log.Error("ActivityHandler(...) failed", e); } } public static void OnMessage(object sender, string message) { if (ActivityHandler == null) return; try { ActivityHandler(sender, new ActivityEventArgs(ActivityEventArgs.Update.Message, null, message, false)); } catch (Exception e) { log.Error("ActivityHandler(...) failed", e); } } public static void OnError(object sender, string message) { if (ActivityHandler == null) return; try { ActivityHandler(sender, new ActivityEventArgs(ActivityEventArgs.Update.Message, null, message, true)); } catch (Exception e) { log.Error("ActivityHandler(...) failed", e); } } public static event EventHandler ActivityHandler; /// /// 'State machine state has changed' event handler /// public static void OnStateChanged(object sender, string stateChangedMsg) { if (StateChangedHandler == null) return; try { StateChangedHandler(sender, new StateChangedEventArgs(stateChangedMsg)); } catch (Exception e) { string s = e.ToString(); } } public static event EventHandler StateChangedHandler; /// /// Called from the state machine when 'Activity' information changes. /// public static void OnEmergencyStopChanged(object sender, EmergencyStopEventArgs.State state) { if (EmergencyStopChangedHandler == null) return; try { EmergencyStopChangedHandler(sender, new EmergencyStopEventArgs(state)); } catch (Exception e) { log.Error("EmergencyStopHandler(...) failed", e); } } public static event EventHandler EmergencyStopChangedHandler; /// /// Called from the state machine when an operation forces a modeless dialog close. /// public static void OnCloseModelessForm(object sender, EventArgs args) { if (CloseModelessFormHandler == null) return; try { CloseModelessFormHandler(sender, null); } catch (Exception e) { log.Error("CloseModelessFormHandler(...) failed", e); } } public static event EventHandler CloseModelessFormHandler; } }