예제 #1
0
        public async static Task Run([TimerTrigger("29 0 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"SetToken function executed at: {DateTime.Now.ToLocalTime().ToString(Constants.DateTimeFormatString)}");
            var slackMessenger = new SlackMessenger(_webhookUrl);

            try
            {
                var currentAuthToken = await KeyVault.GetAuthTokenFromKeyVault(_keyVaultName);

                var currentAuthTokenExpiryDate = GetExpiryDate(currentAuthToken);

                if (currentAuthTokenExpiryDate < DateTimeOffset.Now.AddDays(2))
                {
                    log.LogInformation("Auth token expires soon, starting refresh process...");

                    SetupHttpClient();

                    var username     = Environment.GetEnvironmentVariable("BrightUsername");
                    var password     = Environment.GetEnvironmentVariable("BrightPassword");
                    var authResponse = await GetAuthResponse(username, password);

                    if (authResponse.Valid)
                    {
                        log.LogInformation($"New auth token was retrieved successfully. Expiry date: {authResponse.ExpiryDate.ToLocalTime().ToString(Constants.DateTimeFormatString)}");
                        var secretVersion = await KeyVault.SaveAuthTokenToKeyVault(authResponse.Token, _keyVaultName);

                        log.LogInformation($"Auth token was saved to Azure Key Vault, secret version: '{secretVersion}'.");
                    }
                    else
                    {
                        throw new Exception("Auth token response was not valid.");
                    }
                }
                else
                {
                    log.LogInformation($"Auth token expires at {currentAuthTokenExpiryDate.ToLocalTime().ToString(Constants.DateTimeFormatString)}, not refreshing yet.");
                }
            }
            catch (Exception ex)
            {
                var error = $"Error in SetToken: {ex.Message}{Environment.NewLine}{ex.StackTrace}";
                log.LogError(error);
                await slackMessenger.SendMessage(error, Emoji.Exclamation, "SetToken Error");

                throw;
            }
            log.LogInformation($"SetToken function completed at: {DateTime.Now.ToLocalTime().ToString(Constants.DateTimeFormatString)}");
        }
예제 #2
0
        public async static Task Run([TimerTrigger("0 0 4 * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"ReadMeter function executed at: {DateTime.Now.ToLocalTime().ToString(Constants.DateTimeFormatString)}");

            var slackMessenger = new SlackMessenger(_webhookUrl);

            try
            {
                var brightAuthToken = await KeyVault.GetAuthTokenFromKeyVault(_keyVaultName);

                SetupHttpClient(brightAuthToken);

                var gasReadingResponse = await GetReading(_gasResourceId);

                var gasReading = gasReadingResponse.GetReading();

                var electricReadingResponse = await GetReading(_electricResourceId);

                var electricReading = electricReadingResponse.GetReading();

                await slackMessenger.SendMessage($"Electric: *{electricReading.Item2 / 1000}* kWh. Date {electricReading.Item1.ToLocalTime().ToString(Constants.DateTimeFormatString)}", Emoji.Zap, "Electric");

                var gasVolume = 3.6M * ((gasReading.Item2) / 1000M) / (_calorificValue * _correction);
                await slackMessenger.SendMessage($"Gas: *{Math.Round(gasVolume)}* m³. Date {gasReading.Item1.ToLocalTime().ToString(Constants.DateTimeFormatString)}", Emoji.Fire, "Gas");
            }
            catch (Exception ex)
            {
                var error = $"Error in ReadMeter: {ex.Message}{Environment.NewLine}{ex.StackTrace}";
                log.LogError(error);
                await slackMessenger.SendMessage(error, Emoji.Exclamation, "ReadMeter Error");

                throw;
            }

            log.LogInformation($"ReadMeter function completed at: {DateTime.Now.ToLocalTime().ToString(Constants.DateTimeFormatString)}");
        }