feat(storage): 新增只读 SMART 辅助服务与寿命通电详情

This commit is contained in:
2026-10-30 16:02:46 +08:00
parent 069e2e100f
commit 7e9728899d
25 changed files with 515 additions and 15 deletions
+20
View File
@@ -0,0 +1,20 @@
import Foundation
nonisolated enum DiskHealthReport {
static func parse(_ data: Data) -> DiskHealthMetrics? {
guard let root = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any],
let smart = root["SMARTDeviceSpecificKeysMayVaryNotGuaranteed"] as? [String: Any] else { return nil }
// Multiple physical stores cannot be represented as one device's lifetime.
if let stores = root["APFSPhysicalStores"] as? [Any], stores.count != 1 { return nil }
func integer(_ key: String) -> UInt64? {
guard let value = smart[key] as? NSNumber,
CFGetTypeID(value) != CFBooleanGetTypeID() else { return nil }
return UInt64(value.stringValue)
}
let used = integer("PERCENTAGE_USED").flatMap { $0 <= 255 ? $0 : nil }
let remaining = used.map { Double(100 - min(100, $0)) / 100 }
let hours = integer("POWER_ON_HOURS_1") == 0 ? integer("POWER_ON_HOURS_0") : nil
guard remaining != nil || hours != nil else { return nil }
return DiskHealthMetrics(remainingLife: remaining, powerOnHours: hours)
}
}