IoSensor.cs 4.1 KB

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