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

200 lines
7.6 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;
using System.Collections.Generic;
using System.Threading;
using NLog;
using Raytheon.Instruments.PowerSupply;
namespace ProgramLib
{
/// <summary>
/// Class to spawn thread to read power supply data
/// </summary>
internal class PowerSupplyReadThread : 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 string _powerSupplySystemName;
private List<string> _powerModuleNameList;
private int _pollRateMs;
Dictionary<string, PowerSupplyModuleInfo> _powerSupplyModuleInfoDict;
/// <summary>
/// Constructor
/// </summary>
public PowerSupplyReadThread(string powerSupplySystemName, int pollRateMs)
{
_logger = LogManager.GetCurrentClassLogger();
_powerSupplySystemName = powerSupplySystemName;
_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}() for {_powerSupplySystemName} 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);
_powerModuleNameList = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.GetPowerSupplyList(_powerSupplySystemName);
_powerSupplyModuleInfoDict = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.GetPowerSupplyModuleInfoDict(_powerSupplySystemName);
while (true)
{
Events id = eventGroup.WaitAny();
if (id == Events.UUT_POWER_ON)
{
ReadPowerSupplyData();
}
else
break;
}
}
catch (Exception ex)
{
_logger?.Error(ex.Message + "\n" + ex.StackTrace);
}
_logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() for {_powerSupplySystemName} is exiting...");
}
/// <summary>
/// Read power supply data and check for faults
/// </summary>
/// <param name=""></param>
/// <returns></returns>
private void ReadPowerSupplyData()
{
_logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() for {_powerSupplySystemName} 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);
while (true)
{
Events id = eventGroup.WaitAny(_pollRateMs);
if (id == Events.EVENT_TIMED_OUT)
{
foreach (string moduleName in _powerModuleNameList)
{
PowerData data = Program.Instance().MalMeasurementLibManager.PowerSupplyMeasurementManager.ReadPowerData(moduleName);
BitArray statusReg = new BitArray(new int[] { data.FaultStatus });
bool ovpTriggeredInPowerSupply = statusReg[0];
bool ocpTriggeredInPowerSupply = statusReg[1];
if (!ovpTriggeredInPowerSupply && !ocpTriggeredInPowerSupply)
{
Program.Instance().PowerSupplySharedData.SetData(moduleName, data.Voltage, data.Current, _powerSupplyModuleInfoDict[moduleName]);
CheckVoltageWithinLimits(moduleName, data.Voltage);
}
else
{
string errorMsg = String.Empty;
if (ovpTriggeredInPowerSupply)
{
errorMsg = moduleName + "'s OVP circuitry has tripped";
}
else if (ocpTriggeredInPowerSupply)
{
errorMsg = moduleName + "'s OCP circuitry has tripped";
}
throw new Exception(errorMsg);
}
}
}
else
break;
}
}
catch (Exception ex)
{
if (!Program.Instance().EventManager[EventManager.Events.FATAL_FAILURE].WaitOne(0))
{
Program.Instance().SetFatalFailureExceptionFromSupportThread(ex);
Program.Instance().EventManager[EventManager.Events.FATAL_FAILURE].Set();
}
}
_logger?.Debug($"{this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() for {_powerSupplySystemName} is exiting...");
}
/// <summary>
/// Check for voltage's limit exceedence
/// </summary>
/// <param name=""></param>
/// <returns></returns>
private void CheckVoltageWithinLimits(string moduleName, double voltage)
{
PowerSupplyModuleInfo powerSupplyModuleInfo = _powerSupplyModuleInfoDict[moduleName];
if ((voltage < powerSupplyModuleInfo.voltageLowerLimit_ || voltage > powerSupplyModuleInfo.voltageUpperLimit_) && powerSupplyModuleInfo.isOn_)
{
string errorMsg = moduleName + "'s voltage is out of limits (" + powerSupplyModuleInfo.voltageLowerLimit_.ToString("0.00") + "V, " + powerSupplyModuleInfo.voltageUpperLimit_.ToString("0.00") + "V). Voltage reading: " + voltage.ToString("0.00") + "V";
throw new Exception(errorMsg);
}
}
}
}