using System; using System.Security.AccessControl; using System.Xml; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Event; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.Log; using Aitex.Core.Util; namespace Aitex.Core.RT.Device.Unit { public class IoSensor : BaseDevice, IDevice { private DIAccessor _di = null; private DOAccessor _do = null; private R_TRIG _trigError = new R_TRIG(); private bool _lowIsNormal; //0 正常,1报警 private string _warningText; private string _alarmText; private string _infoText; public Action WarningAction { get; set; } [Subscription(AITSensorProperty.Value)] public bool Value { get { if (_di != null) return _di.Value; if (_do != null) return _do.Value; return NormalValue; } } public bool NormalValue { get { return !_lowIsNormal; } } public bool IsError { get { return Value != NormalValue; } } public IoSensor(string module, XmlElement node) { base.Module = module; base.Name = node.GetAttribute("id"); base.Display = node.GetAttribute("display"); base.DeviceID = node.GetAttribute("schematicId"); _di = ParseDiNode("di", node);// IO.DI[node.GetAttribute("di")]; _do = ParseDoNode("do", node); _infoText = node.GetAttribute("infoText"); _warningText = node.GetAttribute("warningText"); _alarmText = node.GetAttribute("alarmText"); _lowIsNormal = Convert.ToBoolean(node.GetAttribute("isLowAsNormal")); } public bool Initialize() { DATA.Subscribe(string.Format("Device.{0}.{1}", Module, Name), () => { AITSensorData data = new AITSensorData() { DeviceName = Name, DeviceSchematicId = DeviceID, DisplayName = Display, Value = Value, IsError = IsError, }; return data; }, SubscriptionAttribute.FLAG.IgnoreSaveDB); return true; } public void Terminate() { } public void Monitor() { try { _trigError.CLK = IsError; if (_trigError.Q) { if (WarningAction != null) { WarningAction(); } else if (!string.IsNullOrEmpty(_warningText.Trim())) { EV.PostMessage(Module, EventEnum.DefaultWarning, _warningText); }else if (!string.IsNullOrEmpty(_alarmText.Trim())) { EV.PostMessage(Module, EventEnum.DefaultAlarm, _alarmText); }else if (!string.IsNullOrEmpty(_infoText.Trim())) { EV.PostMessage(Module, EventEnum.GeneralInfo, _infoText); } } } catch (Exception ex) { LOG.Write(ex); } } public void Reset() { _trigError.RST = true; } } }