Repository navigation
Expand file tree
/
Copy pathPlugin.cs
More file actions
130 lines (113 loc) · 3.79 KB
/
Copy pathPlugin.cs
File metadata and controls
130 lines (113 loc) · 3.79 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
using System.IO;
using System.Threading;
using Avalonia.Threading;
using ClassIsland.Core;
using ClassIsland.Core.Abstractions;
using ClassIsland.Core.Attributes;
using ClassIsland.Core.Extensions.Registry;
using ClassIsland.Shared.Helpers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using cn.lixiaotuan.notifyisland.Controls;
using cn.lixiaotuan.notifyisland.Models;
using cn.lixiaotuan.notifyisland.Services;
using cn.lixiaotuan.notifyisland.Services.NotificationProviders;
using cn.lixiaotuan.notifyisland.Views;
namespace cn.lixiaotuan.notifyisland;
[PluginEntrance]
public class Plugin : PluginBase
{
private const int RestartDebounceMilliseconds = 400;
private readonly object _restartLock = new();
private Timer? _restartTimer;
/// <summary>
/// 插件设置。
/// </summary>
public PluginSettings Settings { get; private set; } = new();
/// <summary>
/// HTTP 服务运行状态。
/// </summary>
public ServerStatus Status { get; } = new();
/// <summary>
/// HTTP 文本组件注册表。
/// </summary>
public HttpTextComponentRegistry Registry { get; } = new();
/// <summary>
/// HTTP 服务实例。
/// </summary>
public PluginHttpServer Server { get; }
public Plugin()
{
Server = new PluginHttpServer(Registry, Status);
}
public override void Initialize(HostBuilderContext context, IServiceCollection services)
{
var settingsPath = Path.Combine(PluginConfigFolder, "settings.json");
Settings = ConfigureFileHelper.LoadConfig<PluginSettings>(settingsPath);
Settings.PropertyChanged += (_, _) =>
ConfigureFileHelper.SaveConfig(settingsPath, Settings);
services.AddSingleton(Settings);
services.AddSingleton(Status);
services.AddSingleton(Registry);
services.AddSingleton(Server);
services.AddSingleton(this);
services.AddNotificationProvider<ApiNotificationProvider>();
services.AddComponent<HttpTextComponent, HttpTextComponentSettingsControl>();
services.AddSettingsPage<NotifyIslandSettingsPage>();
AppBase.Current.AppStarted += OnAppStarted;
AppBase.Current.AppStopping += OnAppStopping;
Settings.PropertyChanged += OnSettingsChanged;
}
private void OnAppStarted(object? sender, EventArgs e)
{
ApplyServerSettings();
}
private void OnAppStopping(object? sender, EventArgs e)
{
Settings.PropertyChanged -= OnSettingsChanged;
_restartTimer?.Dispose();
_restartTimer = null;
Server.Dispose();
}
private void OnSettingsChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(PluginSettings.Enabled):
case nameof(PluginSettings.Host):
case nameof(PluginSettings.Port):
case nameof(PluginSettings.Token):
case nameof(PluginSettings.TextApiRequireToken):
ScheduleServerRestart();
break;
}
}
private void ScheduleServerRestart()
{
lock (_restartLock)
{
_restartTimer?.Dispose();
_restartTimer = new Timer(_ =>
Dispatcher.UIThread.Post(ApplyServerSettings), null,
RestartDebounceMilliseconds, Timeout.Infinite);
}
}
private void ApplyServerSettings()
{
try
{
if (Settings.Enabled)
{
Server.Start(Settings.Host, Settings.Port, Settings.Token, Settings.TextApiRequireToken);
}
else
{
Server.Stop();
}
}
catch (Exception ex)
{
Status.LastError = ex.Message;
}
}
}