Repository navigation
Expand file tree
/
Copy pathTimerPlugin.cs
More file actions
53 lines (42 loc) · 1.46 KB
/
Copy pathTimerPlugin.cs
File metadata and controls
53 lines (42 loc) · 1.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ThinkingHome.Core.Plugins;
using ThinkingHome.Core.Plugins.Utils;
namespace ThinkingHome.Plugins.Timer
{
public class TimerPlugin : PluginBase
{
#region fields
private static readonly Random random = new();
private readonly List<InternalTimer> timers = new();
#endregion
public override void InitPlugin()
{
var callbacks = Context.GetAllPlugins()
.SelectMany(plugin =>
plugin.FindMethods<TimerCallbackAttribute, TimerCallbackDelegate>());
foreach (var callback in callbacks)
{
var info = callback.Method.GetMethodInfo();
Logger.LogInformation("Register timer callback {Name} for {TypeName}", info.Name, info.DeclaringType?.FullName);
var timer = new InternalTimer(
callback.Meta.Delay ?? random.Next(callback.Meta.Interval),
callback.Meta.Interval,
callback.Method, Logger);
timers.Add(timer);
}
}
public override void StartPlugin()
{
timers.ForEach(timer => timer.Start());
}
public override void StopPlugin()
{
timers.ForEach(timer => timer.Dispose());
}
}
}