| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 | using Aitex.Common.Util;using Aitex.Core.RT.DataCenter;using Aitex.Core.Util;using MECF.Framework.Common.Utilities;using System;using System.Collections.Generic;using System.IO;using System.IO.Ports;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MECF.Framework.Common.Device.ResistivityProbe{    public class ThorntonConfigManager : Singleton<ThorntonConfigManager>    {        #region 常量        private const string THORNTON_DEVICELIST = "ThorntonDeviceList";        private const string RESISTIVITY_VALUE = "ResistivityValue";        private const string IS_CONNECTED = "IsConnected";        #endregion        #region 内部变量        /// <summary>        /// 模块设备字典        /// </summary>        private Dictionary<string, ThorntonDeviceConfig> _thorntonDeviceConfigDic = new Dictionary<string, ThorntonDeviceConfig>();        /// <summary>        /// 模块数值字典        /// </summary>        private Dictionary<string, string> _thorntonResistivityValueDic = new Dictionary<string, string>();        /// <summary>        /// 模块串口设备字典        /// </summary>        private Dictionary<string, ThorntonSerialDevice> _thorntonSerialDeviceDic = new Dictionary<string, ThorntonSerialDevice>();        #endregion        /// <summary>        /// 初始化        /// </summary>        public void Initialize()        {            string xmlPath = PathManager.GetCfgDir() + "Devices\\ThorntonCfg.xml";            ThorntonConfig cfg = CustomXmlSerializer.Deserialize<ThorntonConfig>(new FileInfo(xmlPath));            if (cfg != null)            {                foreach (ThorntonDeviceConfig config in cfg.ThorntonDeviceConfigs)                {                    DATA.Subscribe($"{config.Name}.{THORNTON_DEVICELIST}", () => config, SubscriptionAttribute.FLAG.IgnoreSaveDB);                    ThorntonSerialDevice thorntonSerialDevice = new ThorntonSerialDevice(config.Name, config.Port, config.BaudRate, (StopBits)config.StopBit,                        config.Data, SerialPortUtil.GetParity(config.Parity), true);                    thorntonSerialDevice.OnDataChanged += ThorntonSerialDevice_OnDataChanged;                    _thorntonDeviceConfigDic[config.Name] = config;                    foreach(ThorntonDevice item in config.ThorntonDevices)                    {                        _thorntonResistivityValueDic[item.Name] = "";                        DATA.Subscribe($"{item.Name}.{RESISTIVITY_VALUE}", () => _thorntonResistivityValueDic[item.Name], SubscriptionAttribute.FLAG.IgnoreSaveDB);                        _thorntonSerialDeviceDic[item.Name] = thorntonSerialDevice;                        DATA.Subscribe($"{item.Name}.{IS_CONNECTED}", () => thorntonSerialDevice.Connected,SubscriptionAttribute.FLAG.IgnoreSaveDB);                    }                }            }        }        /// <summary>        /// 接收到数据        /// </summary>        /// <param name="name"></param>        /// <param name="lstContent"></param>        private void ThorntonSerialDevice_OnDataChanged(string name, List<string> lstContent)        {            if(_thorntonDeviceConfigDic.ContainsKey(name))            {                ThorntonDeviceConfig thorntonDeviceConfig = _thorntonDeviceConfigDic[name];                for(int i=0;i<thorntonDeviceConfig.ThorntonDevices.Count;i++)                {                    ThorntonDevice thorntonDevice = thorntonDeviceConfig.ThorntonDevices[i];                    if(_thorntonResistivityValueDic.ContainsKey(thorntonDevice.Name)&&lstContent.Count>i)                    {                        _thorntonResistivityValueDic[thorntonDevice.Name] = lstContent[i];                    }                }            }        }        /// <summary>        /// 初始化设备        /// </summary>        /// <param name="name"></param>        public void InitialDevice(string name)        {            if (_thorntonSerialDeviceDic.ContainsKey(name))            {                _thorntonSerialDeviceDic[name].Start();            }        }        /// <summary>        /// 查询连接状态        /// </summary>        /// <param name="name"></param>        /// <returns></returns>        public bool GetDeviceConnect(string name)        {            if (_thorntonSerialDeviceDic.ContainsKey(name))            {                return _thorntonSerialDeviceDic[name].Connected;            }            return false;        }        /// <summary>        /// 获取数值        /// </summary>        /// <param name="name"></param>        /// <returns></returns>        public string GetResisitivityValueByName(string name)        {            return _thorntonResistivityValueDic.ContainsKey(name) ? _thorntonResistivityValueDic[name] : "";        }    }}
 |