using Hardware.Info;
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
namespace XiaoZhiSharp
{
public class Global
{
public static IHardwareInfo HardwareInfo { get; } = new HardwareInfo();
public static bool IsDebug { get; set; } = false;
public static bool IsAudio { get; set; } = true;
public static bool IsMcp { get; set; } = false;
public static int SampleRate_WaveOut { get; set; } = 24000;
public static int SampleRate_WaveIn { get; set; } = 16000;
public static int VadThreshold { get; set; } = 20; // 语音活动检测阈值,单位为毫秒
public static string OS { get; set; }
public static string LocalIP { get; set; }
static Global()
{
HardwareInfo.RefreshOperatingSystem();
OS = $"{HardwareInfo.OperatingSystem.Name} {HardwareInfo.OperatingSystem.Version}";
var host = Dns.GetHostEntry(Dns.GetHostName());
var ipAddr = host.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
LocalIP = ipAddr?.ToString();
}
///
/// 格式化字节数为易读的字符串
/// 自动选择合适的单位(B, KB, MB, GB, TB)
///
/// 字节数
/// 格式化后的字符串
public static string FormatBytes(ulong bytes)
{
if (bytes == 0) return "0 B";
string[] suffixes = { "B", "KB", "MB", "GB", "TB" };
int counter = 0;
decimal number = bytes;
// 循环除以1024,直到找到合适的单位
while (Math.Round(number / 1024) >= 1 && counter < suffixes.Length - 1)
{
number /= 1024;
counter++;
}
// 返回格式化后的字符串(保留1位小数)
return $"{number:n1} {suffixes[counter]}";
}
}
}