You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.7 KiB
C#

2 years ago
using CZGL.SystemInfo;
using IoTSharp.Gateways.Data;
using IoTSharp.MqttSdk;
using Microsoft.Extensions.Caching.Memory;
using Quartz;
namespace IoTSharp.Gateways.Jobs
{
public class SystemInfoJob : IJob
{
private ILogger _logger;
private ApplicationDbContext _dbContext;
private MQTTClient _client;
private IMemoryCache _cache;
private IServiceScope _serviceScope;
2 years ago
public SystemInfoJob(ILogger<SystemInfoJob> logger, IServiceScopeFactory scopeFactor, MQTTClient client, IMemoryCache cache)
2 years ago
{
2 years ago
_logger = logger;
2 years ago
_serviceScope = scopeFactor.CreateScope();
_dbContext = _serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
_client = client;
_cache = cache;
}
int GetCPULoad()
{
CPUTime v1 = CPUHelper.GetCPUTime();
Thread.Sleep(1000);
var v2 = CPUHelper.GetCPUTime();
var value = CPUHelper.CalculateCPULoad(v1, v2);
v1 = v2;
return (int)(value * 100);
}
public async Task Execute(IJobExecutionContext context)
{
var network = NetworkInfo.TryGetRealNetworkInfo();
var memory = MemoryHelper.GetMemoryValue();
await _client.UploadAttributeAsync(new
{
SystemPlatformInfo.MachineName,
SystemPlatformInfo.OSArchitecture,
SystemPlatformInfo.OSDescription,
SystemPlatformInfo.OSPlatformID,
SystemPlatformInfo.OSVersion,
SystemPlatformInfo.ProcessArchitecture,
SystemPlatformInfo.ProcessorCount,
SystemPlatformInfo.FrameworkVersion,
SystemPlatformInfo.FrameworkDescription,
SystemPlatformInfo.GetLogicalDrives,
SystemPlatformInfo.UserName,
2 years ago
network.NetworkType,
NetworkName = network.Name,
NetworkId = network.Id,
NetworkTrademark = network.Trademark,
2 years ago
memory.TotalPhysicalMemory,
memory.TotalVirtualMemory
});
2 years ago
await _client.UploadTelemetryDataAsync(new
{
CPULoad = GetCPULoad(),
2 years ago
memory.UsedPercentage,
memory.AvailableVirtualMemory,
memory.AvailablePhysicalMemory,
2 years ago
NetworkSend = network.GetIpv4Speed().SendLength,
NetworkReceived = network.GetIpv4Speed().ReceivedLength,
NetworkSpeed = network.Speed
2 years ago
});
}
}
}