Skip to content

Instantly share code, notes, and snippets.

@anandharaj-dotworld
Created January 28, 2023 08:40
Show Gist options
  • Save anandharaj-dotworld/faaab24f1c79844e4c5878e4e783f3ed to your computer and use it in GitHub Desktop.
Save anandharaj-dotworld/faaab24f1c79844e4c5878e4e783f3ed to your computer and use it in GitHub Desktop.
Get Battery Information in darwin
package main
import (
"encoding/json"
"fmt"
"os/exec"
plist "howett.net/plist"
)
type PowerTelemetry struct {
AccumulatedAdapterEfficiencyLoss int `json:"accumulated_adapter_efficiency_loss"`
AccumulatedSystemEnergyConsumed int `json:"accumulated_system_energy_consumed"`
AccumulatedSystemLoad int `json:"accumulated_system_load"`
AccumulatedSystemPowerIn int `json:"accumulated_system_power_in"`
AccumulatedWallEnergyEstimate int `json:"accumulated_wall_energy_estimate"`
AdapterEfficiencyLoss int `json:"adapter_efficiency_loss"`
AdapterEfficiencyLossAccumulatorCount int `json:"adapter_efficiency_loss_accumulator_count"`
SystemEnergyConsumed int `json:"system_energy_consumed"`
SystemLoad int `json:"system_load"`
SystemLoadAccumulatorCount int `json:"system_load_accumulator_count"`
SystemPowerIn int `json:"system_power_in"`
SystemPowerInAccumulatorCount int `json:"system_power_in_accumulator_count"`
WallEnergyEstimate int `json:"wall_energy_estimate"`
}
type battery struct {
AdapterInfo int `plist:"AdapterInfo" json:"adapter"`
Amperage int `plist:"Amperage" json:"amperage"`
BatteryInstalled bool `plist:"BatteryInstalled" json:"battery_installed"`
BatteryInvalidWakeSeconds int `plist:"BatteryInvalidWakeSeconds" json:"battery_invalid_wake_seconds"`
BootPathUpdated int `plist:"BootPathUpdated" json:"boot_bath_updated"`
CurrentCapacity int `plist:"CurrentCapacity" json:"current_capacity"`
CycleCount int `plist:"CycleCount" json:"cycle_capacity"`
ExternalChargeCapable bool `plist:"ExternalChargeCapable" json:"external_charge_capable"`
ExternalConnected bool `plist:"ExternalConnected" json:"external_connected"`
FullPathUpdated int `plist:"FullPathUpdated" json:"full_path_updated"`
IOGeneralInterest string `plist:"IOGeneralInterest" json:"io_general_interest"`
IOObjectClass string `plist:"IOObjectClass" json:"io_object_class"`
IOObjectRetainCount int `plist:"IOObjectRetainCount" json:"io_object_retain_count"`
IORegistryEntryID int `plist:"IORegistryEntryID" json:"io_registry_entry_id"`
IORegistryEntryName string `plist:"IORegistryEntryName" json:"io_registry_entry_name"`
IOReportLegend interface{} `plist:"IOReportLegend" json:"io_report_legend"`
IOReportLegendPublic bool `plist:"IOReportLegendPublic" json:"io_report_legend_public"`
IOServiceBusyState int `plist:"IOServiceBusyState" json:"io_service_busy_state"`
IOServiceBusyTime int `plist:"IOServiceBusyTime" json:"io_service_busy_time"`
IOServiceState int `plist:"IOServiceState" json:"io_service_state"`
IsCharging bool `plist:"IsCharging" json:"is_charging"`
Location int `plist:"Location" json:"location"`
MaxCapacity int `plist:"MaxCapacity" json:"max_capacity"`
PostChargeWaitSeconds int `json:"post_charge_wait_seconds"`
PostDischargeWaitSeconds int `json:"post_discharge_wait_seconds"`
PowerTelemetryData interface{} `json:"power_telemetry_data"`
TimeRemaining int `json:"time_remaining"`
UpdateTime int `json:"update_time"`
Voltage int `json:"voltage"`
BuiltIn bool `plist:"built-in"`
}
func readBatteries() ([]*battery, error) {
out, err := exec.Command("ioreg", "-n", "AppleSmartBattery", "-r", "-a").Output()
if err != nil {
return nil, err
}
if len(out) == 0 {
// No batteries.
return nil, nil
}
var data []*battery
if _, err = plist.Unmarshal(out, &data); err != nil {
return nil, err
}
return data, nil
}
func main() {
batt, err := readBatteries()
if err == nil {
for _, b := range batt {
mar, _ := json.Marshal(b)
fmt.Println(string(mar))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment