-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemPowerViewModel.cs
More file actions
150 lines (124 loc) · 4.46 KB
/
Copy pathSystemPowerViewModel.cs
File metadata and controls
150 lines (124 loc) · 4.46 KB
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
using System;
using BatteryMonitor.Properties;
using Forms = System.Windows.Forms;
namespace BatteryMonitor
{
internal class SystemPowerViewModel : ViewModelBase
{
private string powerState;
private string chargeState;
private string powerLineStatus;
private string remainingTime;
private string capacity;
public string PowerState
{
get => powerState;
private set => SetProperty(ref powerState, value);
}
public string ChargeState
{
get => chargeState;
private set => SetProperty(ref chargeState, value);
}
public string PowerLineStatus
{
get => powerLineStatus;
private set => SetProperty(ref powerLineStatus, value);
}
public string RemainingTime
{
get => remainingTime;
private set => SetProperty(ref remainingTime, value);
}
public string Capacity
{
get => capacity;
private set => SetProperty(ref capacity, value);
}
public bool SetPowerStatus(Forms.PowerStatus status)
{
return SetPowerStatus(status.BatteryChargeStatus,
status.PowerLineStatus,
status.BatteryLifeRemaining,
status.BatteryLifePercent);
}
public bool SetPowerStatus(Forms.BatteryChargeStatus chargeStatus,
Forms.PowerLineStatus powerLineStatus,
int batteryLifeRemaining,
float batteryLifePercent)
{
PowerState = ConvertPowerState(chargeStatus);
ChargeState = ConvertChargeState(chargeStatus, powerLineStatus);
PowerLineStatus = ConvertPowerLineStatus(powerLineStatus);
if ((chargeStatus & Forms.BatteryChargeStatus.NoSystemBattery) != 0)
{
RemainingTime = string.Empty;
Capacity = string.Empty;
return false;
}
if (batteryLifeRemaining > 0)
{
RemainingTime = TimeSpan.FromSeconds(batteryLifeRemaining).ToString();
}
else
{
RemainingTime = string.Empty;
}
Capacity = (batteryLifePercent * 100.0).ToString("F0");
return true;
}
private string ConvertPowerLineStatus(Forms.PowerLineStatus powerLineStatus)
{
if (powerLineStatus == Forms.PowerLineStatus.Online)
{
return Resources.PowerLineStateOnline;
}
if (powerLineStatus == Forms.PowerLineStatus.Offline)
{
return Resources.PowerLineStateOffline;
}
return Resources.ChargeStatusUnknown;
}
private string ConvertPowerState(Forms.BatteryChargeStatus powerState)
{
if (powerState == Forms.BatteryChargeStatus.Unknown)
{
return Resources.ChargeStatusUnknown;
}
if ((powerState & Forms.BatteryChargeStatus.NoSystemBattery) != 0)
{
return string.Empty;
}
if ((powerState & Forms.BatteryChargeStatus.Low) != 0)
{
return Resources.ChargeStatusLow;
}
if ((powerState & Forms.BatteryChargeStatus.High) != 0)
{
return Resources.ChargeStatusHigh;
}
if ((powerState & Forms.BatteryChargeStatus.Critical) != 0)
{
return Resources.ChargeStatusCritical;
}
return Resources.ChargeStatusOk;
}
private string ConvertChargeState(Forms.BatteryChargeStatus powerState,
Forms.PowerLineStatus powerLineStatus)
{
if (powerState == Forms.BatteryChargeStatus.Unknown)
{
return Resources.ChargeStatusUnknown;
}
if ((powerState & Forms.BatteryChargeStatus.Charging) != 0)
{
return Resources.ChargeStatusCharging;
}
if (powerLineStatus == Forms.PowerLineStatus.Offline)
{
return Resources.PowerStateDischarging;
}
return Resources.PowerStateNotCharging;
}
}
}