using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.IOCore; using MECF.Framework.Common.OperationCenter; using OpenSEMI.ClientBase; using OpenSEMI.ClientBase.IO; namespace VirgoUI.Client.Models.PMs { public class AOItem32 : IOItem { public static readonly DependencyProperty NewValueProperty = DependencyProperty.Register(nameof(NewValue), typeof(float), typeof(AOItem32), new PropertyMetadata((object)(float)0)); public static readonly DependencyProperty TextSavedProperty = DependencyProperty.Register(nameof(TextSaved), typeof(bool), typeof(AOItem32), new PropertyMetadata((object)true)); public float NewValue { get { return (float)this.GetValue( NewValueProperty); } set { this.SetValue( NewValueProperty, (object)value); } } public bool TextSaved { get { return (bool)this.GetValue( TextSavedProperty); } set { this.SetValue( TextSavedProperty, (object)value); } } } public class PmIoViewModel : BaseModel, ISupportMultipleSystem { private int MenuPermission; private bool _TwoColumnMode; public bool TwoColumnMode { get { return _TwoColumnMode; } set { _TwoColumnMode = value; NotifyOfPropertyChange("TwoColumnMode"); } } public string SystemName { get; set; } public ObservableCollection> AIs { get; private set; } public ObservableCollection AOs { get; private set; } public ObservableCollection> DIs { get; private set; } public ObservableCollection> DOs { get; private set; } private Dictionary> _diMap = new Dictionary>(); private Dictionary> _doMap = new Dictionary>(); private Dictionary> _aiMap = new Dictionary>(); private Dictionary _aoMap = new Dictionary(); protected override void OnInitialize() { base.OnInitialize(); TwoColumnMode = true; MenuPermission = ClientApp.Instance.GetPermission($"IO{SystemName}"); BuildIoSchema(); } protected override void OnActivate() { base.OnActivate(); BaseApp.Instance.UIHandler.Register(SystemName + "IOView", GetIOs); } protected override void OnDeactivate(bool close) { base.OnDeactivate(close); BaseApp.Instance.UIHandler.UnRegister(SystemName + "IOView"); } public void SetAO(AOItem32 aoItem) { if (MenuPermission != 3) return; InvokeClient.Instance.Service.DoOperation("System.SetAoValue32", aoItem.Name, aoItem.NewValue); } public void SetDO(IOItem doItem) { InvokeClient.Instance.Service.DoOperation("System.SetDoValue", doItem.Name, !doItem.Value); } public void BuildIoSchema() { if (DIs == null) { var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.DIItemList"); if (itemList != null) { DIs = new ObservableCollection>(); foreach (var item in (List)itemList) { var io = new IOItem() { Index = item.Index, Name = item.Name, Value = item.BoolValue, Address = item.Address }; DIs.Add(io); _diMap.Add($"IO.{item.Name}", io); } } NotifyOfPropertyChange(nameof(DIs)); } if (DOs == null) { var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.DOItemList"); if (itemList != null) { DOs = new ObservableCollection>(); foreach (var item in (List)itemList) { var io = new IOItem() { Index = item.Index, Name = item.Name, Value = item.BoolValue, Address = item.Address }; DOs.Add(io); _doMap.Add($"IO.{item.Name}", io); } } NotifyOfPropertyChange(nameof(DOs)); } if (AIs == null) { var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.AIItemList"); if (itemList != null) { AIs = new ObservableCollection>(); foreach (var item in (List)itemList) { var io = new IOItem() { Index = item.Index, Name = item.Name, Value = item.ShortValue, Address = item.Address }; AIs.Add(io); _aiMap.Add($"IO32.{item.Name}", io); } } NotifyOfPropertyChange(nameof(AIs)); } if (AOs == null) { var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.AOItemList"); if (itemList != null) { AOs = new ObservableCollection(); foreach (var item in (List)itemList) { var io = new AOItem32() { Index = item.Index, Name = item.Name, Value = item.ShortValue, Address = item.Address }; AOs.Add(io); _aoMap.Add($"IO32.{item.Name}", io); } } NotifyOfPropertyChange(nameof(AOs)); } } public void GetIOs() { var diValues = QueryDataClient.Instance.Service.PollData(_diMap.Keys); foreach (var item in _diMap) { if (diValues.ContainsKey(item.Key)) _diMap[item.Key].Value = (bool)diValues[item.Key]; } var doValues = QueryDataClient.Instance.Service.PollData(_doMap.Keys); foreach (var item in _doMap) { if (doValues.ContainsKey(item.Key)) _doMap[item.Key].Value = (bool)doValues[item.Key]; } var aiValues = QueryDataClient.Instance.Service.PollData(_aiMap.Keys); foreach (var item in _aiMap) { if (aiValues.ContainsKey(item.Key)) _aiMap[item.Key].Value = (float)aiValues[item.Key]; } var aoValues = QueryDataClient.Instance.Service.PollData(_aoMap.Keys); foreach (var item in _aoMap) { if (aoValues.ContainsKey(item.Key)) _aoMap[item.Key].Value = (float)aoValues[item.Key]; } } } }