Refactor NVML wrapper and update CORS policy; add appsettings configuration files

This commit is contained in:
Din
2025-04-30 17:01:33 +08:00
parent 413360ece2
commit 294438145a
8 changed files with 122 additions and 19 deletions
+4 -4
View File
@@ -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);
+37 -15
View File
@@ -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()
{
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
+19
View File
@@ -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"
}
}
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
+19
View File
@@ -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"
}
}
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
@@ -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"
}
}