IoSensor.cs 4.4 KB

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