using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Device; using Aitex.Core.RT.Event; using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using MECF.Framework.Common.Communications; using RTOverEthernetDevelopmentKit; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml; namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps { public class SensorTemp : BaseDevice, IConnection, IDevice { private SensorTempConnection _connection; private TempBasFunction tempBasFunction; private R_TRIG _trigCommunicationError = new R_TRIG(); private PeriodicJob _thread; private LinkedList _lstHandler = new LinkedList(); private object _locker = new object(); private string PMName; private bool _enableLog = true; public int TempMin { get; set; } = 600; public int NumberOfChannels { get; set; } = 1; public float[] TempDatasArray { get; set; } public string Address { get; set; } public bool IsConnected { get { return _connection != null && _connection.IsConnected; } } public bool Connect() { return _connection.Connect(); } public bool Disconnect() { return true; } public SensorTemp(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule) { } public void QueryTemp() { SetQuery("QueryBup", "00mw0\r"); } public void SetQuery(string name, string strCmd) { lock (_locker) { if (_connection.IsBusy) { _connection.ForceClear(); } byte[] bCmd = Encoding.Default.GetBytes(strCmd); _lstHandler.AddLast(new SensorTempQueryHandler(this, name, bCmd)); } } public bool Initialize() { try { if (!SC.GetValue($"{ScBasePath}.EnableDevice")) return true; IniData(); IniTemp(); } catch (Exception) { } return true; } private void IniData() { //添加参数设置 tempBasFunction = new TempBasFunction(Name, TempMin); tempBasFunction.SetPm1Pm2DoForInterlock(false); Address = SC.GetStringValue($"{ScBasePath}.Address"); _enableLog = SC.GetValue($"{Name}.EnableLogMessage"); _connection = new SensorTempConnection(Address); _connection.EnableLog(_enableLog); var scOnTime = SC.GetValue($"TempDevice.OnTimer"); _thread = new PeriodicJob(scOnTime, OnTimer, $"{Module}.{Name} MonitorHandler", true); SC.RegisterValueChangedCallback($"TempDevice.OnTimer", (obj) => { _thread.ChangeInterval((int)obj); }); } private void IniTemp() { TempDatasArray = new float[NumberOfChannels]; for (int i = 0; i < NumberOfChannels; i++)//初始化温度下限 初始化获取配置的通道是否禁用 { TempDatasArray[i] = TempMin; DATA.Subscribe($"{Name}.t{i+1}", () => TempDatasArray[i]); } } private bool OnTimer() { try { _connection.MonitorTimeout(); if (!_connection.IsConnected) { _lstHandler.Clear(); //只要断开,就进行连接 if (!_connection.Connect()) { _trigCommunicationError.CLK = !_connection.IsConnected; if (_trigCommunicationError.Q) { _connection.SetPortAddress(SC.GetStringValue($"{Name}.Address")); if (!_connection.Connect()) { EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}"); } } } else//已连接,自动复位断联信号 { _trigCommunicationError.RST = true; LOG.Write($"{Module}.SensorTemp.SetPyroCommunicationError,reconnected."); } Thread.Sleep(2000);//重连延时 _connection.ForceClear(); return true; } lock (_locker) { if (_lstHandler.Count == 0) QueryTemp(); if (_lstHandler.Count > 0 && !_connection.IsBusy) { HandlerBase handler = _lstHandler.First.Value; _lstHandler.RemoveFirst(); if (handler != null) _connection.Execute(handler); } } } catch (Exception ex) { LOG.Write(ex); } return true; } public void ResponseQuery(string name, byte[] data) { try { string sData = Encoding.Default.GetString(data); if (name == "QueryBup") { string strTemp = sData; if (!sData.Contains("\r")) return; int temp = Convert.ToInt32(strTemp.Replace("\r", ""), 16); TempDatasArray[0] = (float)temp / 10; return; } } catch (Exception ex) { } } public void ResponseError() { //报警 } public void Reset() { _trigCommunicationError.RST = true; } public void Terminate() { _connection.Disconnect(); } } }