jellyfin/MediaBrowser.Common/ScheduledTasks/SystemEventTrigger.cs

84 lines
2.7 KiB
C#
Raw Normal View History

2015-07-28 12:33:30 +00:00
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
2013-02-21 01:33:05 +00:00
using Microsoft.Win32;
2013-02-24 21:53:54 +00:00
using System;
2013-02-21 01:33:05 +00:00
using System.Threading.Tasks;
namespace MediaBrowser.Common.ScheduledTasks
{
/// <summary>
/// Class SystemEventTrigger
/// </summary>
2013-02-24 21:53:54 +00:00
public class SystemEventTrigger : ITaskTrigger
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the system event.
/// </summary>
/// <value>The system event.</value>
public SystemEvent SystemEvent { get; set; }
/// <summary>
/// Gets the execution properties of this task.
/// </summary>
/// <value>
/// The execution properties of this task.
/// </value>
public TaskExecutionOptions TaskOptions { get; set; }
2013-02-21 01:33:05 +00:00
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
2015-07-28 12:33:30 +00:00
/// <param name="lastResult">The last result.</param>
2013-02-23 07:57:11 +00:00
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
2015-07-28 12:33:30 +00:00
public void Start(TaskResult lastResult, bool isApplicationStartup)
2013-02-21 01:33:05 +00:00
{
switch (SystemEvent)
{
case SystemEvent.WakeFromSleep:
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
break;
}
}
/// <summary>
/// Stops waiting for the trigger action
/// </summary>
2013-02-24 21:53:54 +00:00
public void Stop()
2013-02-21 01:33:05 +00:00
{
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
}
/// <summary>
/// Handles the PowerModeChanged event of the SystemEvents control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PowerModeChangedEventArgs" /> instance containing the event data.</param>
async void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Resume && SystemEvent == SystemEvent.WakeFromSleep)
{
// This value is a bit arbitrary, but add a delay to help ensure network connections have been restored before running the task
2013-06-02 13:43:53 +00:00
await Task.Delay(10000).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
OnTriggered();
}
}
2013-02-24 21:53:54 +00:00
/// <summary>
/// Occurs when [triggered].
/// </summary>
public event EventHandler<GenericEventArgs<TaskExecutionOptions>> Triggered;
2013-02-24 21:53:54 +00:00
/// <summary>
/// Called when [triggered].
/// </summary>
private void OnTriggered()
{
if (Triggered != null)
{
Triggered(this, new GenericEventArgs<TaskExecutionOptions>(TaskOptions));
2013-02-24 21:53:54 +00:00
}
}
2013-02-21 01:33:05 +00:00
}
}