PollingService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using EDosApiWrapper;
  2. using GDNXFD.Data;
  3. using GDNXFD.Data.Repositories;
  4. using log4net;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading;
  9. namespace GDNXFD.Alert.Interpreter
  10. {
  11. public class PollingService
  12. {
  13. private ILog logger = LogManager.GetLogger("AppLog");
  14. private bool cancelPolling = false;
  15. private bool isRunning = false;
  16. private int pollingInterval;
  17. private EDosBJMonitor eDosMonitor;
  18. //Thread windTurbineMonitorThread;
  19. public bool IsRunning
  20. {
  21. get { return isRunning; }
  22. }
  23. public PollingService()
  24. {
  25. pollingInterval = GlobalVar.PollingInterval;
  26. }
  27. public void Start()
  28. {
  29. logger.Info("轮询服务开始!");
  30. //windTurbineMonitorThread = new Thread(WindTurbineMonitor);
  31. //windTurbineMonitorThread.Start();
  32. isRunning = true;
  33. int cnt = 1;
  34. while (!cancelPolling)
  35. {
  36. if (cnt > 1000000) cnt = 1;
  37. logger.InfoFormat("开始第{0}次扫描......", cnt++);
  38. try
  39. {
  40. WindTurbineMonitor();
  41. WindPowerStationMonitor();
  42. ProjectMonitor();
  43. LineMonitor();
  44. ElectricalMonitor();
  45. EDosMonitor();
  46. System.Threading.Thread.Sleep(pollingInterval * 1000);
  47. }
  48. catch (Exception ex)
  49. {
  50. logger.ErrorFormat("轮询服务异常:{0}", ex.Message);
  51. System.Threading.Thread.Sleep(pollingInterval * 500);
  52. }
  53. }
  54. }
  55. public void Stop()
  56. {
  57. logger.Info("正在停止轮询服务...");
  58. cancelPolling = true;
  59. //if (windTurbineMonitorThread != null)
  60. // windTurbineMonitorThread.Join();
  61. isRunning = false;
  62. logger.Info("轮询服务停止!");
  63. }
  64. private void WindTurbineMonitor()
  65. {
  66. var lstWt = DataCache.Instance.WindTurbinies.ToList();
  67. //var lstWt = DataCache.Instance.WindTurbinies.Where(q=>q.Id== "MG01_01").ToList();
  68. foreach (WindTurbine wt in lstWt)
  69. {
  70. if (cancelPolling) break;
  71. try
  72. {
  73. logger.Debug("获取风机:" + wt.Id + "," + wt.Name + "," + wt.ModelId);
  74. WindTurbineCalculator wtCal = new WindTurbineCalculator(wt);
  75. wtCal.Calculate();
  76. }
  77. catch (EDosDataLackException)
  78. {
  79. //logger.WarnFormat("有效数据不足!");
  80. }
  81. catch (EDosException ex)
  82. {
  83. logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message);
  84. System.Threading.Thread.Sleep(pollingInterval * 2000);
  85. }
  86. catch (Exception ex)
  87. {
  88. logger.ErrorFormat("风机监视线程异常:{0}", ex.Message);
  89. System.Threading.Thread.Sleep(pollingInterval * 2000);
  90. }
  91. }
  92. }
  93. private void WindPowerStationMonitor()
  94. {
  95. var lstObj = DataCache.Instance.WindPowerStation;
  96. //var lstWt = DataCache.Instance.WindTurbinies.Where(q=>q.Id== "MG01_01").ToList();
  97. foreach (WindPowerStation wt in lstObj)
  98. {
  99. if (cancelPolling) break;
  100. try
  101. {
  102. logger.Debug("获取风场:" + wt.Id + "," + wt.Name);
  103. WindPowerStationCalculator cal = new WindPowerStationCalculator(wt);
  104. cal.Calculate();
  105. }
  106. catch (EDosDataLackException)
  107. {
  108. logger.WarnFormat("有效数据不足!");
  109. }
  110. catch (EDosException ex)
  111. {
  112. logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message);
  113. System.Threading.Thread.Sleep(pollingInterval * 2000);
  114. }
  115. catch (Exception ex)
  116. {
  117. logger.ErrorFormat("风机监视线程异常:{0}", ex.Message);
  118. System.Threading.Thread.Sleep(pollingInterval * 2000);
  119. }
  120. }
  121. }
  122. private void ElectricalMonitor()
  123. {
  124. var lstObj = DataCache.Instance.WindPowerStation;
  125. foreach (WindPowerStation wt in lstObj)
  126. {
  127. if (cancelPolling) break;
  128. try
  129. {
  130. logger.Debug("获取风场(升压站):" + wt.Id + "," + wt.Name);
  131. ElectricalCalculator cal = new ElectricalCalculator(wt);
  132. cal.Calculate();
  133. }
  134. catch (EDosDataLackException)
  135. {
  136. //logger.WarnFormat("有效数据不足!");
  137. }
  138. catch (EDosException ex)
  139. {
  140. logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message);
  141. System.Threading.Thread.Sleep(pollingInterval * 2000);
  142. }
  143. catch (Exception ex)
  144. {
  145. logger.ErrorFormat("电气监视线程异常:{0}", ex.Message);
  146. System.Threading.Thread.Sleep(pollingInterval * 2000);
  147. }
  148. }
  149. }
  150. private void ProjectMonitor()
  151. {
  152. var lstObj = DataCache.Instance.Projects;
  153. foreach (Project wt in lstObj)
  154. {
  155. if (cancelPolling) break;
  156. try
  157. {
  158. logger.Debug("获取工程:" + wt.Id + "," + wt.Name);
  159. ProjectCalculator cal = new ProjectCalculator(wt);
  160. cal.Calculate();
  161. }
  162. catch (EDosDataLackException)
  163. {
  164. //logger.WarnFormat("有效数据不足!");
  165. }
  166. catch (EDosException ex)
  167. {
  168. logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message);
  169. System.Threading.Thread.Sleep(pollingInterval * 2000);
  170. }
  171. catch (Exception ex)
  172. {
  173. logger.ErrorFormat("工程监视线程异常:{0}", ex.Message);
  174. System.Threading.Thread.Sleep(pollingInterval * 2000);
  175. }
  176. }
  177. }
  178. private void LineMonitor()
  179. {
  180. var lstObj = DataCache.Instance.Lines;
  181. foreach (Line wt in lstObj)
  182. {
  183. if (cancelPolling) break;
  184. try
  185. {
  186. logger.Debug("获取线路:" + wt.Id + "," + wt.Name);
  187. LineCalculator cal = new LineCalculator(wt);
  188. cal.Calculate();
  189. }
  190. catch (EDosDataLackException)
  191. {
  192. //logger.WarnFormat("有效数据不足!");
  193. }
  194. catch (EDosException ex)
  195. {
  196. logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message);
  197. System.Threading.Thread.Sleep(pollingInterval * 2000);
  198. }
  199. catch (Exception ex)
  200. {
  201. logger.ErrorFormat("线路监视线程异常:{0}", ex.Message);
  202. System.Threading.Thread.Sleep(pollingInterval * 2000);
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// 正对一期完成的FJBJ、FJBJ2、及IFix上的报警
  208. /// </summary>
  209. private void EDosMonitor()
  210. {
  211. if (eDosMonitor == null)
  212. eDosMonitor = new EDosBJMonitor();
  213. eDosMonitor.WindTurbineBJMonitor();
  214. eDosMonitor.IFixBJMonitor();
  215. }
  216. }
  217. }