master
MysticBoy 2 years ago
parent c8a7e7d08e
commit 85fef61761

@ -8,6 +8,10 @@ namespace IoTSharp.Gateways.Data
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
if (Database.GetPendingMigrations().Count() > 0)
{
Database.Migrate();
}
}
public DbSet<Client> Clients { get; set; }

@ -29,6 +29,7 @@
<PackageReference Include="AMWD.Modbus.Proxy" Version="1.2.0" />
<PackageReference Include="AMWD.Modbus.Serial" Version="1.2.0" />
<PackageReference Include="AMWD.Modbus.Tcp" Version="1.2.0" />
<PackageReference Include="CZGL.SystemInfo" Version="2.0.0" />
<PackageReference Include="Modbus.SerialOverTCP" Version="1.2.1-1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.*" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.*" />

@ -0,0 +1,75 @@
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 readonly ILoggerFactory _factory;
private IServiceScope _serviceScope;
public SystemInfoJob(ILoggerFactory factory, IServiceScopeFactory scopeFactor, MQTTClient client, IMemoryCache cache)
{
_factory = factory;
_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,
network.NetworkType ,
NetworkName =network.Name,
NetworkId= network.Id,
NetworkTrademark= network.Trademark,
memory.TotalPhysicalMemory,
memory.TotalVirtualMemory
});
await _client.UploadTelemetryDataAsync(new {
CPULoad= GetCPULoad(),
memory.UsedPercentage,
memory.AvailableVirtualMemory,
memory.AvailablePhysicalMemory,
NetworkSend=network.GetIpv4Speed().SendLength,
NetworkReceived= network.GetIpv4Speed().ReceivedLength,
NetworkSpeed=network.Speed
});
}
}
}

@ -35,6 +35,15 @@ namespace IoTSharp.Gateways
builder.Services.AddQuartz(q =>
{
q.UseMicrosoftDependencyInjectionJobFactory();
var SystemInfoJobKey = new JobKey("SystemInfoJob");
q.AddJob<SystemInfoJob>(SystemInfoJobKey);
q.AddTrigger(opts => opts
.ForJob(SystemInfoJobKey)
.WithIdentity("SystemInfoJob-trigger")
.WithSimpleSchedule(x => x
.WithIntervalInMinutes(1)
.RepeatForever()).StartNow());
var ModbusSchedulerJobKey = new JobKey("ModbusSchedulerJob");
q.AddJob<SchedulerJob>(opts => opts.WithIdentity(ModbusSchedulerJobKey));

@ -6,6 +6,5 @@
"Microsoft.AspNetCore": "Warning"
}
},
"MqttBroker": "mqtt://a555dd7958a54b3d9ba967eae5310ba9@139.9.232.10:1883/ta60334bc-63bd-4638-99d7-820cb7592d75",
"DeviceId": "ta60334bc-63bd-4638-99d7-820cb7592d75"
"MqttBroker": "mqtt://03f132537c46497c880591d6d3ab1564@localhost:1883"
}

Loading…
Cancel
Save