using NEIntelligentControl2.Models.Alarm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace NEIntelligentControl2.Views.Alarm { /// /// 报警首页卡片 /// public partial class AlarmCard : UserControl { public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(AlarmCard)); /// /// 标题 /// public string Title { get => GetValue(TitleProperty) as string; set => SetValue(TitleProperty, value); } /// /// 是否是风机 /// public bool IsWindtubine { get; set; } private ObservableCollection _List; // 报警集合 private HashSet _AlarmIDs; // 报警ID集合 public AlarmCard() { InitializeComponent(); _List = new ObservableCollection(); _DGMain.ItemsSource = _List; _AlarmIDs = new HashSet(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { if (!IsWindtubine) { _DGMain.Columns[1].Visibility = Visibility.Collapsed; } } /// /// 添加升压站历史报警 /// internal void UpdateAlarmData(AlarmPage ap) { if (ap == null || ap.Records == null) return; var vs = ap.Records.ToDictionary(v => v.Id, v => v); Dispatcher.Invoke(() => { if (_List.Count > 100) return; foreach (var v in ap.Records) { var ai = AlarmInfos.GetAlarmInfo(v); if (_AlarmIDs.Contains(ai.Feature)) continue; _AlarmIDs.Add(ai.Feature); _List.Add(ai); } }); } /// /// 更新报警列表 /// /// 报警数据 internal void UpdateAlarmData(FaultInfo fi) { AlarmInfos ai = AlarmInfos.GetAlarmInfo(fi); if (_AlarmIDs.Contains(ai.Feature)) return; AddData(ai); } /// /// 更新报警列表 /// /// 数据 internal void UpdateAlarmData(AlarmInfo v) { AlarmInfos ai = AlarmInfos.GetAlarmInfo(v); if (_AlarmIDs.Contains(ai.Feature)) return; AddData(ai); } /// /// 将数据添加到列表中 /// /// 报警数据 private void AddData(AlarmInfos ai) { Dispatcher.Invoke(() => { _List.Insert(0, ai); _AlarmIDs.Add(ai.Feature); if (_List.Count > 100) { var v = _List.Last(); if (_AlarmIDs.Count > 5000) _AlarmIDs.Remove(v.Feature); _List.Remove(v); } }); } } /// /// 报警信息 /// public class AlarmInfos : INotifyPropertyChanged { /// /// ID /// public long ID { get; set; } /// /// 报警时间 /// public string Time { get; set; } /// /// 报警设备名称 /// public string Title { get; set; } private string message; /// /// 报警信息 /// public string Message { get => message; set { message = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Message")); } } /// /// 报警级别 /// public string Rank { get; set; } /// /// 报警类型 /// public string AlarmType { get; set; } private string feature; /// /// 报警特征 /// public string Feature { get => feature ?? (feature = $"{Time}{Title}{Message}{AlarmType}"); } public event PropertyChangedEventHandler PropertyChanged; internal static AlarmInfos GetAlarmInfo(AlarmInfo value) { AlarmInfos ai = new AlarmInfos() { ID = value.Id, Time = value.LastUpdateTime != null ? value.LastUpdateTime?.ToString("yyyy-MM-dd HH:mm:ss") : value.AlertTime?.ToString("yyyy-MM-dd HH:mm:ss"), Title = value.WindturbineName, Message = value.AlertText, Rank = value.Rank, AlarmType = value.MessageType == 1 ? "触发" : "解除" }; return ai; } /// /// 获取报警数据 /// internal static AlarmInfos GetAlarmInfo(FaultInfo fi) { AlarmInfos ai = new AlarmInfos() { ID = fi.Id, Time = fi.FaultTime?.ToString("yyyy-MM-dd HH:mm:ss"), Title = fi.WindturbineName, Message = fi.AlertText, Rank = fi.Rank, AlarmType = fi.MessageType == 1 ? "触发" : "解除" }; return ai; } } }