tbf/TestBenchFramework/UiBridge/Bridge.cs
2014-07-28 09:56:40 +02:00

189 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
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));
static Queue<UI2BenchCmd> ui2machineQueue;
public static Queue<UI2BenchCmd> Ui2MachineQueue { get { return 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<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 test process data change and UI needs 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 the test is completed and UI needs to be updated.
/// </summary>
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<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 the procedure is completed and UI needs to be updated.
/// </summary>
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<CameraStateChangedEventArgs> CameraStateChangedHandler;
/// <summary>
/// Called from the state machine when the procedure is completed and UI needs to be updated.
/// </summary>
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<ImageUpdatedEventArgs> ImageUpdatedHandler;
/// <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(activity)); }
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;
}
}