Fdc.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.SCCore;
  11. using Aitex.Core.Util;
  12. using MECF.Framework.Common.CommonData;
  13. using MECF.Framework.Common.SCCore;
  14. namespace JetVirgoPM.PMs.Routines
  15. {
  16. public class Fdc
  17. {
  18. public List<FdcDataItem> DataList
  19. {
  20. get
  21. {
  22. return _lstItems;
  23. }
  24. }
  25. private List<FdcDataItem> _lstItems = new List<FdcDataItem>();
  26. private PeriodicJob _monitorThread;
  27. private string _module;
  28. private Stopwatch _delaytimer = new Stopwatch();
  29. private int _delayTime;
  30. private Dictionary<string, string> _setpointDataMap = new Dictionary<string, string>();
  31. public Fdc(string module)
  32. {
  33. _monitorThread = new PeriodicJob(300, OnMonitor, "fdc thread");
  34. _module = module;
  35. }
  36. public void Reset()
  37. {
  38. _lstItems.Clear();
  39. _setpointDataMap.Clear();
  40. string groupName = SC.GetStringValue("System.FDC.DataGroupName");
  41. if (string.IsNullOrEmpty(groupName))
  42. groupName = "Process";
  43. int interval = SC.GetValue<int>("System.FDC.SampleInterval");
  44. if (interval < 50)
  45. interval = 50;
  46. _delayTime = SC.GetValue<int>("System.FDC.DelayTime");
  47. if (_delayTime < 0)
  48. _delayTime = 10;
  49. var content = TypedConfigManager.Instance.GetTypedConfigContent("DataGroup", "");
  50. XmlDocument xmlContent = new XmlDocument();
  51. xmlContent.LoadXml(content);
  52. var items = xmlContent.SelectNodes($"DataGroupConfig/DataGroup[@name='{groupName}']/DataItem");
  53. foreach (var item in items)
  54. {
  55. var node = item as XmlElement;
  56. string name = node.GetAttribute("name");
  57. if (!string.IsNullOrEmpty(name) && name.StartsWith($"{_module}.") && (_lstItems.FirstOrDefault(x => x.Name == name) == null))
  58. {
  59. var dataType = Singleton<DataManager>.Instance.GetDataType(name);
  60. if (dataType == typeof(double) || dataType == typeof(float) ||
  61. dataType == typeof(int) || dataType == typeof(ushort) || dataType == typeof(short))
  62. {
  63. _lstItems.Add(new FdcDataItem()
  64. {
  65. Name = name,
  66. });
  67. }
  68. }
  69. }
  70. var fdcMap = xmlContent.SelectNodes($"DataGroupConfig/DataGroup[@name='Fdc']/DataItem");
  71. foreach (var item in fdcMap)
  72. {
  73. var node = item as XmlElement;
  74. string name = node.GetAttribute("name");
  75. if (!string.IsNullOrEmpty(name) && name.StartsWith($"{_module}.") && (_lstItems.FirstOrDefault(x => x.Name == name) != null))
  76. {
  77. string controlName = node.GetAttribute("control_name");
  78. if (!string.IsNullOrEmpty(controlName))
  79. _setpointDataMap[name] = controlName;
  80. }
  81. }
  82. _monitorThread.ChangeInterval(interval);
  83. }
  84. //pair: controlname - setpoint value
  85. public void Start(Dictionary<string, string> setpointControlData)
  86. {
  87. ClearPreviousData();
  88. foreach (var fdcDataItem in _lstItems)
  89. {
  90. if (!_setpointDataMap.ContainsKey(fdcDataItem.Name))
  91. continue;
  92. if (!setpointControlData.ContainsKey(_setpointDataMap[fdcDataItem.Name]))
  93. continue;
  94. if (!float.TryParse(setpointControlData[_setpointDataMap[fdcDataItem.Name]], out float floatValue))
  95. continue;
  96. fdcDataItem.SetPoint = floatValue;
  97. }
  98. _delaytimer.Restart();
  99. _monitorThread.Start();
  100. }
  101. public void Stop()
  102. {
  103. _monitorThread.Pause();
  104. ClearPreviousData();
  105. }
  106. public void ClearPreviousData()
  107. {
  108. foreach (var fdcDataItem in _lstItems)
  109. {
  110. fdcDataItem.Clear();
  111. }
  112. }
  113. private bool OnMonitor()
  114. {
  115. try
  116. {
  117. if (_delaytimer.IsRunning)
  118. {
  119. if (_delaytimer.ElapsedMilliseconds < _delayTime)
  120. return true;
  121. else
  122. {
  123. _delaytimer.Stop();
  124. }
  125. }
  126. foreach (var fdcDataItem in _lstItems)
  127. {
  128. var objValue = DATA.Poll(fdcDataItem.Name);
  129. float floatValue = 0f;
  130. if (objValue != null)
  131. {
  132. float.TryParse(objValue.ToString(), out floatValue);
  133. }
  134. fdcDataItem.Update(floatValue);
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. LOG.Write(ex);
  140. }
  141. return true;
  142. }
  143. }
  144. }