IoSensor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Security.AccessControl;
  3. using System.Xml;
  4. using Aitex.Core.Common.DeviceData;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.IOCore;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.Util;
  10. using MECF.Framework.Common.CommonData.EnumData;
  11. namespace Aitex.Core.RT.Device.Unit
  12. {
  13. public class IoSensor : BaseDevice, IDevice
  14. {
  15. private DIAccessor _di = null;
  16. private DOAccessor _do = null;
  17. public DIAccessor SensorDI => _di;
  18. public DOAccessor SensorDO => _do;
  19. private R_TRIG _trigTextOut = new R_TRIG();
  20. private bool _textOutTrigValue;
  21. public bool AlarmTrigValue
  22. {
  23. get { return _textOutTrigValue&&!string.IsNullOrEmpty(_alarmText); }
  24. }
  25. private string _warningText;
  26. private string _alarmText;
  27. private string _infoText;
  28. public string SensorCategoryKey;
  29. public Action WarningAction
  30. {
  31. get;
  32. set;
  33. }
  34. public event Action<IoSensor, bool> OnSignalChanged;
  35. public bool Value
  36. {
  37. get
  38. {
  39. if (_di != null)
  40. return _di.Value;
  41. if (_do != null)
  42. return _do.Value;
  43. return false;
  44. }
  45. }
  46. private AITSensorData DeviceData
  47. {
  48. get
  49. {
  50. AITSensorData data = new AITSensorData()
  51. {
  52. DeviceName = Name,
  53. DeviceSchematicId = DeviceID,
  54. DisplayName = Display,
  55. Value = Value,
  56. };
  57. return data;
  58. }
  59. }
  60. private bool _previous;
  61. public IoSensor(string module, XmlElement node, string ioModule = "")
  62. {
  63. var attrModule = node.GetAttribute("module");
  64. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  65. base.Name = node.GetAttribute("id");
  66. base.Display = node.GetAttribute("display");
  67. base.DeviceID = node.GetAttribute("schematicId");
  68. _di = ParseDiNode("di", node, ioModule);// IO.DI[node.GetAttribute("di")];
  69. //_do = ParseDoNode("do", node, ioModule);
  70. SensorCategoryKey = string.IsNullOrEmpty(node.GetAttribute("SensorCategory")) ? ((int)SensorCategoryEnum.None).ToString() : node.GetAttribute("SensorCategory");
  71. _infoText = node.GetAttribute("infoText");
  72. _warningText = node.GetAttribute("warningText");
  73. _alarmText = node.GetAttribute("alarmText");
  74. _textOutTrigValue = Convert.ToBoolean(string.IsNullOrEmpty(node.GetAttribute("textOutTrigValue")) ? "false" : node.GetAttribute("textOutTrigValue"));
  75. }
  76. public bool Initialize()
  77. {
  78. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  79. DATA.Subscribe($"{Module}.{Name}.Value", () => Value);
  80. return true;
  81. }
  82. public void Terminate()
  83. {
  84. }
  85. public void Monitor()
  86. {
  87. try
  88. {
  89. _trigTextOut.CLK = (Value == _textOutTrigValue) ;
  90. if (_trigTextOut.Q)
  91. {
  92. if (WarningAction != null)
  93. {
  94. WarningAction();
  95. }
  96. else if (!string.IsNullOrEmpty(_warningText.Trim()))
  97. {
  98. EV.PostWarningLog(Module, _warningText);
  99. }else if (!string.IsNullOrEmpty(_alarmText.Trim()))
  100. {
  101. EV.PostAlarmLog(Module, _alarmText);
  102. }else if (!string.IsNullOrEmpty(_infoText.Trim()))
  103. {
  104. EV.PostInfoLog(Module, _infoText);
  105. }
  106. }
  107. if (_previous != Value)
  108. {
  109. if (OnSignalChanged != null)
  110. OnSignalChanged(this, Value);
  111. _previous = Value;
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. LOG.Write(ex);
  117. }
  118. }
  119. public void Reset()
  120. {
  121. _trigTextOut.RST = true;
  122. }
  123. }
  124. }