IoPressureMeter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.SCCore;
  7. using Aitex.Core.RT.Tolerance;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.Event;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Xml;
  16. namespace FurnaceRT.Devices
  17. {
  18. public class IoPressureMeter : BaseDevice, IDevice
  19. {
  20. public double Value
  21. {
  22. get
  23. {
  24. return FeedBack;
  25. }
  26. }
  27. public double FeedBack
  28. {
  29. get
  30. {
  31. if (_aiValue != null)
  32. {
  33. double _feedback = _isFloatAioType ? _aiValue.FloatValue : _aiValue.Value;
  34. return _unSigned ? Math.Max(0, _feedback) : _feedback;
  35. }
  36. return 0;
  37. }
  38. }
  39. public double Precision
  40. {
  41. get
  42. {
  43. return _scPrecision == null ? 1000 : _scPrecision.DoubleValue;
  44. }
  45. }
  46. private AITWaterFlowMeterData DeviceData
  47. {
  48. get
  49. {
  50. AITWaterFlowMeterData data = new AITWaterFlowMeterData()
  51. {
  52. DeviceName = Name,
  53. DeviceSchematicId = DeviceID,
  54. DisplayName = Display,
  55. FeedBack = FeedBack,
  56. Unit = Unit
  57. };
  58. return data;
  59. }
  60. }
  61. public bool IsWarning
  62. {
  63. get
  64. {
  65. return _checkWarning.Result;
  66. }
  67. }
  68. public bool IsError
  69. {
  70. get
  71. {
  72. return _checkAlarm.Result;
  73. }
  74. }
  75. public double MinPressure
  76. {
  77. get
  78. {
  79. return _scMinValue == null ? 0 : _scMinValue.DoubleValue;
  80. }
  81. }
  82. public double MaxPressure
  83. {
  84. get
  85. {
  86. return _scMaxValue == null ? 0 : _scMaxValue.DoubleValue;
  87. }
  88. }
  89. public int WarningTime
  90. {
  91. get
  92. {
  93. return _scWarningTime == null ? 0 : _scWarningTime.IntValue;
  94. }
  95. }
  96. public int AlarmTime
  97. {
  98. get
  99. {
  100. return _scAlarmTime == null ? 0 : _scAlarmTime.IntValue;
  101. }
  102. }
  103. public bool EnableAlarm
  104. {
  105. get
  106. {
  107. return _scEnableAlarm == null ? true : _scEnableAlarm.BoolValue;
  108. }
  109. }
  110. public bool IsOutOfRange
  111. {
  112. get
  113. {
  114. if (MinPressure < 0.01 && MaxPressure < 0.01)
  115. return false;
  116. return (Value < MinPressure) || (Value > MaxPressure);
  117. }
  118. }
  119. public string Unit { get; set; }
  120. private AIAccessor _aiValue = null;
  121. private string _formatString = "F5";
  122. //是否为无符号数(只显示大于等于0)
  123. private bool _unSigned = false;
  124. private SCConfigItem _scMinValue;
  125. private SCConfigItem _scMaxValue;
  126. private SCConfigItem _scEnableAlarm;
  127. private SCConfigItem _scWarningTime;
  128. private SCConfigItem _scAlarmTime;
  129. private SCConfigItem _scPrecision;
  130. private ToleranceChecker _checkWarning = new ToleranceChecker();
  131. private ToleranceChecker _checkAlarm = new ToleranceChecker();
  132. public AlarmEventItem AlarmToleranceWarning { get; set; }
  133. public AlarmEventItem AlarmToleranceAlarm { get; set; }
  134. private bool _isFloatAioType = false;
  135. private float _tuningPercent;
  136. private double _rangeMin = Int16.MinValue * 0.1;
  137. private double _rangeMax = Int16.MaxValue * 0.9;
  138. private double _min = Int16.MinValue * 0.1;
  139. private double _max = Int16.MaxValue * 0.9;
  140. public IoPressureMeter(string module, XmlElement node, string ioModule = "")
  141. {
  142. var attrModule = node.GetAttribute("module");
  143. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  144. Name = node.GetAttribute("id");
  145. Display = node.GetAttribute("display");
  146. DeviceID = node.GetAttribute("schematicId");
  147. Unit = node.GetAttribute("unit");
  148. _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType").ToLower() == "float");
  149. _aiValue = ParseAiNode("aiFeedback", node, ioModule);
  150. string[] range = node.GetAttribute("scale").Split(',');
  151. double.TryParse(range[0], out _rangeMin);
  152. double.TryParse(range[1], out _rangeMax);
  153. string[] physical = node.GetAttribute("physical").Split(',');
  154. double.TryParse(physical[0], out _min);
  155. double.TryParse(physical[1], out _max);
  156. if (node.HasAttribute("formatString"))
  157. _formatString = string.IsNullOrEmpty(node.GetAttribute("formatString")) ? "F5" : node.GetAttribute("formatString");
  158. string scBasePath = node.GetAttribute("scBasePath");
  159. if (string.IsNullOrEmpty(scBasePath))
  160. scBasePath = $"{Module}.{Name}";
  161. else
  162. {
  163. scBasePath = scBasePath.Replace("{module}", Module);
  164. }
  165. if (node.HasAttribute("unSigned"))
  166. {
  167. string unSignedStr = node.GetAttribute("unSigned");
  168. if (!string.IsNullOrEmpty(unSignedStr))
  169. {
  170. bool.TryParse(unSignedStr, out _unSigned);
  171. }
  172. }
  173. //_scale = SC.GetValue<double>($"{scBasePath}.PressureScale");
  174. //_phyScale = SC.GetValue<double>($"{scBasePath}.PhyScale");
  175. _scMinValue = ParseScNode("", node, "", $"{scBasePath}.{Name}.MinValue");
  176. _scMaxValue = ParseScNode("", node, "", $"{scBasePath}.{Name}.MaxValue");
  177. _scEnableAlarm = ParseScNode("", node, "", $"{scBasePath}.{Name}.EnableAlarm");
  178. _scWarningTime = ParseScNode("", node, "", $"{scBasePath}.{Name}.WarningTime");
  179. _scAlarmTime = ParseScNode("", node, "", $"{scBasePath}.{Name}.AlarmTime");
  180. _scPrecision = ParseScNode("", node, "", $"{scBasePath}.{Name}.Precision");
  181. }
  182. public bool Initialize()
  183. {
  184. DATA.Subscribe($"{Module}.{Name}.Value", () => Value);
  185. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  186. //AlarmToleranceWarning = SubscribeAlarm($"{Module}.{Name}.OutOfToleranceWarning", "", ResetWarningChecker, EventLevel.Warning);
  187. //AlarmToleranceError = SubscribeAlarm($"{Module}.{Name}.OutOfToleranceError", "", ResetErrorChecker);
  188. AlarmToleranceWarning = SubscribeAlarm(new AlarmEventItem()
  189. {
  190. EventEnum = $"{Module}.{Name}ToleranceWarning",
  191. Description = $"{Name} tolerance warning ",
  192. Solution = "No information available. Press[Clear] to delete alarm message.",
  193. Explaination = "No information available.",
  194. AutoRecovery = false,
  195. Level = EventLevel.Warning,
  196. Action = EventAction.Clear,
  197. Category = "TubeAlarm",
  198. }, () => { Reset(); return true; });
  199. AlarmToleranceAlarm = SubscribeAlarm(new AlarmEventItem()
  200. {
  201. EventEnum = $"{Module}.{Name}ToleranceAlarm",
  202. Description = $"{Name} tolerance alarm ",
  203. Solution = "No information available. Press[Clear] to delete alarm message.",
  204. Explaination = "No information available.",
  205. AutoRecovery = false,
  206. Level = EventLevel.Alarm,
  207. Action = EventAction.Clear,
  208. Category = "TubeAlarm",
  209. }, () => { Reset(); return true; });
  210. return true;
  211. }
  212. public void Terminate()
  213. {
  214. }
  215. public void SetTuning(float percent)
  216. {
  217. if (percent > 0 && percent <= 100)
  218. _tuningPercent = percent;
  219. }
  220. public void UnsetTuning()
  221. {
  222. _tuningPercent = 0;
  223. }
  224. public void Monitor()
  225. {
  226. if (EnableAlarm && (WarningTime > 0))
  227. {
  228. _checkWarning.Monitor(Value, MinPressure, MaxPressure, WarningTime);
  229. if (_checkWarning.Trig)
  230. {
  231. AlarmToleranceWarning.Description =
  232. $"{Display} out of range [{MinPressure},{MaxPressure}]{Unit} for {WarningTime} seconds";
  233. AlarmToleranceWarning.Set();
  234. }
  235. if (!_checkWarning.Result)
  236. {
  237. AlarmToleranceWarning.Reset();
  238. }
  239. }
  240. else
  241. {
  242. AlarmToleranceWarning.Reset();
  243. }
  244. if (EnableAlarm && (AlarmTime > 0))
  245. {
  246. _checkAlarm.Monitor(Value, MinPressure, MaxPressure, AlarmTime);
  247. if (_checkAlarm.Trig)
  248. {
  249. AlarmToleranceAlarm.Description =
  250. $"{Display} out of range [{MinPressure},{MaxPressure}]{Unit} for {AlarmTime} seconds";
  251. AlarmToleranceAlarm.Set();
  252. }
  253. if (!_checkAlarm.Result)
  254. {
  255. AlarmToleranceAlarm.Reset();
  256. }
  257. }
  258. else
  259. {
  260. AlarmToleranceAlarm.Reset();
  261. }
  262. }
  263. public bool ResetWarningChecker()
  264. {
  265. _checkWarning.RST = true;
  266. return true;
  267. }
  268. public bool ResetErrorChecker()
  269. {
  270. _checkAlarm.RST = true;
  271. return true;
  272. }
  273. public void Reset()
  274. {
  275. _checkWarning.RST = true;
  276. _checkAlarm.RST = true;
  277. AlarmToleranceWarning.Reset();
  278. AlarmToleranceAlarm.Reset();
  279. }
  280. private double GetAIScale()
  281. {
  282. return Converter.Phy2Logic(_isFloatAioType ? _aiValue.FloatValue : _aiValue.Value, _rangeMin, _rangeMax, _min, _max);
  283. }
  284. }
  285. }