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; namespace IntelligentControlForsx { public class AlertBackWorker { public const string CUSTOM = "custom"; public const string WINDTURBINE = "windturbine"; //private ILog logger = LogManager.GetLogger("AppLog"); private AlertServiceClient asc = null; private bool cancelPolling = false; private bool isRunning = false; private int pollingInterval; private Thread loadDataThread; public bool IsRunning { get { return isRunning; } } #region 单例模式 private AlertBackWorker() { string str = ConfigurationManager.AppSettings["PollingInterval"]; if (!int.TryParse(str, out pollingInterval)) { pollingInterval = 5; } } public static AlertBackWorker Instance { get { return SingletonCreator.instance; } } class SingletonCreator { internal static readonly AlertBackWorker instance = new AlertBackWorker(); } #endregion public void Start() { if (isRunning) return; loadDataThread = new Thread(LoadData); loadDataThread.SetApartmentState(ApartmentState.STA); loadDataThread.IsBackground = true; loadDataThread.Start(); } public void Stop() { //logger.Info("正在停止后台线程..."); cancelPolling = true; if (loadDataThread != null) loadDataThread.Join(); } private void LoadData() { //logger.Info("后台线程开始!"); isRunning = true; while (!cancelPolling) { try { if (asc == null) asc = AlertServiceClientFactory.CreateAlertServiceClient(); if (ClientCache.Instance.DataDict == null) { IList lst2 = asc.GetDataDictionary(); ClientCache.Instance.DataDict = lst2; } IList lst = asc.GetAlertSnaps(); ClientCache.Instance.AlertSnaps = lst; MediaManager.Instance.UpdateAlertMediaStatus(); analysisAlertSnaps(); } 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("后台线程停止!"); } public void analysisAlertSnaps() { var lst = ClientCache.Instance.AlertSnaps; Dictionary lpDict = new Dictionary(); Dictionary agDict = new Dictionary(); if (lst != null && lst.Count > 0) { foreach(AlertSnap snap in lst) { try { string stId = snap.StationId; int rank = 0; int.TryParse(snap.Rank, out rank); if (lpDict.ContainsKey(stId) == false) { LightPlateModel lp = new LightPlateModel(); lp.LId = stId; lpDict.Add(stId, lp); } LightPlateModel lp1 = lpDict[stId]; lp1.LCount++; if (lp1.LRank < rank) lp1.LRank = rank; if (CUSTOM.Equals(snap.Category1)) { if (lpDict.ContainsKey(CUSTOM) == false) { LightPlateModel lp = new LightPlateModel(); lp.LId = CUSTOM; lpDict.Add(CUSTOM, lp); } LightPlateModel lp2 = lpDict[CUSTOM]; lp2.LCount++; if (lp2.LRank < rank) lp2.LRank = rank; } if (WINDTURBINE.Equals(snap.Category1)) { string part = getLPId(snap.Category2); if (lpDict.ContainsKey(part) == false) { LightPlateModel lp = new LightPlateModel(); lp.LId = part; lpDict.Add(part, lp); } LightPlateModel lp3 = lpDict[part]; lp3.LCount++; if (lp3.LRank < rank) lp3.LRank = rank; } #region AlarmGroup string thingName = snap.ObjectName; if (agDict.ContainsKey(thingName) == false) { AlarmGroupModel ag = new AlarmGroupModel(); ag.AlarmText = snap.AlertText; ag.ThingName = snap.ObjectName; ag.MaxRank = int.Parse(snap.Rank); ag.AgTime = snap.LastUpdateTime.Value; ag.SnapList = new List(); agDict.Add(thingName, ag); } AlarmGroupModel ag1 = agDict[thingName]; ag1.AgCount++; ag1.SnapList.Add(snap); if (ag1.MaxRank < rank) { ag1.MaxRank = rank; ag1.AgTime = snap.LastUpdateTime.Value; ag1.AlarmText = snap.AlertText; } #endregion } catch { } } } ClientCache.Instance.LPModels = lpDict; ClientCache.Instance.AlarmGroups = agDict; } //other //kz //jcjr //yy //dw //ph //fdj //clx //bj public string getLPId(string category) { //yl-- bj //bpq, blq --kz //zz-- clx //rh --other if ("yl".Equals(category)) return "bj"; if ("bpq".Equals(category) || "blq".Equals(category)) return "kz"; if ("zz".Equals(category)) return "clx"; if ("rh".Equals(category)) return "other"; return category; } } }