using Aitex.Core.Util; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Device.ResistivityProbe; using MECF.Framework.Common.Utilities; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows.Threading; namespace CyberX8_MainPages.ViewModels { public class ResistivityProbeViewModel : BindableBase { #region 常量 private const string THORNTON_DEVICELIST = "ThorntonDeviceList"; private const string RESISTIVITY_VALUE = "ResistivityValue"; private const string IS_CONNECTED = "IsConnected"; #endregion #region 内部变量 /// /// Thornton设备配置 /// private ThorntonDeviceConfig _thorntonDeviceConfig; /// /// 模块名称 /// private string _moduleName; /// /// 查询后台数据集合 /// private List _rtDataKeys = new List(); /// /// 查询后台的数据 /// private Dictionary _rtDataValues; /// /// 定时器 /// private DispatcherTimer _timer; #endregion #region 属性 /// /// 模块名称 /// public string ModuleName { get { return _moduleName; } set { SetProperty(ref _moduleName, value); } } /// /// Thornton设备配置 /// public ThorntonDeviceConfig ThorntonDeviceConfig { get { return _thorntonDeviceConfig; } set { SetProperty(ref _thorntonDeviceConfig, value); } } #endregion /// /// 构造函数 /// public ResistivityProbeViewModel() { } /// /// 加载数据 /// public void LoadData(string systemName) { ModuleName = systemName; List lst = new List(); lst.Add($"{ModuleName}.{THORNTON_DEVICELIST}"); _rtDataValues = QueryDataClient.Instance.Service.PollData(lst); if (_rtDataValues != null) { ThorntonDeviceConfig = CommonFunction.GetValue(_rtDataValues, $"{ModuleName}.{THORNTON_DEVICELIST}"); if (ThorntonDeviceConfig != null && ThorntonDeviceConfig.ThorntonDevices != null) { InitKeys(); } } if (_timer == null) { _timer = new DispatcherTimer(); _timer.Interval = TimeSpan.FromMilliseconds(200); _timer.Tick += Timer_Tick; } _timer.Start(); } /// /// 定时器执行 /// /// /// /// private void Timer_Tick(object sender, EventArgs e) { if (_rtDataKeys.Count != 0) { _rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys); if (_rtDataValues != null && _rtDataValues.Count != 0 && ThorntonDeviceConfig != null && ThorntonDeviceConfig.ThorntonDevices != null) { foreach (ThorntonDevice item in ThorntonDeviceConfig.ThorntonDevices) { item.IsConnected = CommonFunction.GetValue(_rtDataValues, $"{item.Name}.{IS_CONNECTED}"); item.ResistivityValue = CommonFunction.GetValue(_rtDataValues, $"{item.Name}.{RESISTIVITY_VALUE}"); //水阻值保留两位小数 string resistivityValue = ""; if (double.TryParse(item.ResistivityValue, out double number)) { double roundedNumber = Math.Round(number,2); resistivityValue = roundedNumber.ToString(); } item.ResistivityValue = resistivityValue; } } } } /// /// 隐藏 /// public void Hide() { if (_timer != null) { _timer.Stop(); } } /// /// 初始化Keys /// private void InitKeys() { _rtDataKeys.Clear(); foreach(ThorntonDevice item in ThorntonDeviceConfig.ThorntonDevices) { _rtDataKeys.Add($"{item.Name}.{IS_CONNECTED}"); _rtDataKeys.Add($"{item.Name}.{RESISTIVITY_VALUE}"); } } } }