Esempio n. 1
0
        public bool EnableAlert(int alertId)
        {
            DatabaseAlert dbAlert = _dbQueryService.GetAlertById(alertId);

            dbAlert.DisabledUntil = null;
            return(_dbQueryService.PersistExistingAlert(dbAlert));
        }
Esempio n. 2
0
 public AlertDetails(DatabaseAlert dbAlert)
 {
     AlertId         = dbAlert.AlertId;
     AlertName       = dbAlert.AlertName;
     CameraId        = dbAlert.CameraId;
     UserId          = dbAlert.UserId;
     ContactMethod   = (ContactMethod)Enum.Parse(typeof(ContactMethod), dbAlert.ContactMethod);
     TriggerOperator = (TriggerOperator)Enum.Parse(typeof(TriggerOperator), dbAlert.TriggerOperator);
     TriggerNumber   = dbAlert.TriggerNumber;
     AlwaysActive    = dbAlert.AlwaysActive;
     //Could throw format exception if times aren't correctly formatted
     if (!dbAlert.StartTime.IsNullOrEmpty())
     {
         StartTime = DateTime.Parse(dbAlert.StartTime);
     }
     if (!dbAlert.EndTime.IsNullOrEmpty())
     {
         EndTime = DateTime.Parse(dbAlert.EndTime);
     }
     if (dbAlert.DisabledUntil != null)
     {
         DisabledUntil = dbAlert.DisabledUntil.Value;
     }
     if (dbAlert.SnoozedUntil != null)
     {
         SnoozedUntil = dbAlert.SnoozedUntil.Value;
     }
 }
Esempio n. 3
0
        private string GetEmailBody(DatabaseAlert alert, DatabasePerSecondStat earliestStatThatTriggersAlert, DatabaseUser user)
        {
            AlertTriggeredEmailInformation alertEmailInfo =
                GetAlertTriggeredInformation(alert, earliestStatThatTriggersAlert, user);
            string emailBody =
                RazorEngineWrapper.RunCompile("Views/Alert", "AlertTriggeredEmailBodyTemplate.cshtml", alertEmailInfo);

            return(emailBody);
        }
Esempio n. 4
0
 private void HandleTriggeringOfAlert(DatabaseAlert alert, DatabasePerSecondStat earliestStatThatTriggersAlert)
 {
     if ((ContactMethod)Enum.Parse(typeof(ContactMethod), alert.ContactMethod) == ContactMethod.Notification)
     {
         CreateNotificationForTriggeredAlert(alert, earliestStatThatTriggersAlert);
     }
     else if ((ContactMethod)Enum.Parse(typeof(ContactMethod), alert.ContactMethod) == ContactMethod.Email)
     {
         bool emailSuccess = SendAlertTriggeredEmail(alert, earliestStatThatTriggersAlert);
         CreateNotificationForTriggeredAlert(alert, earliestStatThatTriggersAlert, !emailSuccess);
     }
 }
Esempio n. 5
0
        private bool SendAlertTriggeredEmail(DatabaseAlert alert, DatabasePerSecondStat earliestStatThatTriggersAlert)
        {
            DatabaseUser databaseUser = _databaseQueryService.GetUserById(alert.UserId);

            if (!databaseUser.EmailAddress.IsNullOrEmpty())
            {
                string emailSubject = GetEmailSubject(alert, earliestStatThatTriggersAlert);
                string emailBody    = GetEmailBody(alert, earliestStatThatTriggersAlert, databaseUser);
                return(_emailService.SendEmail(databaseUser.EmailAddress, emailSubject, emailBody));
            }

            return(false);
        }
Esempio n. 6
0
        private void CreateNotificationForTriggeredAlert(DatabaseAlert alert,
                                                         DatabasePerSecondStat earliestStatThatTriggersAlert, bool failedEmail = false)
        {
            DatabaseNotification dbNotification = new DatabaseNotification
            {
                AlertId         = alert.AlertId,
                Acknowledged    = false,
                TriggerDateTime = earliestStatThatTriggersAlert.DateTime,
                FailedEmail     = failedEmail
            };

            _databaseQueryService.PersistNewNotification(dbNotification);
        }
Esempio n. 7
0
        public bool DoesNotificationNeedImage(DatabaseAlert dbAlert, DatabaseNotification dbNotification)
        {
            if (DateTime.Now.Subtract(dbNotification.TriggerDateTime).TotalMinutes <= 30)
            {
                //does notification have an image associated with it?
                List <DatabasePerSecondStat> perSecondStatsWithImageForNotification =
                    _databaseQueryService.GetPerSecondStatsWithFrmTriggeringAlert(dbAlert,
                                                                                  dbNotification.TriggerDateTime, dbNotification.TriggerDateTime.AddMinutes(30));
                return(perSecondStatsWithImageForNotification.IsNullOrEmpty());
            }

            return(false);
        }
Esempio n. 8
0
        public bool SaveAlert(AlertDetails alertDetails)
        {
            DatabaseAlert dbAlert = new DatabaseAlert(alertDetails);

            dbAlert.EscapeStringFields();
            if (alertDetails.AlertId != 0)
            {
                return(_dbQueryService.PersistExistingAlert(dbAlert));
            }
            else
            {
                return(_dbQueryService.PersistNewAlert(dbAlert));
            }
        }
Esempio n. 9
0
        private void HandleAlertMonitoring(DatabaseAlert alert, DateTime lastCheckup, DateTime checkupDateTime)
        {
            //check if any persecondstat has a count higher than threshold since last checkup
            DatabasePerSecondStat earliestStatThatTriggersAlert
                = _databaseQueryService.GetEarliestPerSecondStatTriggeringAlert(alert, lastCheckup, checkupDateTime);

            if (earliestStatThatTriggersAlert != null)
            {
                LogManager.GetLogger("AlertMonitoringService").Info($"Following stat triggered {alert.AlertName}: " +
                                                                    $"{earliestStatThatTriggersAlert.NumDetectedObjects} at {earliestStatThatTriggersAlert.DateTime}");
                HandleTriggeringOfAlert(alert, earliestStatThatTriggersAlert);
                //Snooze alert so that it doesn't get retriggered constantly if it monitors a constantly busy area
                SnoozeAlert(alert);
            }
        }
Esempio n. 10
0
        public bool DisableAlert(AlertDisablingInformation alertDisablingInformation)
        {
            DatabaseAlert dbAlert = _dbQueryService.GetAlertById(alertDisablingInformation.AlertId);
            DateTime      disabledUntilDateTime = DateTime.MaxValue;

            if (!alertDisablingInformation.DisableForever && alertDisablingInformation.DisabledUntil != null)
            {
                disabledUntilDateTime = alertDisablingInformation.DisabledUntil.Value;
            }
            else if (!alertDisablingInformation.DisableForever && alertDisablingInformation.DisabledUntil == null)
            {
                throw new InvalidDataException("Told to disable alert temporarily but no end date");
            }

            dbAlert.DisabledUntil = disabledUntilDateTime;
            return(_dbQueryService.PersistExistingAlert(dbAlert));
        }
Esempio n. 11
0
        private bool DoesAlertNeedsFrameImage(DatabaseAlert dbAlert)
        {
            if (dbAlert.SnoozedUntil == null || dbAlert.SnoozedUntil.Value < DateTime.Now)
            {
                return(true);
            }
            else
            {
                List <DatabaseNotification> dbNotifications = _dbQueryService.GetNotificationsForAlert(dbAlert.AlertId);
                foreach (var dbNotification in dbNotifications)
                {
                    if (_notificationService.DoesNotificationNeedImage(dbAlert, dbNotification))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Esempio n. 12
0
        private AlertTriggeredEmailInformation GetAlertTriggeredInformation(DatabaseAlert alert,
                                                                            DatabasePerSecondStat earliestStatThatTriggersAlert, DatabaseUser user)
        {
            DatabaseCamera camera = _databaseQueryService.GetCameraById(alert.CameraId);
            AlertTriggeredEmailInformation alertEmailInformation = new AlertTriggeredEmailInformation
            {
                CameraName    = camera.CameraName,
                AlertName     = alert.AlertName,
                DateTriggered = earliestStatThatTriggersAlert.DateTime
            };

            alertEmailInformation.Name = GetUserName(user);

            if (camera.LocationId != null)
            {
                alertEmailInformation.LocationName = _databaseQueryService.GetLocationById(camera.LocationId.Value).LocationName;
            }

            return(alertEmailInformation);
        }
Esempio n. 13
0
        public FrameInformation GetEarliestStatFrameForNotification(int notificationId)
        {
            DatabaseNotification         dbNotification = _dbQueryService.GetNotificationById(notificationId);
            DatabaseAlert                dbAlert        = _dbQueryService.GetAlertById(dbNotification.AlertId);
            List <DatabasePerSecondStat> statsForCamera =
                _dbQueryService.GetPerSecondStatsWithFrmTriggeringAlert(dbAlert, dbNotification.TriggerDateTime, dbNotification.TriggerDateTime.AddMinutes(30));
            var sortedStats = statsForCamera.OrderBy(stat => stat.DateTime);

            foreach (DatabasePerSecondStat stat in sortedStats)
            {
                if (!stat.FrameJpgPath.IsNullOrEmpty())
                {
                    FrameInformation frmInfo = new FrameInformation(stat);
                    if (frmInfo.FrmJpgRelPath.IsNullOrEmpty() == false)
                    {
                        return(frmInfo);
                    }
                }
            }

            return(null);
        }
Esempio n. 14
0
        public JpgStatFrameList GetTriggeringStatsFrameList(int notificationId)
        {
            DatabaseNotification         dbNotification = _dbQueryService.GetNotificationById(notificationId);
            DatabaseAlert                dbAlert        = _dbQueryService.GetAlertById(dbNotification.AlertId);
            List <DatabasePerSecondStat> statsForCamera = _dbQueryService.GetPerSecondStatsWithFrmTriggeringAlert(dbAlert, dbNotification.TriggerDateTime, dbNotification.TriggerDateTime.AddMinutes(30));
            JpgStatFrameList             frmList        = new JpgStatFrameList();

            frmList.JpgFramePathList = new List <FrameInformation>();
            foreach (DatabasePerSecondStat stat in statsForCamera)
            {
                if (!stat.FrameJpgPath.IsNullOrEmpty())
                {
                    FrameInformation frmInfo = new FrameInformation(stat);
                    if (frmInfo.FrmJpgRelPath.IsNullOrEmpty() == false)
                    {
                        frmList.JpgFramePathList.Add(frmInfo);
                    }
                }
            }

            return(frmList);
        }
Esempio n. 15
0
        public void setup()
        {
            DatabaseAlert existingDatabaseAlert = new DatabaseAlert()
            {
                AlertId         = 1,
                AlertName       = "test alert",
                AlwaysActive    = true,
                CameraId        = 1,
                ContactMethod   = "Notification",
                DisabledUntil   = null,
                EndTime         = "",
                SnoozedUntil    = null,
                StartTime       = "",
                TriggerNumber   = 1,
                TriggerOperator = "More",
                UserId          = 1,
            };
            DatabaseAlert newDatabaseAlert = new DatabaseAlert()
            {
                AlertId         = 0,
                AlertName       = "test alert",
                AlwaysActive    = true,
                CameraId        = 1,
                ContactMethod   = "Notification",
                DisabledUntil   = null,
                EndTime         = "",
                SnoozedUntil    = null,
                StartTime       = "",
                TriggerNumber   = 1,
                TriggerOperator = "More",
                UserId          = 1,
            };

            dbAlerts = new List <DatabaseAlert>();
            dbAlerts.Add(existingDatabaseAlert);
            dbAlerts.Add(newDatabaseAlert);
        }
Esempio n. 16
0
 public AlertSummary(DatabaseAlert alert)
 {
     TriggerNumber     = alert.TriggerNumber;
     IsMoreThanTrigger = (TriggerOperator)Enum.Parse(typeof(TriggerOperator), alert.TriggerOperator) == TriggerOperator.More;
 }
Esempio n. 17
0
 private void SnoozeAlert(DatabaseAlert alert)
 {
     alert.SnoozedUntil = DateTime.Now.AddMinutes(snoozeDurationMinutes);
     _databaseQueryService.PersistExistingAlert(alert);
 }
Esempio n. 18
0
 private static string GetEmailSubject(DatabaseAlert alert, DatabasePerSecondStat earliestStatThatTriggersAlert)
 {
     return($"Your alert '{alert.AlertName}' was triggered on {earliestStatThatTriggersAlert.DateTime.ToShortTimeString()}");
 }