From 294438145ac4f01c1c3d79a26fe19756c1f51508 Mon Sep 17 00:00:00 2001 From: Din Date: Wed, 30 Apr 2025 17:01:33 +0800 Subject: [PATCH] Refactor NVML wrapper and update CORS policy; add appsettings configuration files --- NvmlWrapper.cs | 8 +-- Worker.cs | 52 +++++++++++++------ publish/publish/appsettings.Development.json | 8 +++ publish/publish/appsettings.json | 19 +++++++ .../publish/appsettings.Development.json | 8 +++ publish/publish/publish/appsettings.json | 19 +++++++ .../publish/appsettings.Development.json | 8 +++ .../publish/publish/publish/appsettings.json | 19 +++++++ 8 files changed, 122 insertions(+), 19 deletions(-) create mode 100644 publish/publish/appsettings.Development.json create mode 100644 publish/publish/appsettings.json create mode 100644 publish/publish/publish/appsettings.Development.json create mode 100644 publish/publish/publish/appsettings.json create mode 100644 publish/publish/publish/publish/appsettings.Development.json create mode 100644 publish/publish/publish/publish/appsettings.json diff --git a/NvmlWrapper.cs b/NvmlWrapper.cs index e32719b..42297dc 100644 --- a/NvmlWrapper.cs +++ b/NvmlWrapper.cs @@ -10,13 +10,13 @@ public static class NvmlWrapper public static extern int NvmlShutdown(); // Get device count - [DllImport("nvml.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern int nvmlDeviceGetCount_v2(ref uint deviceCount); + /* [DllImport("nvml.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int nvmlDeviceGetCount_v2(ref uint deviceCount); */ - public static int NvmlDeviceGetCount(ref uint deviceCount) + /* public static int NvmlDeviceGetCount(ref uint deviceCount) { return nvmlDeviceGetCount_v2(ref deviceCount); - } + } */ [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetHandleByIndex_v2")] public static extern int NvmlDeviceGetHandleByIndex(int index, out IntPtr device); diff --git a/Worker.cs b/Worker.cs index 34b218b..502b5a7 100644 --- a/Worker.cs +++ b/Worker.cs @@ -25,7 +25,8 @@ namespace ResourceMonitorService builder.Services.AddCors(options => { options.AddPolicy("AllowAllOrigins", - builder => builder.AllowAnyOrigin() + builder => builder + .WithOrigins("http://localhost:4200","http://192.168.50.52:4200","http://vmwin11:4200") .AllowAnyHeader() .AllowAnyMethod()); }); @@ -42,17 +43,17 @@ namespace ResourceMonitorService // and compares it with the expected API key from appsettings.json. // If the API key is missing or invalid, it returns a 401 Unauthorized response. // - app.Use(async (context, next) => - { - if (!context.Request.Headers.TryGetValue("X-API-KEY", out var extractedApiKey) || extractedApiKey != apiKey) - { - context.Response.StatusCode = StatusCodes.Status401Unauthorized; - await context.Response.WriteAsync("Unauthorized: Invalid API Key"); - return; - } + /* app.Use(async (context, next) => + { + if (!context.Request.Headers.TryGetValue("X-API-KEY", out var extractedApiKey) || extractedApiKey != apiKey) + { + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + await context.Response.WriteAsync("Unauthorized: Invalid API Key"); + return; + } - await next(); - }); + await next(); + }); */ // Apply CORS policy to allow all origins app.UseCors("AllowAllOrigins"); @@ -230,7 +231,26 @@ namespace ResourceMonitorService if (usage > 80) { // Get the current processes and sort them by CPU usage in descending order - var processes = Process.GetProcesses().OrderByDescending(p => p.TotalProcessorTime); + var processes = Process.GetProcesses() + .Select(p => + { + try + { + return new + { + Process = p, + TotalProcessorTime = p.TotalProcessorTime + }; + } + catch + { + return null; // Skip processes that throw exceptions + } + }) + .Where(p => p != null) + .OrderByDescending(p => p.TotalProcessorTime) + .Select(p => p.Process) + .ToList(); // Create a new anonymous type containing the CPU usage, RAM usage, and the top 3 highest CPU-using processes return new @@ -327,7 +347,9 @@ namespace ResourceMonitorService { Usage = utilization.Gpu, Temperature = temperature, - FanSpeed = fanSpeed + FanSpeed = fanSpeed, + IsAvailable = false, + Error = "" }; } catch (Exception ex) @@ -343,7 +365,7 @@ namespace ResourceMonitorService } } - private bool IsNvidiaGpuPresent() + /* private bool IsNvidiaGpuPresent() { try { @@ -371,7 +393,7 @@ namespace ResourceMonitorService return false; } } - } + } */ private object GetCurrentlyRunningGame() { diff --git a/publish/publish/appsettings.Development.json b/publish/publish/appsettings.Development.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/publish/publish/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/publish/publish/appsettings.json b/publish/publish/appsettings.json new file mode 100644 index 0000000..78a1652 --- /dev/null +++ b/publish/publish/appsettings.json @@ -0,0 +1,19 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "RunAsWindowsService": true, + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://*:5000" + } + } + }, + "ApiSettings": { + "ApiKey": "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a" + } +} diff --git a/publish/publish/publish/appsettings.Development.json b/publish/publish/publish/appsettings.Development.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/publish/publish/publish/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/publish/publish/publish/appsettings.json b/publish/publish/publish/appsettings.json new file mode 100644 index 0000000..78a1652 --- /dev/null +++ b/publish/publish/publish/appsettings.json @@ -0,0 +1,19 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "RunAsWindowsService": true, + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://*:5000" + } + } + }, + "ApiSettings": { + "ApiKey": "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a" + } +} diff --git a/publish/publish/publish/publish/appsettings.Development.json b/publish/publish/publish/publish/appsettings.Development.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/publish/publish/publish/publish/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/publish/publish/publish/publish/appsettings.json b/publish/publish/publish/publish/appsettings.json new file mode 100644 index 0000000..78a1652 --- /dev/null +++ b/publish/publish/publish/publish/appsettings.json @@ -0,0 +1,19 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "RunAsWindowsService": true, + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://*:5000" + } + } + }, + "ApiSettings": { + "ApiKey": "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a" + } +}