IoMfm.cs 12 KB

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