using Opc.Ua;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Models;
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;
///
/// Maps between the library's SecurityMode enum and OPC UA SDK MessageSecurityMode.
///
public static class SecurityModeMapper
{
/// Converts a SecurityMode to an OPC UA MessageSecurityMode.
/// The security mode to convert.
/// The corresponding message security mode.
public static MessageSecurityMode ToMessageSecurityMode(SecurityMode mode)
{
return mode switch
{
SecurityMode.None => MessageSecurityMode.None,
SecurityMode.Sign => MessageSecurityMode.Sign,
SecurityMode.SignAndEncrypt => MessageSecurityMode.SignAndEncrypt,
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, "Unknown SecurityMode value.")
};
}
///
/// Parses a string to a value, case-insensitively.
///
/// The string to parse (e.g., "none", "sign", "encrypt", "signandencrypt").
/// The corresponding SecurityMode.
/// Thrown for unrecognized values.
public static SecurityMode FromString(string value)
{
return (value ?? "none").Trim().ToLowerInvariant() switch
{
"none" => SecurityMode.None,
"sign" => SecurityMode.Sign,
"encrypt" or "signandencrypt" => SecurityMode.SignAndEncrypt,
_ => throw new ArgumentException(
$"Unknown security mode '{value}'. Valid values: none, sign, encrypt, signandencrypt")
};
}
}