Files
mxaccessgw/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/SyntheticAlarmGuid.cs
T

43 lines
1.8 KiB
C#

using System;
using System.Security.Cryptography;
using System.Text;
namespace ZB.MOM.WW.MxGateway.Worker.MxAccess;
/// <summary>
/// Derives a deterministic synthetic <see cref="Guid"/> from an alarm
/// reference for the subtag-provider fallback path, which has no native
/// MxAccess alarm GUID. Hashing the reference yields a stable identity so
/// repeated transitions for the same alarm reference correlate downstream
/// (acknowledge, snapshot, OPC UA mapping) without an alarmmgr-supplied GUID.
/// </summary>
public static class SyntheticAlarmGuid
{
/// <summary>
/// Produces a stable <see cref="Guid"/> for the given alarm reference.
/// The same reference always maps to the same GUID; distinct references
/// map to distinct GUIDs with overwhelming probability.
/// </summary>
/// <param name="reference">
/// The fully-qualified alarm reference (for example
/// <c>"Galaxy!Area.Tag.HiHi"</c>). Treated as UTF-8 bytes.
/// </param>
/// <returns>A deterministic, non-empty GUID derived from the reference.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="reference"/> is <see langword="null"/>.
/// </exception>
public static Guid ForReference(string reference)
{
if (reference is null) throw new ArgumentNullException(nameof(reference));
byte[] bytes = Encoding.UTF8.GetBytes(reference);
// MD5 is used purely for a stable, non-cryptographic identity mapping
// (reference -> 16-byte GUID), never for security. Its 128-bit output
// fits a GUID exactly, which is why it is preferred here.
using MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(bytes);
return new Guid(hash);
}
}