123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- 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<DictItem> lst2 = asc.GetDataDictionary();
- ClientCache.Instance.DataDict = lst2;
- }
- IList<AlertSnap> 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<string, LightPlateModel> lpDict = new Dictionary<string, LightPlateModel>();
- Dictionary<string, AlarmGroupModel> agDict = new Dictionary<string, AlarmGroupModel>();
- 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<AlertSnap>();
- 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;
- }
- }
- }
|