IoSensor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Xml;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.IOCore;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.RT.SCCore;
  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. private R_TRIG _trigTextOut = new R_TRIG();
  17. private bool _textOutTrigValue;
  18. private string _warningText;
  19. private string _alarmText;
  20. private string _infoText;
  21. public Action WarningAction
  22. {
  23. get;
  24. set;
  25. }
  26. [Subscription(AITSensorProperty.Value)]
  27. public bool Value
  28. {
  29. get
  30. {
  31. if (_di != null)
  32. return _di.Value;
  33. if (_do != null)
  34. return _do.Value;
  35. return false;
  36. }
  37. }
  38. private AITSensorData DeviceData
  39. {
  40. get
  41. {
  42. AITSensorData data = new AITSensorData()
  43. {
  44. DeviceName = Name,
  45. DeviceSchematicId = DeviceID,
  46. DisplayName = Display,
  47. Value = Value,
  48. };
  49. return data;
  50. }
  51. }
  52. public IoSensor(string module, XmlElement node, string ioModule = "")
  53. {
  54. var attrModule = node.GetAttribute("module");
  55. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  56. base.Name = node.GetAttribute("id");
  57. base.Display = node.GetAttribute("display");
  58. base.DeviceID = node.GetAttribute("schematicId");
  59. _di = ParseDiNode("di", node, ioModule);// IO.DI[node.GetAttribute("di")];
  60. //_do = ParseDoNode("do", node, ioModule);
  61. _infoText = node.GetAttribute("infoText");
  62. _warningText = node.GetAttribute("warningText");
  63. _alarmText = node.GetAttribute("alarmText");
  64. _textOutTrigValue = Convert.ToBoolean(string.IsNullOrEmpty(node.GetAttribute("textOutTrigValue")) ? "false" : node.GetAttribute("textOutTrigValue"));
  65. }
  66. public bool Initialize()
  67. {
  68. DATA.Subscribe($"{Module}.{Name}", () => DeviceData);
  69. return true;
  70. }
  71. public void Terminate()
  72. {
  73. }
  74. public void Monitor()
  75. {
  76. try
  77. {
  78. if (Name == "SensorCDAPressureOk2" && SC.GetValue<bool>("System.IsIgnoreCDAPressure2Alarm")) return;
  79. _trigTextOut.CLK = (Value == _textOutTrigValue);
  80. if (_trigTextOut.Q)
  81. {
  82. if (WarningAction != null)
  83. {
  84. WarningAction();
  85. }
  86. else if (!string.IsNullOrEmpty(_warningText.Trim()))
  87. {
  88. EV.PostWarningLog(Module, _warningText);
  89. }
  90. else if (!string.IsNullOrEmpty(_alarmText.Trim()))
  91. {
  92. EV.PostAlarmLog(Module, _alarmText);
  93. }
  94. else if (!string.IsNullOrEmpty(_infoText.Trim()))
  95. {
  96. EV.PostInfoLog(Module, _infoText);
  97. }
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. LOG.Write(ex);
  103. }
  104. }
  105. public void Reset()
  106. {
  107. _trigTextOut.RST = true;
  108. }
  109. }
  110. }