IoSensor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. return true;
  69. }
  70. public void Terminate()
  71. {
  72. }
  73. public void Monitor()
  74. {
  75. try
  76. {
  77. _trigTextOut.CLK = (Value == _textOutTrigValue);
  78. if (_trigTextOut.Q)
  79. {
  80. if (WarningAction != null)
  81. {
  82. WarningAction();
  83. }
  84. else if (!string.IsNullOrEmpty(_warningText.Trim()))
  85. {
  86. EV.PostWarningLog(Module, _warningText);
  87. }
  88. else if (!string.IsNullOrEmpty(_alarmText.Trim()))
  89. {
  90. EV.PostAlarmLog(Module, _alarmText);
  91. }
  92. else if (!string.IsNullOrEmpty(_infoText.Trim()))
  93. {
  94. EV.PostInfoLog(Module, _infoText);
  95. }
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. LOG.Write(ex);
  101. }
  102. }
  103. public void Reset()
  104. {
  105. _trigTextOut.RST = true;
  106. }
  107. }
  108. }