Files
GenericTeProgramLibrary/Source/Program/Threads/PassthroughDataUpdateThread.cs
2025-10-24 15:18:11 -07:00

187 lines
6.7 KiB
C#

/*-------------------------------------------------------------------------
// UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.
THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Threading;
using NLog;
using ProgramLib.GUI.Model;
using ProgramLib.GUI.View;
using ProgramLib.GUI.ViewModel;
namespace ProgramLib
{
/// <summary>
/// Class to spawn thread to update passthrough data
/// </summary>
internal class PassthroughDataUpdateThread : BasicThread
{
private enum Events
{
GLOBAL_QUIT,
QUIT,
UUT_POWER_ON,
UUT_POWER_OFF,
// DO NOT change the name
// This must be the last member in the enum
EVENT_TIMED_OUT
}
private ILogger _logger;
private LiveDataWindow _liveDataWindow;
private PassthroughData _passthroughData = null;
int _pollRateMs;
/// <summary>
/// Constructor
/// </summary>
public PassthroughDataUpdateThread(int pollRateMs)
{
_logger = LogManager.GetCurrentClassLogger();
_liveDataWindow = (LiveDataWindow)ProgramLib.Program.Instance().GuiManager[ProgramGuiManager.WINDOWS.LIVE_DATA];
_passthroughData = new PassthroughData(_liveDataWindow.datagridPassthroughData.Columns.Count);
_pollRateMs = pollRateMs;
_liveDataWindow.Dispatcher.Invoke((Action)delegate
{
_liveDataWindow.LiveDataWindowViewModel.AddPassthroughData(_passthroughData.GetPassthroughDataModelDict());
});
_pollRateMs = pollRateMs;
}
/// <summary>
/// Method that executes on the thread.
/// </summary>
/// <param name=""></param>
/// <returns></returns>
protected override void DoWork()
{
_logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() is running...");
try
{
Dictionary<Events, EventWaitHandle> eventDict = new Dictionary<Events, EventWaitHandle>();
eventDict[Events.GLOBAL_QUIT] = Program.Instance().EventManager[EventManager.Events.GLOBAL_QUIT];
eventDict[Events.QUIT] = _quitEvent;
eventDict[Events.UUT_POWER_ON] = Program.Instance().EventManager[EventManager.Events.UUT_POWER_ON];
eventDict[Events.EVENT_TIMED_OUT] = null;
EventGroup<Events, EventWaitHandle> eventGroup = new EventGroup<Events, EventWaitHandle>(eventDict);
while (true)
{
Events id = eventGroup.WaitAny();
if (id == Events.UUT_POWER_ON)
{
UpdatePassthroughData();
}
else
break;
}
}
catch (Exception ex)
{
_logger?.Error(ex.Message + "\n" + ex.StackTrace);
}
_logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() is exiting...");
}
/// <summary>
/// Update GUI with data
/// </summary>
/// <param name=""></param>
/// <returns></returns>
private void UpdatePassthroughData()
{
_logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() is running...");
try
{
Dictionary<Events, EventWaitHandle> eventDict = new Dictionary<Events, EventWaitHandle>();
eventDict[Events.GLOBAL_QUIT] = Program.Instance().EventManager[EventManager.Events.GLOBAL_QUIT];
eventDict[Events.QUIT] = _quitEvent;
eventDict[Events.UUT_POWER_OFF] = Program.Instance().EventManager[EventManager.Events.UUT_POWER_OFF];
eventDict[Events.EVENT_TIMED_OUT] = null;
EventGroup<Events, EventWaitHandle> eventGroup = new EventGroup<Events, EventWaitHandle>(eventDict);
_liveDataWindow.LiveDataWindowViewModel.UutPowerLedImagePath = _liveDataWindow.LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_ON];
while (true)
{
Events id = eventGroup.WaitAny(_pollRateMs);
if (id == Events.EVENT_TIMED_OUT)
{
if (!Program.Instance().IsThereHardware)
SimulatePassThroughData();
}
else
break;
}
}
catch (Exception)
{
throw;
}
finally
{
_passthroughData.BlankAllData();
_liveDataWindow.LiveDataWindowViewModel.UutPowerLedImagePath = _liveDataWindow.LiveDataWindowViewModel.ImageToResourcePathDict[LiveDataWindowViewModel.Images.LED_OFF];
}
_logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() is exiting...");
}
/// <summary>
/// Simulate passthrough data
/// </summary>
private void SimulatePassThroughData()
{
Random rnd = new Random();
float num = 70.0f + Util.GenerateRandomFraction();
_passthroughData.SetValue(PassthroughData.Variables.VAR01, num.ToString("0.00"));
Thread.Sleep(100);
num = 30.0f + Util.GenerateRandomFraction();
_passthroughData.SetValue(PassthroughData.Variables.VAR02, num.ToString("0.00"));
Thread.Sleep(200);
num = 40.0f + Util.GenerateRandomFraction();
_passthroughData.SetValue(PassthroughData.Variables.VAR03, num.ToString("0.00"));
Thread.Sleep(100);
num = 50.0f + Util.GenerateRandomFraction();
_passthroughData.SetValue(PassthroughData.Variables.VAR04, num.ToString("0.00"));
Thread.Sleep(100);
num = 60.0f + Util.GenerateRandomFraction();
_passthroughData.SetValue(PassthroughData.Variables.VAR05, num.ToString("0.00"));
Thread.Sleep(200);
num = 10.0f + Util.GenerateRandomFraction();
_passthroughData.SetValue(PassthroughData.Variables.VAR06, num.ToString("0.00"));
}
}
}