LinMotViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Aitex.Core.RT.ConfigCenter;
  2. using Aitex.Core.Util;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.Common.Device.LinMot;
  5. using MECF.Framework.Common.Utilities;
  6. using Prism.Mvvm;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Timers;
  13. using System.Windows.Threading;
  14. namespace CyberX8_MainPages.ViewModels
  15. {
  16. public class LinMotViewModel : BindableBase
  17. {
  18. #region 常量
  19. private const string LINMOT_DEVICELIST = "LinMotDeviceList";
  20. /// <summary>
  21. /// 控制字
  22. /// </summary>
  23. private const string STATUS_WORD = "StatusWord";
  24. #endregion
  25. #region 内部变量
  26. /// <summary>
  27. /// LinMot设备配置
  28. /// </summary>
  29. private LinmotDeviceConfig _linMotDeviceConfig;
  30. /// <summary>
  31. /// 模块名称
  32. /// </summary>
  33. private string _module;
  34. /// <summary>
  35. /// 查询后台数据集合
  36. /// </summary>
  37. private List<string> _rtDataKeys = new List<string>();
  38. /// <summary>
  39. /// 查询后台的数据
  40. /// </summary>
  41. private Dictionary<string, object> _rtDataValues;
  42. private DispatcherTimer _timer;
  43. #endregion
  44. #region 属性
  45. /// <summary>
  46. /// 模块名称
  47. /// </summary>
  48. public string Module { get { return _module; } set { SetProperty(ref _module, value); } }
  49. /// <summary>
  50. /// LinMot设备配置
  51. /// </summary>
  52. public LinmotDeviceConfig LinmotDeviceConfig { get { return _linMotDeviceConfig; } set { SetProperty(ref _linMotDeviceConfig, value); } }
  53. #endregion
  54. /// <summary>
  55. /// 构造函数
  56. /// </summary>
  57. public LinMotViewModel()
  58. {
  59. }
  60. /// <summary>
  61. /// 初始化模块名称
  62. /// </summary>
  63. /// <param name="systemName"></param>
  64. public void Init(string systemName)
  65. {
  66. Module = systemName;
  67. }
  68. /// <summary>
  69. /// 加载数据
  70. /// </summary>
  71. public void LoadData(string systemName)
  72. {
  73. Module = systemName;
  74. List<string> lst = new List<string>();
  75. lst.Add($"{Module}.{LINMOT_DEVICELIST}");
  76. _rtDataValues = QueryDataClient.Instance.Service.PollData(lst);
  77. if (_rtDataValues != null)
  78. {
  79. LinmotDeviceConfig = CommonFunction.GetValue<LinmotDeviceConfig>(_rtDataValues, $"{Module}.{LINMOT_DEVICELIST}");
  80. }
  81. InitialKeys();
  82. if (_timer == null)
  83. {
  84. _timer = new DispatcherTimer();
  85. _timer.Interval = TimeSpan.FromMilliseconds(200);
  86. _timer.Tick += Timer_Tick; ;
  87. }
  88. _timer.Start();
  89. }
  90. /// <summary>
  91. /// 定时器执行
  92. /// </summary>
  93. /// <param name="sender"></param>
  94. /// <param name="e"></param>
  95. private void Timer_Tick(object sender, EventArgs e)
  96. {
  97. if (_rtDataKeys.Count != 0)
  98. {
  99. _rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  100. if (_rtDataValues != null && _rtDataValues.Count != 0)
  101. {
  102. foreach (LinMotDevice item in LinmotDeviceConfig.LinmotDevices)
  103. {
  104. item.StatusWord = CommonFunction.GetValue<short>(_rtDataValues, $"{item.Name}.{STATUS_WORD}");
  105. item.IsConnected = CommonFunction.GetValue<bool>(_rtDataValues, $"{item.Name}.IsConnectd");
  106. item.Speed = CommonFunction.GetValue<int>(_rtDataValues, $"{item.Name}.Speed");
  107. item.Position = CommonFunction.GetValue<double>(_rtDataValues, $"{item.Name}.Position");
  108. }
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 隐藏
  114. /// </summary>
  115. public void Hide()
  116. {
  117. if (_timer != null)
  118. {
  119. _timer.Stop();
  120. }
  121. }
  122. /// <summary>
  123. /// 初始化Keys
  124. /// </summary>
  125. private void InitialKeys()
  126. {
  127. _rtDataKeys.Clear();
  128. if (LinmotDeviceConfig != null)
  129. {
  130. foreach (LinMotDevice item in LinmotDeviceConfig.LinmotDevices)
  131. {
  132. _rtDataKeys.Add($"{item.Name}.{STATUS_WORD}");
  133. _rtDataKeys.Add($"{item.Name}.IsConnectd");
  134. _rtDataKeys.Add($"{item.Name}.Speed");
  135. _rtDataKeys.Add($"{item.Name}.Position");
  136. }
  137. }
  138. }
  139. }
  140. }