tbf/TBF/UiBridge/Bridge.cs

271 lines
11 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using Config.Entities;
using log4net;
namespace TBF.UiBridge
{
/// <summary>
/// Provides separation of the UI and the state machine.
/// </summary>
static class Bridge
{
static readonly ILog log = LogManager.GetLogger(typeof(Bridge));
public static readonly Queue<UI2BenchCmd> 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<UI2BenchCmd>();
}
/// <summary>
/// This function should be called from the UI
/// </summary>
/// <param name="cmd"></param>
public static void Ui2Bench(UI2BenchCmd cmd)
{
lock (Ui2MachineQueue)
{
Ui2MachineQueue.Enqueue(cmd);
}
}
/// <summary>
/// This function should be called from state machine operations
/// </summary>
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<ButtonsEtcEventArgs> ButtonsEtcHandler;
/// <summary>
/// Called from the state machine when a procedure is selected and UI needs to be updated.
/// </summary>
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<ProcedureSelectedEventArgs> ProcedureSelectedHandler;
/// <summary>
/// Called from the state machine when a test is selected and UI needs to be updated.
/// </summary>
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<TestSelectedEventArgs> TestSelectedHandler;
/// <summary>
/// Called from the state machine when process data changed and UI needs to be updated.
/// </summary>
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<ProcessDataEventArgs> ProcessDataHandler;
/// <summary>
/// Called from the state machine when progress bars need to be updated.
/// </summary>
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<TestProgressEventArgs> TestProgressHandler;
/// <summary>
/// Called from the state machine when one test of the adjustment process was completed and UI needs to be updated.
/// </summary>
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<AdjustmentInProgressEventArgs> AdjustmentInProgressHandler;
/// <summary>
/// Called from the state machine when one test of the pulses test process was completed and UI needs to be updated.
/// </summary>
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<PulsesTestInProgressEventArgs> PulsesTestInProgressHandler;
/// <summary>
/// Called from the state machine when the test is completed and UI needs to be updated.
/// </summary>
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<TestCompletedEventArgs> TestCompletedHandler;
/// <summary>
/// Called from the state machine when the procedure is completed and UI needs to be updated.
/// </summary>
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<ProcedureCompletedEventArgs> ProcedureCompletedHandler;
/// <summary>
/// Called from the state machine when there is and information to be logged.
/// </summary>
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<LogEventArgs> LogHandler;
/// <summary>
/// Called from the state machine when 'Activity' information changes.
/// </summary>
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<ActivityEventArgs> ActivityHandler;
/// <summary>
/// 'State machine state has changed' event handler
/// </summary>
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<StateChangedEventArgs> StateChangedHandler;
/// <summary>
/// Called from the state machine when 'Activity' information changes.
/// </summary>
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<EmergencyStopEventArgs> EmergencyStopChangedHandler;
/// <summary>
/// Called from the state machine when an operation forces a modeless dialog close.
/// </summary>
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<EventArgs> CloseModelessFormHandler;
/// <summary>
/// Called from the state machine when a procedure is selected and UI needs to be updated.
/// </summary>
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<CameraUICmdEventArgs> CameraUICmdHandler;
/// <summary>
/// Called from a camera when camera state (shown as a caption in PicturesTabPage) changes
/// </summary>
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<CameraInfoEventArgs> CameraInfoHandler;
/// <summary>
/// Called from any RTP listener thread (e.g. MJpeg RTP listener thread)
/// </summary>
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<ImageEventArgs> ImageHandler;
/// <summary>
/// Called from TestMethods.Counter component when counters in LocalSettings should be saved.
/// </summary>
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<SaveSettingsEventArgs> SaveSettingsHandler;
}
}