137 lines
4.5 KiB
C#
137 lines
4.5 KiB
C#
using System.ComponentModel;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace HikvisionAttendanceManager.App.Models;
|
|
|
|
public sealed class DashboardViewModel : INotifyPropertyChanged
|
|
{
|
|
private int _totalDevices;
|
|
private int _onlineDevices;
|
|
private int _offlineDevices;
|
|
private int _authenticationFailedDevices;
|
|
private int _totalUsersOnDevices;
|
|
private bool _isInitialLoad = true;
|
|
private bool _isLoadingConnectivity;
|
|
|
|
public int TotalDevices
|
|
{
|
|
get => _totalDevices;
|
|
set => SetField(ref _totalDevices, value);
|
|
}
|
|
|
|
public int OnlineDevices
|
|
{
|
|
get => _onlineDevices;
|
|
set => SetField(ref _onlineDevices, value);
|
|
}
|
|
|
|
public int OfflineDevices
|
|
{
|
|
get => _offlineDevices;
|
|
set => SetField(ref _offlineDevices, value);
|
|
}
|
|
|
|
public int NotTestedDevices
|
|
{
|
|
get => _notTestedDevices;
|
|
set => SetField(ref _notTestedDevices, value);
|
|
}
|
|
|
|
public int AuthenticationFailedDevices
|
|
{
|
|
get => _authenticationFailedDevices;
|
|
set => SetField(ref _authenticationFailedDevices, value);
|
|
}
|
|
|
|
private int _notTestedDevices;
|
|
|
|
public int TotalUsersOnDevices
|
|
{
|
|
get => _totalUsersOnDevices;
|
|
set => SetField(ref _totalUsersOnDevices, value);
|
|
}
|
|
|
|
public bool IsInitialLoad
|
|
{
|
|
get => _isInitialLoad;
|
|
set
|
|
{
|
|
if (SetField(ref _isInitialLoad, value))
|
|
NotifyDisplayProperties();
|
|
}
|
|
}
|
|
|
|
public bool IsLoadingConnectivity
|
|
{
|
|
get => _isLoadingConnectivity;
|
|
set
|
|
{
|
|
_isLoadingConnectivity = value;
|
|
OnPropertyChanged();
|
|
NotifyConnectivityDisplays();
|
|
}
|
|
}
|
|
|
|
public void EndConnectivityRefresh()
|
|
{
|
|
_isLoadingConnectivity = false;
|
|
OnPropertyChanged(nameof(IsLoadingConnectivity));
|
|
NotifyConnectivityDisplays();
|
|
}
|
|
|
|
public string TotalDevicesDisplay => IsInitialLoad ? "Loading..." : TotalDevices.ToString("N0");
|
|
public string OnlineDevicesDisplay => IsInitialLoad || IsLoadingConnectivity ? "Loading..." : OnlineDevices.ToString("N0");
|
|
public string OfflineDevicesDisplay => IsInitialLoad || IsLoadingConnectivity ? "Loading..." : OfflineDevices.ToString("N0");
|
|
public string TotalUsersDisplay => IsInitialLoad ? "Loading..." : TotalUsersOnDevices.ToString("N0");
|
|
|
|
public string DeviceStatusSummary => TotalDevices == 0
|
|
? "No devices configured"
|
|
: AuthenticationFailedDevices > 0
|
|
? $"{OnlineDevices} online · {OfflineDevices} offline · {AuthenticationFailedDevices} auth failed · {NotTestedDevices} not tested"
|
|
: $"{OnlineDevices} online · {OfflineDevices} offline · {NotTestedDevices} not tested";
|
|
|
|
public void UpdateFromDevices(IEnumerable<Device> devices)
|
|
{
|
|
var list = devices as IReadOnlyList<Device> ?? devices.ToList();
|
|
TotalDevices = list.Count;
|
|
OnlineDevices = list.Count(d => IsStatus(d, "Online") || IsStatus(d, "API Error"));
|
|
OfflineDevices = list.Count(d => IsStatus(d, "Offline"));
|
|
AuthenticationFailedDevices = list.Count(d => IsStatus(d, "Authentication Failed"));
|
|
NotTestedDevices = list.Count(d => IsStatus(d, "Not Tested"));
|
|
TotalUsersOnDevices = list.Sum(d => d.RegisteredUserCount ?? 0);
|
|
OnPropertyChanged(nameof(DeviceStatusSummary));
|
|
NotifyDisplayProperties();
|
|
}
|
|
|
|
private void NotifyDisplayProperties()
|
|
{
|
|
OnPropertyChanged(nameof(TotalDevicesDisplay));
|
|
NotifyConnectivityDisplays();
|
|
OnPropertyChanged(nameof(TotalUsersDisplay));
|
|
}
|
|
|
|
private void NotifyConnectivityDisplays()
|
|
{
|
|
OnPropertyChanged(nameof(OnlineDevicesDisplay));
|
|
OnPropertyChanged(nameof(OfflineDevicesDisplay));
|
|
OnPropertyChanged(nameof(DeviceStatusSummary));
|
|
}
|
|
|
|
private static bool IsStatus(Device device, string status) =>
|
|
string.Equals(device.Status, status, StringComparison.OrdinalIgnoreCase);
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
private void OnPropertyChanged([CallerMemberName] string? propertyName = null) =>
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
|
{
|
|
if (EqualityComparer<T>.Default.Equals(field, value))
|
|
return false;
|
|
field = value;
|
|
OnPropertyChanged(propertyName);
|
|
return true;
|
|
}
|
|
}
|