AdviceWorker.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using GDNXFD.Data.Repositories;
  4. //using log4net;
  5. using System;
  6. using GDNXFD.Data;
  7. using System.Threading;
  8. using System.Configuration;
  9. using System.Collections;
  10. using GDNXFD.WcfService.RealtimeState;
  11. using log4net;
  12. namespace GDNXFD.WcfService
  13. {
  14. public class AdviceWorker
  15. {
  16. private static ILog logger = LogManager.GetLogger("AppInfoLog");
  17. private bool isRunning = false;
  18. private bool cancelPolling = false;
  19. private int pollingInterval;
  20. //实时计算线程
  21. private Thread RealtimeStateThread;
  22. //预测计算线程
  23. private Thread PredictCalcThread;
  24. //风机信息线程
  25. private Thread WindturbineInfoThread;
  26. private Thread TryMaintainWindturbineThread;
  27. public bool IsRunning
  28. {
  29. get { return isRunning; }
  30. }
  31. #region 单例模式
  32. private AdviceWorker()
  33. {
  34. string str = ConfigurationManager.AppSettings["PollingInterval"];
  35. if (!int.TryParse(str, out pollingInterval))
  36. {
  37. pollingInterval = 10;
  38. }
  39. }
  40. public static AdviceWorker Instance
  41. {
  42. get { return SingletonCreator.instance; }
  43. }
  44. class SingletonCreator
  45. {
  46. internal static readonly AdviceWorker instance = new AdviceWorker();
  47. }
  48. #endregion
  49. public void Start()
  50. {
  51. try
  52. {
  53. AdviceHistoryModelRepository.UpdateAllCalcResult();
  54. }
  55. catch (Exception ex)
  56. {
  57. logger.Info(ex.Message);
  58. }
  59. if (isRunning)
  60. return;
  61. //首次更新风机信息数据
  62. AdviceCache.Instance.GetWindturbinInfo();
  63. Console.WriteLine("风机状态更新完毕");
  64. WindturbineInfoThread = new Thread(WindturbinInfoUpdate);
  65. WindturbineInfoThread.SetApartmentState(ApartmentState.STA);
  66. WindturbineInfoThread.IsBackground = true;
  67. WindturbineInfoThread.Start();
  68. Console.WriteLine("风机状态线程启动");
  69. RealtimeStateThread = new Thread(RealtimeStateCalc);
  70. RealtimeStateThread.SetApartmentState(ApartmentState.STA);
  71. RealtimeStateThread.IsBackground = true;
  72. RealtimeStateThread.Start();
  73. Console.WriteLine("风机状态计算线程启动");
  74. PredictCalcThread = new Thread(PredictCalc);
  75. PredictCalcThread.SetApartmentState(ApartmentState.STA);
  76. PredictCalcThread.IsBackground = true;
  77. PredictCalcThread.Start();
  78. Console.WriteLine("预测计算线程启动");
  79. //....
  80. TryMaintainWindturbineThread = new Thread(TryMaintainWindturbine);
  81. TryMaintainWindturbineThread.SetApartmentState(ApartmentState.STA);
  82. TryMaintainWindturbineThread.IsBackground = true;
  83. TryMaintainWindturbineThread.Name = "TryMaintainWindturbineThread";
  84. TryMaintainWindturbineThread.Start();
  85. Console.WriteLine("推荐风机维护线程启动");
  86. //System.Diagnostics.Debug.WriteLine("推荐风机维护线程启动");
  87. isRunning = true;
  88. }
  89. public void Stop()
  90. {
  91. //logger.Info("正在停止后台线程...");
  92. cancelPolling = true;
  93. if (RealtimeStateThread != null)
  94. RealtimeStateThread.Join();
  95. }
  96. /// <summary>
  97. /// 风机启停推荐方法 (实时状态计算)
  98. /// </summary>
  99. private void RealtimeStateCalc()
  100. {
  101. //logger.Info("后台线程开始!");
  102. isRunning = true;
  103. while (!cancelPolling)
  104. {
  105. try
  106. {
  107. CalcStateSvc.Instance.CalcWindturbine();
  108. }
  109. catch (Exception ex)
  110. {
  111. logger.Info(ex.Message);
  112. }
  113. finally
  114. {
  115. Thread.Sleep(pollingInterval * 1000);
  116. GC.Collect();
  117. GC.WaitForPendingFinalizers();
  118. GC.Collect();
  119. //GC.GetTotalMemory(true);
  120. }
  121. }
  122. isRunning = false;
  123. cancelPolling = false;
  124. //logger.Info("后台线程停止!");
  125. }
  126. /// <summary>
  127. /// 风机启停推荐方法 (预测计算)
  128. /// </summary>
  129. private void PredictCalc()
  130. {
  131. while (!cancelPolling)
  132. {
  133. try
  134. {
  135. //AdviceCache.Instance.GetPredictAdviceModel();
  136. }
  137. catch (Exception ex)
  138. {
  139. //
  140. }
  141. finally
  142. {
  143. Thread.Sleep(pollingInterval * 1000);
  144. }
  145. }
  146. cancelPolling = false;
  147. }
  148. /// <summary>
  149. /// 全场无风将对待机状态的风机推荐维护
  150. /// </summary>
  151. private void TryMaintainWindturbine()
  152. {
  153. while (!cancelPolling)
  154. {
  155. try
  156. {
  157. CalcStateSvc.Instance.TryMaintainWindturbine();
  158. }
  159. catch (Exception ex)
  160. {
  161. logger.Info(ex.Message);
  162. D(ex.ToString());
  163. //System.Diagnostics.Debug.WriteLine(ex.ToString());
  164. }
  165. finally
  166. {
  167. Thread.Sleep(5 * 1000 * 60);//1000*60);
  168. GC.Collect();
  169. GC.WaitForPendingFinalizers();
  170. GC.Collect();
  171. //GC.GetTotalMemory(true);
  172. }
  173. }
  174. }
  175. bool allowDebug = ConfigurationManager.AppSettings["AllowDebug"].ToUpper() == "Y";
  176. bool allowPrint = ConfigurationManager.AppSettings["AllowPrint"].ToUpper() == "Y";
  177. private void D(string debugText)
  178. {
  179. try
  180. {
  181. if (allowDebug)
  182. {
  183. System.Diagnostics.Debug.WriteLine(debugText);
  184. }
  185. if (allowPrint)
  186. {
  187. Console.WriteLine(debugText);
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. //logger.Info(ex.Message);
  193. System.Diagnostics.Debug.WriteLine(ex.ToString());
  194. }
  195. }
  196. /// <summary>
  197. /// 风机信息刷新方法
  198. /// </summary>
  199. private void WindturbinInfoUpdate()
  200. {
  201. //logger.Info("后台线程开始!");
  202. isRunning = true;
  203. while (!cancelPolling)
  204. {
  205. try
  206. {
  207. AdviceCache.Instance.GetWindturbinInfo();
  208. }
  209. catch (Exception ex)
  210. {
  211. //
  212. }
  213. finally
  214. {
  215. Thread.Sleep(1000);
  216. GC.Collect();
  217. GC.WaitForPendingFinalizers();
  218. GC.Collect();
  219. //GC.GetTotalMemory(true);
  220. }
  221. }
  222. isRunning = false;
  223. cancelPolling = false;
  224. }
  225. }
  226. }