123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- using System.Collections.Generic;
- using System.Linq;
- using GDNXFD.Data.Repositories;
- //using log4net;
- using System;
- using GDNXFD.Data;
- using System.Threading;
- using System.Configuration;
- using System.Collections;
- using GDNXFD.WcfService.RealtimeState;
- using log4net;
- namespace GDNXFD.WcfService
- {
- public class AdviceWorker
- {
- private static ILog logger = LogManager.GetLogger("AppInfoLog");
- private bool isRunning = false;
- private bool cancelPolling = false;
- private int pollingInterval;
- //实时计算线程
- private Thread RealtimeStateThread;
- //预测计算线程
- private Thread PredictCalcThread;
- //风机信息线程
- private Thread WindturbineInfoThread;
- private Thread TryMaintainWindturbineThread;
- public bool IsRunning
- {
- get { return isRunning; }
- }
- #region 单例模式
- private AdviceWorker()
- {
- string str = ConfigurationManager.AppSettings["PollingInterval"];
- if (!int.TryParse(str, out pollingInterval))
- {
- pollingInterval = 10;
- }
- }
- public static AdviceWorker Instance
- {
- get { return SingletonCreator.instance; }
- }
- class SingletonCreator
- {
- internal static readonly AdviceWorker instance = new AdviceWorker();
- }
- #endregion
- public void Start()
- {
- try
- {
- AdviceHistoryModelRepository.UpdateAllCalcResult();
- }
- catch (Exception ex)
- {
- logger.Info(ex.Message);
- }
- if (isRunning)
- return;
- //首次更新风机信息数据
- AdviceCache.Instance.GetWindturbinInfo();
- Console.WriteLine("风机状态更新完毕");
- WindturbineInfoThread = new Thread(WindturbinInfoUpdate);
- WindturbineInfoThread.SetApartmentState(ApartmentState.STA);
- WindturbineInfoThread.IsBackground = true;
- WindturbineInfoThread.Start();
- Console.WriteLine("风机状态线程启动");
- RealtimeStateThread = new Thread(RealtimeStateCalc);
- RealtimeStateThread.SetApartmentState(ApartmentState.STA);
- RealtimeStateThread.IsBackground = true;
- RealtimeStateThread.Start();
- Console.WriteLine("风机状态计算线程启动");
- PredictCalcThread = new Thread(PredictCalc);
- PredictCalcThread.SetApartmentState(ApartmentState.STA);
- PredictCalcThread.IsBackground = true;
- PredictCalcThread.Start();
- Console.WriteLine("预测计算线程启动");
- //....
- TryMaintainWindturbineThread = new Thread(TryMaintainWindturbine);
- TryMaintainWindturbineThread.SetApartmentState(ApartmentState.STA);
- TryMaintainWindturbineThread.IsBackground = true;
- TryMaintainWindturbineThread.Name = "TryMaintainWindturbineThread";
- TryMaintainWindturbineThread.Start();
- Console.WriteLine("推荐风机维护线程启动");
- //System.Diagnostics.Debug.WriteLine("推荐风机维护线程启动");
- isRunning = true;
- }
- public void Stop()
- {
- //logger.Info("正在停止后台线程...");
- cancelPolling = true;
- if (RealtimeStateThread != null)
- RealtimeStateThread.Join();
- }
- /// <summary>
- /// 风机启停推荐方法 (实时状态计算)
- /// </summary>
- private void RealtimeStateCalc()
- {
- //logger.Info("后台线程开始!");
- isRunning = true;
- while (!cancelPolling)
- {
- try
- {
- CalcStateSvc.Instance.CalcWindturbine();
- }
- catch (Exception ex)
- {
- logger.Info(ex.Message);
- }
- finally
- {
- Thread.Sleep(pollingInterval * 1000);
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- //GC.GetTotalMemory(true);
- }
- }
- isRunning = false;
- cancelPolling = false;
- //logger.Info("后台线程停止!");
- }
- /// <summary>
- /// 风机启停推荐方法 (预测计算)
- /// </summary>
- private void PredictCalc()
- {
- while (!cancelPolling)
- {
- try
- {
- //AdviceCache.Instance.GetPredictAdviceModel();
- }
- catch (Exception ex)
- {
- //
- }
- finally
- {
- Thread.Sleep(pollingInterval * 1000);
- }
- }
- cancelPolling = false;
- }
- /// <summary>
- /// 全场无风将对待机状态的风机推荐维护
- /// </summary>
- private void TryMaintainWindturbine()
- {
- while (!cancelPolling)
- {
- try
- {
- CalcStateSvc.Instance.TryMaintainWindturbine();
- }
- catch (Exception ex)
- {
- logger.Info(ex.Message);
- D(ex.ToString());
- //System.Diagnostics.Debug.WriteLine(ex.ToString());
- }
- finally
- {
- Thread.Sleep(5 * 1000 * 60);//1000*60);
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- //GC.GetTotalMemory(true);
- }
- }
- }
- bool allowDebug = ConfigurationManager.AppSettings["AllowDebug"].ToUpper() == "Y";
- bool allowPrint = ConfigurationManager.AppSettings["AllowPrint"].ToUpper() == "Y";
- private void D(string debugText)
- {
- try
- {
- if (allowDebug)
- {
- System.Diagnostics.Debug.WriteLine(debugText);
- }
- if (allowPrint)
- {
- Console.WriteLine(debugText);
- }
- }
- catch (Exception ex)
- {
- //logger.Info(ex.Message);
- System.Diagnostics.Debug.WriteLine(ex.ToString());
- }
- }
- /// <summary>
- /// 风机信息刷新方法
- /// </summary>
- private void WindturbinInfoUpdate()
- {
- //logger.Info("后台线程开始!");
- isRunning = true;
- while (!cancelPolling)
- {
- try
- {
- AdviceCache.Instance.GetWindturbinInfo();
- }
- catch (Exception ex)
- {
- //
- }
- finally
- {
- Thread.Sleep(1000);
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- //GC.GetTotalMemory(true);
- }
- }
- isRunning = false;
- cancelPolling = false;
- }
- }
- }
|