using NEIntelligentControl2.Models.Windturbine;
using System;
using System.Collections.Generic;
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.Matrix
{
///
/// 关注风机
///
public partial class FollowedMatrix : UserControl
{
private bool _IsActive; // 此页面是否活跃
private Dictionary _BlockPairs; // 数据集合
public FollowedMatrix()
{
InitializeComponent();
_BlockPairs = new Dictionary();
}
///
/// 更新数据
///
internal void UpdateData(Dictionary vs)
{
try
{
Dispatcher.Invoke(() =>
{
foreach (var v in _BlockPairs)
{
if (!vs.ContainsKey(v.Key)) continue;
v.Value.Update(vs[v.Key]);
}
});
}
catch { }
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
_IsActive = true;
Task.Factory.StartNew(RefreshData, TaskCreationOptions.LongRunning);
}
///
/// 刷新数据
///
private async void RefreshData()
{
while (_IsActive)
{
try
{
Refresh();
await Task.Delay(3000);
}
catch { }
}
}
///
/// 刷新数据
///
private void Refresh()
{
try
{
/*var vs = RestfulClient.GetWindturbineFollows();
if (vs == null) return;
HashSet set = new HashSet();
foreach (var v in vs)
{
set.Add(v.WindturbineID);
if (!_BlockPairs.ContainsKey(v.WindturbineID))
{
this.Dispatcher.Invoke(() =>
{
BlockSmallWPF bs = new BlockSmallWPF();
_BlockPairs.Add(v.WindturbineID, bs);
_UGMain.Children.Add(bs);
});
}
}
List ll = new List();
foreach (var v in _BlockPairs)
{
if (v.Value.WindturbineInfo != null && !set.Contains(v.Value.WindturbineInfo.WindturbineId))
{
ll.Add(v.Value);
}
}
this.Dispatcher.Invoke(() =>
{
foreach (var v in ll)
{
_BlockPairs.Remove(v.WindturbineInfo.WindturbineId);
_UGMain.Children.Remove(v);
}
});*/
}
catch { }
}
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
_IsActive = false;
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
switch (((Control)sender).Tag)
{
case "removefollow":// 取消关注
RemoveFollow();
break;
}
}
///
/// 取消关注
///
private void RemoveFollow()
{
try
{
var ls = GetSelectedIds();
//var s = ls.GetString(v => v.WindturbineInfo.WindturbineId);
//RestfulClient.RemoveWindturbineFollows(s);
Refresh();
}
catch (Exception e)
{
Console.WriteLine($"取消关注出现错误:{e.Message}");
}
}
///
/// 获取被选中的风机
///
///
private List GetSelectedIds()
{
List ls = new List();
foreach (var v in _BlockPairs)
{
if (v.Value.IsSelected)
{
ls.Add(v.Value);
}
}
return ls;
}
}
}