Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6473a1a4b2 | ||
|
|
f34bb6bafe | ||
|
|
9b3f6ae50e | ||
|
|
53dad97c6c | ||
|
|
e49c471bad | ||
|
|
c8743e2578 |
@@ -20,7 +20,7 @@ concurrency:
|
|||||||
jobs:
|
jobs:
|
||||||
build-and-test:
|
build-and-test:
|
||||||
name: Build & Test (Intel x86_64)
|
name: Build & Test (Intel x86_64)
|
||||||
runs-on: [macos, intel]
|
runs-on: macos-14
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository
|
- name: Check out repository
|
||||||
@@ -59,12 +59,12 @@ jobs:
|
|||||||
if command -v xcbeautify &> /dev/null; then
|
if command -v xcbeautify &> /dev/null; then
|
||||||
xcodebuild clean build \
|
xcodebuild clean build \
|
||||||
-scheme MacMonitor \
|
-scheme MacMonitor \
|
||||||
-destination 'generic/platform=macOS,arch=x86_64' \
|
-destination 'platform=macOS,arch=x86_64' \
|
||||||
CODE_SIGNING_ALLOWED=NO | xcbeautify
|
CODE_SIGNING_ALLOWED=NO | xcbeautify
|
||||||
else
|
else
|
||||||
xcodebuild clean build \
|
xcodebuild clean build \
|
||||||
-scheme MacMonitor \
|
-scheme MacMonitor \
|
||||||
-destination 'generic/platform=macOS,arch=x86_64' \
|
-destination 'platform=macOS,arch=x86_64' \
|
||||||
CODE_SIGNING_ALLOWED=NO
|
CODE_SIGNING_ALLOWED=NO
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
disabled_rules:
|
||||||
|
- type_body_length
|
||||||
|
- function_body_length
|
||||||
|
- file_length
|
||||||
|
- cyclomatic_complexity
|
||||||
|
- identifier_name
|
||||||
|
- line_length
|
||||||
|
- large_tuple
|
||||||
|
- multiple_closures_with_trailing_closure
|
||||||
|
- trailing_whitespace
|
||||||
|
- implicit_optional_initialization
|
||||||
|
|
||||||
|
opt_in_rules:
|
||||||
|
- empty_count
|
||||||
|
|
||||||
|
included:
|
||||||
|
- Sources
|
||||||
|
- Tests
|
||||||
|
|
||||||
|
excluded:
|
||||||
|
- MacMonitor.xcodeproj
|
||||||
|
- build
|
||||||
|
- DerivedData
|
||||||
@@ -81,6 +81,143 @@ public struct PowerMetrics: Sendable {
|
|||||||
public var sensorReadings: [(key: String, name: String, value: Double, unit: String)] = []
|
public var sensorReadings: [(key: String, name: String, value: Double, unit: String)] = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Milestone 3 Advanced Telemetry Models
|
||||||
|
|
||||||
|
public struct KernelCountersMetrics: Sendable {
|
||||||
|
public var contextSwitchesPerSec: Double = 0
|
||||||
|
public var syscallsPerSec: Double = 0
|
||||||
|
public var pageFaultsPerSec: Double = 0
|
||||||
|
public var cowFaultsPerSec: Double = 0
|
||||||
|
public var zeroFillFaultsPerSec: Double = 0
|
||||||
|
public var pageinsPerSec: Double = 0
|
||||||
|
public var pageoutsPerSec: Double = 0
|
||||||
|
public var totalContextSwitches: UInt64 = 0
|
||||||
|
public var totalSyscalls: UInt64 = 0
|
||||||
|
public var totalPageFaults: UInt64 = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct SystemLoadMetrics: Sendable {
|
||||||
|
public var load1Min: Double = 0
|
||||||
|
public var load5Min: Double = 0
|
||||||
|
public var load15Min: Double = 0
|
||||||
|
public var taskCount: Int = 0
|
||||||
|
public var threadCount: Int = 0
|
||||||
|
public var machFactor: Double = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct DiskIOItem: Identifiable, Sendable {
|
||||||
|
public var id: String { bsdName }
|
||||||
|
public let bsdName: String
|
||||||
|
public let readBps: Double
|
||||||
|
public let writeBps: Double
|
||||||
|
public let readIOPS: Double
|
||||||
|
public let writeIOPS: Double
|
||||||
|
public let totalBytesRead: UInt64
|
||||||
|
public let totalBytesWritten: UInt64
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct NetworkBandwidthItem: Identifiable, Sendable {
|
||||||
|
public var id: String { interfaceName }
|
||||||
|
public let interfaceName: String
|
||||||
|
public let ipAddress: String
|
||||||
|
public let isUp: Bool
|
||||||
|
public let downloadBps: Double
|
||||||
|
public let uploadBps: Double
|
||||||
|
public let downloadPps: Double
|
||||||
|
public let uploadPps: Double
|
||||||
|
public let totalBytesIn: UInt64
|
||||||
|
public let totalBytesOut: UInt64
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct SocketItem: Identifiable, Sendable {
|
||||||
|
public var id: String { "\(pid)_\(fd)_\(localAddress):\(localPort)" }
|
||||||
|
public let pid: Int32
|
||||||
|
public let fd: Int32
|
||||||
|
public let processName: String
|
||||||
|
public let protocolName: String
|
||||||
|
public let localAddress: String
|
||||||
|
public let localPort: UInt16
|
||||||
|
public let remoteAddress: String
|
||||||
|
public let remotePort: UInt16
|
||||||
|
public let tcpState: String
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct ProcessItem: Identifiable, Sendable {
|
||||||
|
public var id: Int32 { pid }
|
||||||
|
public let pid: Int32
|
||||||
|
public let ppid: Int32
|
||||||
|
public let name: String
|
||||||
|
public let cpuPercent: Double
|
||||||
|
public let residentSize: UInt64
|
||||||
|
public let virtualSize: UInt64
|
||||||
|
public let threadCount: Int32
|
||||||
|
public let uid: UInt32
|
||||||
|
public let username: String
|
||||||
|
public let isStopped: Bool
|
||||||
|
public let isZombie: Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct GPUCardItem: Identifiable, Sendable {
|
||||||
|
public var id: String { name }
|
||||||
|
public let name: String
|
||||||
|
public let isDiscrete: Bool
|
||||||
|
public let isLowPower: Bool
|
||||||
|
public let vramTotalBytes: UInt64
|
||||||
|
public let vramUsedBytes: UInt64
|
||||||
|
public let utilizationPercent: Double
|
||||||
|
public let temperature: Double
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct BatteryHealthMetrics: Sendable {
|
||||||
|
public var hasBattery: Bool = false
|
||||||
|
public var installed: Bool = false
|
||||||
|
public var healthCondition: String = "Normal"
|
||||||
|
public var healthPercent: Double = 100.0
|
||||||
|
public var cycleCount: Int = 0
|
||||||
|
public var designCycleCount: Int = 1000
|
||||||
|
public var currentCapacity: Int = 0
|
||||||
|
public var maxCapacity: Int = 0
|
||||||
|
public var designCapacity: Int = 0
|
||||||
|
public var temperature: Double = 0
|
||||||
|
public var voltage: Double = 0
|
||||||
|
public var amperage: Double = 0
|
||||||
|
public var watts: Double = 0
|
||||||
|
public var isCharging: Bool = false
|
||||||
|
public var isCharged: Bool = false
|
||||||
|
public var externalConnected: Bool = false
|
||||||
|
public var timeRemainingMinutes: Int = -1
|
||||||
|
public var manufacturer: String = "Apple"
|
||||||
|
public var serial: String = ""
|
||||||
|
public var adapterWatts: Int = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct PeripheralDeviceItem: Identifiable, Sendable {
|
||||||
|
public var id: String { "\(busType)_\(locationID)_\(name)" }
|
||||||
|
public let name: String
|
||||||
|
public let busType: String
|
||||||
|
public let vendorName: String
|
||||||
|
public let vendorID: UInt32
|
||||||
|
public let productID: UInt32
|
||||||
|
public let serialNumber: String
|
||||||
|
public let locationID: UInt64
|
||||||
|
public let isBuiltIn: Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct AudioDeviceItem: Identifiable, Sendable {
|
||||||
|
public var id: UInt32 { deviceID }
|
||||||
|
public let deviceID: UInt32
|
||||||
|
public let name: String
|
||||||
|
public let manufacturer: String
|
||||||
|
public let isInput: Bool
|
||||||
|
public let isOutput: Bool
|
||||||
|
public let isDefaultInput: Bool
|
||||||
|
public let isDefaultOutput: Bool
|
||||||
|
public let sampleRate: Double
|
||||||
|
public let channelCount: UInt32
|
||||||
|
public let volume: Float
|
||||||
|
public let isMuted: Bool
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - SystemTelemetryStore
|
// MARK: - SystemTelemetryStore
|
||||||
|
|
||||||
@Observable
|
@Observable
|
||||||
@@ -95,7 +232,7 @@ public final class SystemTelemetryStore {
|
|||||||
/// Raw snapshots organized by provider identifier
|
/// Raw snapshots organized by provider identifier
|
||||||
public private(set) var latestSnapshot: [String: [String: Any]] = [:]
|
public private(set) var latestSnapshot: [String: [String: Any]] = [:]
|
||||||
|
|
||||||
/// Structured Telemetry Metrics
|
/// Milestone 2 Structured Metrics
|
||||||
public private(set) var cpuLoad = CPULoadMetrics()
|
public private(set) var cpuLoad = CPULoadMetrics()
|
||||||
public private(set) var memory = MemoryMetrics()
|
public private(set) var memory = MemoryMetrics()
|
||||||
public private(set) var storageVolumes: [StorageVolumeItem] = []
|
public private(set) var storageVolumes: [StorageVolumeItem] = []
|
||||||
@@ -104,6 +241,21 @@ public final class SystemTelemetryStore {
|
|||||||
public private(set) var componentTemps: [ComponentThermalItem] = []
|
public private(set) var componentTemps: [ComponentThermalItem] = []
|
||||||
public private(set) var power = PowerMetrics()
|
public private(set) var power = PowerMetrics()
|
||||||
|
|
||||||
|
/// Milestone 3 Structured Metrics
|
||||||
|
public private(set) var kernelCounters = KernelCountersMetrics()
|
||||||
|
public private(set) var systemLoad = SystemLoadMetrics()
|
||||||
|
public private(set) var diskIO: [DiskIOItem] = []
|
||||||
|
public private(set) var networkBandwidth: [NetworkBandwidthItem] = []
|
||||||
|
public private(set) var networkSockets: [SocketItem] = []
|
||||||
|
public private(set) var processes: [ProcessItem] = []
|
||||||
|
public private(set) var gpus: [GPUCardItem] = []
|
||||||
|
public private(set) var batteryHealth = BatteryHealthMetrics()
|
||||||
|
public private(set) var peripherals: [PeripheralDeviceItem] = []
|
||||||
|
public private(set) var audioDevices: [AudioDeviceItem] = []
|
||||||
|
|
||||||
|
/// Process detail inspection helper
|
||||||
|
public let processInspector = MMProcessDetailInspector()
|
||||||
|
|
||||||
/// System identification
|
/// System identification
|
||||||
public let hostModel: String
|
public let hostModel: String
|
||||||
public let osVersion: String
|
public let osVersion: String
|
||||||
@@ -125,7 +277,7 @@ public final class SystemTelemetryStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func registerDefaultProviders() {
|
private func registerDefaultProviders() {
|
||||||
// Register all Primary System & Thermal Telemetry providers (M2)
|
// Milestone 2 Primary System & Thermal Telemetry
|
||||||
coordinator.register(MMCPULoadProvider())
|
coordinator.register(MMCPULoadProvider())
|
||||||
coordinator.register(MMMemoryTelemetryProvider())
|
coordinator.register(MMMemoryTelemetryProvider())
|
||||||
coordinator.register(MMStorageTelemetryProvider())
|
coordinator.register(MMStorageTelemetryProvider())
|
||||||
@@ -133,6 +285,18 @@ public final class SystemTelemetryStore {
|
|||||||
coordinator.register(MMFanTelemetryProvider())
|
coordinator.register(MMFanTelemetryProvider())
|
||||||
coordinator.register(MMComponentThermalProvider())
|
coordinator.register(MMComponentThermalProvider())
|
||||||
coordinator.register(MMPowerTelemetryProvider())
|
coordinator.register(MMPowerTelemetryProvider())
|
||||||
|
|
||||||
|
// Milestone 3 Advanced Kernel, Process & Peripheral Telemetry
|
||||||
|
coordinator.register(MMKernelTelemetryProvider())
|
||||||
|
coordinator.register(MMLoadAverageProvider())
|
||||||
|
coordinator.register(MMDiskIOProvider())
|
||||||
|
coordinator.register(MMNetworkBandwidthProvider())
|
||||||
|
coordinator.register(MMNetworkSocketsProvider())
|
||||||
|
coordinator.register(MMProcessTelemetryProvider())
|
||||||
|
coordinator.register(MMGPUTelemetryProvider())
|
||||||
|
coordinator.register(MMBatteryTelemetryProvider())
|
||||||
|
coordinator.register(MMPeripheralsProvider())
|
||||||
|
coordinator.register(MMAudioTelemetryProvider())
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupCoordinator() {
|
private func setupCoordinator() {
|
||||||
@@ -178,7 +342,7 @@ public final class SystemTelemetryStore {
|
|||||||
self.memory = m
|
self.memory = m
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Storage
|
// 3. Storage Volumes
|
||||||
if let stgDict = snapshot["com.i3omb.macmonitor.telemetry.storage"],
|
if let stgDict = snapshot["com.i3omb.macmonitor.telemetry.storage"],
|
||||||
let volArray = stgDict["volumes"] as? [[String: Any]] {
|
let volArray = stgDict["volumes"] as? [[String: Any]] {
|
||||||
self.storageVolumes = volArray.compactMap { dict in
|
self.storageVolumes = volArray.compactMap { dict in
|
||||||
@@ -269,6 +433,230 @@ public final class SystemTelemetryStore {
|
|||||||
}
|
}
|
||||||
self.power = m
|
self.power = m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 8. Kernel Counters
|
||||||
|
if let kDict = snapshot["com.i3omb.macmonitor.telemetry.kernel"] {
|
||||||
|
var m = KernelCountersMetrics()
|
||||||
|
m.contextSwitchesPerSec = (kDict["contextSwitchesPerSec"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.syscallsPerSec = (kDict["syscallsPerSec"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.pageFaultsPerSec = (kDict["pageFaultsPerSec"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.cowFaultsPerSec = (kDict["cowFaultsPerSec"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.zeroFillFaultsPerSec = (kDict["zeroFillFaultsPerSec"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.pageinsPerSec = (kDict["pageinsPerSec"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.pageoutsPerSec = (kDict["pageoutsPerSec"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.totalContextSwitches = (kDict["totalContextSwitches"] as? NSNumber)?.uint64Value ?? 0
|
||||||
|
m.totalSyscalls = (kDict["totalSyscalls"] as? NSNumber)?.uint64Value ?? 0
|
||||||
|
m.totalPageFaults = (kDict["totalPageFaults"] as? NSNumber)?.uint64Value ?? 0
|
||||||
|
self.kernelCounters = m
|
||||||
|
}
|
||||||
|
|
||||||
|
// 9. System Load Average & Mach Factor
|
||||||
|
if let loadDict = snapshot["com.i3omb.macmonitor.telemetry.systemload"] {
|
||||||
|
var m = SystemLoadMetrics()
|
||||||
|
m.load1Min = (loadDict["load1Min"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.load5Min = (loadDict["load5Min"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.load15Min = (loadDict["load15Min"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.taskCount = (loadDict["taskCount"] as? NSNumber)?.intValue ?? 0
|
||||||
|
m.threadCount = (loadDict["threadCount"] as? NSNumber)?.intValue ?? 0
|
||||||
|
m.machFactor = (loadDict["machFactor"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
self.systemLoad = m
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10. Disk I/O & IOPS
|
||||||
|
if let diskDict = snapshot["com.i3omb.macmonitor.telemetry.diskio"],
|
||||||
|
let disks = diskDict["disks"] as? [[String: Any]] {
|
||||||
|
self.diskIO = disks.compactMap { d in
|
||||||
|
guard let bsd = d["bsdName"] as? String else { return nil }
|
||||||
|
return DiskIOItem(
|
||||||
|
bsdName: bsd,
|
||||||
|
readBps: (d["readBps"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
writeBps: (d["writeBps"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
readIOPS: (d["readIOPS"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
writeIOPS: (d["writeIOPS"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
totalBytesRead: (d["totalBytesRead"] as? NSNumber)?.uint64Value ?? 0,
|
||||||
|
totalBytesWritten: (d["totalBytesWritten"] as? NSNumber)?.uint64Value ?? 0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 11. Network Bandwidth
|
||||||
|
if let netDict = snapshot["com.i3omb.macmonitor.telemetry.network.bandwidth"],
|
||||||
|
let ifaces = netDict["interfaces"] as? [[String: Any]] {
|
||||||
|
self.networkBandwidth = ifaces.compactMap { d in
|
||||||
|
guard let name = d["interfaceName"] as? String else { return nil }
|
||||||
|
return NetworkBandwidthItem(
|
||||||
|
interfaceName: name,
|
||||||
|
ipAddress: d["ipAddress"] as? String ?? "",
|
||||||
|
isUp: (d["isUp"] as? NSNumber)?.boolValue ?? false,
|
||||||
|
downloadBps: (d["downloadBps"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
uploadBps: (d["uploadBps"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
downloadPps: (d["downloadPps"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
uploadPps: (d["uploadPps"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
totalBytesIn: (d["totalBytesIn"] as? NSNumber)?.uint64Value ?? 0,
|
||||||
|
totalBytesOut: (d["totalBytesOut"] as? NSNumber)?.uint64Value ?? 0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 12. Network Sockets
|
||||||
|
if let sockDict = snapshot["com.i3omb.macmonitor.telemetry.network.sockets"],
|
||||||
|
let socks = sockDict["sockets"] as? [[String: Any]] {
|
||||||
|
self.networkSockets = socks.compactMap { d in
|
||||||
|
let pid = (d["pid"] as? NSNumber)?.int32Value ?? 0
|
||||||
|
let fd = (d["fd"] as? NSNumber)?.int32Value ?? 0
|
||||||
|
return SocketItem(
|
||||||
|
pid: pid,
|
||||||
|
fd: fd,
|
||||||
|
processName: d["processName"] as? String ?? "",
|
||||||
|
protocolName: d["protocol"] as? String ?? "TCP",
|
||||||
|
localAddress: d["localAddress"] as? String ?? "*",
|
||||||
|
localPort: (d["localPort"] as? NSNumber)?.uint16Value ?? 0,
|
||||||
|
remoteAddress: d["remoteAddress"] as? String ?? "*",
|
||||||
|
remotePort: (d["remotePort"] as? NSNumber)?.uint16Value ?? 0,
|
||||||
|
tcpState: d["tcpState"] as? String ?? "ESTABLISHED"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 13. Processes
|
||||||
|
if let procDict = snapshot["com.i3omb.macmonitor.telemetry.process"],
|
||||||
|
let procs = procDict["processes"] as? [[String: Any]] {
|
||||||
|
self.processes = procs.compactMap { d in
|
||||||
|
guard let pid = (d["pid"] as? NSNumber)?.int32Value else { return nil }
|
||||||
|
return ProcessItem(
|
||||||
|
pid: pid,
|
||||||
|
ppid: (d["ppid"] as? NSNumber)?.int32Value ?? 0,
|
||||||
|
name: d["name"] as? String ?? "Unknown",
|
||||||
|
cpuPercent: (d["cpuPercent"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
residentSize: (d["residentSize"] as? NSNumber)?.uint64Value ?? 0,
|
||||||
|
virtualSize: (d["virtualSize"] as? NSNumber)?.uint64Value ?? 0,
|
||||||
|
threadCount: (d["threadCount"] as? NSNumber)?.int32Value ?? 1,
|
||||||
|
uid: (d["uid"] as? NSNumber)?.uint32Value ?? 0,
|
||||||
|
username: d["username"] as? String ?? "",
|
||||||
|
isStopped: (d["isStopped"] as? NSNumber)?.boolValue ?? false,
|
||||||
|
isZombie: (d["isZombie"] as? NSNumber)?.boolValue ?? false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 14. GPU Telemetry
|
||||||
|
if let gpuDict = snapshot["com.i3omb.macmonitor.telemetry.gpu"],
|
||||||
|
let cards = gpuDict["gpus"] as? [[String: Any]] {
|
||||||
|
self.gpus = cards.compactMap { d in
|
||||||
|
guard let name = d["name"] as? String else { return nil }
|
||||||
|
return GPUCardItem(
|
||||||
|
name: name,
|
||||||
|
isDiscrete: (d["isDiscrete"] as? NSNumber)?.boolValue ?? false,
|
||||||
|
isLowPower: (d["isLowPower"] as? NSNumber)?.boolValue ?? false,
|
||||||
|
vramTotalBytes: (d["vramTotalBytes"] as? NSNumber)?.uint64Value ?? 0,
|
||||||
|
vramUsedBytes: (d["vramUsedBytes"] as? NSNumber)?.uint64Value ?? 0,
|
||||||
|
utilizationPercent: (d["utilizationPercent"] as? NSNumber)?.doubleValue ?? 0,
|
||||||
|
temperature: (d["temperature"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 15. Battery Health
|
||||||
|
if let battDict = snapshot["com.i3omb.macmonitor.telemetry.battery"] {
|
||||||
|
var m = BatteryHealthMetrics()
|
||||||
|
m.hasBattery = (battDict["hasBattery"] as? NSNumber)?.boolValue ?? false
|
||||||
|
m.installed = (battDict["installed"] as? NSNumber)?.boolValue ?? false
|
||||||
|
m.healthCondition = battDict["healthCondition"] as? String ?? "Normal"
|
||||||
|
m.healthPercent = (battDict["healthPercent"] as? NSNumber)?.doubleValue ?? 100.0
|
||||||
|
m.cycleCount = (battDict["cycleCount"] as? NSNumber)?.intValue ?? 0
|
||||||
|
m.designCycleCount = (battDict["designCycleCount"] as? NSNumber)?.intValue ?? 1000
|
||||||
|
m.currentCapacity = (battDict["currentCapacity"] as? NSNumber)?.intValue ?? 0
|
||||||
|
m.maxCapacity = (battDict["maxCapacity"] as? NSNumber)?.intValue ?? 0
|
||||||
|
m.designCapacity = (battDict["designCapacity"] as? NSNumber)?.intValue ?? 0
|
||||||
|
m.temperature = (battDict["temperature"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.voltage = (battDict["voltage"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.amperage = (battDict["amperage"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.watts = (battDict["watts"] as? NSNumber)?.doubleValue ?? 0
|
||||||
|
m.isCharging = (battDict["isCharging"] as? NSNumber)?.boolValue ?? false
|
||||||
|
m.isCharged = (battDict["isCharged"] as? NSNumber)?.boolValue ?? false
|
||||||
|
m.externalConnected = (battDict["externalConnected"] as? NSNumber)?.boolValue ?? false
|
||||||
|
m.timeRemainingMinutes = (battDict["timeRemainingMinutes"] as? NSNumber)?.intValue ?? -1
|
||||||
|
m.manufacturer = battDict["manufacturer"] as? String ?? "Apple"
|
||||||
|
m.serial = battDict["serial"] as? String ?? ""
|
||||||
|
m.adapterWatts = (battDict["adapterWatts"] as? NSNumber)?.intValue ?? 0
|
||||||
|
self.batteryHealth = m
|
||||||
|
}
|
||||||
|
|
||||||
|
// 16. Connected Peripherals
|
||||||
|
if let periphDict = snapshot["com.i3omb.macmonitor.telemetry.peripherals"] {
|
||||||
|
var allDevs: [PeripheralDeviceItem] = []
|
||||||
|
if let usb = periphDict["usbDevices"] as? [[String: Any]] {
|
||||||
|
allDevs.append(contentsOf: usb.compactMap { d in
|
||||||
|
guard let name = d["name"] as? String else { return nil }
|
||||||
|
return PeripheralDeviceItem(
|
||||||
|
name: name,
|
||||||
|
busType: "USB",
|
||||||
|
vendorName: d["vendorName"] as? String ?? "",
|
||||||
|
vendorID: (d["vendorID"] as? NSNumber)?.uint32Value ?? 0,
|
||||||
|
productID: (d["productID"] as? NSNumber)?.uint32Value ?? 0,
|
||||||
|
serialNumber: d["serialNumber"] as? String ?? "",
|
||||||
|
locationID: (d["locationID"] as? NSNumber)?.uint64Value ?? 0,
|
||||||
|
isBuiltIn: (d["isBuiltIn"] as? NSNumber)?.boolValue ?? false
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if let tb = periphDict["thunderboltDevices"] as? [[String: Any]] {
|
||||||
|
allDevs.append(contentsOf: tb.compactMap { d in
|
||||||
|
guard let name = d["name"] as? String else { return nil }
|
||||||
|
return PeripheralDeviceItem(
|
||||||
|
name: name,
|
||||||
|
busType: "Thunderbolt",
|
||||||
|
vendorName: d["vendorName"] as? String ?? "",
|
||||||
|
vendorID: (d["vendorID"] as? NSNumber)?.uint32Value ?? 0,
|
||||||
|
productID: (d["productID"] as? NSNumber)?.uint32Value ?? 0,
|
||||||
|
serialNumber: "",
|
||||||
|
locationID: 0,
|
||||||
|
isBuiltIn: false
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if let pci = periphDict["pciDevices"] as? [[String: Any]] {
|
||||||
|
allDevs.append(contentsOf: pci.compactMap { d in
|
||||||
|
guard let name = d["name"] as? String else { return nil }
|
||||||
|
return PeripheralDeviceItem(
|
||||||
|
name: name,
|
||||||
|
busType: "PCI",
|
||||||
|
vendorName: "Apple / Intel",
|
||||||
|
vendorID: (d["vendorID"] as? NSNumber)?.uint32Value ?? 0,
|
||||||
|
productID: (d["productID"] as? NSNumber)?.uint32Value ?? 0,
|
||||||
|
serialNumber: "",
|
||||||
|
locationID: 0,
|
||||||
|
isBuiltIn: true
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
self.peripherals = allDevs
|
||||||
|
}
|
||||||
|
|
||||||
|
// 17. Audio Devices
|
||||||
|
if let audioDict = snapshot["com.i3omb.macmonitor.telemetry.audio"],
|
||||||
|
let devs = audioDict["devices"] as? [[String: Any]] {
|
||||||
|
self.audioDevices = devs.compactMap { d in
|
||||||
|
guard let devID = (d["deviceID"] as? NSNumber)?.uint32Value else { return nil }
|
||||||
|
return AudioDeviceItem(
|
||||||
|
deviceID: devID,
|
||||||
|
name: d["name"] as? String ?? "Audio Device",
|
||||||
|
manufacturer: d["manufacturer"] as? String ?? "Apple Inc.",
|
||||||
|
isInput: (d["isInput"] as? NSNumber)?.boolValue ?? false,
|
||||||
|
isOutput: (d["isOutput"] as? NSNumber)?.boolValue ?? false,
|
||||||
|
isDefaultInput: (d["isDefaultInput"] as? NSNumber)?.boolValue ?? false,
|
||||||
|
isDefaultOutput: (d["isDefaultOutput"] as? NSNumber)?.boolValue ?? false,
|
||||||
|
sampleRate: (d["sampleRate"] as? NSNumber)?.doubleValue ?? 44100.0,
|
||||||
|
channelCount: (d["channelCount"] as? NSNumber)?.uint32Value ?? 2,
|
||||||
|
volume: (d["volume"] as? NSNumber)?.floatValue ?? 0,
|
||||||
|
isMuted: (d["isMuted"] as? NSNumber)?.boolValue ?? false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func terminateProcess(pid: pid_t, force: Bool) -> Bool {
|
||||||
|
return MMProcessTelemetryProvider.terminateProcess(withPID: pid, force: force)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func start() {
|
public func start() {
|
||||||
|
|||||||
@@ -30,5 +30,6 @@
|
|||||||
#import "MMBatteryTelemetryProvider.h"
|
#import "MMBatteryTelemetryProvider.h"
|
||||||
#import "MMPeripheralsProvider.h"
|
#import "MMPeripheralsProvider.h"
|
||||||
#import "MMAudioTelemetryProvider.h"
|
#import "MMAudioTelemetryProvider.h"
|
||||||
|
#import "MMDisplayManager.h"
|
||||||
|
|
||||||
#endif /* MacMonitor_Bridging_Header_h */
|
#endif /* MacMonitor_Bridging_Header_h */
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
#import <CoreGraphics/CoreGraphics.h>
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface MMDisplayInfo : NSObject
|
||||||
|
|
||||||
|
@property (nonatomic, readonly) CGDirectDisplayID displayID;
|
||||||
|
@property (nonatomic, readonly, copy) NSString *name;
|
||||||
|
@property (nonatomic, readonly) BOOL isBuiltin;
|
||||||
|
@property (nonatomic, readonly) BOOL isMain;
|
||||||
|
@property (nonatomic, readonly) BOOL isOnline;
|
||||||
|
@property (nonatomic, readonly) uint32_t width;
|
||||||
|
@property (nonatomic, readonly) uint32_t height;
|
||||||
|
@property (nonatomic, readonly) double refreshRate;
|
||||||
|
@property (nonatomic, readonly) float brightness; // 0.0 to 1.0, or -1.0 if not supported
|
||||||
|
|
||||||
|
- (instancetype)initWithDisplayID:(CGDirectDisplayID)displayID
|
||||||
|
name:(NSString *)name
|
||||||
|
isBuiltin:(BOOL)isBuiltin
|
||||||
|
isMain:(BOOL)isMain
|
||||||
|
isOnline:(BOOL)isOnline
|
||||||
|
width:(uint32_t)width
|
||||||
|
height:(uint32_t)height
|
||||||
|
refreshRate:(double)refreshRate
|
||||||
|
brightness:(float)brightness NS_DESIGNATED_INITIALIZER;
|
||||||
|
|
||||||
|
- (instancetype)init NS_UNAVAILABLE;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface MMDisplayManager : NSObject
|
||||||
|
|
||||||
|
+ (instancetype)sharedManager;
|
||||||
|
|
||||||
|
- (NSArray<MMDisplayInfo *> *)activeDisplays;
|
||||||
|
- (float)brightnessForDisplay:(CGDirectDisplayID)displayID;
|
||||||
|
- (BOOL)setBrightness:(float)brightness forDisplay:(CGDirectDisplayID)displayID;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
||||||
@@ -0,0 +1,168 @@
|
|||||||
|
#import "MMDisplayManager.h"
|
||||||
|
#import <IOKit/graphics/IOGraphicsLib.h>
|
||||||
|
#import <dlfcn.h>
|
||||||
|
|
||||||
|
// Private DisplayServices API signatures
|
||||||
|
typedef int (*DisplayServicesGetBrightnessFunc)(CGDirectDisplayID display, float *brightness);
|
||||||
|
typedef int (*DisplayServicesSetBrightnessFunc)(CGDirectDisplayID display, float brightness);
|
||||||
|
|
||||||
|
@implementation MMDisplayInfo
|
||||||
|
|
||||||
|
- (instancetype)initWithDisplayID:(CGDirectDisplayID)displayID
|
||||||
|
name:(NSString *)name
|
||||||
|
isBuiltin:(BOOL)isBuiltin
|
||||||
|
isMain:(BOOL)isMain
|
||||||
|
isOnline:(BOOL)isOnline
|
||||||
|
width:(uint32_t)width
|
||||||
|
height:(uint32_t)height
|
||||||
|
refreshRate:(double)refreshRate
|
||||||
|
brightness:(float)brightness {
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
_displayID = displayID;
|
||||||
|
_name = [name copy] ?: @"Unknown Display";
|
||||||
|
_isBuiltin = isBuiltin;
|
||||||
|
_isMain = isMain;
|
||||||
|
_isOnline = isOnline;
|
||||||
|
_width = width;
|
||||||
|
_height = height;
|
||||||
|
_refreshRate = refreshRate;
|
||||||
|
_brightness = brightness;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation MMDisplayManager {
|
||||||
|
void *_displayServicesHandle;
|
||||||
|
DisplayServicesGetBrightnessFunc _getBrightnessFunc;
|
||||||
|
DisplayServicesSetBrightnessFunc _setBrightnessFunc;
|
||||||
|
}
|
||||||
|
|
||||||
|
+ (instancetype)sharedManager {
|
||||||
|
static MMDisplayManager *sharedInstance = nil;
|
||||||
|
static dispatch_once_t onceToken;
|
||||||
|
dispatch_once(&onceToken, ^{
|
||||||
|
sharedInstance = [[self alloc] init];
|
||||||
|
});
|
||||||
|
return sharedInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)init {
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
[self loadDisplayServices];
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)dealloc {
|
||||||
|
if (_displayServicesHandle) {
|
||||||
|
dlclose(_displayServicesHandle);
|
||||||
|
_displayServicesHandle = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)loadDisplayServices {
|
||||||
|
_displayServicesHandle = dlopen("/System/Library/PrivateFrameworks/DisplayServices.framework/DisplayServices", RTLD_LAZY);
|
||||||
|
if (_displayServicesHandle) {
|
||||||
|
_getBrightnessFunc = (DisplayServicesGetBrightnessFunc)dlsym(_displayServicesHandle, "DisplayServicesGetBrightness");
|
||||||
|
_setBrightnessFunc = (DisplayServicesSetBrightnessFunc)dlsym(_displayServicesHandle, "DisplayServicesSetBrightness");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSArray<MMDisplayInfo *> *)activeDisplays {
|
||||||
|
uint32_t maxDisplays = 16;
|
||||||
|
CGDirectDisplayID onlineDisplays[16];
|
||||||
|
uint32_t displayCount = 0;
|
||||||
|
|
||||||
|
CGError err = CGGetOnlineDisplayList(maxDisplays, onlineDisplays, &displayCount);
|
||||||
|
if (err != kCGErrorSuccess || displayCount == 0) {
|
||||||
|
return @[];
|
||||||
|
}
|
||||||
|
|
||||||
|
NSMutableArray<MMDisplayInfo *> *result = [NSMutableArray arrayWithCapacity:displayCount];
|
||||||
|
CGDirectDisplayID mainDisplay = CGMainDisplayID();
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < displayCount; i++) {
|
||||||
|
CGDirectDisplayID dID = onlineDisplays[i];
|
||||||
|
BOOL isBuiltin = CGDisplayIsBuiltin(dID);
|
||||||
|
BOOL isMain = (dID == mainDisplay);
|
||||||
|
BOOL isOnline = CGDisplayIsOnline(dID);
|
||||||
|
|
||||||
|
uint32_t width = (uint32_t)CGDisplayPixelsWide(dID);
|
||||||
|
uint32_t height = (uint32_t)CGDisplayPixelsHigh(dID);
|
||||||
|
|
||||||
|
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(dID);
|
||||||
|
double refreshRate = 0.0;
|
||||||
|
if (mode) {
|
||||||
|
refreshRate = CGDisplayModeGetRefreshRate(mode);
|
||||||
|
CGDisplayModeRelease(mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
NSString *displayName = isBuiltin ? @"Built-in Retina Display" : [NSString stringWithFormat:@"External Display (%u)", (unsigned int)dID];
|
||||||
|
float brightness = [self brightnessForDisplay:dID];
|
||||||
|
|
||||||
|
MMDisplayInfo *info = [[MMDisplayInfo alloc] initWithDisplayID:dID
|
||||||
|
name:displayName
|
||||||
|
isBuiltin:isBuiltin
|
||||||
|
isMain:isMain
|
||||||
|
isOnline:isOnline
|
||||||
|
width:width
|
||||||
|
height:height
|
||||||
|
refreshRate:refreshRate
|
||||||
|
brightness:brightness];
|
||||||
|
[result addObject:info];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [result copy];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (float)brightnessForDisplay:(CGDirectDisplayID)displayID {
|
||||||
|
if (_getBrightnessFunc) {
|
||||||
|
float b = 0.0f;
|
||||||
|
int status = _getBrightnessFunc(displayID, &b);
|
||||||
|
if (status == 0) {
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback using IOKit
|
||||||
|
io_service_t service = CGDisplayIOServicePort(displayID);
|
||||||
|
if (service != MACH_PORT_NULL) {
|
||||||
|
float brightness = 0.0f;
|
||||||
|
CFStringRef key = CFSTR(kIODisplayBrightnessKey);
|
||||||
|
kern_return_t kr = IODisplayGetFloatParameter(service, kNilOptions, key, &brightness);
|
||||||
|
if (kr == kIOReturnSuccess) {
|
||||||
|
return brightness;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)setBrightness:(float)brightness forDisplay:(CGDirectDisplayID)displayID {
|
||||||
|
if (brightness < 0.0f) brightness = 0.0f;
|
||||||
|
if (brightness > 1.0f) brightness = 1.0f;
|
||||||
|
|
||||||
|
if (_setBrightnessFunc) {
|
||||||
|
int status = _setBrightnessFunc(displayID, brightness);
|
||||||
|
if (status == 0) {
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
io_service_t service = CGDisplayIOServicePort(displayID);
|
||||||
|
if (service != MACH_PORT_NULL) {
|
||||||
|
CFStringRef key = CFSTR(kIODisplayBrightnessKey);
|
||||||
|
kern_return_t kr = IODisplaySetFloatParameter(service, kNilOptions, key, brightness);
|
||||||
|
if (kr == kIOReturnSuccess) {
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
+647
-191
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,46 @@
|
|||||||
|
import XCTest
|
||||||
|
@testable import MacMonitor
|
||||||
|
|
||||||
|
final class MMDisplayTests: XCTestCase {
|
||||||
|
|
||||||
|
func testDisplayManagerActiveDisplays() {
|
||||||
|
let manager = MMDisplayManager.shared()
|
||||||
|
XCTAssertNotNil(manager, "MMDisplayManager instance should not be nil")
|
||||||
|
|
||||||
|
let displays = manager.activeDisplays()
|
||||||
|
XCTAssertNotNil(displays, "Active displays array should not be nil")
|
||||||
|
// In CI or headless/headless VM or real Mac, online display list can be checked
|
||||||
|
for display in displays {
|
||||||
|
XCTAssertGreaterThan(display.displayID, 0)
|
||||||
|
XCTAssertFalse(display.name.isEmpty)
|
||||||
|
if display.isOnline {
|
||||||
|
XCTAssertGreaterThan(display.width, 0)
|
||||||
|
XCTAssertGreaterThan(display.height, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDisplayInfoInitialization() {
|
||||||
|
let info = MMDisplayInfo(
|
||||||
|
displayID: 1001,
|
||||||
|
name: "Test Display",
|
||||||
|
isBuiltin: true,
|
||||||
|
isMain: true,
|
||||||
|
isOnline: true,
|
||||||
|
width: 2560,
|
||||||
|
height: 1600,
|
||||||
|
refreshRate: 60.0,
|
||||||
|
brightness: 0.75
|
||||||
|
)
|
||||||
|
|
||||||
|
XCTAssertEqual(info.displayID, 1001)
|
||||||
|
XCTAssertEqual(info.name, "Test Display")
|
||||||
|
XCTAssertTrue(info.isBuiltin)
|
||||||
|
XCTAssertTrue(info.isMain)
|
||||||
|
XCTAssertTrue(info.isOnline)
|
||||||
|
XCTAssertEqual(info.width, 2560)
|
||||||
|
XCTAssertEqual(info.height, 1600)
|
||||||
|
XCTAssertEqual(info.refreshRate, 60.0)
|
||||||
|
XCTAssertEqual(info.brightness, 0.75, accuracy: 0.001)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,7 +28,7 @@ final class MacMonitorCoreTests: XCTestCase {
|
|||||||
func testSystemTelemetryStoreInitialStateAndProviders() {
|
func testSystemTelemetryStoreInitialStateAndProviders() {
|
||||||
let store = SystemTelemetryStore.shared
|
let store = SystemTelemetryStore.shared
|
||||||
XCTAssertNotNil(store)
|
XCTAssertNotNil(store)
|
||||||
XCTAssertEqual(store.registeredProviderCount, 7)
|
XCTAssertEqual(store.registeredProviderCount, 17)
|
||||||
XCTAssertFalse(store.hostModel.isEmpty)
|
XCTAssertFalse(store.hostModel.isEmpty)
|
||||||
XCTAssertFalse(store.kernelVersion.isEmpty)
|
XCTAssertFalse(store.kernelVersion.isEmpty)
|
||||||
XCTAssertGreaterThanOrEqual(store.physicalCpuCount, 1)
|
XCTAssertGreaterThanOrEqual(store.physicalCpuCount, 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user