public HarddriveGroup(ISettings settings) {
      int p = (int)Environment.OSVersion.Platform;
      if (p == 4 || p == 128) return;

      ISmart smart = new WindowsSmart();

      for (int drive = 0; drive < MAX_DRIVES; drive++) {
        AbstractHarddrive instance =
          AbstractHarddrive.CreateInstance(smart, drive, settings);
        if (instance != null) {
          this.hardware.Add(instance);
        }
      }
    }
예제 #2
0
        public HarddriveGroup(ISettings settings)
        {
            if (OperatingSystem.IsUnix)
            {
                return;
            }

            ISmart smart = new WindowsSmart();

            for (int drive = 0; drive < MAX_DRIVES; drive++)
            {
                AbstractHarddrive instance =
                    AbstractHarddrive.CreateInstance(smart, drive, settings);
                if (instance != null)
                {
                    this.hardware.Add(instance);
                }
            }
        }
예제 #3
0
        public HarddriveGroup(ISettings settings)
        {
            int p = (int)Environment.OSVersion.Platform;

            if (p == 4 || p == 128)
            {
                return;
            }

            ISmart smart = new WindowsSmart();

            for (int drive = 0; drive < MAX_DRIVES; drive++)
            {
                AbstractHarddrive instance = AbstractHarddrive.CreateInstance(smart, drive, settings);
                if (instance != null)
                {
                    this.hardware.Add(instance);
                }
            }
        }
예제 #4
0
        public static AbstractStorage CreateInstance(StorageInfo info, ISettings settings)
        {
            ISmart smart = new WindowsSmart(info.Index);

            string name             = null;
            string firmwareRevision = null;

            DriveAttributeValue[] values        = { };
            IEnumerable <string>  logicalDrives = WindowsStorage.GetLogicalDrives(info.Index);

            if (smart.IsValid)
            {
                bool nameValid    = smart.ReadNameAndFirmwareRevision(out name, out firmwareRevision);
                bool smartEnabled = smart.EnableSmart();

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

                if (!nameValid)
                {
                    name             = null;
                    firmwareRevision = null;
                }
            }
            else
            {
                if (logicalDrives == null || !logicalDrives.Any())
                {
                    smart.Close();
                    return(null);
                }

                bool hasNonZeroSizeDrive = false;
                foreach (string logicalDrive in logicalDrives)
                {
                    try {
                        DriveInfo di = new DriveInfo(logicalDrive);
                        if (di.TotalSize > 0)
                        {
                            hasNonZeroSizeDrive = true;
                            break;
                        }
                    } catch (Exception x) when(x is ArgumentException || x is IOException || x is UnauthorizedAccessException)
                    {
                        Logging.LogError(x, $"Unable to get drive info on {info.Name} for logical drive {logicalDrive}");
                    }
                }

                if (!hasNonZeroSizeDrive)
                {
                    Logging.LogInfo($"Excluding {info.Name} because it has no valid partitions and is not SMART capable.");
                    smart.Close();
                    return(null);
                }
            }

            if (string.IsNullOrEmpty(name))
            {
                name = string.IsNullOrEmpty(info.Name) ? "Generic Hard Disk" : info.Name;
            }

            if (string.IsNullOrEmpty(firmwareRevision))
            {
                firmwareRevision = string.IsNullOrEmpty(info.Revision) ? "Unknown" : info.Revision;
            }

            if (logicalDrives.Any())
            {
                logicalDrives = logicalDrives.Select(x => $"{x}:");
                name         += " (" + string.Join(", ", logicalDrives) + ")";
            }

            Logging.LogInfo($"Attempting to initialize sensor instance for {name}");

            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))
                    {
                        Logging.LogInfo($"Drive appears to be an instance of {type}");
                        return(Activator.CreateInstance(type, smart, name, firmwareRevision,
                                                        info.Index, settings) as ATAStorage);
                    }
                }
            }

            Logging.LogInfo($"Could not find a matching sensor type for this device");
            // no matching type has been found
            smart.Close();
            return(null);
        }
        public static AbstractStorage CreateInstance(StorageInfo info, ISettings settings)
        {
            ISmart smart = new WindowsSmart(info.Index);

            string name             = null;
            string firmwareRevision = null;

            DriveAttributeValue[] values = { };

            if (smart.IsValid)
            {
                bool nameValid    = smart.ReadNameAndFirmwareRevision(out name, out firmwareRevision);
                bool smartEnabled = smart.EnableSmart();

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

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

                bool hasNonZeroSizeDrive = false;
                foreach (string logicalDrive in logicalDrives)
                {
                    try {
                        DriveInfo di = new DriveInfo(logicalDrive);
                        if (di.TotalSize > 0)
                        {
                            hasNonZeroSizeDrive = true;
                            break;
                        }
                    } catch (ArgumentException) {
                    } catch (IOException) {
                    } catch (UnauthorizedAccessException) {
                    }
                }

                if (!hasNonZeroSizeDrive)
                {
                    smart.Close();
                    return(null);
                }
            }

            if (string.IsNullOrEmpty(name))
            {
                name = string.IsNullOrEmpty(info.Name) ? "Generic Hard Disk" : info.Name;
            }

            if (string.IsNullOrEmpty(firmwareRevision))
            {
                firmwareRevision = string.IsNullOrEmpty(info.Revision) ? "Unknown" : info.Revision;
            }

            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,
                                                        info.Index, settings) as ATAStorage);
                    }
                }
            }

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