feature/laptop #2

Merged
king merged 4 commits from feature/laptop into master 2025-04-30 17:08:10 +08:00
8 changed files with 122 additions and 19 deletions
Showing only changes of commit 294438145a - Show all commits
+4 -4
View File
@@ -10,13 +10,13 @@ public static class NvmlWrapper
public static extern int NvmlShutdown(); public static extern int NvmlShutdown();
// Get device count // Get device count
[DllImport("nvml.dll", CallingConvention = CallingConvention.Cdecl)] /* [DllImport("nvml.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int nvmlDeviceGetCount_v2(ref uint deviceCount); 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); return nvmlDeviceGetCount_v2(ref deviceCount);
} } */
[DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetHandleByIndex_v2")] [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetHandleByIndex_v2")]
public static extern int NvmlDeviceGetHandleByIndex(int index, out IntPtr device); public static extern int NvmlDeviceGetHandleByIndex(int index, out IntPtr device);
+37 -15
View File
@@ -25,7 +25,8 @@ namespace ResourceMonitorService
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy("AllowAllOrigins", options.AddPolicy("AllowAllOrigins",
builder => builder.AllowAnyOrigin() builder => builder
.WithOrigins("http://localhost:4200","http://192.168.50.52:4200","http://vmwin11:4200")
.AllowAnyHeader() .AllowAnyHeader()
.AllowAnyMethod()); .AllowAnyMethod());
}); });
@@ -42,17 +43,17 @@ namespace ResourceMonitorService
// and compares it with the expected API key from appsettings.json. // 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. // If the API key is missing or invalid, it returns a 401 Unauthorized response.
// //
app.Use(async (context, next) => /* app.Use(async (context, next) =>
{ {
if (!context.Request.Headers.TryGetValue("X-API-KEY", out var extractedApiKey) || extractedApiKey != apiKey) if (!context.Request.Headers.TryGetValue("X-API-KEY", out var extractedApiKey) || extractedApiKey != apiKey)
{ {
context.Response.StatusCode = StatusCodes.Status401Unauthorized; context.Response.StatusCode = StatusCodes.Status401Unauthorized;
await context.Response.WriteAsync("Unauthorized: Invalid API Key"); await context.Response.WriteAsync("Unauthorized: Invalid API Key");
return; return;
} }
await next(); await next();
}); }); */
// Apply CORS policy to allow all origins // Apply CORS policy to allow all origins
app.UseCors("AllowAllOrigins"); app.UseCors("AllowAllOrigins");
@@ -230,7 +231,26 @@ namespace ResourceMonitorService
if (usage > 80) if (usage > 80)
{ {
// Get the current processes and sort them by CPU usage in descending order // 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 // Create a new anonymous type containing the CPU usage, RAM usage, and the top 3 highest CPU-using processes
return new return new
@@ -327,7 +347,9 @@ namespace ResourceMonitorService
{ {
Usage = utilization.Gpu, Usage = utilization.Gpu,
Temperature = temperature, Temperature = temperature,
FanSpeed = fanSpeed FanSpeed = fanSpeed,
IsAvailable = false,
Error = ""
}; };
} }
catch (Exception ex) catch (Exception ex)
@@ -343,7 +365,7 @@ namespace ResourceMonitorService
} }
} }
private bool IsNvidiaGpuPresent() /* private bool IsNvidiaGpuPresent()
{ {
try try
{ {
@@ -371,7 +393,7 @@ namespace ResourceMonitorService
return false; return false;
} }
} }
} } */
private object GetCurrentlyRunningGame() 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"
}
}