From 2bc12a363f46122ef8f250053f36f67bf4332e49 Mon Sep 17 00:00:00 2001 From: Michal Date: Fri, 11 Jan 2019 05:40:35 -0800 Subject: [PATCH] Removal of ValueTuple reference. (#78) It turned out that usage of ValueTuple can be avoided if the code gets simplified (or rather turned into series of LINQ queries). --- Hardware/HDD/AbstractHarddrive.cs | 63 +++++++++++++------------------ OpenHardwareMonitorLib.csproj | 1 - 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/Hardware/HDD/AbstractHarddrive.cs b/Hardware/HDD/AbstractHarddrive.cs index 548d01f..ee212d8 100644 --- a/Hardware/HDD/AbstractHarddrive.cs +++ b/Hardware/HDD/AbstractHarddrive.cs @@ -15,6 +15,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; +using System.Linq; namespace OpenHardwareMonitor.Hardware.HDD { @@ -40,10 +41,10 @@ namespace OpenHardwareMonitor.Hardware.HDD private readonly int index; private int count; - private readonly IList smartAttributes; + private readonly IReadOnlyList smartAttributes; private IDictionary sensors; - private DriveInfo[] driveInfos; + private readonly DriveInfo[] driveInfos; private Sensor usageSensor; protected AbstractHarddrive(ISmart smart, string name, @@ -62,7 +63,7 @@ namespace OpenHardwareMonitor.Hardware.HDD this.index = index; this.count = 0; - this.smartAttributes = new List(smartAttributes); + this.smartAttributes = smartAttributes.ToList(); string[] logicalDrives = smart.GetLogicalDrives(index); List driveInfoList = new List(logicalDrives.Length); @@ -178,37 +179,26 @@ namespace OpenHardwareMonitor.Hardware.HDD sensors = new Dictionary(); if (handle != smart.InvalidHandle) { - IList<(SensorType, int)> sensorTypeAndChannels = - new List<(SensorType, int)>(); - DriveAttributeValue[] values = smart.ReadSmartData(handle, index); + var smartIds = smart.ReadSmartData(handle, index) + .Select(attrValue => attrValue.Identifier); - foreach (SmartAttribute attribute in smartAttributes) { - if (!attribute.SensorType.HasValue) - continue; + // unique attributes by SensorType and SensorChannel. + var uniqueAtrributes = smartAttributes + .Where(a => a.SensorType.HasValue) + .Where(a => smartIds.Contains(a.Identifier)) + .GroupBy(a => new { a.SensorType.Value, a.SensorChannel }) + .Select(g => g.First()); - bool found = false; - foreach (DriveAttributeValue value in values) { - if (value.Identifier == attribute.Identifier) { - found = true; - break; - } - } - if (!found) - continue; - var pair = (vaule: attribute.SensorType.Value, - sensorChannel: attribute.SensorChannel); + sensors = uniqueAtrributes.ToDictionary(attr => attr, + attr => new Sensor(attr.SensorName, + attr.SensorChannel, attr.DefaultHiddenSensor, + attr.SensorType.Value, this, attr.ParameterDescriptions, + settings)); - if (!sensorTypeAndChannels.Contains(pair)) { - Sensor sensor = new Sensor(attribute.SensorName, - attribute.SensorChannel, attribute.DefaultHiddenSensor, - attribute.SensorType.Value, this, attribute.ParameterDescriptions, - settings); - - sensors.Add(attribute, sensor); - ActivateSensor(sensor); - sensorTypeAndChannels.Add(pair); - } + foreach (var sensor in sensors) + { + ActivateSensor(sensor.Value); } } @@ -307,13 +297,12 @@ namespace OpenHardwareMonitor.Hardware.HDD string description = "Unknown"; float? physical = null; - foreach (SmartAttribute a in smartAttributes) { - if (a.Identifier == value.Identifier) { - description = a.Name; - if (a.HasRawValueConversion | a.SensorType.HasValue) - physical = a.ConvertValue(value, null); - else - physical = null; + + var attr = smartAttributes.FirstOrDefault(a => a.Identifier == value.Identifier); + if (attr != null) { + description = attr.Name; + if (attr.HasRawValueConversion | attr.SensorType.HasValue) { + physical = attr.ConvertValue(value, null); } } diff --git a/OpenHardwareMonitorLib.csproj b/OpenHardwareMonitorLib.csproj index a35ce60..a42eb8a 100644 --- a/OpenHardwareMonitorLib.csproj +++ b/OpenHardwareMonitorLib.csproj @@ -136,6 +136,5 @@ - \ No newline at end of file