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

254 lines
11 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.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using NLog;
using ProgramLib.GUI.Model;
using ProgramLib.GUI.View;
using ProgramLib.GUI.ViewModel;
using Raytheon.Common.PdelWriter.Utilities;
using static MeasurementManagerLib.SwitchMeasurementManager;
namespace ProgramLib
{
/// <summary>
/// Perform Safe-to-turn-on (STTO)
/// </summary>
internal class PerformSttoAction : BasicAction
{
private List<string> _dmmResistanceMeasurements = new List<string>();
private static NLog.ILogger _logger;
private Exception _exceptionFromSuportThread;
public PerformSttoAction()
{
_logger = LogManager.GetCurrentClassLogger();
}
/// <summary>
/// Run the action
/// </summary>
public override void Run()
{
// No need to perform STTO when connected to real missile because signals are not looped back to the switch matrix
if (!Program.Instance().IsThereHardware || Program.Instance().UutInfo.UutBuildLevel == UutInfo.BuildLevel.SELF_TEST)
{
ParseDmmResistanceMeasurementList();
Task.Factory.StartNew(() => PerformSttoTask());
ProgramLib.Program.Instance().GuiManager[ProgramGuiManager.WINDOWS.DEFAULT].Dispatcher.Invoke((Action)delegate
{
ProgramLib.Program.Instance().GuiManager[ProgramGuiManager.WINDOWS.IMPEDANCE_CHECK].ShowDialog();
});
if (_exceptionFromSuportThread != null)
{
throw new Exception("Check inner exception.", _exceptionFromSuportThread);
}
RecordDateOfSucessfulSelfTestRunToLog();
}
else
throw new Exception("Please connect W5 cable to perform STTO checks");
}
/// <summary>
/// Record date of successful self test run to log
/// </summary>
private void RecordDateOfSucessfulSelfTestRunToLog()
{
if (Program.Instance().IsThereHardware)
{
XmlDocumentWrapper doc = new XmlDocumentWrapper(Program.Instance().FileAndFolderManager.GetFile(FileAndFolderManager.Files.CABLE_SELF_TEST_RUN_LOG));
XmlNode node = doc.GetNode(CableSelfTestConfigXml.SelfTestPath);
bool existingNodeFound = false;
while (node != null)
{
if (node.Attributes[CableSelfTestConfigXml.UniversalCableAttributeName] != null && String.Equals(node.Attributes[CableSelfTestConfigXml.UniversalCableAttributeName].Value, Program.Instance().UutInfo.UniversalCableId.ToString(), StringComparison.OrdinalIgnoreCase)
&& node.Attributes[CableSelfTestConfigXml.SacrificialCableAttributeName] != null && String.Equals(node.Attributes[CableSelfTestConfigXml.SacrificialCableAttributeName].Value, Program.Instance().UutInfo.SacrificialCableId.ToString(), StringComparison.OrdinalIgnoreCase))
{
Dictionary<string, string> attributesDict = new Dictionary<string, string>();
attributesDict[CableSelfTestConfigXml.LastRunDateAttributeName] = DateTime.Now.ToString("MM/dd/yyyy");
doc.ChangeNode(node, attributesDict);
existingNodeFound = true;
break;
}
node = node.NextSibling;
}
if (!existingNodeFound)
{
Dictionary<string, string> attributesDict = new Dictionary<string, string>();
attributesDict[CableSelfTestConfigXml.UniversalCableAttributeName] = Program.Instance().UutInfo.UniversalCableId.ToString();
attributesDict[CableSelfTestConfigXml.SacrificialCableAttributeName] = Program.Instance().UutInfo.SacrificialCableId.ToString();
attributesDict[CableSelfTestConfigXml.LastRunDateAttributeName] = DateTime.Now.ToString("MM/dd/yyyy");
doc.AddNode(CableSelfTestConfigXml.SelfTestPath, attributesDict);
}
doc.SaveToFile();
}
}
/// <summary>
/// Parse resistance measurement list
/// </summary>
private void ParseDmmResistanceMeasurementList()
{
string SECTION_NAME = "_STTO";
string cable = "W3";
if (ProgramLib.Program.Instance().UutInfo.SacrificialCableId == UutInfo.SacrificialCable.W4)
{
cable = "W4";
}
SECTION_NAME = cable + SECTION_NAME;
List<string> keys = Program.Instance().ProgramSpecificConfig.ReadAllKeys(SECTION_NAME);
foreach (string key in keys)
{
_dmmResistanceMeasurements.Add(Program.Instance().ProgramSpecificConfig.ReadValue(SECTION_NAME, key));
}
}
/// <summary>
/// Perform Safe-to-turn-on checks
/// </summary>
private void PerformSttoTask()
{
ImpedanceDataModel impedanceDataModel;
ImpedanceCheckWindow impedanceCheckWindow = (ImpedanceCheckWindow)ProgramLib.Program.Instance().GuiManager[ProgramGuiManager.WINDOWS.IMPEDANCE_CHECK];
bool sttoSuccess = true;
int firstFailIndex = -1;
try
{
_logger?.Debug($"{this.GetType().Name}::PerformSttoTask() running...");
string measurementStatus = "PASSED";
ImpedanceCheckWindowViewModel.Images passFailImage = ImpedanceCheckWindowViewModel.Images.PASS_CHECK;
impedanceCheckWindow.ViewModel.ClearData();
string sttoFailMsg = String.Empty;
int loopCount = 0;
foreach (string measurement in _dmmResistanceMeasurements)
{
TestResult pdelResultInfo = new TestResult();
measurementStatus = "PASSED";
passFailImage = ImpedanceCheckWindowViewModel.Images.PASS_CHECK;
impedanceDataModel = new ImpedanceDataModel();
DMMResistanceMeasurementFields dmmResistanceMeasurementInfo = Program.Instance().MalMeasurementLibManager.SwitchMeasurementManager.DmmResistanceMeasurements[measurement];
double testResult = Program.Instance().MalMeasurementLibManager.SwitchMeasurementManager.DmmReadResistance(measurement);
if (testResult < dmmResistanceMeasurementInfo._lowerLimit || testResult > dmmResistanceMeasurementInfo._upperLimit)
{
passFailImage = ImpedanceCheckWindowViewModel.Images.FAIL_CHECK;
measurementStatus = "FAILED";
sttoSuccess = false;
}
string cableAndPinId = dmmResistanceMeasurementInfo._cableAndPinId;
if (Program.Instance().UutInfo.UniversalCableId == UutInfo.UniversalCable.W2)
cableAndPinId = Regex.Replace(cableAndPinId, UutInfo.UniversalCable.W1.ToString() + @"_", UutInfo.UniversalCable.W2.ToString() + @"_", RegexOptions.IgnoreCase);
impedanceDataModel.PassFailImagePath = impedanceCheckWindow.ViewModel.ImageToResourcePathDict[passFailImage];
impedanceDataModel.Description = $"{measurement}, {cableAndPinId} Measured {Util.AutoFormatNumberToString(testResult)} Range [{Util.AutoFormatNumberToString(dmmResistanceMeasurementInfo._lowerLimit)}, {Util.AutoFormatNumberToString(dmmResistanceMeasurementInfo._upperLimit)}]";
if (sttoSuccess == false && String.IsNullOrEmpty(sttoFailMsg))
{
sttoFailMsg = impedanceDataModel.Description;
}
if (Program.TestStandSeqContext != null)
{
Program.TestStandSeqContext.Step.AdditionalResults.CustomResults.Insert($"\"{measurement}, {cableAndPinId}\"", $"\"Measured: {Util.AutoFormatNumberToString(testResult)} Range [{Util.AutoFormatNumberToString(dmmResistanceMeasurementInfo._lowerLimit)}, {Util.AutoFormatNumberToString(dmmResistanceMeasurementInfo._upperLimit)}] - {measurementStatus}\"");
TestStand.ProgramManager.SaveOriginalStepSetting(ProgramLib.Program.TestStandSeqContext.Step.StepIndex, ProgramLib.Program.TestStandSeqContext.StepGroup, ProgramLib.Program.TestStandSeqContext.Step.PostExpression, true);
}
impedanceCheckWindow.ViewModel.AddData(impedanceDataModel);
pdelResultInfo.PCode = dmmResistanceMeasurementInfo._pcode;
pdelResultInfo.TestName = dmmResistanceMeasurementInfo._cableAndPinId;
pdelResultInfo.MeasuredValue = Math.Round(testResult, 5);
pdelResultInfo.Result = PassFailStatus.Fail;
if (measurementStatus == "PASSED")
{
pdelResultInfo.Result = PassFailStatus.Pass;
}
ProgramLib.Program.Instance().PcodeTestResultList.List.Add(pdelResultInfo);
if (!sttoSuccess && firstFailIndex == -1)
{
firstFailIndex = loopCount;
}
loopCount++;
}
if (!sttoSuccess)
{
throw new Exception(sttoFailMsg);
}
}
catch (Exception ex)
{
_exceptionFromSuportThread = ex;
}
finally
{
ProgramLib.Program.Instance().PdelData.TestStopTime = DateTime.Now;
if (!sttoSuccess)
{
impedanceCheckWindow.Dispatcher.Invoke((Action)delegate
{
impedanceCheckWindow.btnClose.Visibility = Visibility.Visible;
// scroll to the first failed item
if (firstFailIndex != -1)
{
impedanceCheckWindow.lvImpedanceCheck.ScrollIntoView(impedanceCheckWindow.lvImpedanceCheck.Items.GetItemAt(firstFailIndex));
}
});
}
else
{
impedanceCheckWindow.Dispatcher.Invoke((Action)delegate
{
impedanceCheckWindow.Hide();
});
}
_logger?.Debug($"{this.GetType().Name}::PerformSttoTask() exiting...");
}
}
}
}