InventoryAgent/Inventory.Core/SlurpitClient.cs
2025-10-20 00:03:49 +08:00

65 lines
2.4 KiB
C#

using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
namespace Inventory.Core
{
public class SlurpitClient
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly SystemInfoCollector _collector;
private readonly HealthMonitor _healthMonitor;
public SlurpitClient(IHttpClientFactory httpClientFactory, SystemInfoCollector collector, HealthMonitor healthMonitor)
{
_httpClientFactory = httpClientFactory;
_collector = collector;
_healthMonitor = healthMonitor;
}
public async Task SendDeviceData()
{
var slurpitUrl = Secrets.SlurpitUrl;
var slurpitApiKey = Secrets.SlurpitApiKey;
if (string.IsNullOrWhiteSpace(slurpitUrl) || string.IsNullOrWhiteSpace(slurpitApiKey))
{
// Don't send if not configured
return;
}
var healthReport = _healthMonitor.CollectHealthMetrics();
var deviceData = new
{
ComputerName = _collector.GetComputerName(),
DeviceType = _collector.GetDeviceType(),
SerialNumber = _collector.GetSystemSerialNumber(),
MotherboardSerialNumber = _collector.GetMotherboardSerialNumber(),
SystemUUID = _collector.GetSystemUUID(),
Processor = _collector.GetProcessor(),
RAM = _collector.GetTotalRAM(),
GPUs = _collector.GetGPUs(), // Send as a proper array
HasOpticalDrive = _collector.HasOpticalDrive(),
OSVersion = _collector.GetOSVersion(),
OsInstallDate = _collector.GetOSInstallDate(),
OSLicenseKey = _collector.GetOSLicenseKey(),
NetworkInfo = _collector.GetNetworkInfo(),
Monitors = _collector.GetMonitors(),
Printers = _collector.GetPrinters(),
StorageDevices = _collector.GetStorage(),
LocalAdmins = _collector.GetLocalAdmins(),
HealthReport = healthReport // Send the whole report
};
var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"ApiKey {slurpitApiKey}");
await client.PostAsJsonAsync(slurpitUrl, deviceData);
}
}
}