# 远程告警通知配置示例 ## 启用通知服务 在 `App.axaml.cs` 的 `StartServices()` 方法中添加: ```csharp // 启用通知服务 var notificationService = NotificationService.Instance; notificationService.IsEnabled = true; notificationService.ThrottleSeconds = 60; // 同一报警至少间隔 60 秒 // 添加企业微信通知 var wechatWebhook = ConfigService.Instance.GetValue("Notification:WeChatWorkWebhook"); if (!string.IsNullOrEmpty(wechatWebhook)) { notificationService.AddChannel(new WeChatWorkNotification(wechatWebhook, new HttpClient())); Log.Information("已启用企业微信告警通知"); } // 添加钉钉通知 var dingtalkWebhook = ConfigService.Instance.GetValue("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("已启用邮件告警通知"); } ``` ## 配置文件示例(yzwater-config.json) ```json { "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" } } } ``` ## 通知触发场景 1. **新报警触发**:当液位、流量、设备故障等报警触发时 2. **报警升级**:未确认报警超过 `EscalationMinutes`(默认 10 分钟)自动升级 3. **PLC 连接断开**:PLC 通信中断,进入降级模式 4. **PLC 连接恢复**:PLC 通信恢复正常 ## 通知限流 - 同一报警在 `ThrottleSeconds`(默认 60 秒)内只发送一次 - 防止报警风暴导致通知泛滥 ## 自定义通知渠道 实现 `INotificationChannel` 接口: ```csharp public class MyCustomNotification : INotificationChannel { public string Name => "MyCustom"; public async Task SendAsync(string title, string message, NotificationLevel level) { // 实现你的通知逻辑 // 例如:SMS、Slack、Telegram 等 } } ```