/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using log4net; using NHibernate; using Config.Entities; using Events.Entities; 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 int BatchNr; public static ProcedureInfo SelectedProcedure { get { return Program.MainWnd.SelectedProcedure; } } 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 one test of the pulses test process was completed and UI needs to be updated. /// public static void OnPulsesTestInProgress(object sender, PulsesTestInProgressEventArgs data) { if (PulsesTestInProgressHandler == null) return; try { PulsesTestInProgressHandler(sender, data); } catch (Exception e) { log.Error("PulsesTestInProgressHandler(...) failed", e); } } public static event EventHandler PulsesTestInProgressHandler; /// /// Called from the state machine when the test is completed and UI needs to be updated. /// public static void OnTestCompleted(object sender, TestCompletedEventArgs data) { TBF.BenchControl.Sequences.ProcessData.SaveProcessData(); 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 '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; /// /// Called from the state machine when a procedure is selected and UI needs to be updated. /// public static void OnCameraUICmd(object sender, CameraUICmdEventArgs data) { if (CameraUICmdHandler == null) return; try { CameraUICmdHandler(sender, data); } catch (Exception e) { log.Error("CameraUICmdHandler(...) failed", e); } } public static event EventHandler CameraUICmdHandler; /// /// Called from a camera when camera state (shown as a caption in PicturesTabPage) changes /// public static void OnCameraInfo(int cameraIdx, string info) { if (CameraInfoHandler == null) return; try { CameraInfoHandler(null, new CameraInfoEventArgs(cameraIdx, info)); } catch (Exception e) { log.Error("CameraInfoHandler(...) failed", e); } } public static event EventHandler CameraInfoHandler; /// /// Called from any RTP listener thread (e.g. MJpeg RTP listener thread) /// public static void OnImage(int cameraIdx, System.Drawing.Image image) { if (ImageHandler == null) return; try { ImageHandler(null, new ImageEventArgs(cameraIdx, image)); } catch (Exception e) { log.Error("ImageHandler(...) failed", e); } } public static event EventHandler ImageHandler; /// /// Called from TestMethods.Counter component when counters in LocalSettings should be saved. /// public static void OnSaveSettings(object sender, SaveSettingsEventArgs data) { if (SaveSettingsHandler == null) return; try { SaveSettingsHandler(sender, data); } catch (Exception e) { log.Error("SaveSettingsHandler(...) failed", e); } } public static event EventHandler SaveSettingsHandler; public static void TriggerEvent(ISession session, string source, EventClass clss, Severity severity, string message, string moreInfo, SubscriberGroup groups) { Events.Entities.Event evnt = new Event(null, source, clss, severity, message, moreInfo, null); Events.DB.SaveEvent(session, evnt, groups); if (EventHandler != null) { try { EventHandler(null, new EventEventArgs(evnt)); } catch (Exception e) { log.Error("EventHandler(...) failed", e); } } } public static void TriggerEvent(ISession session, Events.Entities.Event evnt) { Events.DB.SaveEvent(session, evnt, evnt.SubscriberGroups); if (EventHandler != null) { try { EventHandler(null, new EventEventArgs(evnt)); } catch (Exception e) { log.Error("EventHandler(...) failed", e); } } } public static event EventHandler EventHandler; } }