AlarmBarController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Timers;
  5. using Edge.WebHost.Hubs;
  6. using Edge.WebHost.Models;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.SignalR;
  9. using Newtonsoft.Json;
  10. namespace Edge.WebHost.Controllers
  11. {
  12. public class AlarmBarController : Controller
  13. {
  14. public static Dictionary<string, AlarmInformation> AlarmInformations= new Dictionary<string, AlarmInformation>();
  15. public static Dictionary<string, AlarmInformation> WarningInformations = new Dictionary<string, AlarmInformation>();
  16. public static Dictionary<string, AlarmInformation> Informations = new Dictionary<string, AlarmInformation>();
  17. public static IHubContext<AlarmHub> _hubContext;
  18. private static Timer AlarmTimer = new Timer(5000);
  19. private static int currentAlarmIndex = 0;
  20. private static int currentWarningIndex = 0;
  21. private static int currentInformationIndex = 0;
  22. public AlarmBarController(IHubContext<AlarmHub> hubContext)
  23. {
  24. if (AlarmBarController._hubContext == null)
  25. {
  26. AlarmBarController._hubContext = hubContext;
  27. }
  28. if (!AlarmTimer.Enabled)
  29. {
  30. AlarmTimer.Elapsed += new ElapsedEventHandler(AlarmTimer_Elapsed);
  31. AlarmTimer.Start();
  32. }
  33. }
  34. private static async void AlarmTimer_Elapsed(object sender, ElapsedEventArgs e)
  35. {
  36. RemoveOldAlarms();
  37. AlarmInformation tempAlarmInfo = null;
  38. int alarmInfoCount = 0;
  39. int warningInfoCount = 0;
  40. lock (AlarmInformations)
  41. {
  42. if ((AlarmInformations.Any() && !(AlarmInformations.Count > currentAlarmIndex)) || !AlarmInformations.Any())
  43. currentAlarmIndex = 0;
  44. if (AlarmInformations.Count > 0 && AlarmInformations.Count > currentAlarmIndex && _hubContext != null)
  45. {
  46. tempAlarmInfo = AlarmInformations.Values.OrderByDescending(_ => _.OccurTime).ElementAt(currentAlarmIndex);
  47. currentAlarmIndex++;
  48. }
  49. alarmInfoCount = AlarmInformations.Count;
  50. }
  51. lock (WarningInformations)
  52. {
  53. warningInfoCount = WarningInformations.Count;
  54. }
  55. if (tempAlarmInfo == null)
  56. {
  57. lock (WarningInformations)
  58. {
  59. if ((WarningInformations.Any() && !(WarningInformations.Count > currentWarningIndex)) || !WarningInformations.Any())
  60. currentWarningIndex = 0;
  61. if (WarningInformations.Count > 0 && WarningInformations.Count > currentWarningIndex && _hubContext != null)
  62. {
  63. tempAlarmInfo = WarningInformations.Values.OrderByDescending(_=>_.OccurTime).ElementAt(currentWarningIndex);
  64. currentWarningIndex++;
  65. }
  66. }
  67. }
  68. if (tempAlarmInfo == null)
  69. {
  70. lock (Informations)
  71. {
  72. if ((Informations.Any() && !(Informations.Count > currentInformationIndex)) || !Informations.Any())
  73. currentInformationIndex = 0;
  74. if (Informations.Count > 0 && Informations.Count > currentInformationIndex && _hubContext != null)
  75. {
  76. tempAlarmInfo = Informations.Values.OrderByDescending(_ => _.OccurTime).ElementAt(currentInformationIndex);
  77. currentInformationIndex++;
  78. }
  79. }
  80. }
  81. if (tempAlarmInfo != null)
  82. await _hubContext.Clients.All.SendAsync("AddMessage", "AlarmController", JsonConvert.SerializeObject(tempAlarmInfo), alarmInfoCount, warningInfoCount);
  83. else
  84. {
  85. tempAlarmInfo = new AlarmInformation() {Description = "",Severity = Severity.Information};
  86. if (_hubContext != null)
  87. await _hubContext.Clients.All.SendAsync("AddMessage", "AlarmController",
  88. JsonConvert.SerializeObject(tempAlarmInfo), alarmInfoCount, warningInfoCount);
  89. }
  90. }
  91. private static void RemoveOldAlarms()
  92. {
  93. var list = new List<string>();
  94. lock (AlarmInformations)
  95. {
  96. if (AlarmInformations.Count > 0)
  97. {
  98. foreach (var alarm in AlarmInformations)
  99. {
  100. if ((DateTime.Now - alarm.Value.OccurTime)?.TotalMinutes > 2)
  101. {
  102. list.Add(alarm.Key);
  103. }
  104. }
  105. }
  106. }
  107. if(list.Count > 0)
  108. {
  109. lock (AlarmInformations)
  110. {
  111. foreach (var key in list)
  112. {
  113. if (AlarmInformations.ContainsKey(key)) AlarmInformations.Remove(key);
  114. }
  115. }
  116. list.Clear();
  117. }
  118. lock (WarningInformations)
  119. {
  120. if (WarningInformations.Count > 0)
  121. {
  122. foreach (var warning in WarningInformations)
  123. {
  124. if ((DateTime.Now - warning.Value.OccurTime)?.TotalMinutes > 2)
  125. {
  126. list.Add(warning.Key);
  127. }
  128. }
  129. }
  130. }
  131. if (list.Count > 0)
  132. {
  133. lock (WarningInformations)
  134. {
  135. foreach (var key in list)
  136. {
  137. if (WarningInformations.ContainsKey(key)) WarningInformations.Remove(key);
  138. }
  139. }
  140. list.Clear();
  141. }
  142. lock (Informations)
  143. {
  144. if (Informations.Count > 0)
  145. {
  146. foreach (var info in Informations)
  147. {
  148. if ((DateTime.Now - info.Value.OccurTime)?.TotalMinutes > 2)
  149. {
  150. list.Add(info.Key);
  151. }
  152. }
  153. }
  154. }
  155. if (list.Count > 0)
  156. {
  157. lock (Informations)
  158. {
  159. foreach (var key in list)
  160. {
  161. if (Informations.ContainsKey(key)) Informations.Remove(key);
  162. }
  163. }
  164. list.Clear();
  165. }
  166. }
  167. [HttpPost]
  168. //[HttpGet]
  169. public IActionResult AddInformation([FromBody]object information)
  170. {
  171. try
  172. {
  173. //var temp = new AlarmInformation() { Key = "2", Description = "testtest in controller", Severity = Severity.Alarm };
  174. //var tempStr = JsonConvert.SerializeObject(temp);
  175. var alarmInformation = JsonConvert.DeserializeObject<AlarmInformation>(information.ToString());
  176. if(alarmInformation.OccurTime == null)
  177. {
  178. alarmInformation.OccurTime = DateTime.Now;
  179. }
  180. //var notifyClients = false;
  181. if (alarmInformation.Severity == Severity.Error)
  182. {
  183. lock (AlarmInformations)
  184. {
  185. if (!AlarmInformations.ContainsKey(alarmInformation.Key))
  186. {
  187. AlarmInformations.Add(alarmInformation.Key, alarmInformation);
  188. }
  189. else
  190. {
  191. AlarmInformations[alarmInformation.Key].OccurTime = alarmInformation.OccurTime;
  192. }
  193. }
  194. return Ok();
  195. }
  196. if (alarmInformation.Severity == Severity.Warning)
  197. {
  198. lock (WarningInformations)
  199. {
  200. if (!WarningInformations.ContainsKey(alarmInformation.Key))
  201. {
  202. WarningInformations.Add(alarmInformation.Key, alarmInformation);
  203. }
  204. else
  205. {
  206. WarningInformations[alarmInformation.Key].OccurTime = alarmInformation.OccurTime;
  207. }
  208. }
  209. return Ok();
  210. }
  211. if (alarmInformation.Severity == Severity.Information)
  212. {
  213. lock (Informations)
  214. {
  215. if (!Informations.ContainsKey(alarmInformation.Key))
  216. {
  217. Informations.Add(alarmInformation.Key, alarmInformation);
  218. }
  219. else
  220. {
  221. Informations[alarmInformation.Key].OccurTime = alarmInformation.OccurTime;
  222. }
  223. }
  224. return Ok();
  225. }
  226. return BadRequest();
  227. }
  228. catch (Exception ex)
  229. {
  230. return BadRequest();
  231. }
  232. }
  233. [HttpPost]
  234. //public async Task<IActionResult> RemoveInformation(string key)
  235. public IActionResult RemoveInformation(string key)
  236. {
  237. try
  238. {
  239. //var notifyClients = false;
  240. lock (AlarmInformations)
  241. {
  242. if (AlarmInformations.ContainsKey(key))
  243. {
  244. //notifyClients = true;
  245. AlarmInformations.Remove(key);
  246. if(!AlarmInformations.Any() || !(AlarmInformations.Values.Any(_ => _.Acked == false)))
  247. {
  248. _hubContext.Clients.All.SendAsync("MuteAlarmAduio");
  249. }
  250. return Ok();
  251. }
  252. }
  253. lock (WarningInformations)
  254. {
  255. if (WarningInformations.ContainsKey(key))
  256. {
  257. WarningInformations.Remove(key);
  258. return Ok();
  259. }
  260. }
  261. lock (Informations)
  262. {
  263. if (Informations.ContainsKey(key))
  264. {
  265. Informations.Remove(key);
  266. return Ok();
  267. }
  268. }
  269. //if (notifyClients && this._hubContext != null)
  270. //{
  271. // await _hubContext.Clients.All.SendAsync("Remove", key);
  272. //}
  273. return BadRequest();
  274. }
  275. catch(Exception ex)
  276. {
  277. return BadRequest();
  278. }
  279. }
  280. [HttpPost]
  281. public IActionResult AlarmAcked()
  282. {
  283. lock (AlarmInformations)
  284. {
  285. foreach (var info in AlarmInformations)
  286. {
  287. info.Value.Acked = true;
  288. }
  289. }
  290. return Ok();
  291. }
  292. [HttpGet]
  293. public IActionResult GetAlarmInformation()
  294. {
  295. List<AlarmInformation> infos = new List<AlarmInformation>();
  296. lock (AlarmInformations)
  297. {
  298. if (AlarmInformations.Any())
  299. {
  300. infos.AddRange(AlarmInformations.Values.OrderByDescending(_=>_.OccurTime));
  301. }
  302. }
  303. lock (WarningInformations)
  304. {
  305. if (WarningInformations.Any())
  306. {
  307. infos.AddRange(WarningInformations.Values.OrderByDescending(_ => _.OccurTime));
  308. }
  309. }
  310. lock (Informations)
  311. {
  312. if (Informations.Any())
  313. {
  314. infos.AddRange(Informations.Values.OrderByDescending(_ => _.OccurTime));
  315. }
  316. }
  317. return Ok(JsonConvert.SerializeObject(infos));
  318. }
  319. }
  320. }