using Disco.Models.Repository; using System; using System.Collections.Generic; using System.Linq; namespace Disco.Services { public static class DeviceDetailExtensions { #region Helpers private 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.Key == Key).FirstOrDefault(); if (detail == null) return null; else return detail.Value; } private 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 } }