using Salmon.Core.Cliff; namespace Salmon.Model.Monitor; public class Drive : Hardware { [PersistentField] public string? RootDirectory { get; set; } [PersistentField] public ulong? TotalSize { get; set; } public string? TotalSizeHuman { get { if (TotalSize is null) return null; return Helper.SizeSuffix((long)TotalSize); } } [PersistentField] public ulong? FreeSpace { get; set; } public string? FreeSpaceHuman { get { if (FreeSpace is null) return null; return Helper.SizeSuffix((long)FreeSpace); } } public float? FreePercentSpace { get { if (TotalSize is null || FreeSpace is null) return null; return (float)FreeSpace / (float)TotalSize; } } public string? FreePercentSpaceHuman { get { if (FreePercentSpace is null) return null; return $"{FreePercentSpace * 100:0.0}%"; } } [PersistentField] public bool? IsReady { get; set; } [PersistentField] public string? Format { get; set; } [PersistentField] public string? Type { get; set; } public Drive(string id) : base(id) { } public override IEnumerable> ImportantProperties() { foreach(var i in base.ImportantProperties()) yield return new KeyValuePair(i.Key, i.Value); if(TotalSizeHuman is not null) yield return new KeyValuePair(nameof(TotalSizeHuman), TotalSizeHuman); if (FreeSpaceHuman is not null) yield return new KeyValuePair(nameof(FreeSpaceHuman), FreeSpaceHuman); if (FreePercentSpaceHuman is not null) yield return new KeyValuePair(nameof(FreePercentSpaceHuman), FreePercentSpaceHuman); } private static Drive FromDriveInfo(DriveInfo driveInfo) { string id = Helper.CreateIdFrom(Helper.GetMachineId(), driveInfo.Name); var drive = new Drive(id) { ShortName = driveInfo.Name, LongName = $"{Environment.MachineName}:{driveInfo.Name}", RootDirectory = driveInfo.RootDirectory.FullName, Type = driveInfo.DriveType.ToString(), IsReady = driveInfo.IsReady, ParentId = Helper.GetMachineId() }; if (driveInfo.IsReady) { drive.TotalSize = (ulong)driveInfo.TotalSize; drive.FreeSpace = (ulong)driveInfo.TotalFreeSpace; drive.Format = driveInfo.DriveFormat; } return drive; } public static IEnumerable FromAllLocalDrives() { foreach(var d in DriveInfo.GetDrives()) yield return Drive.FromDriveInfo(d); } }