調整歌曲點播、調整心跳參數

This commit is contained in:
jasonchenwork 2025-06-04 11:09:00 +08:00
parent c623bbe26d
commit a58601e1fb
3 changed files with 26 additions and 12 deletions

View File

@ -3,18 +3,13 @@ using System.Text;
using System.Text.Json; // 適用於 .NET Core 3.0+ / .NET 5/6/7/8 using System.Text.Json; // 適用於 .NET Core 3.0+ / .NET 5/6/7/8
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualBasic.Devices;
namespace HeartbeatSender 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 public class heartbeatSender
{ {
private readonly HttpClient httpClient = new HttpClient(); private readonly HttpClient httpClient = new HttpClient();
@ -95,6 +90,9 @@ namespace HeartbeatSender
branch_name = "測試", branch_name = "測試",
hostname = "PC" + hostName.Substring(Math.Max(0, hostName.Length - 3)), hostname = "PC" + hostName.Substring(Math.Max(0, hostName.Length - 3)),
ip = GetLocalIPv4(), ip = GetLocalIPv4(),
cpu = GetCpuUsage(),
memory = GetTotalMemoryInMB(),
disk = GetDiskTotalSizeInGB()
}; };
var json = JsonSerializer.Serialize(heartbeatData); 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
}
} }
} }

View File

@ -247,7 +247,7 @@ namespace DualScreenDemo{
} }
public void AddSongCount(string id){ 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); Console.WriteLine(query);
//string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;"; //string connectionString = "Server=192.168.11.4;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";
string connectionString = GetConnectionString(); string connectionString = GetConnectionString();

View File

@ -33,7 +33,7 @@ namespace DualScreenDemo
while (true) while (true)
{ {
await sender.SendHeartbeatAsync(); await sender.SendHeartbeatAsync();
await Task.Delay(3000); // 每3秒送一次 await Task.Delay(300000); // 每3秒送一次
} }
}); });
} }