diff --git a/DbToDeviceFaceErrors.cs b/DbToDeviceFaceErrors.cs new file mode 100644 index 0000000..1765ee8 --- /dev/null +++ b/DbToDeviceFaceErrors.cs @@ -0,0 +1,44 @@ +using System; +using System.Text.RegularExpressions; + +namespace HanvonF710XAttendanceService +{ + internal sealed class DbToDeviceDuplicateFaceEntry + { + public DbToDeviceDuplicateFaceEntry(string employeeId, string existingDeviceId, string targetDevice) + { + EmployeeId = employeeId ?? ""; + ExistingDeviceId = existingDeviceId ?? ""; + TargetDevice = targetDevice ?? ""; + } + + public string EmployeeId { get; } + public string ExistingDeviceId { get; } + public string TargetDevice { get; } + } + + internal static class DbToDeviceFaceErrors + { + private static readonly Regex DuplicateFaceRegex = new Regex( + @"face\s+is\s+double\s*,\s*id\s*=\s*(\d+)", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + + public static bool TryParseDuplicateFace(string errorMessage, out string existingDeviceId) + { + existingDeviceId = null; + if (string.IsNullOrWhiteSpace(errorMessage)) + { + return false; + } + + Match match = DuplicateFaceRegex.Match(errorMessage.Trim()); + if (!match.Success || match.Groups.Count < 2) + { + return false; + } + + existingDeviceId = match.Groups[1].Value?.Trim(); + return !string.IsNullOrWhiteSpace(existingDeviceId); + } + } +} diff --git a/DbToDeviceFaceErrorsTests.cs b/DbToDeviceFaceErrorsTests.cs new file mode 100644 index 0000000..f500ad3 --- /dev/null +++ b/DbToDeviceFaceErrorsTests.cs @@ -0,0 +1,48 @@ +using System; + +namespace HanvonF710XAttendanceService +{ + internal static class DbToDeviceFaceErrorsTests + { + public static int RunAll() + { + int failed = 0; + failed += Run("parse duplicate face error", TestParseDuplicateFace); + failed += Run("ignore generic upload error", TestIgnoreGenericError); + return failed; + } + + private static int Run(string name, Func test) + { + try + { + if (!test()) + { + Console.WriteLine("FAIL: " + name); + return 1; + } + + Console.WriteLine("PASS: " + name); + return 0; + } + catch (Exception ex) + { + Console.WriteLine("FAIL: " + name + " err=" + ex.Message); + return 1; + } + } + + private static bool TestParseDuplicateFace() + { + return DbToDeviceFaceErrors.TryParseDuplicateFace("face is double,id=6400", out string existingId) + && existingId == "6400" + && DbToDeviceFaceErrors.TryParseDuplicateFace("Face is double, id=6475", out string existingId2) + && existingId2 == "6475"; + } + + private static bool TestIgnoreGenericError() + { + return !DbToDeviceFaceErrors.TryParseDuplicateFace("no face in picture", out _); + } + } +}