123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using Aitex.Core.RT.ConfigCenter;
- using Aitex.Core.Util;
- using MECF.Framework.Common.DataCenter;
- using MECF.Framework.Common.Device.LinMot;
- using MECF.Framework.Common.Utilities;
- using Prism.Mvvm;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Timers;
- using System.Windows.Threading;
- namespace CyberX8_MainPages.ViewModels
- {
- public class LinMotViewModel : BindableBase
- {
- #region 常量
- private const string LINMOT_DEVICELIST = "LinMotDeviceList";
- /// <summary>
- /// 控制字
- /// </summary>
- private const string STATUS_WORD = "StatusWord";
- #endregion
- #region 内部变量
- /// <summary>
- /// LinMot设备配置
- /// </summary>
- private LinmotDeviceConfig _linMotDeviceConfig;
- /// <summary>
- /// 模块名称
- /// </summary>
- private string _module;
- /// <summary>
- /// 查询后台数据集合
- /// </summary>
- private List<string> _rtDataKeys = new List<string>();
- /// <summary>
- /// 查询后台的数据
- /// </summary>
- private Dictionary<string, object> _rtDataValues;
- private DispatcherTimer _timer;
- #endregion
- #region 属性
- /// <summary>
- /// 模块名称
- /// </summary>
- public string Module { get { return _module; } set { SetProperty(ref _module, value); } }
- /// <summary>
- /// LinMot设备配置
- /// </summary>
- public LinmotDeviceConfig LinmotDeviceConfig { get { return _linMotDeviceConfig; } set { SetProperty(ref _linMotDeviceConfig, value); } }
- #endregion
- /// <summary>
- /// 构造函数
- /// </summary>
- public LinMotViewModel()
- {
- }
- /// <summary>
- /// 初始化模块名称
- /// </summary>
- /// <param name="systemName"></param>
- public void Init(string systemName)
- {
- Module = systemName;
- }
- /// <summary>
- /// 加载数据
- /// </summary>
- public void LoadData(string systemName)
- {
- Module = systemName;
- List<string> lst = new List<string>();
- lst.Add($"{Module}.{LINMOT_DEVICELIST}");
- _rtDataValues = QueryDataClient.Instance.Service.PollData(lst);
- if (_rtDataValues != null)
- {
- LinmotDeviceConfig = CommonFunction.GetValue<LinmotDeviceConfig>(_rtDataValues, $"{Module}.{LINMOT_DEVICELIST}");
- }
- InitialKeys();
- if (_timer == null)
- {
- _timer = new DispatcherTimer();
- _timer.Interval = TimeSpan.FromMilliseconds(200);
- _timer.Tick += Timer_Tick; ;
- }
- _timer.Start();
- }
- /// <summary>
- /// 定时器执行
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Timer_Tick(object sender, EventArgs e)
- {
- if (_rtDataKeys.Count != 0)
- {
- _rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
- if (_rtDataValues != null && _rtDataValues.Count != 0)
- {
- foreach (LinMotDevice item in LinmotDeviceConfig.LinmotDevices)
- {
- item.StatusWord = CommonFunction.GetValue<short>(_rtDataValues, $"{item.Name}.{STATUS_WORD}");
- item.IsConnected = CommonFunction.GetValue<bool>(_rtDataValues, $"{item.Name}.IsConnectd");
- item.Speed = CommonFunction.GetValue<int>(_rtDataValues, $"{item.Name}.Speed");
- item.Position = CommonFunction.GetValue<double>(_rtDataValues, $"{item.Name}.Position");
- }
- }
- }
- }
- /// <summary>
- /// 隐藏
- /// </summary>
- public void Hide()
- {
- if (_timer != null)
- {
- _timer.Stop();
- }
- }
- /// <summary>
- /// 初始化Keys
- /// </summary>
- private void InitialKeys()
- {
- _rtDataKeys.Clear();
- if (LinmotDeviceConfig != null)
- {
- foreach (LinMotDevice item in LinmotDeviceConfig.LinmotDevices)
- {
- _rtDataKeys.Add($"{item.Name}.{STATUS_WORD}");
- _rtDataKeys.Add($"{item.Name}.IsConnectd");
- _rtDataKeys.Add($"{item.Name}.Speed");
- _rtDataKeys.Add($"{item.Name}.Position");
- }
- }
- }
- }
- }
|