IoFlowMeter.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using Aitex.Core.Common.DeviceData;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.IOCore;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.RT.Tolerance;
  13. using Aitex.Core.Util;
  14. namespace Aitex.Core.RT.Device.Unit
  15. {
  16. public class IoFlowMeter : BaseDevice, IDevice
  17. {
  18. public double Feedback
  19. {
  20. get
  21. {
  22. if (_aiFlowValue != null)
  23. {
  24. ushort value = (ushort)_aiFlowValue.Value;
  25. double e = value * 1.0 / 65536 * _factor;
  26. return (e - _physicalMin) * (_rangeMax - _rangeMin) / (_physicalMax - _physicalMin) + _rangeMin;
  27. //return (e-4) * 40 / 16;
  28. //return Converter.Phy2Logic((ushort)_aiFlowValue.Value, 0, 40, 0, 0xFFFF/5 );
  29. }
  30. return 0;
  31. }
  32. }
  33. public double FilteredFeedback
  34. {
  35. get
  36. {
  37. return _queueData.ToList().Average();
  38. }
  39. }
  40. public bool IsWarning
  41. {
  42. get
  43. {
  44. return _checkWarning.Result;
  45. }
  46. }
  47. public bool IsError
  48. {
  49. get
  50. {
  51. return _checkAlarm.Result;
  52. }
  53. }
  54. private double _minSetPoint;
  55. public double MinSetPoint
  56. {
  57. get
  58. {
  59. return _minSetPoint;
  60. }
  61. set
  62. {
  63. _minSetPoint = value;
  64. //if (_aoMin != null)
  65. // _aoMin.Value = (short)value;
  66. }
  67. }
  68. private double _maxSetPoint;
  69. public double MaxSetPoint
  70. {
  71. get
  72. {
  73. return _maxSetPoint;
  74. }
  75. set
  76. {
  77. _maxSetPoint = value;
  78. //if (_aoMax != null)
  79. // _aoMax.Value = (short)value;
  80. }
  81. }
  82. public double MinFlow
  83. {
  84. get
  85. {
  86. return _scMinFlow == null ? 0 : _scMinFlow.DoubleValue;
  87. }
  88. }
  89. public double MaxFlow
  90. {
  91. get
  92. {
  93. return _scMaxFlow == null ? 0 : _scMaxFlow.DoubleValue;
  94. }
  95. }
  96. public double WarningTime
  97. {
  98. get
  99. {
  100. return _scWarningTime == null ? 0 : _scWarningTime.IntValue;
  101. }
  102. }
  103. public double AlarmTime
  104. {
  105. get
  106. {
  107. return _scAlarmTime == null ? 0 : _scAlarmTime.IntValue;
  108. }
  109. }
  110. public bool IsOutOfTolerance
  111. {
  112. get
  113. {
  114. if (MinFlow < 0.01 && MaxFlow < 0.01)
  115. return false;
  116. return (Feedback < MinFlow) || (Feedback > MaxFlow);
  117. }
  118. }
  119. public bool EnableToleranceCheck { get; set; }
  120. private AITWaterFlowMeterData DeviceData
  121. {
  122. get
  123. {
  124. AITWaterFlowMeterData data = new AITWaterFlowMeterData()
  125. {
  126. DeviceName = Name,
  127. DeviceSchematicId = DeviceID,
  128. DisplayName = Display,
  129. FeedBack = Feedback,
  130. IsWarning = IsWarning,
  131. Unit = Unit,
  132. IsOutOfTolerance = IsOutOfTolerance,
  133. };
  134. return data;
  135. }
  136. }
  137. public string Unit { get; set; }
  138. private AIAccessor _aiFlowValue = null;
  139. //private DIAccessor _diValve;
  140. //private DOAccessor _doWarning;
  141. //private DOAccessor _doAlarm;
  142. //private AOAccessor _aoMin;
  143. //private AOAccessor _aoMax;
  144. private SCConfigItem _scMinFlow;
  145. private SCConfigItem _scMaxFlow;
  146. private SCConfigItem _scWarningTime;
  147. private SCConfigItem _scAlarmTime;
  148. private SCConfigItem _scEnableTolerance;
  149. private ToleranceChecker _checkWarning = new ToleranceChecker();
  150. private ToleranceChecker _checkAlarm = new ToleranceChecker();
  151. private RD_TRIG _trigValveOpenClose = new RD_TRIG();
  152. private int _factor;
  153. private int _rangeMin;
  154. private int _rangeMax;
  155. private int _physicalMin;
  156. private int _physicalMax;
  157. FixSizeQueue<double> _queueData = new FixSizeQueue<double>(20);
  158. public IoFlowMeter(string module, XmlElement node, string ioModule = "")
  159. {
  160. var attrModule = node.GetAttribute("module");
  161. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  162. Name = node.GetAttribute("id");
  163. Display = node.GetAttribute("display");
  164. DeviceID = node.GetAttribute("schematicId");
  165. Unit = node.GetAttribute("unit");
  166. _aiFlowValue = ParseAiNode("aiFeedback", node, ioModule);
  167. int.TryParse(node.GetAttribute("factor"), out _factor);
  168. string[] range = node.GetAttribute("range").Split(',');
  169. int.TryParse(range[0], out _rangeMin);
  170. int.TryParse(range[1], out _rangeMax);
  171. string[] physical = node.GetAttribute("physical").Split(',');
  172. int.TryParse(physical[0], out _physicalMin);
  173. int.TryParse(physical[1], out _physicalMax);
  174. _scMinFlow = SC.GetConfigItem($"Modules.{Module}.{Name}.MinFlow");
  175. _scMaxFlow = SC.GetConfigItem($"Modules.{Module}.{Name}.MaxFlow");
  176. _scWarningTime = SC.GetConfigItem($"Modules.{Module}.{Name}.WarningTime");
  177. _scAlarmTime = SC.GetConfigItem($"Modules.{Module}.{Name}.AlarmTime");
  178. _scAlarmTime = SC.GetConfigItem($"Modules.{Module}.{Name}.AlarmTime");
  179. _scEnableTolerance = SC.GetConfigItem($"Modules.{Module}.{Name}.EnableTolerance");
  180. MinSetPoint = MinFlow;
  181. MaxSetPoint = MaxFlow;
  182. }
  183. public bool Initialize()
  184. {
  185. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  186. DATA.Subscribe($"{Module}.{Name}.Feedback", () => Feedback);
  187. return true;
  188. }
  189. public void Terminate()
  190. {
  191. }
  192. public void Monitor()
  193. {
  194. MinSetPoint = MinFlow;
  195. MaxSetPoint = MaxFlow;
  196. _queueData.Enqueue(Feedback);
  197. //_trigValveOpenClose.CLK = (_diValve == null || _diValve.Value);
  198. //关闭阀门
  199. if (_trigValveOpenClose.T)
  200. {
  201. _checkWarning.RST = true;
  202. _checkAlarm.RST = true;
  203. }
  204. //打开过程中,一直监控
  205. if (_trigValveOpenClose.M && _scEnableTolerance.BoolValue)
  206. {
  207. if (WarningTime > 0.1)
  208. {
  209. _checkWarning.Monitor(Feedback, MinFlow, MaxFlow, WarningTime);
  210. if (_checkWarning.Trig)
  211. {
  212. EV.PostWarningLog(Module, $"Flow meter {Display} feedback out of range");
  213. }
  214. //if (_doWarning != null)
  215. //{
  216. // _doWarning.Value = _checkWarning.Result;
  217. //}
  218. }
  219. if (AlarmTime > 0.1)
  220. {
  221. _checkAlarm.Monitor(Feedback, MinFlow, MaxFlow, AlarmTime);
  222. if (_checkAlarm.Trig)
  223. {
  224. EV.PostAlarmLog(Module, $"Flow meter {Display} feedback out of range");
  225. }
  226. //if (_doAlarm != null)
  227. //{
  228. // _doAlarm.Value = _checkAlarm.Result;
  229. //}
  230. }
  231. }
  232. }
  233. public void Reset()
  234. {
  235. _checkWarning.RST = true;
  236. _checkAlarm.RST = true;
  237. }
  238. }
  239. }