IoSensor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. private R_TRIG _trigError = new R_TRIG();
  17. private bool _lowIsNormal; //0 正常,1报警
  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 NormalValue;
  36. }
  37. }
  38. public bool NormalValue
  39. {
  40. get
  41. {
  42. return !_lowIsNormal;
  43. }
  44. }
  45. public bool IsError
  46. {
  47. get
  48. {
  49. return Value != NormalValue;
  50. }
  51. }
  52. public IoSensor(string module, XmlElement node)
  53. {
  54. base.Module = module;
  55. base.Name = node.GetAttribute("id");
  56. base.Display = node.GetAttribute("display");
  57. base.DeviceID = node.GetAttribute("schematicId");
  58. _di = ParseDiNode("di", node);// IO.DI[node.GetAttribute("di")];
  59. _do = ParseDoNode("do", node);
  60. _infoText = node.GetAttribute("infoText");
  61. _warningText = node.GetAttribute("warningText");
  62. _alarmText = node.GetAttribute("alarmText");
  63. _lowIsNormal = Convert.ToBoolean(node.GetAttribute("isLowAsNormal"));
  64. }
  65. public bool Initialize()
  66. {
  67. DATA.Subscribe(string.Format("Device.{0}.{1}", Module, Name), () =>
  68. {
  69. AITSensorData data = new AITSensorData()
  70. {
  71. DeviceName = Name,
  72. DeviceSchematicId = DeviceID,
  73. DisplayName = Display,
  74. Value = Value,
  75. IsError = IsError,
  76. };
  77. return data;
  78. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  79. return true;
  80. }
  81. public void Terminate()
  82. {
  83. }
  84. public void Monitor()
  85. {
  86. try
  87. {
  88. _trigError.CLK = IsError;
  89. if (_trigError.Q)
  90. {
  91. if (WarningAction != null)
  92. {
  93. WarningAction();
  94. }
  95. else if (!string.IsNullOrEmpty(_warningText.Trim()))
  96. {
  97. EV.PostMessage(Module, EventEnum.DefaultWarning, _warningText);
  98. }else if (!string.IsNullOrEmpty(_alarmText.Trim()))
  99. {
  100. EV.PostMessage(Module, EventEnum.DefaultAlarm, _alarmText);
  101. }else if (!string.IsNullOrEmpty(_infoText.Trim()))
  102. {
  103. EV.PostMessage(Module, EventEnum.GeneralInfo, _infoText);
  104. }
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. LOG.Write(ex);
  110. }
  111. }
  112. public void Reset()
  113. {
  114. _trigError.RST = true;
  115. }
  116. }
  117. }