| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 | 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 PunkHPX8_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 内部变量        /// <summary>        /// Thornton设备配置        /// </summary>        private ThorntonDeviceConfig _thorntonDeviceConfig;        /// <summary>        /// 模块名称        /// </summary>        private string _moduleName;        /// <summary>        /// 查询后台数据集合        /// </summary>        private List<string> _rtDataKeys = new List<string>();        /// <summary>        /// 查询后台的数据        /// </summary>        private Dictionary<string, object> _rtDataValues;        /// <summary>        /// 定时器        /// </summary>        private DispatcherTimer _timer;        #endregion        #region 属性        /// <summary>        /// 模块名称        /// </summary>        public string ModuleName { get { return _moduleName; } set { SetProperty(ref _moduleName, value); } }        /// <summary>        /// Thornton设备配置        /// </summary>        public ThorntonDeviceConfig ThorntonDeviceConfig { get { return _thorntonDeviceConfig; } set { SetProperty(ref _thorntonDeviceConfig, value); } }        #endregion        /// <summary>        /// 构造函数        /// </summary>        public ResistivityProbeViewModel()        {        }        /// <summary>        /// 加载数据        /// </summary>        public void LoadData(string systemName)        {            ModuleName = systemName;            List<string> lst = new List<string>();            lst.Add($"{ModuleName}.{THORNTON_DEVICELIST}");            _rtDataValues = QueryDataClient.Instance.Service.PollData(lst);            if (_rtDataValues != null)            {                ThorntonDeviceConfig = CommonFunction.GetValue<ThorntonDeviceConfig>(_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();        }        /// <summary>        /// 定时器执行        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        /// <exception cref="NotImplementedException"></exception>        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<bool>(_rtDataValues, $"{item.Name}.{IS_CONNECTED}");                        item.ResistivityValue = CommonFunction.GetValue<string>(_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;                    }                }            }        }        /// <summary>        /// 隐藏        /// </summary>        public void Hide()        {            if (_timer != null)            {                _timer.Stop();            }        }        /// <summary>        /// 初始化Keys        /// </summary>        private void InitKeys()        {            _rtDataKeys.Clear();            foreach(ThorntonDevice item in ThorntonDeviceConfig.ThorntonDevices)            {                _rtDataKeys.Add($"{item.Name}.{IS_CONNECTED}");                _rtDataKeys.Add($"{item.Name}.{RESISTIVITY_VALUE}");            }        }    }}
 |