Created
March 27, 2013 12:57
-
-
Save suapapa/5253976 to your computer and use it in GitHub Desktop.
Check battery status to notify unplug.
Tested on my notebook, Dell mini 12.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
) | |
const ( | |
PATH_B_STAT = "/sys/class/power_supply/BAT1/status" // Full or Discharging | |
PATH_E_NOW = "/sys/class/power_supply/BAT1/energy_now" | |
PATH_E_FULL = "/sys/class/power_supply/BAT1/energy_full" | |
) | |
func readIntFromFile(path string) (n int) { | |
f, err := os.Open(path) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if _, err := fmt.Fscanf(f, "%d", &n); err != nil { | |
log.Fatal(err) | |
} | |
return | |
} | |
func main() { | |
full := readIntFromFile(PATH_E_FULL) | |
curr := readIntFromFile(PATH_E_NOW) | |
p := curr * 100 / full | |
fmt.Println(p) | |
f, err := os.Open(PATH_B_STAT) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var stat string | |
if _, err := fmt.Fscanln(f, &stat); err != nil { | |
log.Fatal(err) | |
} | |
if stat == "Discharging" { | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment