AlertBackWorker.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. namespace IntelligentControlForsx
  11. {
  12. public class AlertBackWorker
  13. {
  14. public const string CUSTOM = "custom";
  15. public const string WINDTURBINE = "windturbine";
  16. //private ILog logger = LogManager.GetLogger("AppLog");
  17. private AlertServiceClient asc = null;
  18. private bool cancelPolling = false;
  19. private bool isRunning = false;
  20. private int pollingInterval;
  21. private Thread loadDataThread;
  22. public bool IsRunning
  23. {
  24. get { return isRunning; }
  25. }
  26. #region 单例模式
  27. private AlertBackWorker()
  28. {
  29. string str = ConfigurationManager.AppSettings["PollingInterval"];
  30. if (!int.TryParse(str, out pollingInterval))
  31. {
  32. pollingInterval = 5;
  33. }
  34. }
  35. public static AlertBackWorker Instance
  36. {
  37. get { return SingletonCreator.instance; }
  38. }
  39. class SingletonCreator
  40. {
  41. internal static readonly AlertBackWorker instance = new AlertBackWorker();
  42. }
  43. #endregion
  44. public void Start()
  45. {
  46. if (isRunning)
  47. return;
  48. loadDataThread = new Thread(LoadData);
  49. loadDataThread.SetApartmentState(ApartmentState.STA);
  50. loadDataThread.IsBackground = true;
  51. loadDataThread.Start();
  52. }
  53. public void Stop()
  54. {
  55. //logger.Info("正在停止后台线程...");
  56. cancelPolling = true;
  57. if (loadDataThread != null)
  58. loadDataThread.Join();
  59. }
  60. private void LoadData()
  61. {
  62. //logger.Info("后台线程开始!");
  63. isRunning = true;
  64. while (!cancelPolling)
  65. {
  66. try
  67. {
  68. if (asc == null)
  69. asc = AlertServiceClientFactory.CreateAlertServiceClient();
  70. if (ClientCache.Instance.DataDict == null)
  71. {
  72. IList<DictItem> lst2 = asc.GetDataDictionary();
  73. ClientCache.Instance.DataDict = lst2;
  74. }
  75. IList<AlertSnap> lst = asc.GetAlertSnaps();
  76. ClientCache.Instance.AlertSnaps = lst;
  77. MediaManager.Instance.UpdateAlertMediaStatus();
  78. analysisAlertSnaps();
  79. }
  80. catch (Exception ex)
  81. {
  82. //logger.Info("后台线程发生异常:" + ex.Message);
  83. }
  84. finally
  85. {
  86. Thread.Sleep(pollingInterval * 1000);
  87. GC.Collect();
  88. GC.WaitForPendingFinalizers();
  89. GC.Collect();
  90. //GC.GetTotalMemory(true);
  91. }
  92. }
  93. isRunning = false;
  94. cancelPolling = false;
  95. //logger.Info("后台线程停止!");
  96. }
  97. public void analysisAlertSnaps()
  98. {
  99. var lst = ClientCache.Instance.AlertSnaps;
  100. Dictionary<string, LightPlateModel> lpDict = new Dictionary<string, LightPlateModel>();
  101. Dictionary<string, AlarmGroupModel> agDict = new Dictionary<string, AlarmGroupModel>();
  102. if (lst != null && lst.Count > 0)
  103. {
  104. foreach(AlertSnap snap in lst)
  105. {
  106. try
  107. {
  108. string stId = snap.StationId;
  109. int rank = 0;
  110. int.TryParse(snap.Rank, out rank);
  111. if (lpDict.ContainsKey(stId) == false)
  112. {
  113. LightPlateModel lp = new LightPlateModel();
  114. lp.LId = stId;
  115. lpDict.Add(stId, lp);
  116. }
  117. LightPlateModel lp1 = lpDict[stId];
  118. lp1.LCount++;
  119. if (lp1.LRank < rank)
  120. lp1.LRank = rank;
  121. if (CUSTOM.Equals(snap.Category1)) {
  122. if (lpDict.ContainsKey(CUSTOM) == false)
  123. {
  124. LightPlateModel lp = new LightPlateModel();
  125. lp.LId = CUSTOM;
  126. lpDict.Add(CUSTOM, lp);
  127. }
  128. LightPlateModel lp2 = lpDict[CUSTOM];
  129. lp2.LCount++;
  130. if (lp2.LRank < rank)
  131. lp2.LRank = rank;
  132. }
  133. if (WINDTURBINE.Equals(snap.Category1))
  134. {
  135. string part = getLPId(snap.Category2);
  136. if (lpDict.ContainsKey(part) == false)
  137. {
  138. LightPlateModel lp = new LightPlateModel();
  139. lp.LId = part;
  140. lpDict.Add(part, lp);
  141. }
  142. LightPlateModel lp3 = lpDict[part];
  143. lp3.LCount++;
  144. if (lp3.LRank < rank)
  145. lp3.LRank = rank;
  146. }
  147. #region AlarmGroup
  148. string thingName = snap.ObjectName;
  149. if (agDict.ContainsKey(thingName) == false)
  150. {
  151. AlarmGroupModel ag = new AlarmGroupModel();
  152. ag.AlarmText = snap.AlertText;
  153. ag.ThingName = snap.ObjectName;
  154. ag.MaxRank = int.Parse(snap.Rank);
  155. ag.AgTime = snap.LastUpdateTime.Value;
  156. ag.SnapList = new List<AlertSnap>();
  157. agDict.Add(thingName, ag);
  158. }
  159. AlarmGroupModel ag1 = agDict[thingName];
  160. ag1.AgCount++;
  161. ag1.SnapList.Add(snap);
  162. if (ag1.MaxRank < rank)
  163. {
  164. ag1.MaxRank = rank;
  165. ag1.AgTime = snap.LastUpdateTime.Value;
  166. ag1.AlarmText = snap.AlertText;
  167. }
  168. #endregion
  169. }
  170. catch { }
  171. }
  172. }
  173. ClientCache.Instance.LPModels = lpDict;
  174. ClientCache.Instance.AlarmGroups = agDict;
  175. }
  176. //other
  177. //kz
  178. //jcjr
  179. //yy
  180. //dw
  181. //ph
  182. //fdj
  183. //clx
  184. //bj
  185. public string getLPId(string category)
  186. {
  187. //yl-- bj
  188. //bpq, blq --kz
  189. //zz-- clx
  190. //rh --other
  191. if ("yl".Equals(category))
  192. return "bj";
  193. if ("bpq".Equals(category) || "blq".Equals(category))
  194. return "kz";
  195. if ("zz".Equals(category))
  196. return "clx";
  197. if ("rh".Equals(category))
  198. return "other";
  199. return category;
  200. }
  201. }
  202. }