using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Aitex.Core.RT.IOCore;
using Aitex.Core.UI.MVVM;
using MECF.Framework.Common.IOCore;
namespace VirgoRT.Backends
{
///
/// IOView.xaml 的交互逻辑
///
public partial class IOView : UserControl
{
public IOView()
{
InitializeComponent();
DataContext = new IOMonitorViewModel();
this.IsVisibleChanged += IOView_IsVisibleChanged;
}
private void IOView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
(DataContext as TimerViewModelBase).EnableTimer(IsVisible);
}
}
public class IoValues
{
public ObservableCollection DiItemList { get; set; }
public ObservableCollection DoItemList { get; set; }
public ObservableCollection AiItemList { get; set; }
public ObservableCollection AoItemList { get; set; }
#region InitDefine
public IoValues(string source)
{
DiItemList = new ObservableCollection();
List diItems = IO.GetDiList(source);
if (diItems != null)
{
foreach (var diItem in diItems)
{
NotifiableIoItem item = new NotifiableIoItem()
{
Name = diItem.Name,
Index = diItem.Index,
Description = diItem.Description,
BoolValue = diItem.Value,
Address = diItem.Addr,
};
DiItemList.Add(item);
}
}
DoItemList = new ObservableCollection();
List doItems = IO.GetDoList(source);
if (doItems != null)
{
foreach (var doItem in doItems)
{
NotifiableIoItem item = new NotifiableIoItem()
{
Name = doItem.Name,
Index = doItem.Index,
Description = doItem.Description,
BoolValue = doItem.Value,
Address = doItem.Addr,
};
DoItemList.Add(item);
}
}
AiItemList = new ObservableCollection();
List aiItems = IO.GetAiList(source);
if (aiItems != null)
{
foreach (var aiItem in aiItems)
{
NotifiableIoItem item = new NotifiableIoItem()
{
Name = aiItem.Name,
Index = aiItem.Index,
Description = aiItem.Description,
ShortValue = aiItem.Value,
Address = aiItem.Addr,
};
AiItemList.Add(item);
}
}
AoItemList = new ObservableCollection();
List aoItems = IO.GetAoList(source);
if (aoItems != null)
{
foreach (var aoItem in aoItems)
{
NotifiableIoItem item = new NotifiableIoItem()
{
Name = aoItem.Name,
Index = aoItem.Index,
Description = aoItem.Description,
ShortValue = aoItem.Value,
Address = aoItem.Addr,
};
AoItemList.Add(item);
}
}
}
#endregion
public void UpdateData()
{
foreach (var item in DiItemList)
{
item.BoolValue = IO.DI[item.Name].Value;
item.InvokePropertyChanged("BoolValue");
}
foreach (var item in DoItemList)
{
item.BoolValue = IO.DO[item.Name].Value;
}
foreach (var item in AiItemList)
{
item.ShortValue = IO.AI[item.Name].Value;
}
foreach (var item in AoItemList)
{
item.ShortValue = IO.AO[item.Name].Value;
}
}
}
public class IOMonitorViewModel : TimerViewModelBase
{
public IoValues[] IoList { get; set; }
public IOMonitorViewModel():base(nameof(IOMonitorViewModel))
{
IoList = new IoValues[2];
for (int i = 0; i < IoList.Length; i++)
{
IoList[i] = new IoValues("System.io"+(i+1));
}
}
protected override void Poll()
{
for (int i = 0; i < IoList.Length; i++)
{
IoList[i].UpdateData();
}
//string[] values = new string[128];
//int i = 0;
//foreach (var notifiableIoItem in Card1.DiItemList)
//{
// notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value;
// notifiableIoItem.InvokePropertyChanged(nameof(notifiableIoItem.BoolValue));
// values[i++] = notifiableIoItem.BoolValue ? "1" : "0";
//}
//System.Diagnostics.Trace.WriteLine(string.Join(",", values));
}
}
}