GasSplitterBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.Device;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.IOCore;
  12. using Aitex.Core.RT.OperationCenter;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.RT.Tolerance;
  15. using Aitex.Core.Util;
  16. namespace MECF.Framework.Common.Device.Bases
  17. {
  18. public abstract class GasSplitterBase : BaseDevice, IDevice
  19. {
  20. public string Unit
  21. {
  22. get; set;
  23. }
  24. public virtual float Scale
  25. {
  26. get
  27. {
  28. if (_scFullScale == null)
  29. return 100;
  30. return (float)(_scFullScale.DoubleValue);
  31. }
  32. }
  33. public virtual float SetPoint { get; set; }
  34. public virtual float FeedBack { get; set; }
  35. public virtual float SwitchFeedBack { get; set; }
  36. public bool IsOutOfTolerance
  37. {
  38. get
  39. {
  40. return _toleranceAlarmChecker.Result;
  41. }
  42. }
  43. public virtual bool EnableAlarm
  44. {
  45. get
  46. {
  47. if (_scEnableAlarm != null)
  48. return _scEnableAlarm.BoolValue;
  49. return false;
  50. }
  51. }
  52. public virtual double AlarmTime
  53. {
  54. get
  55. {
  56. if (_scAlarmTime != null)
  57. return _scAlarmTime.IntValue != 0 ? _scAlarmTime.IntValue : _scAlarmTime.DoubleValue;
  58. return 10;
  59. }
  60. }
  61. public virtual double AlarmRange
  62. {
  63. get
  64. {
  65. if (_currentAlarmRange > 0)
  66. return _currentAlarmRange;
  67. if (_scAlarmRange != null)
  68. return _scAlarmRange.DoubleValue;
  69. return 0;
  70. }
  71. }
  72. public virtual double WarningTime
  73. {
  74. get
  75. {
  76. if (_scWarningTime != null)
  77. return _scWarningTime.DoubleValue;
  78. return 0;
  79. }
  80. }
  81. public virtual double WarningRange
  82. {
  83. get
  84. {
  85. if (_currentWarningRange > 0)
  86. return _currentWarningRange;
  87. if (_scWarningRange != null)
  88. return _scWarningRange.DoubleValue;
  89. return 0;
  90. }
  91. }
  92. public virtual double FineTuningValue
  93. {
  94. get
  95. {
  96. if (_scFineTuningEnable == null || !_scFineTuningEnable.BoolValue)
  97. return 1;
  98. if (_currentFineTuningValue != 0)
  99. return 1 + _currentFineTuningValue / 100;
  100. return _scFineTuningValue != null ? 1 + _scFineTuningValue.DoubleValue / 100 : 1;
  101. }
  102. }
  103. public virtual AITGasSplitterData DeviceData
  104. {
  105. get
  106. {
  107. AITGasSplitterData data = new AITGasSplitterData()
  108. {
  109. UniqueName = _uniqueName,
  110. Type = "Splitter",
  111. DeviceName = Name,
  112. DeviceSchematicId = DeviceID,
  113. DisplayName = "",
  114. FeedBack = FeedBack,
  115. SetPoint = SetPoint,
  116. Scale = Scale,
  117. };
  118. return data;
  119. }
  120. }
  121. private DeviceTimer _rampTimer = new DeviceTimer();
  122. protected float _rampTarget;
  123. private float _rampInitValue;
  124. private int _rampTime;
  125. private float _currentWarningRange;
  126. private float _currentAlarmRange;
  127. protected ToleranceChecker _toleranceAlarmChecker = new ToleranceChecker();
  128. protected ToleranceChecker _toleranceWarningChecker = new ToleranceChecker();
  129. protected SCConfigItem _scFullScale;
  130. protected SCConfigItem _scAlarmRange;
  131. protected SCConfigItem _scEnableAlarm;
  132. protected SCConfigItem _scAlarmTime;
  133. protected SCConfigItem _scWarningTime;
  134. protected SCConfigItem _scWarningRange;
  135. protected SCConfigItem _scDefaultSetPoint;
  136. protected SCConfigItem _scRegulationFactor;
  137. private SCConfigItem _scFineTuningEnable;
  138. private SCConfigItem _scFineTuningValue;
  139. protected double _currentFineTuningValue;
  140. protected string _uniqueName;
  141. public GasSplitterBase() : base()
  142. {
  143. }
  144. public GasSplitterBase(string module, XmlElement node, string ioModule = "")
  145. {
  146. Unit = node.GetAttribute("unit");
  147. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  148. base.Name = node.GetAttribute("id");
  149. base.Display = node.GetAttribute("display");
  150. base.DeviceID = node.GetAttribute("schematicId");
  151. _scFullScale = ParseScNode("scFullScale", node, ioModule, $"{Module}.{Name}.FullScale");
  152. _scAlarmRange = ParseScNode("scAlarmRange", node, ioModule, $"{Module}.{Name}.AlarmRange");
  153. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{Module}.{Name}.EnableAlarm");
  154. _scAlarmTime = ParseScNode("scAlarmTime", node, ioModule, $"{Module}.{Name}.AlarmTime");
  155. _scWarningTime = ParseScNode("scWarningTime", node, ioModule, $"{Module}.{DeviceID}.WarningTime");
  156. _scWarningRange = ParseScNode("scWarningRange", node, ioModule, $"{Module}.{DeviceID}.WarningRange");
  157. _scFineTuningValue = ParseScNode("scFineTuningValue", node, ioModule, $"{Module}.FineTuning.{Name}");
  158. _scFineTuningEnable = ParseScNode("scFineTuningEnable", node, ioModule, $"{Module}.FineTuning.IsEnable");
  159. _scDefaultSetPoint = ParseScNode("scDefaultSetPoint", node);
  160. _scRegulationFactor = ParseScNode("scFlowRegulationFactor", node, ioModule, $"{Module}.{Name}.RegulationFactor");
  161. _uniqueName = $"{Module}.{Name}";
  162. }
  163. public virtual bool Initialize()
  164. {
  165. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  166. DATA.Subscribe($"{Module}.{Name}.RatioFeedback", () => FeedBack);
  167. DATA.Subscribe($"{Module}.{Name}.RatioSetPoint", () => SetPoint);
  168. OP.Subscribe($"{Module}.{Name}.SetRatio", (function, args) =>
  169. {
  170. SetRatio(Convert.ToSingle(args[0]));
  171. return true;
  172. });
  173. //for recipe
  174. OP.Subscribe($"{Module}.{Name}.SetRatio", (out string reason, int time, object[] param) =>
  175. {
  176. reason = string.Empty;
  177. SetRatio(Convert.ToSingle(param[0]));
  178. return true;
  179. });
  180. //for recipe
  181. OP.Subscribe($"{Module}.{Name}.SetFineTuning", (out string reason, int time, object[] param) =>
  182. {
  183. reason = string.Empty;
  184. SetFineTuning(Convert.ToSingle(param[0]));
  185. return true;
  186. });
  187. //for recipe
  188. OP.Subscribe($"{Module}.{Name}.SetTolerance", (out string reason, int time, object[] param) =>
  189. {
  190. reason = string.Empty;
  191. var warning = Convert.ToSingle(param[0]);
  192. var alarm = Convert.ToSingle(param[1]);
  193. SetTolerance((float)warning, (float)alarm);
  194. return true;
  195. });
  196. return true;
  197. }
  198. public virtual void SetTolerance(float warning, float alarm)
  199. {
  200. _currentWarningRange = warning;
  201. _currentAlarmRange = alarm;
  202. _toleranceAlarmChecker.Reset(AlarmTime);
  203. _toleranceWarningChecker.Reset(WarningTime);
  204. }
  205. public virtual void CheckTolerance()
  206. {
  207. if (!EnableAlarm || SetPoint == 0)
  208. return;
  209. _toleranceAlarmChecker.Monitor(FeedBack, (SetPoint * (1 - AlarmRange / 100)), (SetPoint * (1 + AlarmRange / 100)), AlarmTime);
  210. _toleranceWarningChecker.Monitor(FeedBack, (SetPoint * (1 - WarningRange / 100)), (SetPoint * (1 + WarningRange / 100)), WarningTime);
  211. }
  212. public virtual bool CheckToleranceAlarm()
  213. {
  214. if (!EnableAlarm)
  215. return false;
  216. return _toleranceAlarmChecker.Result;
  217. }
  218. public virtual bool CheckToleranceWarning()
  219. {
  220. if (!EnableAlarm)
  221. return false;
  222. return _toleranceWarningChecker.Result;
  223. }
  224. public virtual void SetFineTuning(float fineTuning)
  225. {
  226. _currentFineTuningValue = fineTuning;
  227. }
  228. public virtual void SetRatio(float ratio)
  229. {
  230. _rampTarget = ratio;
  231. }
  232. private bool InvokeRamp(string method, object[] args)
  233. {
  234. float target = Convert.ToSingle((string)(args[0].ToString()));
  235. target = Math.Min(target, Scale);
  236. target = Math.Max(target, 0);
  237. int time = 0;
  238. if (args.Length >= 2)
  239. time = Convert.ToInt32((string)(args[1].ToString()));
  240. Ramp(target, time);
  241. EV.PostInfoLog(Module, $"{_uniqueName} ramp to {target}{Unit} in {time} seconds");
  242. return true;
  243. }
  244. public virtual void Monitor()
  245. {
  246. MonitorRamping();
  247. MonitorTolerance();
  248. }
  249. public virtual void Reset()
  250. {
  251. _toleranceAlarmChecker.Reset(AlarmTime);
  252. _toleranceWarningChecker.Reset(WarningTime);
  253. }
  254. public virtual void Terminate()
  255. {
  256. Ramp(0, 0);
  257. }
  258. public void Ramp(int time)
  259. {
  260. Ramp(0, time);
  261. }
  262. public void Ramp(float target, int time)
  263. {
  264. target = Math.Max(0, target);
  265. target = Math.Min(Scale, target);
  266. _rampInitValue = SetPoint; //ramp 初始值取当前设定值,而非实际读取值.零漂问题
  267. _rampTime = time;
  268. _rampTarget = target;
  269. _rampTimer.Start(_rampTime);
  270. }
  271. public void StopRamp()
  272. {
  273. Ramp(SetPoint, 0);
  274. }
  275. private void MonitorRamping()
  276. {
  277. if (_rampTimer.IsTimeout() || _rampTime == 0)
  278. {
  279. SetPoint = _rampTarget;
  280. }
  281. else
  282. {
  283. SetPoint = (float)(_rampInitValue + (_rampTarget - _rampInitValue) * _rampTimer.GetElapseTime() / _rampTime);
  284. }
  285. }
  286. protected virtual void MonitorTolerance()
  287. {
  288. CheckTolerance();
  289. }
  290. }
  291. }