/// <inheritdoc /> public void NotifyPowerFromWall() { //if (!_isBatteryDetected) //{ // _logger.Warn($"{nameof(NotifyPowerFromWall)} was triggered but there is no connected battery... wtf??"); // return; //} if (_taskRunningOnBattery == null) { _logger.Warn($"Received call to {nameof(NotifyPowerFromWall)} but the battery task {nameof(_taskRunningOnBattery)} was not running. Exit early."); return; } _ctsBattery?.Cancel(); var message = "Changed to wall power"; _logger.Info(message); _smtpHelper.Send("Power on Wall", message); }
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); } }
public async Task SendEmailAsync(List <string> recipients, string subject, string htmlMessage) { _smtpHelper.ApiKey = _sendGridConfiguration.ApiKey; _smtpHelper.Subject = subject; _smtpHelper.Sender = _sendGridConfiguration.Sender; _smtpHelper.ToRecipients = recipients; _smtpHelper.EmailBody = htmlMessage; _smtpHelper.SmtpSettings = new SmtpSettings { SmtpHost = _smtpConfiguration.SmtpHost, SmtpPassword = _smtpConfiguration.SmtpPassword, SmtpPort = _smtpConfiguration.SmtpPort, SmtpUserName = _smtpConfiguration.SmtpUserName }; await _smtpHelper.Send(); }