在 App.axaml.cs 的 StartServices() 方法中添加:
// 启用通知服务
var notificationService = NotificationService.Instance;
notificationService.IsEnabled = true;
notificationService.ThrottleSeconds = 60; // 同一报警至少间隔 60 秒
// 添加企业微信通知
var wechatWebhook = ConfigService.Instance.GetValue<string>("Notification:WeChatWorkWebhook");
if (!string.IsNullOrEmpty(wechatWebhook))
{
notificationService.AddChannel(new WeChatWorkNotification(wechatWebhook, new HttpClient()));
Log.Information("已启用企业微信告警通知");
}
// 添加钉钉通知
var dingtalkWebhook = ConfigService.Instance.GetValue<string>("Notification:DingTalkWebhook");
if (!string.IsNullOrEmpty(dingtalkWebhook))
{
notificationService.AddChannel(new DingTalkNotification(dingtalkWebhook, new HttpClient()));
Log.Information("已启用钉钉告警通知");
}
// 添加邮件通知
var emailConfig = ConfigService.Instance.GetSection("Notification:Email");
if (emailConfig != null)
{
var emailChannel = new EmailNotification(
emailConfig["SmtpServer"],
int.Parse(emailConfig["SmtpPort"] ?? "587"),
emailConfig["FromEmail"],
emailConfig["FromPassword"],
emailConfig["ToEmails"].Split(',')
);
notificationService.AddChannel(emailChannel);
Log.Information("已启用邮件告警通知");
}
{
"Notification": {
"Enabled": true,
"ThrottleSeconds": 60,
"NotifyOnAlarm": true,
"NotifyOnEscalation": true,
"NotifyOnPlcDisconnect": true,
"WeChatWorkWebhook": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY",
"DingTalkWebhook": "https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN",
"Email": {
"SmtpServer": "smtp.example.com",
"SmtpPort": "587",
"FromEmail": "alarm@example.com",
"FromPassword": "your_password",
"ToEmails": "admin@example.com,operator@example.com"
}
}
}
EscalationMinutes(默认 10 分钟)自动升级ThrottleSeconds(默认 60 秒)内只发送一次实现 INotificationChannel 接口:
public class MyCustomNotification : INotificationChannel
{
public string Name => "MyCustom";
public async Task SendAsync(string title, string message, NotificationLevel level)
{
// 实现你的通知逻辑
// 例如:SMS、Slack、Telegram 等
}
}