hikvision-sync-service/DeviceIdentity.cs

34 lines
1.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Text.RegularExpressions;
namespace HikvisionAttendanceService;
/// <summary>
/// Single place for DeviceId trimming and session/fetch lookup keys so config, CLI, and logs stay aligned.
/// </summary>
internal static class DeviceIdentity
{
private static readonly Regex FMNumericSuffix = new Regex(@"-FM(\d+)$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
/// <summary>Value stored from JSON after load — trim only; never replace users chosen id.</summary>
public static string NormalizeConfigured(string? raw)
{
if (string.IsNullOrWhiteSpace(raw))
return "";
return raw.Trim();
}
/// <summary>
/// Stable key for FindSession / fetch: same physical device if only the "-FM" serial prefix differs
/// (e.g. DS-K1T642MFW-FM7378360 vs DS-K1T642MFW-7378360).
/// </summary>
public static string CanonicalLookupKey(string? raw)
{
var s = NormalizeConfigured(raw);
if (s.Length == 0)
return "";
s = FMNumericSuffix.Replace(s, "-$1");
return s.ToUpperInvariant();
}
}