130 lines
4.8 KiB
C#
130 lines
4.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Inventory.Core
|
|
{
|
|
public class DatabaseUpdater
|
|
{
|
|
private readonly InventoryContext _db;
|
|
private readonly SystemInfoCollector _collector;
|
|
private readonly HealthMonitor _healthMonitor;
|
|
|
|
private static readonly TimeZoneInfo ManilaTimeZone = GetManilaTimeZone();
|
|
|
|
private static TimeZoneInfo GetManilaTimeZone()
|
|
{
|
|
try
|
|
{
|
|
return TimeZoneInfo.FindSystemTimeZoneById("Asia/Manila"); // IANA ID for Linux/macOS
|
|
}
|
|
catch (TimeZoneNotFoundException)
|
|
{
|
|
return TimeZoneInfo.FindSystemTimeZoneById("Singapore Standard Time"); // Windows ID
|
|
}
|
|
}
|
|
public DatabaseUpdater(InventoryContext db, SystemInfoCollector collector, HealthMonitor healthMonitor)
|
|
{
|
|
_db = db;
|
|
_collector = collector;
|
|
_healthMonitor = healthMonitor;
|
|
}
|
|
|
|
public async Task UpsertDevice()
|
|
{
|
|
string? localIdentifier = GetLocalIdentifier();
|
|
|
|
if (string.IsNullOrWhiteSpace(localIdentifier))
|
|
{
|
|
// Cannot proceed without a unique identifier
|
|
return;
|
|
}
|
|
|
|
var existingDevice = await _db.Devices.FirstOrDefaultAsync(d => d.HardwareIdentifier == localIdentifier);
|
|
|
|
if (existingDevice != null)
|
|
{
|
|
// Update existing device
|
|
UpdateDeviceProperties(existingDevice);
|
|
}
|
|
else
|
|
{
|
|
// Create new device
|
|
var newDevice = new Device();
|
|
newDevice.HardwareIdentifier = localIdentifier;
|
|
UpdateDeviceProperties(newDevice);
|
|
_db.Devices.Add(newDevice);
|
|
}
|
|
|
|
await _db.SaveChangesAsync();
|
|
}
|
|
|
|
private void UpdateDeviceProperties(Device device)
|
|
{
|
|
var (ipAddresses, macAddress) = _collector.GetNetworkInfo();
|
|
var healthReport = _healthMonitor.CollectHealthMetrics();
|
|
|
|
device.ComputerName = _collector.GetComputerName();
|
|
device.DeviceType = _collector.GetDeviceType();
|
|
device.SerialNumber = _collector.GetSystemSerialNumber();
|
|
device.MotherboardSerialNumber = _collector.GetMotherboardSerialNumber();
|
|
device.SystemUUID = _collector.GetSystemUUID();
|
|
device.Processor = _collector.GetProcessor();
|
|
device.RAM = _collector.GetTotalRAM();
|
|
device.GPUs = JsonSerializer.Serialize(_collector.GetGPUs()); // Save as JSON array
|
|
device.HasOpticalDrive = _collector.HasOpticalDrive();
|
|
device.OSVersion = _collector.GetOSVersion();
|
|
device.OsInstallDate = _collector.GetOSInstallDate();
|
|
device.OSLicenseKey = _collector.GetOSLicenseKey();
|
|
device.IPAddress = JsonSerializer.Serialize(ipAddresses);
|
|
device.MACAddress = macAddress;
|
|
|
|
// Health Metrics
|
|
device.CpuTemperature = healthReport.CpuTemp;
|
|
device.GpuTemperature = healthReport.GpuTemp;
|
|
device.BatteryHealthPercent = healthReport.BatteryHealth;
|
|
|
|
// JSON Properties
|
|
device.Monitors = JsonSerializer.Serialize(_collector.GetMonitors());
|
|
device.Printers = JsonSerializer.Serialize(_collector.GetPrinters());
|
|
device.StorageDevices = JsonSerializer.Serialize(_collector.GetStorage());
|
|
device.DriveHealth = JsonSerializer.Serialize(healthReport.DriveHealth);
|
|
device.HealthMetrics = JsonSerializer.Serialize(healthReport);
|
|
device.LocalAdmins = JsonSerializer.Serialize(_collector.GetLocalAdmins());
|
|
|
|
device.LastSeen = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, ManilaTimeZone);
|
|
}
|
|
|
|
private string? GetLocalIdentifier()
|
|
{
|
|
string? motherboardSerial = _collector.GetMotherboardSerialNumber();
|
|
if (IsValidIdentifier(motherboardSerial))
|
|
{
|
|
return motherboardSerial;
|
|
}
|
|
|
|
string? systemUuid = _collector.GetSystemUUID();
|
|
if (IsValidIdentifier(systemUuid))
|
|
{
|
|
return systemUuid;
|
|
}
|
|
|
|
var (_, macAddress) = _collector.GetNetworkInfo();
|
|
if (IsValidIdentifier(macAddress))
|
|
{
|
|
return macAddress;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private bool IsValidIdentifier(string? identifier)
|
|
{
|
|
return !string.IsNullOrWhiteSpace(identifier) &&
|
|
!identifier.Equals("To be filled by O.E.M.", StringComparison.OrdinalIgnoreCase) &&
|
|
!identifier.Equals("Default string", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
} |