AlarmBarController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Timers;
  5. using Applications.UniversalApi_WebConsole_App.Hubs;
  6. using Applications.UniversalApi_WebConsole_App.Models;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.SignalR;
  9. using Newtonsoft.Json;
  10. namespace Applications.UniversalApi_WebConsole_App.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. //var notifyClients = false;
  177. if (alarmInformation.Severity == Severity.Alarm)
  178. {
  179. lock (AlarmInformations)
  180. {
  181. if (!AlarmInformations.ContainsKey(alarmInformation.Key))
  182. {
  183. AlarmInformations.Add(alarmInformation.Key, alarmInformation);
  184. }
  185. else
  186. {
  187. AlarmInformations[alarmInformation.Key].OccurTime = alarmInformation.OccurTime;
  188. }
  189. }
  190. return Ok();
  191. }
  192. if (alarmInformation.Severity == Severity.Warning)
  193. {
  194. lock (WarningInformations)
  195. {
  196. if (!WarningInformations.ContainsKey(alarmInformation.Key))
  197. {
  198. WarningInformations.Add(alarmInformation.Key, alarmInformation);
  199. }
  200. else
  201. {
  202. WarningInformations[alarmInformation.Key].OccurTime = alarmInformation.OccurTime;
  203. }
  204. }
  205. return Ok();
  206. }
  207. if (alarmInformation.Severity == Severity.Information)
  208. {
  209. lock (Informations)
  210. {
  211. if (!Informations.ContainsKey(alarmInformation.Key))
  212. {
  213. Informations.Add(alarmInformation.Key, alarmInformation);
  214. }
  215. else
  216. {
  217. Informations[alarmInformation.Key].OccurTime = alarmInformation.OccurTime;
  218. }
  219. }
  220. return Ok();
  221. }
  222. return BadRequest();
  223. }
  224. catch (Exception ex)
  225. {
  226. return BadRequest();
  227. }
  228. }
  229. [HttpPost]
  230. //public async Task<IActionResult> RemoveInformation(string key)
  231. public IActionResult RemoveInformation(string key)
  232. {
  233. try
  234. {
  235. //var notifyClients = false;
  236. lock (AlarmInformations)
  237. {
  238. if (AlarmInformations.ContainsKey(key))
  239. {
  240. //notifyClients = true;
  241. AlarmInformations.Remove(key);
  242. if(!AlarmInformations.Any() || !(AlarmInformations.Values.Any(_ => _.Acked == false)))
  243. {
  244. _hubContext.Clients.All.SendAsync("MuteAlarmAduio");
  245. }
  246. return Ok();
  247. }
  248. }
  249. lock (WarningInformations)
  250. {
  251. if (WarningInformations.ContainsKey(key))
  252. {
  253. WarningInformations.Remove(key);
  254. return Ok();
  255. }
  256. }
  257. lock (Informations)
  258. {
  259. if (Informations.ContainsKey(key))
  260. {
  261. Informations.Remove(key);
  262. return Ok();
  263. }
  264. }
  265. //if (notifyClients && this._hubContext != null)
  266. //{
  267. // await _hubContext.Clients.All.SendAsync("Remove", key);
  268. //}
  269. return BadRequest();
  270. }
  271. catch(Exception ex)
  272. {
  273. return BadRequest();
  274. }
  275. }
  276. [HttpPost]
  277. public IActionResult AlarmAcked()
  278. {
  279. lock (AlarmInformations)
  280. {
  281. foreach (var info in AlarmInformations)
  282. {
  283. info.Value.Acked = true;
  284. }
  285. }
  286. return Ok();
  287. }
  288. [HttpGet]
  289. public IActionResult GetAlarmInformation()
  290. {
  291. List<AlarmInformation> infos = new List<AlarmInformation>();
  292. lock (AlarmInformations)
  293. {
  294. if (AlarmInformations.Any())
  295. {
  296. infos.AddRange(AlarmInformations.Values.OrderByDescending(_=>_.OccurTime));
  297. }
  298. }
  299. lock (WarningInformations)
  300. {
  301. if (WarningInformations.Any())
  302. {
  303. infos.AddRange(WarningInformations.Values.OrderByDescending(_ => _.OccurTime));
  304. }
  305. }
  306. lock (Informations)
  307. {
  308. if (Informations.Any())
  309. {
  310. infos.AddRange(Informations.Values.OrderByDescending(_ => _.OccurTime));
  311. }
  312. }
  313. return Ok(JsonConvert.SerializeObject(infos));
  314. }
  315. }
  316. }