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

117 lines
2.6 KiB
C#

using Hardware.Info;
using Salmon.Core;
using Salmon.Core.Cliff;
namespace Salmon.Model.Monitor;
public class Machine
: Element
{
[PersistentField]
public DateTime? LastStart { get; set; } = DateTime.Now;
[PersistentField]
public bool? Online { get; set; } = true;
[PersistentField]
public string? OperatingSystem { get; set; }
[PersistentField]
public ulong? TotalRam { get; set; } = 0;
public string? TotalRamHuman
{
get
{
if (TotalRam is null)
return null;
return Helper.SizeSuffix((long)TotalRam);
}
}
[PersistentField]
public ulong? AvailableRam { get; set; } = 0;
public string? AvailableRamHuman
{
get
{
if (AvailableRam is null)
return null;
return Helper.SizeSuffix((long)AvailableRam);
}
}
public float? AvailablePercentRam
{
get
{
if(TotalRam is null || AvailableRam is null)
return null;
return (float)AvailableRam / (float)TotalRam;
}
}
public string? AvailablePercentRamHuman
{
get
{
var percent = AvailablePercentRam;
if (percent is null)
return null;
return $"{percent*100:0.0}%";
}
}
public Machine(string id)
: base(id)
{
}
public override IEnumerable<KeyValuePair<string, object>> ImportantProperties()
{
foreach(var kv in base.ImportantProperties())
yield return kv;
if (TotalRamHuman is not null)
yield return new KeyValuePair<string, object>(nameof(TotalRamHuman), TotalRamHuman);
if (AvailableRamHuman is not null)
yield return new KeyValuePair<string, object>(nameof(AvailableRamHuman), AvailableRamHuman);
if (AvailablePercentRamHuman is not null)
yield return new KeyValuePair<string, object>(nameof(AvailablePercentRamHuman), AvailablePercentRamHuman);
}
public static Machine FromLocal()
{
Machine machine = new(Helper.GetMachineId())
{
ShortName = Environment.MachineName
};
try
{
HardwareInfo hardwareInfo = new ();
hardwareInfo.RefreshMemoryStatus();
machine.OperatingSystem = hardwareInfo.OperatingSystem.ToString();
machine.TotalRam = hardwareInfo.MemoryStatus.TotalPhysical;
machine.AvailableRam = hardwareInfo.MemoryStatus.AvailablePhysical;
}
catch (Exception ex)
{
}
return machine;
}
}