243 lines
9.0 KiB
C#
243 lines
9.0 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.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using NLog;
|
|
using ProgramLib.GUI.Model;
|
|
using ProgramLib.GUI.View;
|
|
|
|
namespace ProgramLib
|
|
{
|
|
/// <summary>
|
|
/// Class to spawn thread to update power supply data on GUI
|
|
/// </summary>
|
|
internal class PowerSupplyUpdateThread : 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 Dictionary<string, PowerModuleDataModel> _powerModuleToPowerDataModelDict = new Dictionary<string, PowerModuleDataModel>(StringComparer.OrdinalIgnoreCase);
|
|
int _pollRateMs;
|
|
private StreamWriter _fileWriter;
|
|
private List<string> _powerModulesToBeDisplayed = new List<string>();
|
|
private string _logNamePrefix;
|
|
private string _logFileExtension;
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
public PowerSupplyUpdateThread(int pollRateMs, string logNamePrefix, string logFileExtension)
|
|
{
|
|
try
|
|
{
|
|
_logger = LogManager.GetCurrentClassLogger();
|
|
|
|
_pollRateMs = pollRateMs;
|
|
|
|
_logNamePrefix = logNamePrefix;
|
|
|
|
_logFileExtension = logFileExtension;
|
|
|
|
_liveDataWindow = (LiveDataWindow)ProgramLib.Program.Instance().GuiManager[ProgramGuiManager.WINDOWS.LIVE_DATA];
|
|
|
|
ParseConfig();
|
|
|
|
foreach (string powerModule in _powerModulesToBeDisplayed)
|
|
{
|
|
if (!_powerModuleToPowerDataModelDict.ContainsKey(powerModule))
|
|
{
|
|
_powerModuleToPowerDataModelDict[powerModule] = new PowerModuleDataModel();
|
|
_powerModuleToPowerDataModelDict[powerModule].Name = powerModule;
|
|
_liveDataWindow.Dispatcher.Invoke((Action)delegate
|
|
{
|
|
_liveDataWindow.LiveDataWindowViewModel.AddPowerData(_powerModuleToPowerDataModelDict[powerModule]);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
UpdatePowerSupplyData();
|
|
}
|
|
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>
|
|
/// Log and update GUI with power supply data
|
|
/// </summary>
|
|
/// <param name=""></param>
|
|
/// <returns></returns>
|
|
private void UpdatePowerSupplyData()
|
|
{
|
|
_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);
|
|
|
|
const string LOG_PREFIX = "DateTime,Module,Voltage,VoltageSetpoint,Current,IsOutputOn";
|
|
string logFilePath = Path.Combine(Program.Instance().FileAndFolderManager.GetFolder(FileAndFolderManager.Folders.DATA_TEST), Util.GenerateUniqueFilenameUsingDateTime(_logNamePrefix, _logFileExtension));
|
|
_fileWriter = new StreamWriter(logFilePath);
|
|
_fileWriter.Write(LOG_PREFIX);
|
|
_fileWriter.Flush();
|
|
|
|
while (true)
|
|
{
|
|
Events id = eventGroup.WaitAny(_pollRateMs);
|
|
|
|
if (id == Events.EVENT_TIMED_OUT)
|
|
{
|
|
string logText = String.Empty;
|
|
foreach (string powerModule in Program.Instance().PowerSupplySharedData.GetAllPowerModules())
|
|
{
|
|
PowerSupplyData data = Program.Instance().PowerSupplySharedData.GetData(powerModule);
|
|
|
|
if (data != null && data.Initialized)
|
|
{
|
|
if (_powerModuleToPowerDataModelDict.ContainsKey(powerModule))
|
|
{
|
|
_powerModuleToPowerDataModelDict[powerModule].ActualVoltage = data.Voltage.ToString("0.00");
|
|
_powerModuleToPowerDataModelDict[powerModule].ExpectedVoltage = data.PowerSupplyModuleInfo.voltageSetpoint_.ToString("0.00");
|
|
_powerModuleToPowerDataModelDict[powerModule].ActualCurrent = data.Current.ToString("0.00");
|
|
}
|
|
}
|
|
|
|
if (!String.IsNullOrEmpty(logText))
|
|
logText += "\r\n";
|
|
|
|
logText += DateTime.Now.ToString("yyyy_MM_dd") + "_" + DateTime.Now.ToString("HH_mm_ss") + "," + powerModule + "," + data.Voltage.ToString("0.00") +
|
|
"," + data.PowerSupplyModuleInfo.voltageSetpoint_.ToString("0.00") + "," + data.Current.ToString("0.00") + "," + data.PowerSupplyModuleInfo.isOn_.ToString();
|
|
|
|
}
|
|
|
|
// log out the data
|
|
_fileWriter.WriteLine(logText);
|
|
_fileWriter.Flush();
|
|
}
|
|
else
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
if (_fileWriter != null)
|
|
{
|
|
_fileWriter.Flush();
|
|
_fileWriter.Close();
|
|
_fileWriter = null;
|
|
}
|
|
|
|
foreach (KeyValuePair<string, PowerModuleDataModel> item in _powerModuleToPowerDataModelDict)
|
|
{
|
|
item.Value.ExpectedVoltage = "";
|
|
item.Value.ActualVoltage = "";
|
|
item.Value.ActualCurrent = "";
|
|
}
|
|
}
|
|
|
|
_logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() is exiting...");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse ini file
|
|
/// </summary>
|
|
public void ParseConfig()
|
|
{
|
|
try
|
|
{
|
|
List<string> keys = Program.Instance().ProgramSpecificConfig.ReadAllKeys(ProgramSpecificConfigIni.POWER_MODULES_TO_BE_DISPLAYED.ToString());
|
|
foreach (string key in keys)
|
|
{
|
|
string powerSupplyModule = Program.Instance().ProgramSpecificConfig.ReadValue(ProgramSpecificConfigIni.POWER_MODULES_TO_BE_DISPLAYED.ToString(), key);
|
|
|
|
if (!_powerModulesToBeDisplayed.Contains(powerSupplyModule, StringComparer.OrdinalIgnoreCase))
|
|
_powerModulesToBeDisplayed.Add(powerSupplyModule);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|