/// <summary>
 /// ctor
 /// </summary>
 /// <param name="runtimeSettings">Runtime settings object</param>
 /// <param name="logger">Application logger object</param>
 /// <param name="fileManager">File system object</param>
 public AlertManager(IRuntimeSettings runtimeSettings, IAppLogger logger, IFileManager fileManager)
 {
     _rs         = runtimeSettings;
     _logger     = logger;
     _fm         = fileManager;
     _smtpHelper = new SmtpHelper(_rs, _logger);
 }
Ejemplo n.º 2
0
 public EmailService(ISmtpHelper smtpHelper,
                     IOptions <SmtpConfiguration> smtpConfiguration,
                     IOptions <SendGridConfiguration> sendGridConfiguration)
 {
     _smtpHelper            = smtpHelper;
     _smtpConfiguration     = smtpConfiguration.Value;
     _sendGridConfiguration = sendGridConfiguration.Value;
 }
        private void SendBatteryNotices(IAppLogger logger, ISmtpHelper smtp, CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return;
            }

            const int DelayBetweenResendingAlertInMinutes = 10;
            var       delayBetweenResendingAlertInSeconds = TimeSpan.FromMinutes(DelayBetweenResendingAlertInMinutes).TotalSeconds;
            const int DelayBetweenCheckingTokenInSeconds  = 5;
            var       message = $"Changed to battery power!  Will send reminder every {DelayBetweenResendingAlertInMinutes} minutes.";

            logger.Info(message);
            smtp.Send("Power on Battery", message);

            var tokenCheckDelayInMilliseconds   = (int)TimeSpan.FromSeconds(DelayBetweenCheckingTokenInSeconds).TotalMilliseconds;
            var intervalCountOfDelayUntilResend = (int)(TimeSpan.FromSeconds(delayBetweenResendingAlertInSeconds).TotalMilliseconds / tokenCheckDelayInMilliseconds);
            var resendCount = 0;

            while (true)
            {
                for (var i = 0; i < intervalCountOfDelayUntilResend; i++)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                    Thread.Sleep(tokenCheckDelayInMilliseconds);
                }

                if (token.IsCancellationRequested)
                {
                    break;
                }

                // still on battery, send additional notice
                resendCount++;
                var repeatMessage = $"Still on battery for about {DelayBetweenResendingAlertInMinutes * resendCount} minutes";
                smtp.Send("Power on Battery", repeatMessage);
            }
        }