-
Notifications
You must be signed in to change notification settings - Fork 3
/
battery.go
51 lines (42 loc) · 945 Bytes
/
battery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @Author Rafal Golarz
* http://rafalgolarz.com/blog/2017/01/15/running_golang_on_android/
*/
package main
import (
"encoding/json"
"fmt"
"log"
"os/exec"
)
/*
* Termux API: termux-battery-status
*/
var Battery struct {
Health string
Percentage int
Status string
Temperature float64
}
func main() {
cmd := exec.Command("termux-battery-status")
//StdoutPipe returns a pipe that will be connected to
//the command's standard output when the command starts
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
if err := json.NewDecoder(stdout).Decode(&Battery); err != nil {
log.Fatal(err)
}
//Wait waits for the command to exit
//It must have been started by Start
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
fmt.Printf("The battery is %s and is charged in %d percentage\n",
Battery.Health, Battery.Percentage)
}