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