123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using MECF.Framework.Common.DataCenter;
- using MECF.Framework.Common.IOCore;
- using MECF.Framework.Common.OperationCenter;
- using OpenSEMI.ClientBase.IO;
- using Prism.Commands;
- using Prism.Mvvm;
- 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.Threading;
- namespace CyberX8_MainPages.ViewModels
- {
- public class IOViewModel : BindableBase
- {
- #region 私有属性
- public string ModuleName;
- private Dictionary<string, IOItem<bool>> _diMap = new Dictionary<string, IOItem<bool>>();
- private Dictionary<string, IOItem<bool>> _doMap = new Dictionary<string, IOItem<bool>>();
- private Dictionary<string, IOItem<float>> _aiMap = new Dictionary<string, IOItem<float>>();
- private Dictionary<string, AOItem32> _aoMap = new Dictionary<string, AOItem32>();
- private ObservableCollection<IOItem<float>> m_AIs = new ObservableCollection<IOItem<float>>();
- private ObservableCollection<AOItem32> m_AOs = new ObservableCollection<AOItem32>();
- private ObservableCollection<IOItem<bool>> m_DIs = new ObservableCollection<IOItem<bool>>();
- private ObservableCollection<IOItem<bool>> m_DOs = new ObservableCollection<IOItem<bool>>();
-
- #endregion
- #region 属性
- public ObservableCollection<IOItem<float>> AIs
- {
- get { return m_AIs; }
- set { SetProperty(ref m_AIs, value); }
- }
- public ObservableCollection<AOItem32> AOs
- {
- get { return m_AOs; }
- set { SetProperty(ref m_AOs, value); }
- }
- public ObservableCollection<IOItem<bool>> DIs
- {
- get { return m_DIs; }
- set { SetProperty(ref m_DIs, value); }
- }
- public ObservableCollection<IOItem<bool>> DOs
- {
- get { return m_DOs; }
- set { SetProperty(ref m_DOs, value); }
- }
- #endregion
- #region 命令
- private DelegateCommand<IOItem<bool>> _SetDOCommand;
- public DelegateCommand<IOItem<bool>> SetDOCommand =>
- _SetDOCommand ?? (_SetDOCommand = new DelegateCommand<IOItem<bool>>(SetDO));
- private DelegateCommand<AOItem32> _SetAOCommand;
- public DelegateCommand<AOItem32> SetAOCommand =>
- _SetAOCommand ?? (_SetAOCommand = new DelegateCommand<AOItem32>(SetAO));
- //private DelegateCommand _SetDOCommand;
- //public DelegateCommand SetDOCommand =>
- // _SetDOCommand ?? (_SetDOCommand = new DelegateCommand(SetDO));
- #endregion
- public IOViewModel()
- {
- DispatcherTimer timer = new DispatcherTimer();
- timer.Interval = TimeSpan.FromSeconds(2);
- timer.Tick += timer_Tick;
- timer.Start();
- BuildIoSchema();
- }
- void timer_Tick(object sender, EventArgs e)
- {
- GetIOs();
- }
- #region 私有方法
- public void SetAO(AOItem32 aoItem)
- {
- InvokeClient.Instance.Service.DoOperation("System.SetAoValue32", aoItem.Name, aoItem.NewValue);
- }
- private void SetDO(IOItem<bool> doItem)
- {
- InvokeClient.Instance.Service.DoOperation("System.SetDoValue", doItem.Name, !doItem.Value);
- }
- public void BuildIoSchema()
- {
- if (DIs.Count == 0)
- {
- var itemList = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PLC.DIItemList");
- if (itemList != null)
- {
- DIs = new ObservableCollection<IOItem<bool>>();
- foreach (var item in (List<NotifiableIoItem>)itemList)
- {
- var io = new IOItem<bool>()
- {
- Index = item.Index,
- Name = item.Name,
- Value = item.BoolValue,
- Address = item.Address
- };
- DIs.Add(io);
- _diMap.Add($"IO.{item.Name}", io);
- }
- }
- }
- if (DOs.Count == 0)
- {
- var itemList = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PLC.DOItemList");
- if (itemList != null)
- {
- DOs = new ObservableCollection<IOItem<bool>>();
- foreach (var item in (List<NotifiableIoItem>)itemList)
- {
- var io = new IOItem<bool>()
- {
- Index = item.Index,
- Name = item.Name,
- Value = item.BoolValue,
- Address = item.Address
- };
- DOs.Add(io);
- _doMap.Add($"IO.{item.Name}", io);
- }
- }
- }
- if (AIs.Count == 0)
- {
- var itemList = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PLC.AIItemList");
- if (itemList != null)
- {
- AIs = new ObservableCollection<IOItem<float>>();
- foreach (var item in (List<NotifiableIoItem>)itemList)
- {
- var io = new IOItem<float>()
- {
- Index = item.Index,
- Name = item.Name,
- Value = item.ShortValue,
- Address = item.Address
- };
- AIs.Add(io);
- _aiMap.Add($"IO32.{item.Name}", io);
- }
- }
- }
- if (AOs.Count == 0)
- {
- var itemList = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PLC.AOItemList");
- if (itemList != null)
- {
- AOs = new ObservableCollection<AOItem32>();
- foreach (var item in (List<NotifiableIoItem>)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);
- }
- }
- }
- }
- 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];
- }
- }
- #endregion
- }
- public class AOItem32 : IOItem<double>
- {
- public static readonly DependencyProperty NewValueProperty = DependencyProperty.Register(nameof(NewValue), typeof(double), typeof(AOItem32), new PropertyMetadata((double)0));
- public static readonly DependencyProperty TextSavedProperty = DependencyProperty.Register(nameof(TextSaved), typeof(bool), typeof(AOItem32), new PropertyMetadata(true));
- public double NewValue
- {
- get
- {
- return (double)this.GetValue(NewValueProperty);
- }
- set
- {
- this.SetValue(NewValueProperty, (object)value);
- }
- }
- public bool TextSaved
- {
- get
- {
- return (bool)this.GetValue(TextSavedProperty);
- }
- set
- {
- this.SetValue(TextSavedProperty, (object)value);
- }
- }
- }
- }
|