ResistivityProbeViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Aitex.Core.Util;
  2. using MECF.Framework.Common.DataCenter;
  3. using MECF.Framework.Common.Device.ResistivityProbe;
  4. using MECF.Framework.Common.Utilities;
  5. using Prism.Mvvm;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  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 ResistivityProbeViewModel : BindableBase
  17. {
  18. #region 常量
  19. private const string THORNTON_DEVICELIST = "ThorntonDeviceList";
  20. private const string RESISTIVITY_VALUE = "ResistivityValue";
  21. private const string IS_CONNECTED = "IsConnected";
  22. #endregion
  23. #region 内部变量
  24. /// <summary>
  25. /// Thornton设备配置
  26. /// </summary>
  27. private ThorntonDeviceConfig _thorntonDeviceConfig;
  28. /// <summary>
  29. /// 模块名称
  30. /// </summary>
  31. private string _moduleName;
  32. /// <summary>
  33. /// 查询后台数据集合
  34. /// </summary>
  35. private List<string> _rtDataKeys = new List<string>();
  36. /// <summary>
  37. /// 查询后台的数据
  38. /// </summary>
  39. private Dictionary<string, object> _rtDataValues;
  40. /// <summary>
  41. /// 定时器
  42. /// </summary>
  43. private DispatcherTimer _timer;
  44. #endregion
  45. #region 属性
  46. /// <summary>
  47. /// 模块名称
  48. /// </summary>
  49. public string ModuleName { get { return _moduleName; } set { SetProperty(ref _moduleName, value); } }
  50. /// <summary>
  51. /// Thornton设备配置
  52. /// </summary>
  53. public ThorntonDeviceConfig ThorntonDeviceConfig { get { return _thorntonDeviceConfig; } set { SetProperty(ref _thorntonDeviceConfig, value); } }
  54. #endregion
  55. /// <summary>
  56. /// 构造函数
  57. /// </summary>
  58. public ResistivityProbeViewModel()
  59. {
  60. }
  61. /// <summary>
  62. /// 加载数据
  63. /// </summary>
  64. public void LoadData(string systemName)
  65. {
  66. ModuleName = systemName;
  67. List<string> lst = new List<string>();
  68. lst.Add($"{ModuleName}.{THORNTON_DEVICELIST}");
  69. _rtDataValues = QueryDataClient.Instance.Service.PollData(lst);
  70. if (_rtDataValues != null)
  71. {
  72. ThorntonDeviceConfig = CommonFunction.GetValue<ThorntonDeviceConfig>(_rtDataValues, $"{ModuleName}.{THORNTON_DEVICELIST}");
  73. if (ThorntonDeviceConfig != null && ThorntonDeviceConfig.ThorntonDevices != null)
  74. {
  75. InitKeys();
  76. }
  77. }
  78. if (_timer == null)
  79. {
  80. _timer = new DispatcherTimer();
  81. _timer.Interval = TimeSpan.FromMilliseconds(200);
  82. _timer.Tick += Timer_Tick;
  83. }
  84. _timer.Start();
  85. }
  86. /// <summary>
  87. /// 定时器执行
  88. /// </summary>
  89. /// <param name="sender"></param>
  90. /// <param name="e"></param>
  91. /// <exception cref="NotImplementedException"></exception>
  92. private void Timer_Tick(object sender, EventArgs e)
  93. {
  94. if (_rtDataKeys.Count != 0)
  95. {
  96. _rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  97. if (_rtDataValues != null && _rtDataValues.Count != 0 && ThorntonDeviceConfig != null && ThorntonDeviceConfig.ThorntonDevices != null)
  98. {
  99. foreach (ThorntonDevice item in ThorntonDeviceConfig.ThorntonDevices)
  100. {
  101. item.IsConnected = CommonFunction.GetValue<bool>(_rtDataValues, $"{item.Name}.{IS_CONNECTED}");
  102. item.ResistivityValue = CommonFunction.GetValue<string>(_rtDataValues, $"{item.Name}.{RESISTIVITY_VALUE}");
  103. //水阻值保留两位小数
  104. string resistivityValue = "";
  105. if (double.TryParse(item.ResistivityValue, out double number))
  106. {
  107. double roundedNumber = Math.Round(number,2);
  108. resistivityValue = roundedNumber.ToString();
  109. }
  110. item.ResistivityValue = resistivityValue;
  111. }
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 隐藏
  117. /// </summary>
  118. public void Hide()
  119. {
  120. if (_timer != null)
  121. {
  122. _timer.Stop();
  123. }
  124. }
  125. /// <summary>
  126. /// 初始化Keys
  127. /// </summary>
  128. private void InitKeys()
  129. {
  130. _rtDataKeys.Clear();
  131. foreach(ThorntonDevice item in ThorntonDeviceConfig.ThorntonDevices)
  132. {
  133. _rtDataKeys.Add($"{item.Name}.{IS_CONNECTED}");
  134. _rtDataKeys.Add($"{item.Name}.{RESISTIVITY_VALUE}");
  135. }
  136. }
  137. }
  138. }