forked from dolfly/termux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batteryStatus.go
41 lines (34 loc) · 999 Bytes
/
batteryStatus.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
package termux
import (
"bytes"
"encoding/json"
"fmt"
)
// BatteryStatusResponse represents the current battery status
type BatteryStatusResponse struct {
Health string `json:"health"`
Percentage int `json:"percentage"`
Plugged string `json:"plugged"`
Status string `json:"status"`
Temperature float64 `json:"temperature"`
}
func (b BatteryStatusResponse) String() string {
return fmt.Sprintf("Health: %v\nPercentage: %v\nPlugged: %v\nStatus: %v\nTemperature: %.2f",
b.Health, b.Percentage, b.Plugged, b.Status, b.Temperature)
}
// BatteryStatus acquires the current audio info
func BatteryStatus() (*BatteryStatusResponse, error) {
buf := bytes.NewBuffer([]byte{})
if err := exec(nil, buf, "BatteryStatus", nil, ""); err != nil {
return nil, err
}
res := buf.Bytes()
if err := checkErr(res); err != nil {
return nil, err
}
ret := new(BatteryStatusResponse)
if err := json.Unmarshal(res, ret); err != nil {
return nil, err
}
return ret, nil
}