refactor (networking)

This commit is contained in:
Kevin Kai Berthold 2023-09-21 22:10:55 +02:00
parent febc4d9488
commit 450a6f2796
153 changed files with 7834 additions and 8004 deletions

View file

@ -1,64 +1,64 @@
using Insight.Agent.Interfaces;
using Insight.Agent.Messages;
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using System.Management;
using System.Runtime.Versioning;
namespace Insight.Agent.Network.Handlers
namespace Insight.Agent.Network.Handlers;
[SupportedOSPlatform("windows")]
public class VideocardHandler : IMessageHandler<AgentSession>
{
[SupportedOSPlatform("windows")]
public class VideocardHandler : IAgentMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IAgentMessage
if (message is InventoryRequest)
{
if (message is GetInventory)
{
var result = new VideocardList();
result.AddRange(GetVideocards());
var result = new VideocardList();
result.AddRange(GetVideocards());
await sender.SendAsync(result, cancellationToken);
}
}
private static List<Videocard> GetVideocards()
{
using var searcher = new ManagementObjectSearcher
{
Scope = new ManagementScope(@"root\cimv2"),
Query = new ObjectQuery("select deviceid, name, adapterram, driverdate, driverversion from win32_videocontroller")
};
if (searcher.TryGet(out var collection) is false)
{
searcher.Query = new ObjectQuery("select * from win32_videocontroller");
if (searcher.TryGet(out collection) is false) throw new InvalidOperationException("WMI Collection NULL");
}
var videocards = new List<Videocard>();
using (collection)
{
foreach (ManagementObject @object in collection.Cast<ManagementObject>())
{
var videocard = new Videocard();
var properties = @object.GetPropertyHashes();
videocard.DeviceId = @object.GetValue<string>(properties, "deviceid")?.Trim();
videocard.Model = @object.GetValue<string>(properties, "name")?.Trim();
if (@object.TryGetValue<object>(properties, "driverdate", out var driverdate))
{
videocard.DriverDate = ManagementDateTimeConverter.ToDateTime(driverdate?.ToString());
}
videocard.DriverVersion = @object.GetValue<string>(properties, "driverversion")?.Trim();
videocards.Add(videocard);
}
}
return videocards;
await sender.SendAsync(result, cancellationToken);
}
}
private static List<Videocard> GetVideocards()
{
using var searcher = new ManagementObjectSearcher
{
Scope = new ManagementScope(@"root\cimv2"),
Query = new ObjectQuery("select deviceid, name, adapterram, driverdate, driverversion from win32_videocontroller")
};
if (searcher.TryGet(out var collection) is false)
{
searcher.Query = new ObjectQuery("select * from win32_videocontroller");
if (searcher.TryGet(out collection) is false) throw new InvalidOperationException("WMI Collection NULL");
}
var videocards = new List<Videocard>();
using (collection)
{
foreach (ManagementObject @object in collection.Cast<ManagementObject>())
{
var videocard = new Videocard();
var properties = @object.GetPropertyHashes();
videocard.DeviceId = @object.GetValue<string>(properties, "deviceid")?.Trim();
videocard.Model = @object.GetValue<string>(properties, "name")?.Trim();
if (@object.TryGetValue<object>(properties, "driverdate", out var driverdate))
{
videocard.DriverDate = ManagementDateTimeConverter.ToDateTime(driverdate?.ToString());
}
videocard.DriverVersion = @object.GetValue<string>(properties, "driverversion")?.Trim();
videocards.Add(videocard);
}
}
return videocards;
}
}