Salmon/Salmon.Model/Monitor/Helper.cs
2024-04-11 21:30:36 +02:00

66 lines
2.1 KiB
C#

using DeviceId;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;
namespace Salmon.Model.Monitor;
public static class Helper
{
public static string GetMachineId()
{
string deviceId = new DeviceIdBuilder()
.OnWindows(windows => windows
.AddMotherboardSerialNumber()
)
.OnLinux(linux => linux
.AddMotherboardSerialNumber()
)
.OnMac(mac => mac
.AddPlatformSerialNumber()
)
.ToString();
return deviceId;
}
public static string CreateIdFrom(params object[] args)
{
var sum = String.Join("", args);
var alg = SHA512.Create();
var hash = alg.ComputeHash(Encoding.UTF8.GetBytes(sum));
return Convert.ToBase64String(hash);
}
//src: https://stackoverflow.com/questions/14488796/does-net-provide-an-easy-way-convert-bytes-to-kb-mb-gb-etc (@JLRishe)
static readonly string[] SizeSuffixes =
{ "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
static public string SizeSuffix(Int64 value, int decimalPlaces = 1)
{
if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); }
if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
// mag is 0 for bytes, 1 for KB, 2, for MB, etc.
int mag = (int)Math.Log(value, 1024);
// 1L << (mag * 10) == 2 ^ (10 * mag)
// [i.e. the number of bytes in the unit corresponding to mag]
decimal adjustedSize = (decimal)value / (1L << (mag * 10));
// make adjustment when the value is large enough that
// it would round up to 1000 or more
if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
{
mag += 1;
adjustedSize /= 1024;
}
return string.Format("{0:n" + decimalPlaces + "} {1}",
adjustedSize,
SizeSuffixes[mag]);
}
}