123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- using Aitex.Core.Util;
- 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.Timers;
- using System.Windows;
- using System.Windows.Threading;
- namespace CyberX8_MainPages.ViewModels
- {
- public class BeckhoffIOViewModel : BindableBase
- {
- #region 私有属性
- 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<double>> _aiMap = new Dictionary<string, IOItem<double>>();
- private Dictionary<string, AOItem32> _aoMap = new Dictionary<string, AOItem32>();
- private ObservableCollection<IOItem<double>> m_AIs = new ObservableCollection<IOItem<double>>();
- 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 string ModuleName { get; set; }
- public ObservableCollection<IOItem<double>> 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));
- #endregion
- #region 内部变量
- private DispatcherTimer _timer;
- #endregion
- public BeckhoffIOViewModel()
- {
- BuildIoSchema();
- }
- /// <summary>
- /// 定时器执行
- /// </summary>
- public void LoadData(string system)
- {
- if(_timer==null)
- {
- _timer = new DispatcherTimer();
- _timer.Interval = TimeSpan.FromSeconds(0.5);
- _timer.Tick += timer_Tick;
- }
- _timer.Start();
- }
- /// <summary>
- /// 停止
- /// </summary>
- public void Hide()
- {
- if (_timer != null)
- {
- _timer.Stop();
- }
- }
- private 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()
- {
- List<string> adDataLst = new List<string>()
- {
- "System.DIItemList","System.DOItemList","System.AIItemList","System.AOItemList"
- };
- Dictionary<string,object> datas= QueryDataClient.Instance.Service.PollData(adDataLst);
- if (DIs.Count == 0)
- {
- var itemList = datas["System.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(item.Name, io);
- }
- }
- }
- if (DOs.Count == 0)
- {
- var itemList = datas["System.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(item.Name, io);
- }
- }
- }
- if (AIs.Count == 0)
- {
- var itemList = datas["System.AIItemList"];
- if (itemList != null)
- {
- AIs = new ObservableCollection<IOItem<double>>();
- foreach (var item in (List<NotifiableIoItem>)itemList)
- {
- var io = new IOItem<double>()
- {
- Index = item.Index,
- Name = item.Name,
- Value = item.DoubleValue,
- Address = item.Address
- };
- AIs.Add(io);
- _aiMap.Add(item.Name, io);
- }
- }
- }
- if (AOs.Count == 0)
- {
- var itemList = datas["System.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.DoubleValue,
- Address = item.Address
- };
- AOs.Add(io);
- _aoMap.Add(item.Name, io);
- }
- }
- }
- }
- public void GetIOs()
- {
- var diValues = QueryDataClient.Instance.Service.PollData(_diMap.Keys);
- if (diValues != null)
- {
- List<string> keys=_diMap.Keys.ToList();
- foreach (string item in keys)
- {
- if (diValues.ContainsKey(item))
- _diMap[item].Value = (bool)diValues[item];
- }
- }
- var doValues = QueryDataClient.Instance.Service.PollData(_doMap.Keys);
- if (doValues != null)
- {
- 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);
- if (aiValues != null)
- {
- foreach (var item in _aiMap)
- {
- if (aiValues.ContainsKey(item.Key))
- _aiMap[item.Key].Value = (double)aiValues[item.Key];
- }
- }
- var aoValues = QueryDataClient.Instance.Service.PollData(_aoMap.Keys);
- if (aoValues != null)
- {
- foreach (var item in _aoMap)
- {
- if (aoValues.ContainsKey(item.Key))
- _aoMap[item.Key].Value = (double)aoValues[item.Key];
- }
- }
- }
- #endregion
- }
- }
|