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 { #region 常量 private const string THORNTON_DEVICELIST = "ThorntonDeviceList"; private const string RESISTIVITY_VALUE = "ResistivityValue"; private const string IS_CONNECTED = "IsConnected"; #endregion #region 内部变量 /// /// 模块设备字典 /// private Dictionary _thorntonDeviceConfigDic = new Dictionary(); /// /// 模块数值字典 /// private Dictionary _thorntonResistivityValueDic = new Dictionary(); /// /// 模块串口设备字典 /// private Dictionary _thorntonSerialDeviceDic = new Dictionary(); #endregion /// /// 初始化 /// public void Initialize() { string xmlPath = PathManager.GetCfgDir() + "Devices\\ThorntonCfg.xml"; ThorntonConfig cfg = CustomXmlSerializer.Deserialize(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); } } } } /// /// 接收到数据 /// /// /// private void ThorntonSerialDevice_OnDataChanged(string name, List lstContent) { if(_thorntonDeviceConfigDic.ContainsKey(name)) { ThorntonDeviceConfig thorntonDeviceConfig = _thorntonDeviceConfigDic[name]; for(int i=0;ii) { _thorntonResistivityValueDic[thorntonDevice.Name] = lstContent[i]; } } } } /// /// 初始化设备 /// /// public void InitialDevice(string name) { if (_thorntonSerialDeviceDic.ContainsKey(name)) { _thorntonSerialDeviceDic[name].Start(); } } /// /// 查询连接状态 /// /// /// public bool GetDeviceConnect(string name) { if (_thorntonSerialDeviceDic.ContainsKey(name)) { return _thorntonSerialDeviceDic[name].Connected; } return false; } /// /// 获取数值 /// /// /// public string GetResisitivityValueByName(string name) { return _thorntonResistivityValueDic.ContainsKey(name) ? _thorntonResistivityValueDic[name] : ""; } } }