Example #1
0
        private void CreateSensors()
        {
            sensors = new Dictionary <SmartAttribute, Sensor>();

            if (handle != smart.InvalidHandle)
            {
                var smartIds = smart.ReadSmartData(handle, index)
                               .Select(attrValue => attrValue.Identifier);

                // 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());

                sensors = uniqueAtrributes.ToDictionary(attr => attr,
                                                        attr => new Sensor(attr.SensorName,
                                                                           attr.SensorChannel, attr.DefaultHiddenSensor,
                                                                           attr.SensorType.Value, this, attr.ParameterDescriptions,
                                                                           settings));

                foreach (var sensor in sensors)
                {
                    ActivateSensor(sensor.Value);
                }
            }

            if (driveInfos.Length > 0)
            {
                usageSensor =
                    new Sensor("Used Space", 0, SensorType.Load, this, settings);
                ActivateSensor(usageSensor);
            }
        }
        private void CreateSensors()
        {
            sensors = new Dictionary <SmartAttribute, Sensor>();

            if (handle != smart.InvalidHandle)
            {
                IList <Pair <SensorType, int> > sensorTypeAndChannels =
                    new List <Pair <SensorType, int> >();

                DriveAttributeValue[] values = smart.ReadSmartData(handle, index);

                foreach (SmartAttribute attribute in smartAttributes)
                {
                    if (!attribute.SensorType.HasValue)
                    {
                        continue;
                    }

                    bool found = false;
                    foreach (DriveAttributeValue value in values)
                    {
                        if (value.Identifier == attribute.Identifier)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        continue;
                    }

                    Pair <SensorType, int> pair = new Pair <SensorType, int>(
                        attribute.SensorType.Value, attribute.SensorChannel);

                    if (!sensorTypeAndChannels.Contains(pair))
                    {
                        Sensor sensor = new Sensor(attribute.Name,
                                                   attribute.SensorChannel, attribute.DefaultHiddenSensor,
                                                   attribute.SensorType.Value, this, attribute.ParameterDescriptions,
                                                   settings);

                        sensors.Add(attribute, sensor);
                        ActivateSensor(sensor);
                        sensorTypeAndChannels.Add(pair);
                    }
                }
            }

            if (driveInfos.Length > 0)
            {
                usageSensor =
                    new Sensor("Used Space", 0, SensorType.Load, this, settings);
                ActivateSensor(usageSensor);
            }
        }
Example #3
0
        protected override sealed void CreateSensors()
        {
            sensors = new Dictionary <SmartAttribute, Sensor>();

            if (smart.IsValid)
            {
                IList <Pair <SensorType, int> > sensorTypeAndChannels =
                    new List <Pair <SensorType, int> >();

                DriveAttributeValue[] values = smart.ReadSmartData();

                foreach (SmartAttribute attribute in smartAttributes)
                {
                    if (!attribute.SensorType.HasValue)
                    {
                        continue;
                    }

                    bool found = false;
                    foreach (DriveAttributeValue value in values)
                    {
                        if (value.Identifier == attribute.Identifier)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        continue;
                    }

                    Pair <SensorType, int> pair = new Pair <SensorType, int>(
                        attribute.SensorType.Value, attribute.SensorChannel);

                    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);
                    }
                }
            }

            base.CreateSensors();
        }
        private void CreateSensors()
        {
            sensors = new Dictionary <SmartAttribute, Sensor>();

            IList <Pair <SensorType, int> > sensorTypeAndChannels =
                new List <Pair <SensorType, int> >();

            DriveAttributeValue[] values = smart.ReadSmartData(handle, index);

            foreach (SmartAttribute attribute in smartAttributes)
            {
                if (!attribute.SensorType.HasValue)
                {
                    continue;
                }

                bool found = false;
                foreach (DriveAttributeValue value in values)
                {
                    if (value.Identifier == attribute.Identifier)
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    continue;
                }

                Pair <SensorType, int> pair = new Pair <SensorType, int>(
                    attribute.SensorType.Value, attribute.SensorChannel);

                if (!sensorTypeAndChannels.Contains(pair))
                {
                    Sensor sensor = new Sensor(attribute.Name,
                                               attribute.SensorChannel, attribute.DefaultHiddenSensor,
                                               attribute.SensorType.Value, this, null, settings);

                    sensors.Add(attribute, sensor);
                    ActivateSensor(sensor);
                    sensorTypeAndChannels.Add(pair);
                }
            }
        }
Example #5
0
        public static AbstractHarddrive CreateInstance(ISmart smart, 
      int driveIndex, ISettings settings)
        {
            IntPtr deviceHandle = smart.OpenDrive(driveIndex);

              string name = null;
              string firmwareRevision = null;
              DriveAttributeValue[] values = { };

              if (deviceHandle != smart.InvalidHandle) {
            bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle,
            driveIndex, out name, out firmwareRevision);
            bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);

            if (smartEnabled)
              values = smart.ReadSmartData(deviceHandle, driveIndex);

            smart.CloseHandle(deviceHandle);

            if (!nameValid) {
              name = null;
              firmwareRevision = null;
            }
              } else {
            string[] logicalDrives = smart.GetLogicalDrives(driveIndex);
            if (logicalDrives == null || logicalDrives.Length == 0)
              return null;
              }

              if (string.IsNullOrEmpty(name))
            name = "Generic Hard Disk";

              if (string.IsNullOrEmpty(firmwareRevision))
            firmwareRevision = "Unknown";

              foreach (Type type in hddTypes) {
            // get the array of name prefixes for the current type
            NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
              typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];

            // get the array of the required SMART attributes for the current type
            RequireSmartAttribute[] requiredAttributes = type.GetCustomAttributes(
              typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];

            // check if all required attributes are present
            bool allRequiredAttributesFound = true;
            foreach (var requireAttribute in requiredAttributes) {
              bool adttributeFound = false;
              foreach (DriveAttributeValue value in values) {
            if (value.Identifier == requireAttribute.AttributeId) {
              adttributeFound = true;
              break;
            }
              }
              if (!adttributeFound) {
            allRequiredAttributesFound = false;
            break;
              }
            }

            // if an attribute is missing, then try the next type
            if (!allRequiredAttributesFound)
              continue;

            // check if there is a matching name prefix for this type
            foreach (NamePrefixAttribute prefix in namePrefixes) {
              if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
            return Activator.CreateInstance(type, smart, name, firmwareRevision,
              driveIndex, settings) as AbstractHarddrive;
            }
              }

              // no matching type has been found
              return null;
        }
        public static AbstractHarddrive CreateInstance(ISmart smart,
                                                       int driveIndex, ISettings settings)
        {
            IntPtr deviceHandle = smart.OpenDrive(driveIndex);

            string name             = null;
            string firmwareRevision = null;

            DriveAttributeValue[] values = { };

            if (deviceHandle != smart.InvalidHandle)
            {
                bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle,
                                                                   driveIndex, out name, out firmwareRevision);
                bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);

                if (smartEnabled)
                {
                    values = smart.ReadSmartData(deviceHandle, driveIndex);
                }

                smart.CloseHandle(deviceHandle);

                if (!nameValid)
                {
                    name             = null;
                    firmwareRevision = null;
                }
            }
            else
            {
                string[] logicalDrives = smart.GetLogicalDrives(driveIndex);
                if (logicalDrives == null || logicalDrives.Length == 0)
                {
                    return(null);
                }
            }

            if (string.IsNullOrEmpty(name))
            {
                name = "Generic Hard Disk";
            }

            if (string.IsNullOrEmpty(firmwareRevision))
            {
                firmwareRevision = "Unknown";
            }

            foreach (Type type in hddTypes)
            {
                // get the array of name prefixes for the current type
                NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
                    typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];

                // get the array of the required SMART attributes for the current type
                RequireSmartAttribute[] requiredAttributes = type.GetCustomAttributes(
                    typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];

                // check if all required attributes are present
                bool allRequiredAttributesFound = true;
                foreach (var requireAttribute in requiredAttributes)
                {
                    bool adttributeFound = false;
                    foreach (DriveAttributeValue value in values)
                    {
                        if (value.Identifier == requireAttribute.AttributeId)
                        {
                            adttributeFound = true;
                            break;
                        }
                    }
                    if (!adttributeFound)
                    {
                        allRequiredAttributesFound = false;
                        break;
                    }
                }

                // if an attribute is missing, then try the next type
                if (!allRequiredAttributesFound)
                {
                    continue;
                }

                // check if there is a matching name prefix for this type
                foreach (NamePrefixAttribute prefix in namePrefixes)
                {
                    if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
                    {
                        return(Activator.CreateInstance(type, smart, name, firmwareRevision,
                                                        driveIndex, settings) as AbstractHarddrive);
                    }
                }
            }

            // no matching type has been found
            return(null);
        }