blob: 2eb5bebb38f0b4cfc060b475934a48cc275b80be (
plain)
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
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SystemService.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// Monitor the system health for common problems that will directly impact encodes.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Services
{
using System;
using System.Timers;
using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Encode.Interfaces;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.Services.Logging.Interfaces;
using HandBrakeWPF.Utilities;
public class SystemService : ISystemService
{
private readonly IUserSettingService userSettingService;
private readonly IEncode encodeService;
private readonly ILog log = null;
private Timer pollTimer;
private bool criticalStateHit = false;
private bool lowStateHit = false;
private bool lowPowerPause = false;
private bool storageLowPause = false;
public SystemService(IUserSettingService userSettingService, IEncode encodeService, ILog logService)
{
this.log = logService;
this.userSettingService = userSettingService;
this.encodeService = encodeService;
}
public void Start()
{
if (this.pollTimer == null)
{
this.pollTimer = new Timer();
this.pollTimer.Interval = 10000; // Check every 10 seconds.
this.pollTimer.Elapsed += (o, e) =>
{
this.CheckSystem();
};
this.pollTimer.Start();
}
}
private void CheckSystem()
{
this.PowerCheck();
this.StorageCheck();
}
private void StorageCheck()
{
string directory = this.encodeService.GetActiveJob()?.Destination;
if (!string.IsNullOrEmpty(directory) && this.encodeService.IsEncoding)
{
long lowLevel = this.userSettingService.GetUserSetting<long>(UserSettingConstants.PauseEncodeOnLowDiskspaceLevel);
if (!this.storageLowPause && this.userSettingService.GetUserSetting<bool>(UserSettingConstants.PauseOnLowDiskspace) && !DriveUtilities.HasMinimumDiskSpace(directory, lowLevel))
{
this.log.LogMessage(
string.Format(
Resources.SystemService_LowDiskSpaceLog,
lowLevel / 1000 / 1000 / 1000));
this.encodeService.Pause();
this.storageLowPause = true;
}
}
}
private void PowerCheck()
{
Win32.PowerState state = Win32.PowerState.GetPowerState();
if (state == null || state.BatteryFlag == Win32.BatteryFlag.NoSystemBattery || state.BatteryFlag == Win32.BatteryFlag.Unknown)
{
return; // Only run if we have a battery.
}
if (state.ACLineStatus == Win32.ACLineStatus.Offline && state.BatteryFlag == Win32.BatteryFlag.Low && !this.lowStateHit)
{
if (this.encodeService.IsEncoding && !this.encodeService.IsPasued)
{
this.lowPowerPause = true;
this.encodeService.Pause();
}
Win32.AllowSleep();
this.ServiceLogMessage(string.Format(Resources.SystemService_LowBatteryLog, state.BatteryLifePercent));
this.lowStateHit = true;
}
if (state.ACLineStatus == Win32.ACLineStatus.Offline && state.BatteryFlag == Win32.BatteryFlag.Critical && !this.criticalStateHit)
{
if (this.encodeService.IsEncoding && !this.encodeService.IsPasued)
{
this.lowPowerPause = true;
this.encodeService.Pause(); // In case we missed the low state!
}
Win32.AllowSleep();
this.ServiceLogMessage(string.Format(Resources.SystemService_CriticalBattery, state.BatteryLifePercent));
this.criticalStateHit = true;
}
// Reset the flags when we start charging.
if (state.ACLineStatus == Win32.ACLineStatus.Online && state.BatteryFlag >= Win32.BatteryFlag.Low)
{
if (this.lowPowerPause && this.encodeService.IsPasued)
{
this.encodeService.Resume();
this.ServiceLogMessage(string.Format(Resources.SystemService_ACMains, state.BatteryLifePercent));
}
this.lowPowerPause = false;
this.criticalStateHit = false;
this.lowStateHit = false;
}
}
private void ServiceLogMessage(string message)
{
this.log.LogMessage(string.Format("{0}# {1}{0}", Environment.NewLine, message));
}
}
}
|