Remotely/Shared/Models/Device.cs
2021-07-29 07:56:02 -07:00

120 lines
4.5 KiB
C#

using Remotely.Shared.Services;
using Microsoft.Management.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Remotely.Shared.Models
{
public class Device
{
public string AgentVersion { get; set; }
[StringLength(100)]
public string Alias { get; set; }
public double CpuUtilization { get; set; }
public string CurrentUser { get; set; }
public virtual DeviceGroup DeviceGroup { get; set; }
public string DeviceGroupID { get; set; }
public string DeviceName { get; set; }
public List<Drive> Drives { get; set; }
public double UsedMemory { get; set; }
public double UsedStorage { get; set; }
[Key]
public string ID { get; set; }
public bool Is64Bit { get; set; }
public bool IsOnline { get; set; }
public DateTime LastOnline { get; set; }
[JsonIgnore]
public virtual Organization Organization { get; set; }
public string OrganizationID { get; set; }
public Architecture OSArchitecture { get; set; }
public string OSDescription { get; set; }
public string Platform { get; set; }
public int ProcessorCount { get; set; }
public string ServerVerificationToken { get; set; }
[StringLength(200)]
public string Tags { get; set; } = "";
public double TotalMemory { get; set; }
public double TotalStorage { get; set; }
public static async Task<Device> Create(ConnectionInfo connectionInfo)
{
OSPlatform platform = OSUtils.GetPlatform();
DriveInfo systemDrive;
if (!string.IsNullOrWhiteSpace(Environment.SystemDirectory))
{
systemDrive = DriveInfo.GetDrives()
.Where(x=>x.IsReady)
.FirstOrDefault(x =>
x.RootDirectory.FullName.Contains(Path.GetPathRoot(Environment.SystemDirectory ?? Environment.CurrentDirectory))
);
}
else
{
systemDrive = DriveInfo.GetDrives().FirstOrDefault(x =>
x.IsReady &&
x.RootDirectory.FullName == Path.GetPathRoot(Environment.CurrentDirectory));
}
var device = new Device()
{
ID = connectionInfo.DeviceID,
DeviceName = Environment.MachineName,
Platform = platform.ToString(),
ProcessorCount = Environment.ProcessorCount,
OSArchitecture = RuntimeInformation.OSArchitecture,
OSDescription = RuntimeInformation.OSDescription,
Is64Bit = Environment.Is64BitOperatingSystem,
IsOnline = true,
Drives = DriveInfo.GetDrives().Where(x => x.IsReady).Select(x => new Drive()
{
DriveFormat = x.DriveFormat,
DriveType = x.DriveType,
Name = x.Name,
RootDirectory = x.RootDirectory.FullName,
FreeSpace = x.TotalSize > 0 ? x.TotalFreeSpace / x.TotalSize : 0,
TotalSize = x.TotalSize > 0 ? Math.Round((double)(x.TotalSize / 1024 / 1024 / 1024), 2) : 0,
VolumeLabel = x.VolumeLabel
}).ToList(),
OrganizationID = connectionInfo.OrganizationID,
CurrentUser = DeviceInformation.GetCurrentUser()
};
if (systemDrive != null && systemDrive.TotalSize > 0 && systemDrive.TotalFreeSpace > 0)
{
device.TotalStorage = Math.Round((double)(systemDrive.TotalSize / 1024 / 1024 / 1024), 2);
device.UsedStorage = Math.Round((double)((systemDrive.TotalSize - systemDrive.TotalFreeSpace) / 1024 / 1024 / 1024), 2);
}
var (usedMemory, totalMemory) = DeviceInformation.GetMemoryInGB();
device.UsedMemory = usedMemory;
device.TotalMemory = totalMemory;
device.CpuUtilization = await DeviceInformation.GetCpuUtilization();
if (File.Exists("Remotely_Agent.dll"))
{
device.AgentVersion = FileVersionInfo.GetVersionInfo("Remotely_Agent.dll")?.FileVersion?.ToString()?.Trim();
}
return device;
}
}
}