diff --git a/Data/ApplicationDbContext.cs b/Data/ApplicationDbContext.cs index 2b9b12c..68464fc 100644 --- a/Data/ApplicationDbContext.cs +++ b/Data/ApplicationDbContext.cs @@ -8,6 +8,10 @@ namespace IoTSharp.Gateways.Data public ApplicationDbContext(DbContextOptions options) : base(options) { + if (Database.GetPendingMigrations().Count() > 0) + { + Database.Migrate(); + } } public DbSet Clients { get; set; } diff --git a/IoTSharp.Gateways.csproj b/IoTSharp.Gateways.csproj index bb99859..faf18eb 100644 --- a/IoTSharp.Gateways.csproj +++ b/IoTSharp.Gateways.csproj @@ -29,6 +29,7 @@ + diff --git a/Jobs/SystemInfoJob.cs b/Jobs/SystemInfoJob.cs new file mode 100644 index 0000000..4a50aa7 --- /dev/null +++ b/Jobs/SystemInfoJob.cs @@ -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(); + _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 + + }); + } + } +} diff --git a/Program.cs b/Program.cs index f97d309..0740c12 100644 --- a/Program.cs +++ b/Program.cs @@ -35,6 +35,15 @@ namespace IoTSharp.Gateways builder.Services.AddQuartz(q => { q.UseMicrosoftDependencyInjectionJobFactory(); + var SystemInfoJobKey = new JobKey("SystemInfoJob"); + q.AddJob(SystemInfoJobKey); + q.AddTrigger(opts => opts + .ForJob(SystemInfoJobKey) + .WithIdentity("SystemInfoJob-trigger") + .WithSimpleSchedule(x => x + .WithIntervalInMinutes(1) + .RepeatForever()).StartNow()); + var ModbusSchedulerJobKey = new JobKey("ModbusSchedulerJob"); q.AddJob(opts => opts.WithIdentity(ModbusSchedulerJobKey)); diff --git a/appsettings.Development.json b/appsettings.Development.json index 1673c1f..2760301 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -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" }