ironmonitor 7.1.0

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
//! Power Supply and Battery Monitoring
//!
//! This module provides monitoring for power supplies, batteries, and AC adapters.
//! Inspired by jetson_stats power monitoring and Linux `/sys/class/power_supply/`.
//!
//! # Examples
//!
//! ## List all power supplies
//!
//! ```no_run
//! use ironmonitor::power_supply::{PowerSupplyMonitor, PowerSupplyType};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let monitor = PowerSupplyMonitor::new()?;
//! for supply in monitor.supplies() {
//!     println!("{}: {:?}", supply.name, supply.supply_type);
//!     if let Some(capacity) = supply.capacity_percent {
//!         println!("  Capacity: {}%", capacity);
//!     }
//!     if let Some(power) = supply.power_now_mw {
//!         println!("  Power: {:.2}W", power as f64 / 1000.0);
//!     }
//! }
//! # Ok(())
//! # }
//! ```

use crate::error::{IronError, Result};
use serde::{Deserialize, Serialize};

/// Type of power supply
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PowerSupplyType {
    /// AC adapter / mains power
    Mains,
    /// Battery (rechargeable)
    Battery,
    /// USB power delivery
    Usb,
    /// USB-C PD
    UsbPd,
    /// Uninterruptible Power Supply
    Ups,
    /// Unknown type
    Unknown,
}

impl std::fmt::Display for PowerSupplyType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PowerSupplyType::Mains => write!(f, "Mains"),
            PowerSupplyType::Battery => write!(f, "Battery"),
            PowerSupplyType::Usb => write!(f, "USB"),
            PowerSupplyType::UsbPd => write!(f, "USB-PD"),
            PowerSupplyType::Ups => write!(f, "UPS"),
            PowerSupplyType::Unknown => write!(f, "Unknown"),
        }
    }
}

/// Battery charging status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChargingStatus {
    /// Battery is charging
    Charging,
    /// Battery is discharging
    Discharging,
    /// Battery is full
    Full,
    /// Not charging (plugged in but not charging)
    NotCharging,
    /// Unknown status
    Unknown,
}

impl std::fmt::Display for ChargingStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChargingStatus::Charging => write!(f, "Charging"),
            ChargingStatus::Discharging => write!(f, "Discharging"),
            ChargingStatus::Full => write!(f, "Full"),
            ChargingStatus::NotCharging => write!(f, "Not Charging"),
            ChargingStatus::Unknown => write!(f, "Unknown"),
        }
    }
}

/// Battery health status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BatteryHealth {
    /// Battery is in good condition
    Good,
    /// Battery is overheating
    Overheat,
    /// Battery is dead
    Dead,
    /// Over voltage condition
    OverVoltage,
    /// Unspecified failure
    UnspecifiedFailure,
    /// Battery is cold
    Cold,
    /// Watchdog timer expired
    WatchdogTimerExpire,
    /// Safety timer expired
    SafetyTimerExpire,
    /// Unknown health status
    Unknown,
}

impl std::fmt::Display for BatteryHealth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BatteryHealth::Good => write!(f, "Good"),
            BatteryHealth::Overheat => write!(f, "Overheat"),
            BatteryHealth::Dead => write!(f, "Dead"),
            BatteryHealth::OverVoltage => write!(f, "Over Voltage"),
            BatteryHealth::UnspecifiedFailure => write!(f, "Failure"),
            BatteryHealth::Cold => write!(f, "Cold"),
            BatteryHealth::WatchdogTimerExpire => write!(f, "Watchdog Expired"),
            BatteryHealth::SafetyTimerExpire => write!(f, "Safety Timer Expired"),
            BatteryHealth::Unknown => write!(f, "Unknown"),
        }
    }
}

/// Information about a power supply
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PowerSupplyInfo {
    /// Name of the power supply
    pub name: String,
    /// Type of power supply
    pub supply_type: PowerSupplyType,
    /// Whether the supply is online/connected
    /// Whether this supply is currently delivering power.
    ///
    /// `None` when the platform did not say. Windows documents
    /// `SYSTEM_POWER_STATUS::ACLineStatus` as 0 offline, 1 online and **255
    /// unknown**, and this was `status.ACLineStatus == 1` -- so a host that
    /// answered "unknown" was recorded as running on battery. The rest of that
    /// function already handles every other sentinel the struct defines (255
    /// for `BatteryLifePercent`, `0xFFFFFFFF` for both time fields, 128 for "no
    /// battery"), which is what made this one worth fixing rather than the
    /// function worth rewriting.
    pub online: Option<bool>,
    /// Charging status (for batteries). `None` if the platform did not report it.
    pub status: Option<ChargingStatus>,
    /// Battery health (for batteries). `None` if the platform did not report it.
    pub health: Option<BatteryHealth>,
    /// Current capacity percentage (0-100)
    pub capacity_percent: Option<u8>,
    /// Voltage now in millivolts
    pub voltage_now_mv: Option<u32>,
    /// Voltage minimum design in millivolts
    pub voltage_min_mv: Option<u32>,
    /// Current now in microamps (positive = charging, negative = discharging)
    pub current_now_ua: Option<i32>,
    /// Power now in milliwatts. Linux reads `power_now` in microwatts and divides
    /// by 1000; the doc said microwatts, which the field never held, and
    /// `power_w()` divides by 1000 as well.
    pub power_now_mw: Option<u32>,
    /// Energy now in microwatt-hours
    pub energy_now_uwh: Option<u64>,
    /// Energy full in microwatt-hours
    pub energy_full_uwh: Option<u64>,
    /// Energy full design in microwatt-hours
    pub energy_full_design_uwh: Option<u64>,
    /// Charge now in microamp-hours
    pub charge_now_uah: Option<u64>,
    /// Charge full in microamp-hours
    pub charge_full_uah: Option<u64>,
    /// Charge full design in microamp-hours
    pub charge_full_design_uah: Option<u64>,
    /// Temperature in tenths of degrees Celsius
    pub temperature_tenths_c: Option<i32>,
    /// Manufacturer name
    pub manufacturer: Option<String>,
    /// Model name
    pub model_name: Option<String>,
    /// Serial number
    pub serial_number: Option<String>,
    /// Technology (Li-ion, NiMH, etc.)
    pub technology: Option<String>,
    /// Cycle count (number of charge cycles)
    pub cycle_count: Option<u32>,
    /// Time to empty in minutes (estimated)
    pub time_to_empty_min: Option<u32>,
    /// Time to full in minutes (estimated)
    pub time_to_full_min: Option<u32>,
}

impl PowerSupplyInfo {
    /// Create a new PowerSupplyInfo with default values
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            supply_type: PowerSupplyType::Unknown,
            online: None,
            status: None,
            health: None,
            capacity_percent: None,
            voltage_now_mv: None,
            voltage_min_mv: None,
            current_now_ua: None,
            power_now_mw: None,
            energy_now_uwh: None,
            energy_full_uwh: None,
            energy_full_design_uwh: None,
            charge_now_uah: None,
            charge_full_uah: None,
            charge_full_design_uah: None,
            temperature_tenths_c: None,
            manufacturer: None,
            model_name: None,
            serial_number: None,
            technology: None,
            cycle_count: None,
            time_to_empty_min: None,
            time_to_full_min: None,
        }
    }

    /// Get temperature in Celsius (if available)
    pub fn temperature_celsius(&self) -> Option<f32> {
        self.temperature_tenths_c.map(|t| t as f32 / 10.0)
    }

    /// Get voltage in volts (if available)
    pub fn voltage_v(&self) -> Option<f32> {
        self.voltage_now_mv.map(|v| v as f32 / 1000.0)
    }

    /// Get current in amps (if available)
    pub fn current_a(&self) -> Option<f32> {
        self.current_now_ua.map(|c| c as f32 / 1_000_000.0)
    }

    /// Get power in watts (if available)
    pub fn power_w(&self) -> Option<f32> {
        self.power_now_mw.map(|p| p as f32 / 1000.0)
    }

    /// Get energy now in watt-hours (if available)
    pub fn energy_wh(&self) -> Option<f32> {
        self.energy_now_uwh.map(|e| e as f32 / 1_000_000.0)
    }

    /// Get energy full in watt-hours (if available)
    pub fn energy_full_wh(&self) -> Option<f32> {
        self.energy_full_uwh.map(|e| e as f32 / 1_000_000.0)
    }

    /// Get energy full design in watt-hours (if available)
    pub fn energy_full_design_wh(&self) -> Option<f32> {
        self.energy_full_design_uwh.map(|e| e as f32 / 1_000_000.0)
    }

    /// Calculate battery wear level as percentage (100% = like new)
    pub fn wear_level_percent(&self) -> Option<f32> {
        match (self.energy_full_uwh, self.energy_full_design_uwh) {
            (Some(full), Some(design)) if design > 0 => Some((full as f32 / design as f32) * 100.0),
            _ => match (self.charge_full_uah, self.charge_full_design_uah) {
                (Some(full), Some(design)) if design > 0 => {
                    Some((full as f32 / design as f32) * 100.0)
                }
                _ => None,
            },
        }
    }

    /// Check if this is a battery
    pub fn is_battery(&self) -> bool {
        self.supply_type == PowerSupplyType::Battery
    }

    /// Check if AC power is connected.
    ///
    /// Only a mains supply answers this. A battery or USB port says nothing about
    /// AC from its type, so for those this is `None`. It used to return
    /// `Some(false)`, which read as "not on AC" for every non-mains supply.
    pub fn is_on_ac_power(&self) -> Option<bool> {
        if self.supply_type != PowerSupplyType::Mains {
            return None;
        }
        self.online
    }
}

/// Power supply monitor
pub struct PowerSupplyMonitor {
    supplies: Vec<PowerSupplyInfo>,
}

impl PowerSupplyMonitor {
    /// Create a new power supply monitor
    pub fn new() -> Result<Self> {
        let supplies = Self::enumerate_supplies()?;
        Ok(Self { supplies })
    }

    /// Get all power supplies
    pub fn supplies(&self) -> &[PowerSupplyInfo] {
        &self.supplies
    }

    /// Get mutable reference to supplies for updating
    pub fn supplies_mut(&mut self) -> &mut Vec<PowerSupplyInfo> {
        &mut self.supplies
    }

    /// Update all power supply information
    pub fn update(&mut self) -> Result<()> {
        self.supplies = Self::enumerate_supplies()?;
        Ok(())
    }

    /// Get the primary battery (if any)
    pub fn primary_battery(&self) -> Option<&PowerSupplyInfo> {
        self.supplies
            .iter()
            .find(|s| s.supply_type == PowerSupplyType::Battery)
    }

    /// Get AC adapter info (if any)
    pub fn ac_adapter(&self) -> Option<&PowerSupplyInfo> {
        self.supplies
            .iter()
            .find(|s| s.supply_type == PowerSupplyType::Mains)
    }

    /// Check if running on AC power.
    ///
    /// `None` when no mains supply was enumerated, or when the one that was
    /// did not report its state. An empty supply list is a failed enumeration
    /// as often as it is a machine with no power supplies, and this used to
    /// answer `false` -- "on battery" -- for both.
    pub fn on_ac_power(&self) -> Option<bool> {
        let mains: Vec<&PowerSupplyInfo> = self
            .supplies
            .iter()
            .filter(|s| s.supply_type == PowerSupplyType::Mains)
            .collect();
        if mains.is_empty() {
            return None;
        }
        // Any mains supply that says it is online settles it. Otherwise the
        // answer is `false` only if at least one of them actually said so.
        if mains.iter().any(|s| s.online == Some(true)) {
            Some(true)
        } else if mains.iter().any(|s| s.online == Some(false)) {
            Some(false)
        } else {
            None
        }
    }

    /// Check if running on battery.
    ///
    /// `None` when the AC state is unknown: "not on mains" cannot be concluded
    /// from "nobody said whether it is on mains".
    pub fn on_battery(&self) -> Option<bool> {
        let has_battery = self
            .supplies
            .iter()
            .any(|s| s.supply_type == PowerSupplyType::Battery);
        Some(!self.on_ac_power()? && has_battery)
    }

    /// Total system power draw in watts, or `None` unless a battery is discharging.
    ///
    /// The system's draw is what its discharging battery supplies. Summing every
    /// supply counted USB and USB-PD ports, which are inputs, and a charging
    /// battery: on Windows `wmi_rate` reports the charge rate as the battery's
    /// rate, so power flowing into a battery was counted as draw. With no
    /// discharging battery there is no reading of draw, so this is `None` and not
    /// a sum of whatever happened to be reported.
    pub fn total_power_w(&self) -> Option<f32> {
        let mut total = 0.0f32;
        let mut found = false;

        let discharging_batteries = self.supplies.iter().filter(|s| {
            s.supply_type == PowerSupplyType::Battery
                && s.status == Some(ChargingStatus::Discharging)
        });
        for supply in discharging_batteries {
            if let Some(power) = supply.power_w() {
                total += power;
                found = true;
            }
        }

        if found {
            Some(total)
        } else {
            None
        }
    }

    /// Enumerate all power supplies on the system
    #[cfg(target_os = "linux")]
    fn enumerate_supplies() -> Result<Vec<PowerSupplyInfo>> {
        use std::fs;
        use std::path::Path;

        let mut supplies = Vec::new();
        let power_supply_path = Path::new("/sys/class/power_supply");

        // A missing class directory means this kernel has no power-supply
        // subsystem to read. That is a failed read, not a machine with no supplies.
        if !power_supply_path.exists() {
            return Err(IronError::System(
                "/sys/class/power_supply does not exist".to_string(),
            ));
        }

        let entries = fs::read_dir(power_supply_path)
            .map_err(|e| IronError::System(format!("Failed to read power_supply: {}", e)))?;

        for entry in entries.flatten() {
            let name = entry.file_name().to_string_lossy().to_string();
            let path = entry.path();

            let mut info = PowerSupplyInfo::new(&name);

            // Read type
            if let Ok(type_str) = fs::read_to_string(path.join("type")) {
                info.supply_type = match type_str.trim().to_lowercase().as_str() {
                    "mains" => PowerSupplyType::Mains,
                    "battery" => PowerSupplyType::Battery,
                    "usb" => PowerSupplyType::Usb,
                    "usb_pd" | "usb-pd" => PowerSupplyType::UsbPd,
                    "ups" => PowerSupplyType::Ups,
                    _ => PowerSupplyType::Unknown,
                };
            }

            // Read online status. Only the kernel's "0" and "1" are readings;
            // anything else is withheld rather than read as offline.
            if let Ok(online_str) = fs::read_to_string(path.join("online")) {
                info.online = match online_str.trim() {
                    "1" => Some(true),
                    "0" => Some(false),
                    _ => None,
                };
            }

            // Read status
            if let Ok(status_str) = fs::read_to_string(path.join("status")) {
                info.status = Some(match status_str.trim().to_lowercase().as_str() {
                    "charging" => ChargingStatus::Charging,
                    "discharging" => ChargingStatus::Discharging,
                    "full" => ChargingStatus::Full,
                    "not charging" => ChargingStatus::NotCharging,
                    // Read, but not one of the kernel's values for a charging state.
                    _ => ChargingStatus::Unknown,
                });
            }

            // Read health
            if let Ok(health_str) = fs::read_to_string(path.join("health")) {
                info.health = Some(match health_str.trim().to_lowercase().as_str() {
                    "good" => BatteryHealth::Good,
                    "overheat" => BatteryHealth::Overheat,
                    "dead" => BatteryHealth::Dead,
                    "overvoltage" | "over voltage" => BatteryHealth::OverVoltage,
                    "unspecified failure" => BatteryHealth::UnspecifiedFailure,
                    "cold" => BatteryHealth::Cold,
                    "watchdog timer expire" => BatteryHealth::WatchdogTimerExpire,
                    "safety timer expire" => BatteryHealth::SafetyTimerExpire,
                    _ => BatteryHealth::Unknown,
                });
            }

            // Read capacity. Above 100 is not a percentage: withheld, not clamped.
            if let Ok(cap_str) = fs::read_to_string(path.join("capacity")) {
                if let Ok(cap) = cap_str.trim().parse::<u8>() {
                    if cap <= 100 {
                        info.capacity_percent = Some(cap);
                    }
                }
            }

            // Read voltage_now (microvolts)
            if let Ok(volt_str) = fs::read_to_string(path.join("voltage_now")) {
                if let Ok(volt_uv) = volt_str.trim().parse::<u64>() {
                    info.voltage_now_mv = Some((volt_uv / 1000) as u32);
                }
            }

            // Read voltage_min_design
            if let Ok(volt_str) = fs::read_to_string(path.join("voltage_min_design")) {
                if let Ok(volt_uv) = volt_str.trim().parse::<u64>() {
                    info.voltage_min_mv = Some((volt_uv / 1000) as u32);
                }
            }

            // Read current_now (microamps)
            if let Ok(curr_str) = fs::read_to_string(path.join("current_now")) {
                if let Ok(curr) = curr_str.trim().parse::<i32>() {
                    info.current_now_ua = Some(curr);
                }
            }

            // Read power_now (microwatts)
            if let Ok(power_str) = fs::read_to_string(path.join("power_now")) {
                if let Ok(power) = power_str.trim().parse::<u64>() {
                    info.power_now_mw = Some((power / 1000) as u32);
                }
            }

            // Read energy values
            if let Ok(energy_str) = fs::read_to_string(path.join("energy_now")) {
                if let Ok(energy) = energy_str.trim().parse::<u64>() {
                    info.energy_now_uwh = Some(energy);
                }
            }

            if let Ok(energy_str) = fs::read_to_string(path.join("energy_full")) {
                if let Ok(energy) = energy_str.trim().parse::<u64>() {
                    info.energy_full_uwh = Some(energy);
                }
            }

            if let Ok(energy_str) = fs::read_to_string(path.join("energy_full_design")) {
                if let Ok(energy) = energy_str.trim().parse::<u64>() {
                    info.energy_full_design_uwh = Some(energy);
                }
            }

            // Read charge values
            if let Ok(charge_str) = fs::read_to_string(path.join("charge_now")) {
                if let Ok(charge) = charge_str.trim().parse::<u64>() {
                    info.charge_now_uah = Some(charge);
                }
            }

            if let Ok(charge_str) = fs::read_to_string(path.join("charge_full")) {
                if let Ok(charge) = charge_str.trim().parse::<u64>() {
                    info.charge_full_uah = Some(charge);
                }
            }

            if let Ok(charge_str) = fs::read_to_string(path.join("charge_full_design")) {
                if let Ok(charge) = charge_str.trim().parse::<u64>() {
                    info.charge_full_design_uah = Some(charge);
                }
            }

            // Read temperature (tenths of degrees C)
            if let Ok(temp_str) = fs::read_to_string(path.join("temp")) {
                if let Ok(temp) = temp_str.trim().parse::<i32>() {
                    info.temperature_tenths_c = Some(temp);
                }
            }

            // Read manufacturer
            if let Ok(mfr_str) = fs::read_to_string(path.join("manufacturer")) {
                let mfr = mfr_str.trim();
                if !mfr.is_empty() {
                    info.manufacturer = Some(mfr.to_string());
                }
            }

            // Read model name
            if let Ok(model_str) = fs::read_to_string(path.join("model_name")) {
                let model = model_str.trim();
                if !model.is_empty() {
                    info.model_name = Some(model.to_string());
                }
            }

            // Read serial number
            if let Ok(serial_str) = fs::read_to_string(path.join("serial_number")) {
                let serial = serial_str.trim();
                if !serial.is_empty() {
                    info.serial_number = Some(serial.to_string());
                }
            }

            // Read technology
            if let Ok(tech_str) = fs::read_to_string(path.join("technology")) {
                let tech = tech_str.trim();
                if !tech.is_empty() {
                    info.technology = Some(tech.to_string());
                }
            }

            // Read cycle count
            if let Ok(cycle_str) = fs::read_to_string(path.join("cycle_count")) {
                if let Ok(cycles) = cycle_str.trim().parse::<u32>() {
                    info.cycle_count = Some(cycles);
                }
            }

            // Read time to empty. The sysfs value is in seconds.
            if let Ok(time_str) = fs::read_to_string(path.join("time_to_empty_avg")) {
                if let Ok(time) = time_str.trim().parse::<u32>() {
                    info.time_to_empty_min = Some(time / 60);
                }
            }

            // Read time to full. The sysfs value is in seconds.
            if let Ok(time_str) = fs::read_to_string(path.join("time_to_full_avg")) {
                if let Ok(time) = time_str.trim().parse::<u32>() {
                    info.time_to_full_min = Some(time / 60);
                }
            }

            supplies.push(info);
        }

        Ok(supplies)
    }

    #[cfg(target_os = "windows")]
    fn enumerate_supplies() -> Result<Vec<PowerSupplyInfo>> {
        let mut supplies = Vec::new();
        // The battery `GetSystemPowerStatus` describes, held until it is joined
        // to the batteries WMI describes.
        let mut status_battery: Option<PowerSupplyInfo> = None;

        // Use Windows API to get battery information
        unsafe {
            use ::windows::Win32::System::Power::{GetSystemPowerStatus, SYSTEM_POWER_STATUS};

            let mut status = SYSTEM_POWER_STATUS::default();
            // A failed call is a failed read, not a machine with no supplies.
            if let Err(e) = GetSystemPowerStatus(&mut status) {
                return Err(IronError::System(format!(
                    "GetSystemPowerStatus failed: {}",
                    e
                )));
            }

            // AC Line Status
            let mut ac_info = PowerSupplyInfo::new("AC");
            ac_info.supply_type = PowerSupplyType::Mains;
            // 0 offline, 1 online, 255 unknown.
            ac_info.online = match status.ACLineStatus {
                0 => Some(false),
                1 => Some(true),
                _ => None,
            };
            supplies.push(ac_info);

            // Battery
            if status.BatteryFlag != 128 {
                // 128 = No battery
                let mut battery_info = PowerSupplyInfo::new("Battery");
                battery_info.supply_type = PowerSupplyType::Battery;
                // `online` stays unset: this API reports the AC line, not whether
                // the battery is delivering power.

                // Capacity
                if status.BatteryLifePercent != 255 {
                    battery_info.capacity_percent = Some(status.BatteryLifePercent);
                }

                // Status. BatteryFlag 255 is "unknown". It also has the charging
                // bit set, so it is excluded before that bit is tested.
                if status.BatteryFlag != 255 {
                    battery_info.status = if status.BatteryFlag & 8 != 0 {
                        Some(ChargingStatus::Charging)
                    } else if status.ACLineStatus == 1 && status.BatteryLifePercent == 100 {
                        Some(ChargingStatus::Full)
                    } else if status.ACLineStatus == 0 {
                        Some(ChargingStatus::Discharging)
                    } else if status.ACLineStatus == 1 {
                        Some(ChargingStatus::NotCharging)
                    } else {
                        None
                    };
                }

                // Time remaining
                if status.BatteryLifeTime != 0xFFFFFFFF {
                    battery_info.time_to_empty_min = Some(status.BatteryLifeTime / 60);
                }

                // `BatteryFullLifeTime` is how long a full battery lasts, not the
                // time until full, so it is not a `time_to_full_min`. Windows
                // reports no time to full here, and the field stays unset.

                status_battery = Some(battery_info);
            }
        }

        supplies.extend(combine_windows_batteries(
            status_battery,
            read_wmi_batteries(),
        ));
        Ok(supplies)
    }

    #[cfg(target_os = "macos")]
    fn enumerate_supplies() -> Result<Vec<PowerSupplyInfo>> {
        use std::process::Command;

        let mut supplies = Vec::new();

        // Use pmset to get battery info. A run that fails is a failed read, not
        // a machine with no power supplies.
        let output = Command::new("pmset")
            .args(["-g", "batt"])
            .output()
            .map_err(|e| IronError::CommandFailed(format!("pmset -g batt failed: {}", e)))?;
        if !output.status.success() {
            return Err(IronError::CommandFailed(format!(
                "pmset -g batt exited with {}",
                output.status
            )));
        }
        let stdout = String::from_utf8_lossy(&output.stdout);

        // Parse AC adapter
        let mut ac_info = PowerSupplyInfo::new("AC");
        ac_info.supply_type = PowerSupplyType::Mains;
        // Only a "drawing from" line says where power comes from; output
        // without one is unknown, not "on battery".
        ac_info.online = crate::power_profile::parse_pmset_power_source(&stdout);
        supplies.push(ac_info);

        // Parse battery. Battery lines start with the battery's name, e.g.
        // " -InternalBattery-0 (id=...)\t87%; discharging; 5:23 remaining". The
        // header line can say "Battery Power", so the name is what is matched.
        for line in stdout.lines() {
            if line.contains("InternalBattery") || line.trim_start().starts_with('-') {
                supplies.push(parse_pmset_battery_line(line));
                break;
            }
        }

        Ok(supplies)
    }

    #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
    fn enumerate_supplies() -> Result<Vec<PowerSupplyInfo>> {
        // No reader exists for this platform. That is not the same as "no
        // power supplies here", so it is reported as an error.
        Err(IronError::UnsupportedPlatform(
            "power supply enumeration is not implemented for this platform".to_string(),
        ))
    }
}

impl Default for PowerSupplyMonitor {
    fn default() -> Self {
        Self::new().unwrap_or(Self {
            supplies: Vec::new(),
        })
    }
}

/// Summary of system power state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PowerSummary {
    /// Whether running on AC power. `None` if that was not established.
    pub on_ac_power: Option<bool>,
    /// Whether running on battery. `None` if that was not established.
    pub on_battery: Option<bool>,
    /// Primary battery capacity (if any)
    pub battery_percent: Option<u8>,
    /// Primary battery status. `None` if not read.
    pub battery_status: Option<ChargingStatus>,
    /// Battery health. `None` if not read.
    pub battery_health: Option<BatteryHealth>,
    /// Estimated time remaining on battery (minutes)
    pub time_remaining_min: Option<u32>,
    /// System power draw in watts, read from the discharging battery. `None` when
    /// no battery is discharging, including on AC power.
    pub power_draw_w: Option<f32>,
    /// Battery wear level (100% = like new)
    pub battery_wear_percent: Option<f32>,
}

impl PowerSummary {
    /// Get power summary from monitor
    pub fn from_monitor(monitor: &PowerSupplyMonitor) -> Self {
        let battery = monitor.primary_battery();

        Self {
            on_ac_power: monitor.on_ac_power(),
            on_battery: monitor.on_battery(),
            battery_percent: battery.and_then(|b| b.capacity_percent),
            battery_status: battery.and_then(|b| b.status),
            battery_health: battery.and_then(|b| b.health),
            time_remaining_min: battery.and_then(|b| b.time_to_empty_min),
            power_draw_w: monitor.total_power_w(),
            battery_wear_percent: battery.and_then(|b| b.wear_level_percent()),
        }
    }
}

/// Get a quick power summary
pub fn power_summary() -> Result<PowerSummary> {
    let monitor = PowerSupplyMonitor::new()?;
    Ok(PowerSummary::from_monitor(&monitor))
}

/// Check if running on AC power
pub fn is_on_ac_power() -> Option<bool> {
    PowerSupplyMonitor::new().ok().and_then(|m| m.on_ac_power())
}

/// Check if running on battery
pub fn is_on_battery() -> Option<bool> {
    PowerSupplyMonitor::new().ok().and_then(|m| m.on_battery())
}

/// Get battery percentage (if available)
pub fn battery_percent() -> Option<u8> {
    PowerSupplyMonitor::new()
        .ok()
        .and_then(|m| m.primary_battery().and_then(|b| b.capacity_percent))
}

/// The batteries WMI describes in `root\WMI`, joined by `InstanceName`.
///
/// Four classes are read in one PowerShell pass: `BatteryStaticData` (design
/// capacity, manufacturer, serial number, chemistry), `BatteryFullChargedCapacity`,
/// `BatteryStatus` (remaining capacity, voltage, charge and discharge rate) and
/// `BatteryCycleCount`. A desktop has none of these instances and the list is
/// empty, which is a reading. A failed query is also empty, and the batteries it
/// would have described are not guessed at.
#[cfg(windows)]
fn read_wmi_batteries() -> Vec<PowerSupplyInfo> {
    const QUERY: &str = concat!(
        "$ns = 'root/wmi'; ",
        "[pscustomobject]@{ ",
        "Static = @(Get-CimInstance -Namespace $ns -ClassName BatteryStaticData -ErrorAction SilentlyContinue | ",
        "Select-Object InstanceName, DesignedCapacity, ManufactureName, SerialNumber, Chemistry); ",
        "Full = @(Get-CimInstance -Namespace $ns -ClassName BatteryFullChargedCapacity -ErrorAction SilentlyContinue | ",
        "Select-Object InstanceName, FullChargedCapacity); ",
        "Status = @(Get-CimInstance -Namespace $ns -ClassName BatteryStatus -ErrorAction SilentlyContinue | ",
        "Select-Object InstanceName, RemainingCapacity, Voltage, ChargeRate, DischargeRate, Charging, Discharging); ",
        "Cycle = @(Get-CimInstance -Namespace $ns -ClassName BatteryCycleCount -ErrorAction SilentlyContinue | ",
        "Select-Object InstanceName, CycleCount) ",
        "} | ConvertTo-Json -Depth 4 -Compress"
    );
    crate::core::command::capture_json("powershell", &["-NoProfile", "-Command", QUERY])
        .ok()
        .flatten()
        .map(|json| batteries_from_wmi(&json))
        .unwrap_or_default()
}

/// Rows of one WMI class in the captured JSON, keyed by `InstanceName`.
#[cfg(any(windows, test))]
fn wmi_rows_by_instance(
    json: &serde_json::Value,
    key: &str,
) -> std::collections::HashMap<String, serde_json::Value> {
    json.get(key)
        .map(crate::core::command::json_items)
        .unwrap_or_default()
        .into_iter()
        .filter_map(|row| {
            let id = row.get("InstanceName")?.as_str()?.to_string();
            Some((id, row))
        })
        .collect()
}

/// A WMI battery integer, or `None` where it is the API's unknown marker.
///
/// The battery API reports `0xFFFFFFFF` for an unknown capacity or voltage.
#[cfg(any(windows, test))]
fn wmi_u32(value: Option<&serde_json::Value>) -> Option<u32> {
    let n = value?.as_u64()?;
    if n == 0xFFFF_FFFF {
        return None;
    }
    u32::try_from(n).ok()
}

/// A charge or discharge rate in mW. The battery API reports `0x80000000` for an
/// unknown rate, and an idle battery reports 0 on both, which is a reading.
#[cfg(any(windows, test))]
fn wmi_rate(
    discharge: Option<&serde_json::Value>,
    charge: Option<&serde_json::Value>,
) -> Option<u32> {
    let rate = |v| wmi_u32(v).filter(|&r| r != 0x8000_0000);
    match (rate(discharge), rate(charge)) {
        // At most one of the two is non-zero, so the larger is the rate.
        (Some(d), Some(c)) => Some(d.max(c)),
        (Some(d), None) if d > 0 => Some(d),
        (None, Some(c)) if c > 0 => Some(c),
        _ => None,
    }
}

/// The technology name for a `BatteryStaticData.Chemistry` value, which is four
/// ASCII bytes. A code not listed here is `None`, not a guess.
#[cfg(any(windows, test))]
fn technology_from_chemistry(chemistry: &serde_json::Value) -> Option<String> {
    let bytes: Vec<u8> = chemistry
        .as_array()?
        .iter()
        .filter_map(|v| v.as_u64().and_then(|n| u8::try_from(n).ok()))
        .collect();
    let code = String::from_utf8(bytes).ok()?;
    match code.trim_end_matches('\0') {
        "LION" => Some("Li-ion".to_string()),
        "LIPO" => Some("LiPo".to_string()),
        "NiMH" => Some("NiMH".to_string()),
        "NiCd" => Some("NiCd".to_string()),
        "PbAc" => Some("Lead-acid".to_string()),
        _ => None,
    }
}

/// Build one battery per `BatteryStaticData` instance, filling the rest from the
/// rows with the same `InstanceName`. A field whose class has no row for that
/// instance stays unset.
#[cfg(any(windows, test))]
fn batteries_from_wmi(json: &serde_json::Value) -> Vec<PowerSupplyInfo> {
    let full = wmi_rows_by_instance(json, "Full");
    let status = wmi_rows_by_instance(json, "Status");
    let cycle = wmi_rows_by_instance(json, "Cycle");
    // mWh to uWh, the unit the struct keeps.
    let mwh_to_uwh = |mwh: u32| u64::from(mwh) * 1000;

    let statics = json
        .get("Static")
        .map(crate::core::command::json_items)
        .unwrap_or_default();

    let mut batteries = Vec::new();
    for row in &statics {
        let Some(id) = row.get("InstanceName").and_then(|v| v.as_str()) else {
            continue;
        };
        let text = |v: Option<&serde_json::Value>| {
            v.and_then(|v| v.as_str())
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .map(str::to_string)
        };
        let full = full.get(id);
        let status = status.get(id);
        let cycle = cycle.get(id);

        let mut info = PowerSupplyInfo::new(id);
        info.supply_type = PowerSupplyType::Battery;
        info.manufacturer = text(row.get("ManufactureName"));
        info.serial_number = text(row.get("SerialNumber"));
        info.technology = row.get("Chemistry").and_then(technology_from_chemistry);
        info.energy_full_design_uwh = wmi_u32(row.get("DesignedCapacity"))
            .filter(|&v| v > 0)
            .map(mwh_to_uwh);
        info.energy_full_uwh = full
            .and_then(|f| wmi_u32(f.get("FullChargedCapacity")))
            .filter(|&v| v > 0)
            .map(mwh_to_uwh);
        info.energy_now_uwh = status
            .and_then(|s| wmi_u32(s.get("RemainingCapacity")))
            .map(mwh_to_uwh);
        info.voltage_now_mv = status.and_then(|s| wmi_u32(s.get("Voltage")));
        info.power_now_mw =
            status.and_then(|s| wmi_rate(s.get("DischargeRate"), s.get("ChargeRate")));
        // The battery's own `Discharging` flag is what makes its rate a system
        // draw. It is read here, not inferred from the rate. Neither flag set is
        // left as `None`, since the API does not say more than that.
        info.status = status.and_then(|s| {
            let flag = |name| s.get(name).and_then(serde_json::Value::as_bool);
            if flag("Discharging") == Some(true) {
                Some(ChargingStatus::Discharging)
            } else if flag("Charging") == Some(true) {
                Some(ChargingStatus::Charging)
            } else {
                None
            }
        });
        info.cycle_count = cycle.and_then(|c| wmi_u32(c.get("CycleCount")));
        batteries.push(info);
    }
    batteries
}

/// Join the battery `GetSystemPowerStatus` describes to the batteries WMI
/// describes.
///
/// `GetSystemPowerStatus` gives one combined percentage and charging state, and
/// no instance name. It is attributed to a battery only when WMI describes
/// exactly one, since then it is that battery's. With several batteries it is
/// dropped rather than given to one of them.
#[cfg(any(windows, test))]
fn combine_windows_batteries(
    status_battery: Option<PowerSupplyInfo>,
    mut wmi: Vec<PowerSupplyInfo>,
) -> Vec<PowerSupplyInfo> {
    match wmi.len() {
        0 => status_battery.into_iter().collect(),
        1 => {
            let mut battery = wmi.remove(0);
            if let Some(status) = status_battery {
                battery.capacity_percent = status.capacity_percent;
                battery.status = status.status;
                battery.time_to_empty_min = status.time_to_empty_min;
            }
            vec![battery]
        }
        _ => wmi,
    }
}

/// Read one pmset battery line, e.g.
/// `" -InternalBattery-0 (id=...)\t87%; discharging; 5:23 remaining present: true"`.
#[cfg(any(target_os = "macos", test))]
fn parse_pmset_battery_line(line: &str) -> PowerSupplyInfo {
    let mut battery_info = PowerSupplyInfo::new("Battery");
    battery_info.supply_type = PowerSupplyType::Battery;
    // `online` stays unset, as on Windows: pmset does not say whether the battery
    // is delivering power.

    // Parse percentage: the number directly before '%'. Digits earlier on the line
    // belong to the battery's name ("InternalBattery-0").
    if let Some(pct_end) = line.find('%') {
        let pct_str: String = line[..pct_end]
            .chars()
            .rev()
            .take_while(|c| c.is_ascii_digit())
            .collect::<String>()
            .chars()
            .rev()
            .collect();
        if let Ok(pct) = pct_str.parse::<u8>() {
            if pct <= 100 {
                battery_info.capacity_percent = Some(pct);
            }
        }
    }

    // Parse status. "discharging" contains "charging", so it is tested first.
    if line.contains("discharging") {
        battery_info.status = Some(ChargingStatus::Discharging);
    } else if line.contains("not charging") {
        battery_info.status = Some(ChargingStatus::NotCharging);
    } else if line.contains("charging") {
        battery_info.status = Some(ChargingStatus::Charging);
    } else if line.contains("charged") {
        battery_info.status = Some(ChargingStatus::Full);
    }

    // Parse time remaining: the "H:MM" token before " remaining". A line without
    // one, such as "(no estimate)", leaves it unset. While charging, pmset's
    // remaining time is the time until full, not until empty.
    if let Some(rem) = line.find(" remaining") {
        if let Some(time_str) = line[..rem].rsplit(' ').next() {
            if let Some((h, m)) = time_str.split_once(':') {
                if let (Ok(hours), Ok(mins)) = (h.parse::<u32>(), m.parse::<u32>()) {
                    let minutes = Some(hours * 60 + mins);
                    match battery_info.status {
                        Some(ChargingStatus::Charging) => battery_info.time_to_full_min = minutes,
                        Some(ChargingStatus::Discharging) => {
                            battery_info.time_to_empty_min = minutes
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    battery_info
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pmset_line_reads_percentage_status_and_time_to_empty() {
        let line =
            " -InternalBattery-0 (id=4653155)\t87%; discharging; 5:23 remaining present: true";
        let info = parse_pmset_battery_line(line);
        // The "0" in the battery's name is not the charge.
        assert_eq!(info.capacity_percent, Some(87));
        assert_eq!(info.status, Some(ChargingStatus::Discharging));
        assert_eq!(info.time_to_empty_min, Some(323));
        assert_eq!(info.time_to_full_min, None);
    }

    #[test]
    fn pmset_charging_time_is_time_to_full() {
        let line = " -InternalBattery-0 (id=4653155)\t52%; charging; 1:20 remaining present: true";
        let info = parse_pmset_battery_line(line);
        assert_eq!(info.status, Some(ChargingStatus::Charging));
        assert_eq!(info.time_to_full_min, Some(80));
        assert_eq!(info.time_to_empty_min, None);
    }

    #[test]
    fn pmset_line_without_estimate_leaves_times_unset() {
        let line =
            " -InternalBattery-0 (id=4653155)\t100%; AC attached; not charging present: true";
        let info = parse_pmset_battery_line(line);
        assert_eq!(info.capacity_percent, Some(100));
        assert_eq!(info.status, Some(ChargingStatus::NotCharging));
        assert_eq!(info.time_to_empty_min, None);
        assert_eq!(info.time_to_full_min, None);
    }

    #[test]
    fn test_power_supply_info_new_defaults() {
        let info = PowerSupplyInfo::new("BAT0");
        assert_eq!(info.name, "BAT0");
        assert_eq!(info.supply_type, PowerSupplyType::Unknown);
        assert_eq!(info.online, None);
        assert_eq!(info.status, None);
        assert_eq!(info.health, None);
        assert!(info.capacity_percent.is_none());
    }

    /// A battery's type says nothing about AC, and a USB port's says nothing
    /// about it either. Only mains answers, so the others are `None`, not
    /// `Some(false)`.
    #[test]
    fn only_a_mains_supply_answers_whether_ac_is_connected() {
        let with = |supply_type, online| {
            let mut info = PowerSupplyInfo::new("x");
            info.supply_type = supply_type;
            info.online = online;
            info
        };
        assert_eq!(
            with(PowerSupplyType::Mains, Some(true)).is_on_ac_power(),
            Some(true)
        );
        assert_eq!(
            with(PowerSupplyType::Mains, Some(false)).is_on_ac_power(),
            Some(false)
        );
        assert_eq!(with(PowerSupplyType::Mains, None).is_on_ac_power(), None);
        assert_eq!(
            with(PowerSupplyType::Battery, Some(false)).is_on_ac_power(),
            None
        );
        assert_eq!(
            with(PowerSupplyType::UsbPd, Some(false)).is_on_ac_power(),
            None
        );
    }

    /// Only a discharging battery is system draw. A charging battery, a USB port
    /// and mains each carry power, and none of it is the system's draw.
    #[test]
    fn system_draw_counts_only_a_discharging_battery() {
        let supply = |name: &str, supply_type, status, mw| {
            let mut info = PowerSupplyInfo::new(name);
            info.supply_type = supply_type;
            info.status = status;
            info.power_now_mw = mw;
            info
        };
        let monitor = |supplies| PowerSupplyMonitor { supplies };

        let discharging = monitor(vec![
            supply("AC", PowerSupplyType::Mains, None, None),
            supply(
                "BAT0",
                PowerSupplyType::Battery,
                Some(ChargingStatus::Discharging),
                Some(12_000),
            ),
            supply("ucsi", PowerSupplyType::UsbPd, None, Some(65_000)),
        ]);
        assert_eq!(discharging.total_power_w(), Some(12.0));

        let charging = monitor(vec![
            supply("AC", PowerSupplyType::Mains, None, None),
            supply(
                "BAT0",
                PowerSupplyType::Battery,
                Some(ChargingStatus::Charging),
                Some(40_000),
            ),
            supply("ucsi", PowerSupplyType::UsbPd, None, Some(65_000)),
        ]);
        assert_eq!(
            charging.total_power_w(),
            None,
            "charge current is not system draw"
        );

        let on_ac_no_battery = monitor(vec![
            supply("AC", PowerSupplyType::Mains, None, None),
            supply("ucsi", PowerSupplyType::UsbPd, None, Some(65_000)),
        ]);
        assert_eq!(on_ac_no_battery.total_power_w(), None);

        let discharging_unread = monitor(vec![supply(
            "BAT0",
            PowerSupplyType::Battery,
            Some(ChargingStatus::Discharging),
            None,
        )]);
        assert_eq!(discharging_unread.total_power_w(), None);
    }

    #[test]
    fn test_is_battery() {
        let mut info = PowerSupplyInfo::new("BAT0");
        assert!(!info.is_battery());
        info.supply_type = PowerSupplyType::Battery;
        assert!(info.is_battery());
        info.supply_type = PowerSupplyType::Mains;
        assert!(!info.is_battery());
    }

    #[test]
    fn test_unit_conversions() {
        let mut info = PowerSupplyInfo::new("BAT0");
        info.voltage_now_mv = Some(12000);
        assert_eq!(info.voltage_v(), Some(12.0));

        info.power_now_mw = Some(5000);
        assert_eq!(info.power_w(), Some(5.0));

        info.energy_now_uwh = Some(40_000_000);
        assert_eq!(info.energy_wh(), Some(40.0));

        info.temperature_tenths_c = Some(350);
        assert_eq!(info.temperature_celsius(), Some(35.0));

        info.current_now_ua = Some(2_000_000);
        assert_eq!(info.current_a(), Some(2.0));
    }

    #[test]
    fn test_wear_level_calculation() {
        let mut info = PowerSupplyInfo::new("BAT0");
        info.energy_full_uwh = Some(40_000_000);
        info.energy_full_design_uwh = Some(50_000_000);
        let wear = info.wear_level_percent().unwrap();
        assert!((wear - 80.0).abs() < 0.01);
    }

    #[test]
    fn test_charging_status_display() {
        assert_eq!(ChargingStatus::Charging.to_string(), "Charging");
        assert_eq!(ChargingStatus::Discharging.to_string(), "Discharging");
        assert_eq!(ChargingStatus::Full.to_string(), "Full");
        assert_eq!(ChargingStatus::NotCharging.to_string(), "Not Charging");
        assert_eq!(ChargingStatus::Unknown.to_string(), "Unknown");
    }

    #[test]
    fn test_power_supply_type_display() {
        assert_eq!(PowerSupplyType::Mains.to_string(), "Mains");
        assert_eq!(PowerSupplyType::Battery.to_string(), "Battery");
        assert_eq!(PowerSupplyType::Usb.to_string(), "USB");
        assert_eq!(PowerSupplyType::UsbPd.to_string(), "USB-PD");
        assert_eq!(PowerSupplyType::Ups.to_string(), "UPS");
    }

    #[test]
    fn test_battery_health_display() {
        assert_eq!(BatteryHealth::Good.to_string(), "Good");
        assert_eq!(BatteryHealth::Dead.to_string(), "Dead");
        assert_eq!(BatteryHealth::Overheat.to_string(), "Overheat");
        assert_eq!(BatteryHealth::Cold.to_string(), "Cold");
    }

    #[test]
    fn test_power_supply_serde_roundtrip() {
        let info = PowerSupplyInfo::new("AC0");
        let json = serde_json::to_string(&info).unwrap();
        let parsed: PowerSupplyInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.name, "AC0");
        assert_eq!(parsed.supply_type, PowerSupplyType::Unknown);
    }
}

#[cfg(test)]
mod wmi_battery_tests {
    use super::*;
    use serde_json::json;

    /// A laptop with two packs. The `Full` and `Status` rows are listed in the
    /// opposite order to `Static`, so only an `InstanceName` join gets them right.
    fn two_pack_laptop() -> serde_json::Value {
        json!({
            "Static": [
                {
                    "InstanceName": "ACPI\\PNP0C0A\\1_0",
                    "DesignedCapacity": 56000,
                    "ManufactureName": "SMP  ",
                    "SerialNumber": " 1234 ",
                    "Chemistry": [76, 73, 79, 78]
                },
                {
                    "InstanceName": "ACPI\\PNP0C0A\\2_0",
                    "DesignedCapacity": 30000,
                    "ManufactureName": "",
                    "SerialNumber": "9876",
                    "Chemistry": [76, 73, 80, 79]
                }
            ],
            "Full": [
                {"InstanceName": "ACPI\\PNP0C0A\\2_0", "FullChargedCapacity": 27500},
                {"InstanceName": "ACPI\\PNP0C0A\\1_0", "FullChargedCapacity": 51234}
            ],
            "Status": [
                {
                    "InstanceName": "ACPI\\PNP0C0A\\2_0",
                    "RemainingCapacity": 20000,
                    "Voltage": 12450,
                    "ChargeRate": 0,
                    "DischargeRate": 8000,
                    "Charging": false,
                    "Discharging": true
                },
                {
                    "InstanceName": "ACPI\\PNP0C0A\\1_0",
                    "RemainingCapacity": 40000,
                    "Voltage": 12600,
                    "ChargeRate": 0,
                    "DischargeRate": 15000,
                    "Charging": false,
                    "Discharging": true
                }
            ],
            "Cycle": [
                {"InstanceName": "ACPI\\PNP0C0A\\1_0", "CycleCount": 213},
                {"InstanceName": "ACPI\\PNP0C0A\\2_0", "CycleCount": 4}
            ]
        })
    }

    #[test]
    fn wmi_fields_are_joined_by_instance_name_and_converted_to_the_struct_units() {
        let batteries = batteries_from_wmi(&two_pack_laptop());
        assert_eq!(batteries.len(), 2);

        let a = &batteries[0];
        assert_eq!(a.name, "ACPI\\PNP0C0A\\1_0");
        assert_eq!(a.supply_type, PowerSupplyType::Battery);
        assert_eq!(a.energy_full_design_uwh, Some(56_000_000));
        assert_eq!(a.energy_full_uwh, Some(51_234_000));
        assert_eq!(a.energy_now_uwh, Some(40_000_000));
        assert_eq!(a.voltage_now_mv, Some(12600));
        assert_eq!(a.power_now_mw, Some(15000));
        assert_eq!(a.status, Some(ChargingStatus::Discharging));
        assert_eq!(a.cycle_count, Some(213));
        assert_eq!(a.manufacturer.as_deref(), Some("SMP"));
        assert_eq!(a.serial_number.as_deref(), Some("1234"));
        assert_eq!(a.technology.as_deref(), Some("Li-ion"));

        let b = &batteries[1];
        assert_eq!(b.energy_full_uwh, Some(27_500_000));
        assert_eq!(b.energy_now_uwh, Some(20_000_000));
        assert_eq!(b.power_now_mw, Some(8000));
        assert_eq!(b.cycle_count, Some(4));
        assert_eq!(b.technology.as_deref(), Some("LiPo"));
        assert_eq!(b.manufacturer, None, "a blank manufacturer is not a name");
    }

    /// `DischargeRate` and `ChargeRate` are both battery rates, and on a charging
    /// battery the one that matters is the charge current. It must not be counted
    /// as the system's draw, so only the discharging battery's rate is summed.
    #[test]
    fn a_charging_battery_does_not_add_to_system_draw() {
        // `Status[0]` is instance 2_0, which the output lists second.
        let mut json = two_pack_laptop();
        json["Status"][0]["Charging"] = json!(true);
        json["Status"][0]["Discharging"] = json!(false);
        json["Status"][0]["ChargeRate"] = json!(40000);
        json["Status"][0]["DischargeRate"] = json!(0);

        let batteries = batteries_from_wmi(&json);
        assert_eq!(batteries[0].status, Some(ChargingStatus::Discharging));
        assert_eq!(batteries[1].status, Some(ChargingStatus::Charging));

        let monitor = PowerSupplyMonitor {
            supplies: batteries,
        };
        assert_eq!(
            monitor.total_power_w(),
            Some(15.0),
            "only the discharging pack's 15 W is system draw, not the 40 W charge"
        );
    }

    #[test]
    fn a_desktop_with_no_battery_instances_has_no_batteries() {
        let desktop = json!({"Static": [], "Full": [], "Status": [], "Cycle": []});
        assert!(batteries_from_wmi(&desktop).is_empty());
        // A single object is serialised without the array, and still a list.
        let single = json!({
            "Static": {"InstanceName": "X", "DesignedCapacity": 1},
            "Full": [], "Status": [], "Cycle": []
        });
        assert_eq!(batteries_from_wmi(&single).len(), 1);
        assert!(batteries_from_wmi(&json!({})).is_empty());
    }

    #[test]
    fn unknown_markers_and_missing_rows_are_withheld_not_zeroed() {
        let json = json!({
            "Static": [{
                "InstanceName": "pack",
                "DesignedCapacity": 0,
                "ManufactureName": "",
                "SerialNumber": "",
                "Chemistry": [88, 89, 90, 0]
            }],
            "Full": [],
            "Status": [{
                "InstanceName": "pack",
                "RemainingCapacity": 4294967295u64,
                "Voltage": 4294967295u64,
                "ChargeRate": 0,
                "DischargeRate": 2147483648u64
            }],
            "Cycle": []
        });
        let b = &batteries_from_wmi(&json)[0];
        assert_eq!(b.energy_full_design_uwh, None, "0 is not a capacity");
        assert_eq!(b.energy_full_uwh, None, "no FullChargedCapacity row");
        assert_eq!(b.energy_now_uwh, None, "0xFFFFFFFF is unknown");
        assert_eq!(b.voltage_now_mv, None, "0xFFFFFFFF is unknown");
        assert_eq!(b.power_now_mw, None, "an unknown rate is not 0 W");
        assert_eq!(b.cycle_count, None, "no CycleCount row");
        assert_eq!(
            b.technology, None,
            "an unlisted chemistry code is not guessed"
        );
        assert_eq!(b.manufacturer, None);
        assert_eq!(b.serial_number, None);
    }

    #[test]
    fn the_rate_is_the_non_zero_direction_and_idle_is_zero() {
        let n = |v: u64| json!(v);
        assert_eq!(wmi_rate(Some(&n(8000)), Some(&n(0))), Some(8000));
        assert_eq!(wmi_rate(Some(&n(0)), Some(&n(3000))), Some(3000));
        assert_eq!(wmi_rate(Some(&n(0)), Some(&n(0))), Some(0), "idle on AC");
        assert_eq!(wmi_rate(None, None), None);
        assert_eq!(wmi_rate(Some(&n(0)), None), None);
    }

    #[test]
    fn a_status_battery_joins_the_one_wmi_battery_and_no_more() {
        let mut status = PowerSupplyInfo::new("Battery");
        status.supply_type = PowerSupplyType::Battery;
        status.capacity_percent = Some(73);
        status.status = Some(ChargingStatus::Discharging);
        status.time_to_empty_min = Some(190);

        let wmi = batteries_from_wmi(&two_pack_laptop());
        let one = combine_windows_batteries(Some(status.clone()), vec![wmi[0].clone()]);
        assert_eq!(one.len(), 1);
        assert_eq!(one[0].capacity_percent, Some(73));
        assert_eq!(one[0].energy_full_uwh, Some(51_234_000), "WMI fields kept");

        // Two packs: the combined percentage is not either pack's, so neither gets it.
        let two = combine_windows_batteries(Some(status.clone()), wmi.clone());
        assert_eq!(two.len(), 2);
        assert!(two.iter().all(|b| b.capacity_percent.is_none()));

        // No WMI battery: the status API's battery stands as it did before.
        let alone = combine_windows_batteries(Some(status), Vec::new());
        assert_eq!(alone.len(), 1);
        assert_eq!(alone[0].capacity_percent, Some(73));
        assert_eq!(alone[0].energy_full_uwh, None);

        assert!(combine_windows_batteries(None, Vec::new()).is_empty());
    }
}