Initial commit to new repository
This commit is contained in:
49
src/AxCopilot/Services/SessionTrackingService.cs
Normal file
49
src/AxCopilot/Services/SessionTrackingService.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace AxCopilot.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Windows 세션 잠금/해제 이벤트를 추적하여 PC 활성 시간을 기록합니다.
|
||||
/// </summary>
|
||||
public sealed class SessionTrackingService : IDisposable
|
||||
{
|
||||
private DateTime? _unlockTime;
|
||||
private bool _disposed;
|
||||
|
||||
public SessionTrackingService()
|
||||
{
|
||||
// 앱 시작 = 잠금 해제 상태로 간주
|
||||
_unlockTime = DateTime.Now;
|
||||
SystemEvents.SessionSwitch += OnSessionSwitch;
|
||||
}
|
||||
|
||||
private void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
|
||||
{
|
||||
if (e.Reason == SessionSwitchReason.SessionLock)
|
||||
{
|
||||
FlushActiveTime();
|
||||
}
|
||||
else if (e.Reason == SessionSwitchReason.SessionUnlock ||
|
||||
e.Reason == SessionSwitchReason.SessionLogon)
|
||||
{
|
||||
_unlockTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
private void FlushActiveTime()
|
||||
{
|
||||
if (!_unlockTime.HasValue) return;
|
||||
var seconds = (int)(DateTime.Now - _unlockTime.Value).TotalSeconds;
|
||||
if (seconds > 0)
|
||||
UsageStatisticsService.AddActiveSeconds(seconds);
|
||||
_unlockTime = null;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
FlushActiveTime();
|
||||
SystemEvents.SessionSwitch -= OnSessionSwitch;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user