feat: init pc-monitor project
- Client: Go-based Windows hardware monitoring (CPU, GPU, memory, disk, network, power) - Server: Go + Gin + SQLite backend with REST API - Frontend: Vue 3 + Element Plus dashboard - Docker deployment support - Windows service installation script
This commit is contained in:
47
client/collector/power.go
Normal file
47
client/collector/power.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
)
|
||||
|
||||
type PowerInfo struct {
|
||||
Status string `json:"status"`
|
||||
BatteryLevel int `json:"battery_level"`
|
||||
PowerSource string `json:"power_source"`
|
||||
}
|
||||
|
||||
func CollectPower() (*PowerInfo, error) {
|
||||
info := &PowerInfo{
|
||||
Status: "no_battery",
|
||||
BatteryLevel: 0,
|
||||
PowerSource: "ac",
|
||||
}
|
||||
|
||||
// Try to get battery info
|
||||
battery, err := host.SensorsBattery()
|
||||
if err != nil {
|
||||
return info, nil
|
||||
}
|
||||
|
||||
if battery == nil {
|
||||
return info, nil
|
||||
}
|
||||
|
||||
info.BatteryLevel = int(battery.Percent)
|
||||
|
||||
if battery.PowerSupply {
|
||||
info.PowerSource = "ac"
|
||||
} else {
|
||||
info.PowerSource = "battery"
|
||||
}
|
||||
|
||||
if battery.Charging {
|
||||
info.Status = "charging"
|
||||
} else if battery.Percent >= 100 {
|
||||
info.Status = "full"
|
||||
} else {
|
||||
info.Status = "discharging"
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
Reference in New Issue
Block a user