using NEIntelligentControl2.Models.Alarm;
using NEIntelligentControl2.Service.Alarm;
using NEIntelligentControl2.Windows.Alarm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 RealTimeFalutInfoView : UserControl
{
// ------------依赖属性--------------
public static DependencyProperty IsFixedProperty = DependencyProperty.Register("IsFixed", typeof(bool), typeof(RealTimeFalutInfoView));
public static DependencyProperty IsFaultProperty = DependencyProperty.Register("IsFault", typeof(bool), typeof(RealTimeFalutInfoView));
public static DependencyProperty IsOpenedProperty = DependencyProperty.Register("IsOpened", typeof(bool), typeof(RealTimeFalutInfoView));
///
/// 是否固定
///
public bool IsFixed { get => (bool)GetValue(IsFixedProperty); set => SetValue(IsFixedProperty, value); }
///
/// 是否是故障
///
public bool IsFault { get => (bool)GetValue(IsFaultProperty); set => SetValue(IsFaultProperty, value); }
///
/// 是否打开
///
public bool IsOpened { get => (bool)GetValue(IsOpenedProperty); set => SetValue(IsOpenedProperty, value); }
private ObservableCollection _FaultList;
private HashSet _FaultIds;
private ObservableCollection _AlarmList;
private HashSet _AlarmIds;
private AlarmManager _AlarmManager;
public RealTimeFalutInfoView()
{
InitializeComponent();
IsFault = true;
_FaultList = new ObservableCollection();
_GDFault.ItemsSource = _FaultList;
_AlarmList = new ObservableCollection();
_GDAlarm.ItemsSource = _AlarmList;
_AlarmIds = new HashSet();
_FaultIds = new HashSet();
_AlarmManager = App.ServiceProvider.GetService(typeof(AlarmManager)) as AlarmManager;
_AlarmManager.OnFaultCount += OnFaultCount;
_AlarmManager.OnAlarmCount += OnAlarmCount;
}
private void OnAlarmCount(List obj)
{
var ls = obj.Select(f => f.Id).ToList();
var removed = _AlarmList.Where(f => !ls.Contains(f.Id)).ToList();
Dispatcher.Invoke(() =>
{
for (int i = obj.Count - 1; i >= 0; i--)
{
var v = obj[i];
if (_AlarmIds.Contains(v.Id)) continue;
_AlarmList.Insert(0, v);
_AlarmIds.Add(v.Id);
}
foreach (var v in removed)
{
_AlarmList.Remove(v);
_AlarmIds.Remove(v.Id);
}
});
}
private void OnFaultCount(List obj)
{
var ls = obj.Select(f => f.FaultInfo.Id).ToList();
var removed = _FaultList.Where(f => !ls.Contains(f.FaultInfo.Id)).ToList();
Dispatcher.Invoke(() =>
{
for (int i = obj.Count - 1; i >= 0; i--)
{
var v = obj[i];
if (_FaultIds.Contains(v.FaultInfo.Id)) continue;
_FaultList.Insert(0, v);
_FaultIds.Add(v.FaultInfo.Id);
}
foreach (var v in removed)
{
_FaultList.Remove(v);
_FaultIds.Remove(v.FaultInfo.Id);
}
});
}
private void cc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
IsFixed = !IsFixed;
}
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var v = ((FrameworkElement)sender).DataContext as FaultInfoModel;
if (v == null || v.FaultInfo == null) return;
Windows.Alarm.DeviceDetailAlarmWindow.Show(v.FaultInfo);
IsOpened = false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
switch (((Control)sender).Tag)
{
case "fault": // 报警故障切换
IsFault = !IsFault;
break;
case "rtalarm":// 实时报警
RealTimeAlarm();
break;
case "hisalarm": // 历史报警
HistoryAlarm();
break;
case "hisfault": // 历史故障
HistoryFault();
break;
default:return;
}
}
///
/// 历史故障
///
private void HistoryFault()
{
HistoryFaultWindow.ShowWindow();
}
///
/// 历史报警
///
private void HistoryAlarm()
{
HistoryAlarmWindow.ShowWindow();
}
///
/// 实时报警
///
private void RealTimeAlarm()
{
RealTimeAlarmWindow.ShowWindow();
}
}
}