-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
battery.go
174 lines (158 loc) · 5.12 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// battery
// Copyright (C) 2016-2017,2023 Karol 'Kenji Takahashi' Woźniak
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Package battery provides cross-platform, normalized battery information.
//
// Gives access to a system independent, typed battery state, capacity, charge and voltage values recalculated as necessary to be returned in mW, mWh or V units.
//
// Currently supported systems:
//
// Linux 2.6.39+
// OS X 10.10+
// Windows XP+
// FreeBSD
// DragonFlyBSD
// NetBSD
// OpenBSD
// Solaris
package battery
import (
"fmt"
)
// AgnosticState type enumerates possible battery states, using platform agnostic naming.
type AgnosticState int8
const (
// Undefined specifies a state that was returned by the controller, but there is no
// platform agnostic mapping for it.
// This generally shouldn't happen, if it does consider opening a report for the library
// (ideally with the contents of `State.Explain()` call as well).
Undefined AgnosticState = -1
// Unknown specifies a literal unknown state returned by the controller.
// This state is also considered the "default", therefore it will be set when an Error
// was returned from the Get/GetAll call.
Unknown AgnosticState = iota - 1
Empty
Full
Charging
Discharging
// Idle specifies a state where battery is in "capacity saving" mode.
// It usually means that it sits idle at around 80% charge while power source is plugged in.
Idle
)
var states = map[AgnosticState]string{
Undefined: "Undefined",
Unknown: "Unknown",
Empty: "Empty",
Full: "Full",
Charging: "Charging",
Discharging: "Discharging",
Idle: "Idle",
}
func (s AgnosticState) String() string {
return states[s]
}
type State struct {
Raw AgnosticState
specific string
}
func (s State) Explain() string {
return s.specific
}
func (s State) String() string {
return s.Raw.String()
}
func (s State) GoString() string {
return fmt.Sprintf("%s (%s)", s.Raw, s.specific)
}
// Battery type represents a single battery entry information.
type Battery struct {
// Current battery state.
State State
// Current (momentary) capacity (in mWh).
Current float64
// Last known full capacity (in mWh).
Full float64
// Reported design capacity (in mWh).
Design float64
// Current (momentary) charge rate (in mW).
// It is always non-negative, consult .State field to check
// whether it means charging or discharging.
ChargeRate float64
// Current voltage (in V).
Voltage float64
// Design voltage (in V).
// Some systems (e.g. macOS) do not provide a separate
// value for this. In such cases, or if getting this fails,
// but getting `Voltage` succeeds, this field will have
// the same value as `Voltage`, for convenience.
DesignVoltage float64
}
func (b *Battery) String() string {
return fmt.Sprintf("%+v", *b)
}
func get(sg func(idx int) (*Battery, error), idx int) (*Battery, error) {
b, err := sg(idx)
return b, wrapError(err)
}
// Get returns battery information for given index.
//
// Note that index taken here is normalized, such that GetAll()[idx] == Get(idx).
// It does not necessarily represent the "name" or "position" a battery was given
// by the underlying system.
//
// If error != nil, it will be either ErrFatal or ErrPartial.
func Get(idx int) (*Battery, error) {
return get(systemGet, idx)
}
func getAll(sg func() ([]*Battery, error)) ([]*Battery, error) {
bs, err := sg()
if errors, ok := err.(Errors); ok {
nils := 0
partials := 0
for i, err := range errors {
err = wrapError(err)
if err == nil {
nils++
}
if _, ok := err.(ErrPartial); ok {
partials++
}
errors[i] = err
}
if nils == len(errors) {
return bs, nil
}
if nils > 0 || partials > 0 {
return bs, errors
}
return nil, ErrFatal{ErrAllNotNil}
}
if err != nil {
return bs, ErrFatal{err}
}
return bs, nil
}
// GetAll returns information about all batteries in the system.
//
// If error != nil, it will be either ErrFatal or Errors.
// If error is of type Errors, it is guaranteed that length of both returned slices is the same and that i-th error coresponds with i-th battery structure.
func GetAll() ([]*Battery, error) {
return getAll(systemGetAll)
}