public List <Notification1> GetNotifications(int itemId) { try { List <Notification1> resultList = new List <Notification1>(); IndustrialMonitoringEntities entities = new IndustrialMonitoringEntities(); List <NotificationItem> notificationItems = entities.NotificationItems.Where(x => x.ItemId == itemId).ToList(); if (notificationItems == null || notificationItems.Count == 0) { return(null); } foreach (var notificationItem in notificationItems) { NotificationItemsLogLatest notificationItemsLogLatest = entities.NotificationItemsLogLatests.FirstOrDefault( x => x.NotificationId == notificationItem.NotificationId); if (notificationItemsLogLatest.Value) { Notification1 notification1 = new Notification1(notificationItem); resultList.Add(notification1); } } return(resultList); } catch (Exception ex) { Logger.LogMonitoringServiceLibrary(ex); return(null); } }
private void CheckNotifications() { IsThreadRunning = true; while (!StopThread) { try { IndustrialMonitoringEntities entities = new IndustrialMonitoringEntities(); List <NotificationItem> notificationItems = entities.NotificationItems.ToList(); foreach (var notificationItem in notificationItems) { entities = new IndustrialMonitoringEntities(); int notificationId = notificationItem.NotificationId; if (notificationItem.DisableNotification) { NotificationItemsLog prev = entities.NotificationItemsLogs.FirstOrDefault(x => x.NotificationId == notificationId); if (!prev.Value) { NotificationItemsLog notificationItemsLog = new NotificationItemsLog(); notificationItemsLog.NotificationId = notificationId; notificationItemsLog.Value = true; notificationItemsLog.Time = DateTime.Now; entities.NotificationItemsLogs.Add(notificationItemsLog); } NotificationItemsLogLatest latest = entities.NotificationItemsLogLatests.FirstOrDefault( x => x.NotificationId == notificationId); if (!latest.Value) { latest.Value = true; latest.Time = DateTime.Now; entities.SaveChanges(); } continue; } NotificationItemsLogLatest notificationItemsLogLatest = entities.NotificationItemsLogLatests.FirstOrDefault(x => x.NotificationId == notificationId); if (notificationItemsLogLatest == null) { notificationItemsLogLatest = new NotificationItemsLogLatest(); notificationItemsLogLatest.NotificationId = notificationId; notificationItemsLogLatest.Value = false; notificationItemsLogLatest.Time = DateTime.Now; entities.NotificationItemsLogLatests.Add(notificationItemsLogLatest); entities.SaveChanges(); } else { if (!notificationItem.DisableNotification) { ItemsLogLatest itemLogLatest = entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == notificationItem.ItemId); double currentValue = double.Parse(itemLogLatest.Value); bool withoutNotification = false; if (notificationItem.NotificationType == (int)NotificationType.Lower) { if (currentValue < notificationItem.High) { withoutNotification = true; } } else if (notificationItem.NotificationType == (int)NotificationType.Between) { if (currentValue > notificationItem.Low & currentValue < notificationItem.High) { withoutNotification = true; } } else if (notificationItem.NotificationType == (int)NotificationType.Higher) { if (currentValue > notificationItem.Low) { withoutNotification = true; } } if (withoutNotification) { if (notificationItemsLogLatest.Value == false) { NotificationItemsLog notificationItemsLog = new NotificationItemsLog(); notificationItemsLog.NotificationId = notificationId; notificationItemsLog.Value = true; notificationItemsLog.Time = DateTime.Now; entities.NotificationItemsLogs.Add(notificationItemsLog); entities.SaveChanges(); //var bot = NotificationsBotInvoker.Instance; //bot.SendNotification(notificationItemsLog.NotificationLogId); NotificationsBotInvoker.RegisterNewRecord(notificationId, notificationItemsLog.NotificationLogId); } } else { // we have notification if (notificationItemsLogLatest.Value) { // we have a change in notification state NotificationItemsLog notificationItemsLog = new NotificationItemsLog(); notificationItemsLog.NotificationId = notificationId; notificationItemsLog.Value = false; notificationItemsLog.Time = DateTime.Now; entities.NotificationItemsLogs.Add(notificationItemsLog); entities.SaveChanges(); //var bot = NotificationsBotInvoker.Instance; //bot.SendNotification(notificationItemsLog.NotificationLogId); NotificationsBotInvoker.RegisterNewRecord(notificationId, notificationItemsLog.NotificationLogId); } } notificationItemsLogLatest.Value = withoutNotification; notificationItemsLogLatest.Time = DateTime.Now; entities.SaveChanges(); } else { if (notificationItemsLogLatest.Value == false) { NotificationItemsLog notificationItemsLog = new NotificationItemsLog(); notificationItemsLog.NotificationId = notificationId; notificationItemsLog.Value = true; notificationItemsLog.Time = DateTime.Now; entities.NotificationItemsLogs.Add(notificationItemsLog); entities.SaveChanges(); NotificationsBotInvoker.RegisterNewRecord(notificationId, notificationItemsLog.NotificationLogId); } } } Thread.Sleep(10); } Thread.Sleep(1000); } catch (Exception ex) { Logger.LogMonitoringServiceLibrary(ex); } } IsThreadRunning = false; }