using EDosApiWrapper; using GDNXFD.Data; using GDNXFD.Data.Repositories; using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Threading; namespace GDNXFD.Alert.Interpreter { public class PollingService { private ILog logger = LogManager.GetLogger("AppLog"); private bool cancelPolling = false; private bool isRunning = false; private int pollingInterval; private EDosBJMonitor eDosMonitor; //Thread windTurbineMonitorThread; public bool IsRunning { get { return isRunning; } } public PollingService() { pollingInterval = GlobalVar.PollingInterval; } public void Start() { logger.Info("轮询服务开始!"); //windTurbineMonitorThread = new Thread(WindTurbineMonitor); //windTurbineMonitorThread.Start(); isRunning = true; int cnt = 1; while (!cancelPolling) { if (cnt > 1000000) cnt = 1; logger.InfoFormat("开始第{0}次扫描......", cnt++); try { WindTurbineMonitor(); WindPowerStationMonitor(); ProjectMonitor(); LineMonitor(); ElectricalMonitor(); EDosMonitor(); System.Threading.Thread.Sleep(pollingInterval * 1000); } catch (Exception ex) { logger.ErrorFormat("轮询服务异常:{0}", ex.Message); System.Threading.Thread.Sleep(pollingInterval * 500); } } } public void Stop() { logger.Info("正在停止轮询服务..."); cancelPolling = true; //if (windTurbineMonitorThread != null) // windTurbineMonitorThread.Join(); isRunning = false; logger.Info("轮询服务停止!"); } private void WindTurbineMonitor() { var lstWt = DataCache.Instance.WindTurbinies.ToList(); //var lstWt = DataCache.Instance.WindTurbinies.Where(q=>q.Id== "MG01_01").ToList(); foreach (WindTurbine wt in lstWt) { if (cancelPolling) break; try { logger.Debug("获取风机:" + wt.Id + "," + wt.Name + "," + wt.ModelId); WindTurbineCalculator wtCal = new WindTurbineCalculator(wt); wtCal.Calculate(); } catch (EDosDataLackException) { //logger.WarnFormat("有效数据不足!"); } catch (EDosException ex) { logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } catch (Exception ex) { logger.ErrorFormat("风机监视线程异常:{0}", ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } } } private void WindPowerStationMonitor() { var lstObj = DataCache.Instance.WindPowerStation; //var lstWt = DataCache.Instance.WindTurbinies.Where(q=>q.Id== "MG01_01").ToList(); foreach (WindPowerStation wt in lstObj) { if (cancelPolling) break; try { logger.Debug("获取风场:" + wt.Id + "," + wt.Name); WindPowerStationCalculator cal = new WindPowerStationCalculator(wt); cal.Calculate(); } catch (EDosDataLackException) { logger.WarnFormat("有效数据不足!"); } catch (EDosException ex) { logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } catch (Exception ex) { logger.ErrorFormat("风机监视线程异常:{0}", ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } } } private void ElectricalMonitor() { var lstObj = DataCache.Instance.WindPowerStation; foreach (WindPowerStation wt in lstObj) { if (cancelPolling) break; try { logger.Debug("获取风场(升压站):" + wt.Id + "," + wt.Name); ElectricalCalculator cal = new ElectricalCalculator(wt); cal.Calculate(); } catch (EDosDataLackException) { //logger.WarnFormat("有效数据不足!"); } catch (EDosException ex) { logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } catch (Exception ex) { logger.ErrorFormat("电气监视线程异常:{0}", ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } } } private void ProjectMonitor() { var lstObj = DataCache.Instance.Projects; foreach (Project wt in lstObj) { if (cancelPolling) break; try { logger.Debug("获取工程:" + wt.Id + "," + wt.Name); ProjectCalculator cal = new ProjectCalculator(wt); cal.Calculate(); } catch (EDosDataLackException) { //logger.WarnFormat("有效数据不足!"); } catch (EDosException ex) { logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } catch (Exception ex) { logger.ErrorFormat("工程监视线程异常:{0}", ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } } } private void LineMonitor() { var lstObj = DataCache.Instance.Lines; foreach (Line wt in lstObj) { if (cancelPolling) break; try { logger.Debug("获取线路:" + wt.Id + "," + wt.Name); LineCalculator cal = new LineCalculator(wt); cal.Calculate(); } catch (EDosDataLackException) { //logger.WarnFormat("有效数据不足!"); } catch (EDosException ex) { logger.ErrorFormat("EDos异常!ErrorCode={0}, Message={1}", ex.ErrorCode, ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } catch (Exception ex) { logger.ErrorFormat("线路监视线程异常:{0}", ex.Message); System.Threading.Thread.Sleep(pollingInterval * 2000); } } } /// /// 正对一期完成的FJBJ、FJBJ2、及IFix上的报警 /// private void EDosMonitor() { if (eDosMonitor == null) eDosMonitor = new EDosBJMonitor(); eDosMonitor.WindTurbineBJMonitor(); eDosMonitor.IFixBJMonitor(); } } }