-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathsource.rs
332 lines (298 loc) · 12.2 KB
/
source.rs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use std::f32;
use std::io;
use std::path::Path;
use lazycell::LazyCell;
use num_traits::identities::Zero;
use super::fs;
use crate::units::power::{microwatt, watt};
use crate::units::{Bound, ElectricCharge, ElectricPotential, Energy, Power, Ratio, ThermodynamicTemperature};
use crate::{Error, Result, State, Technology};
#[derive(Debug)]
pub struct InstantData {
pub state_of_health: Ratio,
pub state_of_charge: Ratio,
pub energy: Energy,
pub energy_full: Energy,
pub energy_full_design: Energy,
pub energy_rate: Power,
pub voltage: ElectricPotential,
pub state: State,
pub temperature: Option<ThermodynamicTemperature>,
pub cycle_count: Option<u32>,
}
pub struct DataBuilder<'p> {
root: &'p Path,
design_voltage: LazyCell<ElectricPotential>,
energy: LazyCell<Energy>,
energy_full: LazyCell<Energy>,
energy_full_design: LazyCell<Energy>,
energy_rate: LazyCell<Power>,
state_of_health: LazyCell<Ratio>,
state_of_charge: LazyCell<Ratio>,
state: LazyCell<State>,
}
impl<'p> DataBuilder<'p> {
pub fn new(path: &'p Path) -> DataBuilder<'p> {
DataBuilder {
root: path,
design_voltage: LazyCell::new(),
energy: LazyCell::new(),
energy_full: LazyCell::new(),
energy_full_design: LazyCell::new(),
energy_rate: LazyCell::new(),
state_of_health: LazyCell::new(),
state_of_charge: LazyCell::new(),
state: LazyCell::new(),
}
}
pub fn collect(self) -> Result<InstantData> {
Ok(InstantData {
state_of_charge: *self.state_of_charge()?,
state_of_health: *self.state_of_health()?,
energy: *self.energy()?,
energy_full: *self.energy_full()?,
energy_full_design: *self.energy_full_design()?,
energy_rate: *self.energy_rate()?,
voltage: self.voltage()?,
state: *self.state()?,
temperature: self.temperature()?,
cycle_count: self.cycle_count()?,
})
}
fn design_voltage(&self) -> Result<&ElectricPotential> {
self.design_voltage.try_borrow_with(|| {
let value = [
"voltage_max_design",
"voltage_min_design",
"voltage_present",
"voltage_now",
]
.iter()
.filter_map(|filename| match fs::voltage(self.root.join(filename)) {
Ok(Some(value)) => Some(value),
_ => None,
})
.next();
match value {
Some(voltage) => Ok(voltage),
None => Err(io::Error::from(io::ErrorKind::NotFound).into()),
}
})
}
// Not cached because used only once
// IO errors are ignored, since later calculations will handle `None` result
fn energy_now(&self) -> Option<Energy> {
["energy_now", "energy_avg"]
.iter()
.filter_map(|filename| match fs::energy(self.root.join(filename)) {
Ok(Some(value)) => Some(value),
_ => None,
})
.next()
}
// Not cached because used only once.
// IO errors are ignored, since later calculations will handle `None` result
fn charge_now(&self) -> Option<ElectricCharge> {
["charge_now", "charge_avg"]
.iter()
.filter_map(|filename| match fs::charge(self.root.join(filename)) {
Ok(Some(value)) => Some(value),
_ => None,
})
.next()
}
// Not cached because used only once
fn charge_full(&self) -> ElectricCharge {
["charge_full", "charge_full_design"]
.iter()
.filter_map(|filename| match fs::charge(self.root.join(filename)) {
Ok(Some(value)) => Some(value),
_ => None,
})
.next()
.unwrap_or_else(|| microampere_hour!(0.0))
}
pub fn state_of_health(&self) -> Result<&Ratio> {
self.state_of_health.try_borrow_with(|| {
let energy_full = self.energy_full()?;
if !energy_full.is_zero() {
let energy_full_design = self.energy_full_design()?;
Ok((*energy_full / *energy_full_design).into_bounded())
} else {
Ok(percent!(100.0))
}
})
}
fn energy(&self) -> Result<&Energy> {
self.energy.try_borrow_with(|| match self.energy_now() {
Some(energy) => Ok(energy),
None => match self.charge_now() {
Some(charge) => Ok(charge * *self.design_voltage()?),
None => match fs::get::<f32, _>(self.root.join("capacity")) {
Ok(Some(capacity)) => Ok(*self.energy_full()? * percent!(capacity).into_bounded()),
_ => Err(Error::not_found("Unable to calculate device energy value")),
},
},
})
}
fn energy_full(&self) -> Result<&Energy> {
self.energy_full
.try_borrow_with(|| match fs::energy(self.root.join("energy_full")) {
Ok(Some(value)) => Ok(value),
Ok(None) => match fs::charge(self.root.join("charge_full")) {
Ok(Some(value)) => Ok(value * *self.design_voltage()?),
Ok(None) => Ok(*self.energy_full_design()?),
Err(e) => Err(e),
},
Err(e) => Err(e),
})
}
fn energy_full_design(&self) -> Result<&Energy> {
self.energy_full_design.try_borrow_with(|| {
match fs::energy(self.root.join("energy_full_design")) {
Ok(Some(value)) => Ok(value),
Ok(None) => match fs::charge(self.root.join("charge_full_design")) {
Ok(Some(value)) => Ok(value * *self.design_voltage()?),
// It is possible that both `energy_full_design` and `charge_full_design`
// files might be missing, see #40.
// As a workaround, doing the same what `upower` does - falling back to zero value
// It will affect other parameters calculation,
// and in a future versions this function probably should return
// `Result<Option<Energy>>` instead to mark missing value.
Ok(None) => Ok(microwatt_hour!(0.0)),
Err(e) => Err(e),
},
Err(e) => Err(e),
}
})
}
fn energy_rate(&self) -> Result<&Power> {
self.energy_rate.try_borrow_with(|| {
let value = match fs::power(self.root.join("power_now"))? {
Some(power) => Some(power),
None => {
match fs::get::<f32, _>(self.root.join("current_now"))? {
Some(current_now) => {
// If charge_full exists, then current_now is always reported in µA.
// In the legacy case, where energy only units exist, and power_now isn't present
// current_now is power in µW.
// Source: upower
if !self.charge_full().is_zero() {
// µA then
Some(microampere!(current_now) * *self.design_voltage()?)
} else {
// µW :|
Some(microwatt!(current_now))
}
}
None => None,
}
}
};
let value = value
// Sanity check if power is greater than 100W (upower)
.map(|power| if power.get::<watt>() > 100.0 { watt!(0.0) } else { power })
// Some batteries give out massive rate values when nearly empty (upower)
.map(|power| {
if power.get::<microwatt>() < 10.0 {
watt!(0.0)
} else {
power
}
})
// ACPI gives out the special 'Ones' (Constant Ones Object) value for rate
// when it's unable to calculate the true rate. We should set the rate zero,
// and wait for the BIOS to stabilise.
// Source: upower
//
// It come as an `0xffff` originally, but we are operating with `Power` now,
// so this `Ones` value is recalculated a little.
.map(|power| {
// TODO: There might be a chance that we had lost a precision during the conversion
// from the microwatts into default watts, so this should be fixed
if (power.get::<watt>() - 65535.0).abs() < f32::EPSILON {
watt!(0.0)
} else {
power
}
})
.unwrap_or_else(|| microwatt!(0.0));
// TODO: Calculate energy_rate manually, if hardware fails.
// if value < 0.01 {
// // Check upower `up_device_supply_calculate_rate` function
// }
Ok(value)
})
}
fn state_of_charge(&self) -> Result<&Ratio> {
self.state_of_charge.try_borrow_with(|| {
match fs::get::<f32, _>(self.root.join("capacity")) {
Ok(Some(capacity)) => Ok(percent!(capacity).into_bounded()),
Ok(None) if self.energy_full()?.is_sign_positive() => Ok(*self.energy()? / *self.energy_full()?),
// Same as upower, falling back to 0.0%
Ok(None) => Ok(percent!(0.0)),
Err(e) => Err(e),
}
})
}
fn state(&self) -> Result<&State> {
self.state
.try_borrow_with(|| match fs::get::<State, _>(self.root.join("status")) {
Ok(Some(state)) => Ok(state),
Ok(None) => Ok(State::Unknown),
Err(e) => Err(e),
})
}
fn voltage(&self) -> Result<ElectricPotential> {
let mut value =
["voltage_now", "voltage_avg"]
.iter()
.filter_map(|filename| match fs::voltage(self.root.join(filename)) {
Ok(Some(value)) => Some(value),
_ => None,
});
match value.next() {
Some(value) => Ok(value),
None => Err(Error::not_found("Unable to calculate device voltage value")),
}
}
fn temperature(&self) -> Result<Option<ThermodynamicTemperature>> {
match fs::get::<f32, _>(self.root.join("temp")) {
Ok(Some(value)) => Ok(Some(celsius!(value / 10.0))),
Ok(None) => Ok(None),
Err(e) => Err(e),
}
}
fn cycle_count(&self) -> Result<Option<u32>> {
fs::get::<u32, _>(self.root.join("cycle_count")).map(|value| {
// Handling zero cycles count as a non-existing value.
// Reason: some drivers are creating `cycle_count` with zero value
// even for old batteries.
// Since it is more often occasion than using fresh battery with zero cycles
// (real one this time), it is better just to ignore this value.
// See: https://github.com/svartalf/rust-battery/issues/23
match value {
Some(cycles) if cycles == 0 => None,
Some(cycles) => Some(cycles),
None => None,
}
})
}
// Following methods are not cached in the struct
pub fn manufacturer(&self) -> Result<Option<String>> {
fs::get_string(self.root.join("manufacturer"))
}
pub fn model(&self) -> Result<Option<String>> {
fs::get_string(self.root.join("model_name"))
}
pub fn serial_number(&self) -> Result<Option<String>> {
fs::get_string(self.root.join("serial_number"))
}
pub fn technology(&self) -> Result<Technology> {
match fs::get::<Technology, _>(self.root.join("technology")) {
Ok(Some(tech)) => Ok(tech),
Ok(None) => Ok(Technology::Unknown),
Err(e) => Err(e),
}
}
}