diff --git a/src/HikvisionAttendanceManager.App/App.xaml.cs b/src/HikvisionAttendanceManager.App/App.xaml.cs index acf2f4a..c28657e 100644 --- a/src/HikvisionAttendanceManager.App/App.xaml.cs +++ b/src/HikvisionAttendanceManager.App/App.xaml.cs @@ -10,6 +10,7 @@ public partial class App : Application AppLogger.Initialize(); AppLogger.Info("[STARTUP] Application starting"); AppLogger.Info("[STARTUP] Configuration loaded"); + HikvisionCredentialsFactory.LogDevelopmentCredentialStatus(); DispatcherUnhandledException += (_, args) => AppLogger.Error("Unhandled UI exception.", args.Exception); } } diff --git a/src/HikvisionAttendanceManager.App/Services/HikvisionCredentialsFactory.cs b/src/HikvisionAttendanceManager.App/Services/HikvisionCredentialsFactory.cs new file mode 100644 index 0000000..98c65cd --- /dev/null +++ b/src/HikvisionAttendanceManager.App/Services/HikvisionCredentialsFactory.cs @@ -0,0 +1,78 @@ +using HikvisionAttendanceManager.App.Models; +using Microsoft.Extensions.Configuration; + +namespace HikvisionAttendanceManager.App.Services; + +/// Resolves Hikvision ISAPI credentials: per-device, environment variables, then Development settings. +public static class HikvisionCredentialsFactory +{ + public const string DefaultUsernameVariable = "HIKVISION_MANAGER_DEFAULT_USERNAME"; + public const string DefaultPasswordVariable = "HIKVISION_MANAGER_DEFAULT_PASSWORD"; + + public static bool TryGetForDevice(Device device, out string username, out string password, out string error) + { + username = device.Username?.Trim() ?? ""; + if (string.IsNullOrWhiteSpace(username)) + username = Environment.GetEnvironmentVariable(DefaultUsernameVariable)?.Trim() ?? ""; + if (string.IsNullOrWhiteSpace(username)) + username = GetDevelopmentSetting("Hikvision:Username") ?? ""; + + password = !string.IsNullOrWhiteSpace(device.ProtectedPassword) + ? PasswordProtector.Unprotect(device.ProtectedPassword) + : Environment.GetEnvironmentVariable(DefaultPasswordVariable) ?? ""; + if (string.IsNullOrWhiteSpace(password)) + password = GetDevelopmentSetting("Hikvision:Password") ?? ""; + + if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) + { + error = "Hikvision credentials are not configured for this device."; + return false; + } + + error = ""; + return true; + } + + public static bool HasCredentials(Device device) => TryGetForDevice(device, out _, out _, out _); + + public static void LogDevelopmentCredentialStatus() + { + if (!IsDevelopment()) + return; + + var username = GetDevelopmentSetting("Hikvision:Username"); + var hasPassword = !string.IsNullOrWhiteSpace(GetDevelopmentSetting("Hikvision:Password")); + if (!string.IsNullOrWhiteSpace(username) && hasPassword) + AppLogger.Info($"Hikvision default credentials loaded for user '{username}'."); + else + AppLogger.Warning("Hikvision default credentials were not found in Development settings."); + } + + private static string? GetDevelopmentSetting(string key) + { + if (!IsDevelopment()) + return null; + + return BuildDevelopmentConfiguration()[key]?.Trim(); + } + + private static IConfiguration BuildDevelopmentConfiguration() => + new ConfigurationBuilder() + .SetBasePath(AppContext.BaseDirectory) + .AddJsonFile("appsettings.json", optional: true) + .AddJsonFile("appsettings.Development.json", optional: true) + .Build(); + + private static bool IsDevelopment() + { + var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")?.Trim(); + if (!string.IsNullOrWhiteSpace(environment)) + return string.Equals(environment, "Development", StringComparison.OrdinalIgnoreCase); + +#if DEBUG + return true; +#else + return false; +#endif + } +} diff --git a/src/HikvisionAttendanceManager.App/appsettings.Development.example.json b/src/HikvisionAttendanceManager.App/appsettings.Development.example.json index 36c2a72..680f37c 100644 --- a/src/HikvisionAttendanceManager.App/appsettings.Development.example.json +++ b/src/HikvisionAttendanceManager.App/appsettings.Development.example.json @@ -1,5 +1,9 @@ { "ConnectionStrings": { "Hrms": "Server=YOUR_SERVER;Port=3306;Database=hrms;User ID=YOUR_USER;Password=YOUR_PASSWORD;" + }, + "Hikvision": { + "Username": "admin", + "Password": "Utopia01" } }