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

235 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.Text;
using NLog;
using ProgramLib.GUI.View;
namespace ProgramLib
{
/// <summary>
/// Send Test Messages to UUT via TCP sockets
/// </summary>
internal class SendTestMessageToUutAction : BasicAction
{
#region PrivateClassMembers
private static NLog.ILogger _logger;
private string _messageName;
private string _iniKeyForData;
#endregion
/// <summary>
/// Constructor
/// </summary>
/// <param name="messageName">name of the message that is defined in the UutTestMessages.XML file</param>
/// <param name="iniKeyForData">section name in the ProgramSpecific.ini that specifies the values for data fields in this message</param>
/// <returns></returns>
public SendTestMessageToUutAction(string messageName, string iniKeyForData = null)
{
_logger = LogManager.GetCurrentClassLogger();
_messageName = messageName;
_iniKeyForData = iniKeyForData;
}
/// <summary>
/// Run the action
/// </summary>
/// <returns></returns>
public override void Run()
{
try
{
_logger?.Debug($"Entering {this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() method...");
if (Program.Instance().IsUutPwrOn)
{
SendMessageToUut();
}
else
{
throw new Exception("Cannot send message to UUT because UUT is not powered on.");
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Send message to UUT
/// </summary>
/// <returns></returns>
private void SendMessageToUut()
{
string msfrTestCase = null;
if (Program.Instance().EthernetSocketManager.GetTcpClient(TcpClientNames.UUT_TEST_PORT) != null)
{
if (String.Equals(_messageName, "MSFRParameterMsg", StringComparison.OrdinalIgnoreCase))
{
MsfrTestCasesWindow win = (MsfrTestCasesWindow)ProgramLib.Program.Instance().GuiManager[ProgramLib.ProgramGuiManager.WINDOWS.MSFR_TEST_CASES];
ProgramLib.Program.Instance().GuiManager[ProgramGuiManager.WINDOWS.DEFAULT].Dispatcher.Invoke((Action)delegate
{
if (win.cbMsfrTestCases.SelectedItem != null)
msfrTestCase = win.cbMsfrTestCases.SelectedValue.ToString();
});
if (!String.IsNullOrEmpty(msfrTestCase))
{
int index = msfrTestCase.IndexOf('.');
_iniKeyForData = $"TEST_CASE_{msfrTestCase.Substring(0, index)}";
}
else
throw new Exception("No MSFR test case was specified");
}
byte[] byteArray = ConstructByteArrayFromMessageData();
Program.Instance().EthernetSocketManager.GetTcpClient(TcpClientNames.UUT_TEST_PORT).Write(byteArray);
string desc = $"Sent {_messageName}.";
if (String.Equals(_messageName, "MSFRParameterMsg", StringComparison.OrdinalIgnoreCase))
desc += $" Test case: {msfrTestCase}";
_logger.Info(desc);
}
else
{
throw new Exception("Cannot send message to UUT because socket connection hasn't been established.");
}
}
/// <summary>
/// Pack all data in the message to byte array to be sent
/// </summary>
/// <returns></returns>
private byte[] ConstructByteArrayFromMessageData()
{
List<byte> byteList = new List<byte>();
string val = String.Empty;
string sourceError = String.Empty;
if (Program.Instance().UutInfo.UutTestMessageDict.ContainsKey(_messageName))
{
string[] dataArray = Program.Instance().ProgramSpecificConfig.ReadValue(_messageName, _iniKeyForData).Split(',');
int dataArrayIndex = -1;
if (String.Equals(_messageName, "MSFRParameterMsg", StringComparison.OrdinalIgnoreCase))
dataArrayIndex++;
foreach (UutTestMessageDataFieldInfo item in Program.Instance().UutInfo.UutTestMessageDict[_messageName])
{
dataArrayIndex++;
val = item.Value;
if (String.Equals(item.Value, "not_set", StringComparison.OrdinalIgnoreCase))
{
sourceError = $"Key: {_iniKeyForData}. Section: {_messageName}. File: {Program.Instance().ProgramSpecificConfig.FileName}";
val = dataArray[dataArrayIndex].Trim();
}
else
{
sourceError = $"Message: {_messageName}. Data Field: {item.Name}. File: {Program.Instance().UutInfo.UutTestMessageConfigFilePath}";
}
string type = item.Type.ToLower();
switch (type)
{
case "string":
if (val.Length > item.ByteCount)
{
throw new Exception($"The value {val} exceeds the allowable length of {item.ByteCount} characters. {sourceError}");
}
else if (val.Length < item.ByteCount)
{
for (int i = val.Length; i < item.ByteCount; i++)
{
val += '\0';
}
}
byteList.AddRange(Encoding.ASCII.GetBytes(val));
break;
case "bool":
if (val == "1")
val = "true";
else if (val == "0")
val = "false";
if (bool.TryParse(val, out bool boolVal))
{
byteList.Add(Convert.ToByte(boolVal));
}
else
{
throw new Exception($"The value {val} is not of type {item.Type}. It must be either true/false. {sourceError}");
}
break;
case "uint8":
if (byte.TryParse(val, out byte byteVal))
{
byteList.Add(byteVal);
}
else
{
throw new Exception($"The value {val} is not of type {item.Type}. {sourceError}");
}
break;
case "uint32":
if (UInt32.TryParse(val, out UInt32 uint32Val))
{
byteList.AddRange(BitConverter.GetBytes(uint32Val));
}
else
{
throw new Exception($"The value {val} is not of type {item.Type}. {sourceError}");
}
break;
case "double":
if (Double.TryParse(val, out Double doubleVal))
{
byteList.AddRange(BitConverter.GetBytes(doubleVal));
}
else
{
throw new Exception($"The value {val} is not of type {item.Type}. {sourceError}");
}
break;
case "float":
if (float.TryParse(val, out float floatVal))
{
byteList.AddRange(BitConverter.GetBytes(floatVal));
}
else
{
throw new Exception($"The value {val} is not of type {item.Type}. {sourceError}");
}
break;
default:
throw new Exception($"{item.Type} is not a supported type. {sourceError}");
}
}
}
else
{
throw new Exception($"Message {_messageName} does not exist in {Program.Instance().UutInfo.UutTestMessageConfigFilePath}.");
}
return byteList.ToArray();
}
}
}