From a58601e1fbc2f5a36d9d7acf5e8505e73eb7442f Mon Sep 17 00:00:00 2001 From: jasonchenwork Date: Wed, 4 Jun 2025 11:09:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AA=BF=E6=95=B4=E6=AD=8C=E6=9B=B2=E9=BB=9E?= =?UTF-8?q?=E6=92=AD=E3=80=81=E8=AA=BF=E6=95=B4=E5=BF=83=E8=B7=B3=E5=8F=83?= =?UTF-8?q?=E6=95=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HeartbeatSender.cs | 34 ++++++++++++++++------- PrimaryFormParts/PrimaryForm.SQLSearch.cs | 2 +- Program.cs | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/HeartbeatSender.cs b/HeartbeatSender.cs index 2317fc2..b5c9cc3 100644 --- a/HeartbeatSender.cs +++ b/HeartbeatSender.cs @@ -3,18 +3,13 @@ using System.Text; using System.Text.Json; // 適用於 .NET Core 3.0+ / .NET 5/6/7/8 using System.Net; using System.Net.Sockets; +using System.Diagnostics; +using System.IO; +using Microsoft.VisualBasic.Devices; namespace HeartbeatSender { - - public class HeartbeatData - { - public string branch_name { get; set; } - public string room_name { get; set; } - public string room_ip { get; set; } - public string email { get; set; } - public string password { get; set; } - } + public class heartbeatSender { private readonly HttpClient httpClient = new HttpClient(); @@ -95,6 +90,9 @@ namespace HeartbeatSender branch_name = "測試", hostname = "PC" + hostName.Substring(Math.Max(0, hostName.Length - 3)), ip = GetLocalIPv4(), + cpu = GetCpuUsage(), + memory = GetTotalMemoryInMB(), + disk = GetDiskTotalSizeInGB() }; var json = JsonSerializer.Serialize(heartbeatData); @@ -127,6 +125,22 @@ namespace HeartbeatSender } } + private float GetCpuUsage() + { + using var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); + cpuCounter.NextValue(); // 需要呼叫兩次 + System.Threading.Thread.Sleep(1000); + return cpuCounter.NextValue(); // 回傳的是百分比,例如 25.3 + } + private float GetTotalMemoryInMB() + { + var computerInfo = new ComputerInfo(); + return computerInfo.TotalPhysicalMemory / (1024f * 1024f); // 轉 MB + } + private float GetDiskTotalSizeInGB(string driveLetter = "C") + { + var drive = new DriveInfo(driveLetter); + return drive.TotalSize / (1024f * 1024f * 1024f); // 轉 GB + } } - } diff --git a/PrimaryFormParts/PrimaryForm.SQLSearch.cs b/PrimaryFormParts/PrimaryForm.SQLSearch.cs index f294d30..5f64a76 100644 --- a/PrimaryFormParts/PrimaryForm.SQLSearch.cs +++ b/PrimaryFormParts/PrimaryForm.SQLSearch.cs @@ -247,7 +247,7 @@ namespace DualScreenDemo{ } public void AddSongCount(string id){ - string query = $"UPDATE songs SET song_counts = song_counts+1 WHERE id = '{id}';"; + string query = $"UPDATE song_library_cache SET song_counts = song_counts+1 WHERE song_id = '{id}';"; Console.WriteLine(query); //string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;"; string connectionString = GetConnectionString(); diff --git a/Program.cs b/Program.cs index 45a6305..4a759ce 100644 --- a/Program.cs +++ b/Program.cs @@ -33,7 +33,7 @@ namespace DualScreenDemo while (true) { await sender.SendHeartbeatAsync(); - await Task.Delay(3000); // 每3秒送一次 + await Task.Delay(300000); // 每3秒送一次 } }); }