using System; using DinkToPdf.Contracts; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Models.Service.BI; namespace TEAMModelOS.Function { public class IESTimerTrigger { private readonly ILogger _logger; /// /// 文档。https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp /// Timer 在线测试 https://ncrontab.swimburger.net/ /// private readonly AzureCosmosFactory _azureCosmos; private readonly DingDing _dingDing; private readonly AzureStorageFactory _azureStorage; private readonly AzureRedisFactory _azureRedis; //private readonly IConverter _converter; private readonly SnowflakeId _snowflakeId; private readonly IHttpClientFactory _httpClient; private readonly CoreAPIHttpService _coreAPIHttpService; //private IPSearcher _ipSearcher; private readonly IConfiguration _configuration; public IESTimerTrigger(ILoggerFactory loggerFactory, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient, SnowflakeId snowflakeId, AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis) { _logger = loggerFactory.CreateLogger(); _azureCosmos = azureCosmos; _dingDing = dingDing; _azureStorage = azureStorage; _azureRedis = azureRedis; // _converter = converter; _snowflakeId=snowflakeId; _httpClient = httpClient; // _ipSearcher = ipSearcher; _coreAPIHttpService = coreAPIHttpService; _configuration= configuration; } /// /// //0 1 * * * * 一天中每小时的第 1 分钟 ///0 */10 * * * * 每五分钟一次 /// /// /// [Function("DailyReport")] public async Task DailyReport([TimerTrigger("0 1 * * * *")] TimerInfo myTimer) { _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); try { await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.location},定时任务{DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss")}", GroupNames.成都开发測試群組); string? local = Environment.GetEnvironmentVariable("Option:Location"); // if (local.Contains("Test")||local.Contains("Dep")) { int am = 8; int pm = 20; //int am = DateTimeOffset.Now.Hour; //int pm = DateTimeOffset.Now.Hour; var url = Environment.GetEnvironmentVariable("HaBookAuth:CoreAPI"); var clientID = Environment.GetEnvironmentVariable("HaBookAuth:CoreService:clientID"); var clientSecret = Environment.GetEnvironmentVariable("HaBookAuth:CoreService:clientSecret"); string location = ""; if (local!.Contains("China")) { location = "China"; } else if (local.Contains("Global")) { location = "Global"; } var client = _httpClient.CreateClient(); var token = CoreTokenExtensions.CreateAccessToken(clientID, clientSecret, location).Result; if (client.DefaultRequestHeaders.Contains("Authorization")) { client.DefaultRequestHeaders.Remove("Authorization"); client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}"); } else { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}"); } var data = await TEAMModelOS.SDK.Models.Service.SystemService.AccumulateDaily(_configuration, _azureRedis, _azureCosmos,_azureStorage, _coreAPIHttpService, _dingDing, client, _snowflakeId, url, am, pm); //await _dingDing.SendBotMsg($"返回数据{data.ToJsonString()}", GroupNames.成都开发測試群組); } } catch (Exception ex) { await _dingDing.SendBotMsg($"{DailyReport}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組); } if (myTimer.ScheduleStatus is not null) { _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}"); } } /// /// 每天執行 取得IOT TeachingData 並統計昨日每校Redis資料 執行時間:每日凌晨1時1分 /// [Function("BICrtDailyAnal")] //0 1 0 * * * 一天中00的第 1 分钟 //0 1 * * * * 一天中每小时的第 1 分钟 //0 */10 * * * * 每五分钟一次 public async Task BICreatDailyAnalData([TimerTrigger("0 1 1 * * *")] TimerInfo myTimer, ILogger log) { var _azureCosmosClient = _azureCosmos.GetCosmosClient(); var _azureCosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2"); var _azureCosmosClientCsv2CnRead = _azureCosmos.GetCosmosClient(name: "CoreServiceV2CnRead"); var datetime = DateTimeOffset.UtcNow.AddHours(-12); //統計昨天的數據 var y = $"{datetime.Year}"; var m = datetime.Month >= 10 ? $"{datetime.Month}" : $"0{datetime.Month}"; var d = datetime.Day >= 10 ? $"{datetime.Day}" : $"0{datetime.Day}"; //生成學校IOT數據 await BIProdAnalysis.BICreatDailyAnalData(_azureRedis, _azureCosmosClient, _azureCosmosClientCsv2, _azureCosmosClientCsv2CnRead, _dingDing, y, m, d); //刪除三個月以前的Redis數據 [待做] } } }