IoStatistics.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.SCCore;
  8. using Aitex.Core.Util;
  9. namespace Aitex.Core.RT.Device.Unit
  10. {
  11. public class IoStatistics : BaseDevice, IDevice
  12. {
  13. public string LastPMTime
  14. {
  15. get
  16. {
  17. return _scLastPMTime != null ? _scLastPMTime.Value : "";
  18. }
  19. }
  20. public double DaysFromLastPM
  21. {
  22. get
  23. {
  24. return _scFromLastPMTime==null ? 0 : _scFromLastPMTime.Value;
  25. }
  26. set
  27. {
  28. if (_scFromLastPMTime != null)
  29. _scFromLastPMTime.Value = value;
  30. }
  31. }
  32. public double TotalDays
  33. {
  34. get
  35. {
  36. return _scTotalTime != null ? _scTotalTime.Value : 0;
  37. }
  38. set
  39. {
  40. if (_scTotalTime != null)
  41. _scTotalTime.Value = value;
  42. }
  43. }
  44. public double PMIntervalDays
  45. {
  46. get
  47. {
  48. return _scPMInterval != null ? _scPMInterval.Value : 0;
  49. }
  50. }
  51. public bool IsPMNeeded
  52. {
  53. get
  54. {
  55. return DaysFromLastPM > PMIntervalDays;
  56. }
  57. }
  58. public bool EnableAlarm
  59. {
  60. get
  61. {
  62. return _scEnableAlarm == null || _scEnableAlarm.Value;
  63. }
  64. }
  65. private DIAccessor _diMonitor = null;
  66. private SCString _scLastPMTime = null;
  67. private SCItem<double> _scFromLastPMTime = null;
  68. private SCItem<double> _scTotalTime = null;
  69. private SCItem<double> _scPMInterval = null;
  70. private SCItem<bool> _scEnableAlarm = null;
  71. private double _total;
  72. private double _fromLast;
  73. private RD_TRIG _trigOnOff = new RD_TRIG();
  74. private DeviceTimer _timerTotal =new DeviceTimer();
  75. private DeviceTimer _timerFromLast = new DeviceTimer();
  76. private R_TRIG _trigPMNeeded = new R_TRIG();
  77. public IoStatistics(string module, XmlElement node)
  78. {
  79. base.Module = module;
  80. base.Name = node.GetAttribute("id");
  81. base.Display = node.GetAttribute("display");
  82. base.DeviceID = node.GetAttribute("schematicId");
  83. _diMonitor = ParseDiNode("diMonitorSignal", node);
  84. _scLastPMTime = ParseScNodeString("scLastPMTime", node);
  85. _scFromLastPMTime = ParseScNodeDouble("scTimeFromLastPM", node);
  86. _scTotalTime = ParseScNodeDouble("scTimeTotal", node);
  87. _scPMInterval = ParseScNodeDouble("scPMInterval", node);
  88. _scEnableAlarm = ParseScNodeBool("scEnableAlarm", node);
  89. }
  90. public bool Initialize()
  91. {
  92. DATA.Subscribe(string.Format("Device.{0}.{1}", Module , Name), () =>
  93. {
  94. AITStatisticsData data = new AITStatisticsData ()
  95. {
  96. DeviceName = Name,
  97. DeviceSchematicId = DeviceID,
  98. DisplayName = Display,
  99. LastPMTime = LastPMTime,
  100. TimeFromLastPM = DaysFromLastPM,
  101. TimeTotal = TotalDays,
  102. PMInterval = PMIntervalDays,
  103. };
  104. return data;
  105. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  106. DEVICE.Register(String.Format("{0}.{1}", Name, AITStatisticsOperation.Reset), (out string reason, int time, object[] param) =>
  107. {
  108. if (_scLastPMTime == null || _scFromLastPMTime==null)
  109. {
  110. reason = string.Format("Reset statistics of {0} failed, not define config entry", Display);
  111. return false;
  112. }
  113. reason = string.Format("Reset statistics of {0}", Display);
  114. _scLastPMTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  115. DaysFromLastPM = 0;
  116. _fromLast = 0;
  117. _timerFromLast.Start(0);
  118. return true;
  119. });
  120. return true;
  121. }
  122. public void Terminate()
  123. {
  124. }
  125. public void Monitor()
  126. {
  127. _trigOnOff.CLK = _diMonitor.RawData;
  128. //第一次检测到打开了,开始计时
  129. if (_trigOnOff.R)
  130. {
  131. _total = TotalDays;
  132. _fromLast = DaysFromLastPM;
  133. _timerTotal.Start(0);
  134. _timerFromLast.Start(0);
  135. }
  136. //第一次检测到从打开到关闭状态
  137. if (_trigOnOff.T)
  138. {
  139. }
  140. //如果开着,就更新SC
  141. if (_trigOnOff.M)
  142. {
  143. TotalDays = _total + _timerTotal.GetElapseTime()/1000/60/60/24;
  144. DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000/60/60/24;
  145. }
  146. if (PMIntervalDays > 0)
  147. {
  148. _trigPMNeeded.CLK = IsPMNeeded;
  149. if (_trigPMNeeded.Q)
  150. {
  151. if (EnableAlarm)
  152. {
  153. EV.PostMessage(Module, EventEnum.PMNeededWarning, Display, PMIntervalDays);
  154. }
  155. else
  156. {
  157. EV.PostMessage(Module, EventEnum.PMNeededInformation, Display, PMIntervalDays);
  158. }
  159. }
  160. }
  161. }
  162. public void Reset()
  163. {
  164. _trigPMNeeded.RST = true;
  165. }
  166. }
  167. }