IoSensor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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);
  76. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  77. DATA.Subscribe($"{Module}.{Name}.Value", () => Value);
  78. return true;
  79. }
  80. public void Terminate()
  81. {
  82. }
  83. public void Monitor()
  84. {
  85. try
  86. {
  87. if(Name == "SensorLowArmHasWafer" || Name == "SensorUpArmHasWafer" || Name == "SensorTMRBReady") return;
  88. _trigTextOut.CLK = (Value == _textOutTrigValue) ;
  89. if (_trigTextOut.Q)
  90. {
  91. if (WarningAction != null)
  92. {
  93. WarningAction();
  94. }
  95. else if (!string.IsNullOrEmpty(_warningText.Trim()))
  96. {
  97. EV.PostWarningLog(Module, _warningText);
  98. }else if (!string.IsNullOrEmpty(_alarmText.Trim()))
  99. {
  100. EV.PostAlarmLog(Module, _alarmText);
  101. }else if (!string.IsNullOrEmpty(_infoText.Trim()))
  102. {
  103. EV.PostInfoLog(Module, _infoText);
  104. }
  105. }
  106. if (_previous != Value)
  107. {
  108. if (OnSignalChanged != null)
  109. OnSignalChanged(this, Value);
  110. _previous = Value;
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. LOG.Write(ex);
  116. }
  117. }
  118. public void Reset()
  119. {
  120. _trigTextOut.RST = true;
  121. }
  122. }
  123. }