IoMFM.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.IOCore;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.OperationCenter;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.RT.Tolerance;
  10. using Aitex.Core.Util;
  11. using Aitex.Core.Utilities;
  12. using MECF.Framework.Common.Event;
  13. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MFCs;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Xml;
  17. namespace FurnaceRT.Equipments.PMs.Devices
  18. {
  19. public class IoMFM : BaseDevice, IDevice
  20. {
  21. public string Unit
  22. {
  23. get; set;
  24. }
  25. public double Scale
  26. {
  27. get
  28. {
  29. if (_scN2Scale == null)
  30. return 0;
  31. return _scN2Scale.DoubleValue;
  32. }
  33. }
  34. public double FeedBack
  35. {
  36. get
  37. {
  38. if (_aiFlow != null)
  39. {
  40. return _isFloatAioType ? _aiFlow.FloatValue : _aiFlow.Value;
  41. }
  42. return 0;
  43. }
  44. }
  45. public bool EnableAlarm
  46. {
  47. get
  48. {
  49. if (_scEnableAlarm != null)
  50. return _scEnableAlarm.BoolValue;
  51. return false;
  52. }
  53. }
  54. private AITWaterFlowMeterData DeviceData
  55. {
  56. get
  57. {
  58. AITWaterFlowMeterData data = new AITWaterFlowMeterData()
  59. {
  60. DeviceName = Name,
  61. DeviceSchematicId = DeviceID,
  62. DisplayName = DisplayName,
  63. FeedBack = FeedBack < 0 ? 0 : FeedBack,
  64. AlarmWatchTable = AlarmWatchTable,
  65. Unit = Unit,
  66. };
  67. return data;
  68. }
  69. }
  70. public string DisplayName
  71. {
  72. get
  73. {
  74. if (_scGasName != null)
  75. return _scGasName.StringValue;
  76. return Display;
  77. }
  78. }
  79. public string AlarmWatchTable
  80. {
  81. get;
  82. set;
  83. }
  84. private float _setpoint;
  85. private double _phyScale;
  86. private ToleranceChecker _toleranceCheckerWarning = new ToleranceChecker();
  87. private ToleranceChecker _toleranceCheckerAlarm = new ToleranceChecker();
  88. private AIAccessor _aiFlow;
  89. private AIAccessor _aiCheck;
  90. protected SCConfigItem _scGasName;
  91. private SCConfigItem _scN2Scale;
  92. private SCConfigItem _scEnableAlarm;
  93. //tolerance check
  94. private float _alarmJudgmentRange;
  95. private float _warningJudgmentRange;
  96. private float _alarmJudgmentTime;
  97. private float _warningJudgmentTime;
  98. private float _toleranceJudgmentDelayTime;
  99. private DeviceTimer _toleranceJudgmentDelayTimer = new DeviceTimer();
  100. private int _toleranceAlarmEventId;
  101. private int _toleranceWarningEventId;
  102. public AlarmEventItem AlarmToleranceWarning { get; set; }
  103. public AlarmEventItem AlarmToleranceAlarm { get; set; }
  104. private bool _isFloatAioType = false;
  105. public IoMFM(string module, XmlElement node, string ioModule = "")
  106. {
  107. Unit = node.GetAttribute("unit");
  108. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  109. base.Name = node.GetAttribute("id");
  110. base.Display = node.GetAttribute("display");
  111. base.DeviceID = node.GetAttribute("schematicId");
  112. int.TryParse(node.GetAttribute("toleranceAlarmEventId"), out _toleranceAlarmEventId);
  113. int.TryParse(node.GetAttribute("toleranceWarningEventId"), out _toleranceWarningEventId);
  114. _aiFlow = ParseAiNode("aiFlow", node, ioModule);
  115. _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType") == "float");
  116. string scBasePath = node.GetAttribute("scBasePath");
  117. if (string.IsNullOrEmpty(scBasePath))
  118. scBasePath = $"{Module}.{Name}";
  119. else
  120. {
  121. scBasePath = scBasePath.Replace("{module}", Module);
  122. }
  123. _scGasName = SC.GetConfigItem($"{scBasePath}.{Name}.Name");
  124. _scN2Scale = ParseScNode("scN2Scale", node, ioModule, $"{scBasePath}.{Name}.N2Scale");
  125. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{scBasePath}.{Name}.EnableAlarm");
  126. if (SC.ContainsItem($"{scBasePath}.{Name}.FlowUnit"))
  127. Unit = SC.GetStringValue($"{scBasePath}.{Name}.FlowUnit");
  128. }
  129. public bool Initialize()
  130. {
  131. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  132. DATA.Subscribe($"{Module}.{Name}.Feedback", () => FeedBack);
  133. DATA.Subscribe($"{Module}.{Name}._setpoint", () => _setpoint);
  134. OP.Subscribe($"{Module}.{Name}.SetParameters", SetParameters);
  135. OP.Subscribe($"{Module}.{Name}.SetMfmValue", SetMfmValue);
  136. //_phyScale = SC.GetValue<double>($"{Module}.MFM.{Name}.PhyScale");
  137. //AlarmToleranceWarning = SubscribeAlarm($"{Module}.{Name}.ToleranceWarning", "", ResetWarningChecker, EventLevel.Warning);
  138. //AlarmToleranceAlarm = SubscribeAlarm($"{Module}.{Name}.ToleranceAlarm", "", ResetAlarmChecker);
  139. //AlarmToleranceWarning.Id = _toleranceWarningEventId;
  140. //AlarmToleranceAlarm.Id = _toleranceAlarmEventId;
  141. return true;
  142. }
  143. public void Monitor()
  144. {
  145. MonitorTolerance();
  146. }
  147. public bool ResetWarningChecker()
  148. {
  149. _toleranceCheckerWarning.Reset(_warningJudgmentTime);
  150. return true;
  151. }
  152. public bool ResetAlarmChecker()
  153. {
  154. _toleranceCheckerAlarm.Reset(_alarmJudgmentTime);
  155. return true;
  156. }
  157. public void Reset()
  158. {
  159. //AlarmToleranceWarning.Reset();
  160. //AlarmToleranceAlarm.Reset();
  161. }
  162. public void Terminate()
  163. {
  164. }
  165. private bool SetParameters(out string reason, int time, object[] param)
  166. {
  167. reason = string.Empty;
  168. var paras = param[0].ToString().Split(';');//flow;alarmTable
  169. if (System.Text.RegularExpressions.Regex.Match(paras[0].ToString(), @"[a-zA-Z]").Success)
  170. {
  171. var table = paras[0].ToString().Split(':')[0];
  172. if (SC.ContainsItem($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}"))
  173. _setpoint = (float)SC.GetValue<double>($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}");
  174. }
  175. else
  176. {
  177. float.TryParse(param[0].ToString(), out _setpoint);
  178. _setpoint = (float)Converter.Logic2Phy(_setpoint, 0, Scale, 0, _phyScale);
  179. }
  180. if (paras.Length >= 2)
  181. {
  182. var alarmWatchTable = paras[1].ToString();
  183. if (SC.ContainsItem($"{Module}.RecipeEditParameter.AlarmWatchTable.FlowAlarmWatch.Table{alarmWatchTable}.DelayTime"))
  184. {
  185. var scPath = $"{Module}.RecipeEditParameter.AlarmWatchTable.PressureAlarmWatch.Table{alarmWatchTable}";
  186. _toleranceJudgmentDelayTime = (float)SC.GetValue<double>($"{scPath}.DelayTime");
  187. _alarmJudgmentRange = (float)SC.GetValue<double>($"{scPath}.AlarmJudgment");
  188. _warningJudgmentRange = (float)SC.GetValue<double>($"{scPath}.WarningJudgment");
  189. _alarmJudgmentTime = (float)SC.GetValue<double>($"{scPath}.AlarmJudgmentTime");
  190. _warningJudgmentTime = (float)SC.GetValue<double>($"{scPath}.WarningJudgmentTime");
  191. _toleranceJudgmentDelayTimer.Start(0);
  192. }
  193. else
  194. {
  195. _toleranceJudgmentDelayTime = 0;
  196. _alarmJudgmentRange = 0;
  197. _warningJudgmentRange = 0;
  198. _alarmJudgmentTime = 0;
  199. _warningJudgmentTime = 0;
  200. _toleranceJudgmentDelayTimer.Stop();
  201. }
  202. ResetWarningChecker();
  203. ResetAlarmChecker();
  204. }
  205. return true;
  206. }
  207. private bool SetMfmValue(out string reason, int time, object[] param)
  208. {
  209. reason = string.Empty;
  210. var paras = param[0].ToString().Split(';'); // setpoint;alarmWatchTable
  211. float.TryParse(paras[0].ToString(), out _setpoint);
  212. _setpoint = (float)Converter.Logic2Phy(_setpoint, 0, Scale, 0, _phyScale);
  213. if (paras.Length > 1)
  214. {
  215. AlarmWatchTable = paras[1];
  216. if (SC.ContainsItem($"{Module}.RecipeEditParameter.AlarmWatchTable.FlowAlarmWatch.Table{AlarmWatchTable}.DelayTime"))
  217. {
  218. var scPath = $"{Module}.RecipeEditParameter.AlarmWatchTable.PressureAlarmWatch.Table{AlarmWatchTable}";
  219. _toleranceJudgmentDelayTime = (float)SC.GetValue<double>($"{scPath}.DelayTime");
  220. _alarmJudgmentRange = (float)SC.GetValue<double>($"{scPath}.AlarmJudgment");
  221. _warningJudgmentRange = (float)SC.GetValue<double>($"{scPath}.WarningJudgment");
  222. _alarmJudgmentTime = (float)SC.GetValue<double>($"{scPath}.AlarmJudgmentTime");
  223. _warningJudgmentTime = (float)SC.GetValue<double>($"{scPath}.WarningJudgmentTime");
  224. }
  225. else
  226. {
  227. _toleranceJudgmentDelayTime = 0;
  228. _alarmJudgmentRange = 0;
  229. _warningJudgmentRange = 0;
  230. _alarmJudgmentTime = 0;
  231. _warningJudgmentTime = 0;
  232. }
  233. }
  234. return true;
  235. }
  236. private void MonitorTolerance()
  237. {
  238. if (!EnableAlarm || _setpoint < 0.01 ||
  239. _toleranceJudgmentDelayTimer.IsIdle() ||
  240. _toleranceJudgmentDelayTimer.GetElapseTime() < _toleranceJudgmentDelayTime * 1000)
  241. {
  242. _toleranceCheckerWarning.RST = true;
  243. _toleranceCheckerAlarm.RST = true;
  244. return;
  245. }
  246. if (_warningJudgmentRange != 0 && _warningJudgmentTime > 0)
  247. {
  248. _toleranceCheckerWarning.Monitor(FeedBack, (_setpoint - Math.Abs(_warningJudgmentRange)), (_setpoint + Math.Abs(_warningJudgmentRange)), _warningJudgmentTime);
  249. if (_toleranceCheckerWarning.Trig)
  250. {
  251. //AlarmToleranceWarning.Description = $"{Display} flow out of range {_warningJudgmentRange} {Unit} in {_warningJudgmentTime:F0} seconds";
  252. AlarmToleranceWarning.Set($"{Display} flow out of range {_warningJudgmentRange} {Unit} in {_warningJudgmentTime:F0} seconds");
  253. }
  254. }
  255. if (_alarmJudgmentRange != 0 && _alarmJudgmentTime > 0)
  256. {
  257. _toleranceCheckerAlarm.Monitor(FeedBack, (_setpoint - Math.Abs(_alarmJudgmentRange)), (_setpoint + Math.Abs(_alarmJudgmentRange)), _alarmJudgmentTime);
  258. if (_toleranceCheckerAlarm.Trig)
  259. {
  260. //AlarmToleranceAlarm.Description = $"{Display} flow out of range {_alarmJudgmentRange} {Unit} in {_alarmJudgmentTime:F0} seconds";
  261. AlarmToleranceAlarm.Set($"{Display} flow out of range {_alarmJudgmentRange} {Unit} in {_alarmJudgmentTime:F0} seconds");
  262. }
  263. }
  264. }
  265. }
  266. }