using Disco.Models.ClientServices.EnrolmentInformation; using Disco.Models.Repository; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; namespace Disco.Services { public static class DeviceDetailExtensions { #region Helpers public static string GetDetail(this IEnumerable details, string scope, string key) { if (details == null) throw new ArgumentNullException("details"); if (string.IsNullOrEmpty(scope)) throw new ArgumentNullException("Scope"); if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("Key"); var detail = details.Where(d => d.Scope == scope && d.Key == key).FirstOrDefault(); if (detail == null) return null; else return detail.Value; } public static void SetDetail(this Device device, string scope, string key, string value) { if (device == null) throw new ArgumentNullException("device"); if (string.IsNullOrEmpty(scope)) throw new ArgumentNullException("Scope"); if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("Key"); var detail = device.DeviceDetails.Where(d => d.Scope == scope && d.Key == key).FirstOrDefault(); // No Detail Stored & Set to Null if (detail == null && value == null) return; if (detail == null) { detail = new DeviceDetail() { DeviceSerialNumber = device.SerialNumber, Scope = scope, Key = key, Value = value }; device.DeviceDetails.Add(detail); } if (detail.Value != value) { if (value == null) { device.DeviceDetails.Remove(detail); } else { detail.Value = value; } } } #endregion #region LanMacAddress /// /// Gets the LanMacAddress Device Detail Value /// /// The LanMacAddress or null public static string LanMacAddress(this IEnumerable details) { return details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyLanMacAddress); } /// /// Sets the LanMacAddress Device Detail Value /// public static void LanMacAddress(this IEnumerable details, Device device, string LanMacAddress) { device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyLanMacAddress, LanMacAddress); } #endregion #region WLanMacAddress /// /// Gets the WLanMacAddress Device Detail Value /// /// The WLanMacAddress or null public static string WLanMacAddress(this IEnumerable details) { return details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyWLanMacAddress); } /// /// Sets the WLanMacAddress Device Detail Value /// public static void WLanMacAddress(this IEnumerable details, Device device, string WLanMacAddress) { device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyWLanMacAddress, WLanMacAddress); } #endregion #region ACAdapter /// /// Gets the ACAdapter Device Detail Value /// /// The ACAdapter or null public static string ACAdapter(this IEnumerable details) { return details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyACAdapter); } /// /// Sets the ACAdapter Device Detail Value /// public static void ACAdapter(this IEnumerable details, Device device, string ACAdapter) { device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyACAdapter, ACAdapter); } #endregion #region Battery /// /// Gets the Battery Device Detail Value /// /// The Battery or null public static string Battery(this IEnumerable details) { return details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyBattery); } /// /// Sets the Battery Device Detail Value /// public static void Battery(this IEnumerable details, Device device, string Battery) { device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyBattery, Battery); } #endregion #region Keyboard /// /// Gets the Keyboard Device Detail Value /// /// The Keyboard or null public static string Keyboard(this IEnumerable details) { return details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyKeyboard); } /// /// Sets the Keyboard Device Detail Value /// public static void Keyboard(this IEnumerable details, Device device, string Keyboard) { device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyKeyboard, Keyboard); } #endregion /// /// Gets the Network Adapters Device Detail Value /// public static List NetworkAdapters(this IEnumerable details) { var json = details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyNetworkAdapters); if (string.IsNullOrEmpty(json)) return null; return JsonConvert.DeserializeObject>(json); } /// /// Sets the Network Adapters Device Detail Value /// public static void NetworkAdapters(this IEnumerable details, Device device, List networkAdapters) { var json = default(string); if (networkAdapters != null && networkAdapters.Count > 0) json = JsonConvert.SerializeObject(networkAdapters); device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyNetworkAdapters, json); } /// /// Gets the Processors Device Detail Value /// public static List Processors(this IEnumerable details) { var json = details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyProcessors); if (string.IsNullOrEmpty(json)) return null; return JsonConvert.DeserializeObject>(json); } /// /// Sets the Processors Device Detail Value /// public static void Processors(this IEnumerable details, Device device, List processors) { var json = default(string); if (processors != null && processors.Count > 0) json = JsonConvert.SerializeObject(processors); device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyProcessors, json); } /// /// Gets the Physical Memory Device Detail Value /// public static List PhysicalMemory(this IEnumerable details) { var json = details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyPhysicalMemory); if (string.IsNullOrEmpty(json)) return null; return JsonConvert.DeserializeObject>(json); } /// /// Sets the Physical Memory Device Detail Value /// public static void PhysicalMemory(this IEnumerable details, Device device, List physicalMemory) { var json = default(string); if (physicalMemory != null && physicalMemory.Count > 0) json = JsonConvert.SerializeObject(physicalMemory); device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyPhysicalMemory, json); } /// /// Gets the Disk Drives Device Detail Value /// public static List DiskDrives(this IEnumerable details) { var json = details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyDiskDrives); if (string.IsNullOrEmpty(json)) return null; return JsonConvert.DeserializeObject>(json); } /// /// Sets the Disk Drives Device Detail Value /// public static void DiskDrives(this IEnumerable details, Device device, List diskDrives) { var json = default(string); if (diskDrives != null && diskDrives.Count > 0) json = JsonConvert.SerializeObject(diskDrives); device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyDiskDrives, json); } /// /// Gets the Bios Device Detail Value /// public static List Bios(this IEnumerable details) { var json = details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyBios); if (string.IsNullOrEmpty(json)) return null; return JsonConvert.DeserializeObject>(json); } /// /// Sets the Bios Device Detail Value /// public static void Bios(this IEnumerable details, Device device, List bios) { var json = default(string); if (bios != null && bios.Count > 0) json = JsonConvert.SerializeObject(bios); device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyBios, json); } /// /// Gets the Base Board Device Detail Value /// public static List BaseBoard(this IEnumerable details) { var json = details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyBaseBoard); if (string.IsNullOrEmpty(json)) return null; return JsonConvert.DeserializeObject>(json); } /// /// Sets the Base Board Device Detail Value /// public static void BaseBoard(this IEnumerable details, Device device, List baseBoard) { var json = default(string); if (baseBoard != null && baseBoard.Count > 0) json = JsonConvert.SerializeObject(baseBoard); device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyBaseBoard, json); } /// /// Gets the Computer System Device Detail Value /// public static List ComputerSystem(this IEnumerable details) { var json = details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyComputerSystem); if (string.IsNullOrEmpty(json)) return null; return JsonConvert.DeserializeObject>(json); } /// /// Sets the Computer System Device Detail Value /// public static void ComputerSystem(this IEnumerable details, Device device, List computerSystem) { var json = default(string); if (computerSystem != null && computerSystem.Count > 0) json = JsonConvert.SerializeObject(computerSystem); device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyComputerSystem, json); } /// /// Gets the Batteries Device Detail Value /// public static List Batteries(this IEnumerable details) { var json = details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyBatteries); if (string.IsNullOrEmpty(json)) return null; return JsonConvert.DeserializeObject>(json); } /// /// Sets the Batteries Device Detail Value /// public static void Batteries(this IEnumerable details, Device device, List batteries) { var json = default(string); if (batteries != null && batteries.Count > 0) json = JsonConvert.SerializeObject(batteries); device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyBatteries, json); } public static string MdmHardwareData(this IEnumerable details) { return details.GetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyMdmHardwareData); } /// /// Sets the Mobile Device Management Hardware Data /// public static void MdmHardwareData(this IEnumerable details, Device device, string mdmHardwareData) { device.SetDetail(DeviceDetail.ScopeHardware, DeviceDetail.HardwareKeyMdmHardwareData, mdmHardwareData); } } }