diff --git a/BackendWorkerService/Quartz/Jobs/WeatherAPIJob.cs b/BackendWorkerService/Quartz/Jobs/WeatherAPIJob.cs index e0127e6..0a8bed1 100644 --- a/BackendWorkerService/Quartz/Jobs/WeatherAPIJob.cs +++ b/BackendWorkerService/Quartz/Jobs/WeatherAPIJob.cs @@ -98,6 +98,21 @@ namespace BackendWorkerService.Quartz.Jobs encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(obixApiConfig.UserName + ":" + obixApiConfig.Password)); #endregion 取得obix 設定 //取得氣象預報 + if (await task_Detail.GetNeedWorkTask("WeatherAPI", "ClearWeatherData")) + { + try + { + await task_Detail.InsertWorkTime("WeatherAPI", "ClearWeatherData", null, new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 0, 0, 0)); + await ClearWeatherDataBef30(); + await task_Detail.InsertWorkTime_End("WeatherAPI", "ClearWeatherData"); + } + catch (Exception ex) + { + await task_Detail.WorkFail("WeatherAPI", "ClearWeatherData", ex.Message.ToString()); + logger.LogError($"ClearWeatherData error: {ex}, {ex.InnerException}, {ex.Message}"); + } + } + if (await task_Detail.GetNeedWorkTask("WeatherAPI", "api_weateher")) { try @@ -195,7 +210,7 @@ namespace BackendWorkerService.Quartz.Jobs { try { - await task_Detail.InsertWorkTime("WeatherAPI", "api_rain"); + await task_Detail.InsertWorkTime("WeatherAPI", "api_rain", null, new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 0, 25, 0)); WebClient mywebClient = new WebClient(); mywebClient.DownloadFile("https://opendata.cwb.gov.tw/fileapi/v1/opendataapi/W-C0033-003?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&downloadType=WEB&format=CAP", @"root/PowerfulRain.xml"); XmlDocument doc = new XmlDocument(); @@ -227,6 +242,7 @@ namespace BackendWorkerService.Quartz.Jobs { var ReStr = Fetch_PostWithJSONFormat($@"{obixApiConfig.ApiBase}obix/config/Arena/D2/CWB/L110/CAP/D2_CWB_L110_CAP_MET1/SeverityLEVL_RAIN/set", val.ToString()); UpdatedNiagara("api_rain", ReStr, id); + logger.LogInformation($"set niagara rain value success"); } } @@ -259,6 +275,7 @@ namespace BackendWorkerService.Quartz.Jobs { var ReStr = Fetch_PostWithJSONFormat($@"{obixApiConfig.ApiBase}obix/config/Arena/D2/CWB/L110/CAP/D2_CWB_L110_CAP_MET1/SeverityLEVL_RAIN/set", val.ToString()); UpdatedNiagara("api_rain", ReStr, id); + logger.LogInformation($"set niagara rain value success"); } } @@ -314,6 +331,7 @@ namespace BackendWorkerService.Quartz.Jobs var ReStr = Fetch_PostWithJSONFormat($@"{obixApiConfig.ApiBase}obix/config/Arena/D2/CWB/L110/CAP/D2_CWB_L110_CAP_MET2/SeverityLEVL_Typhoon/set", observation.Alert.Info.Urgency); UpdatedNiagara("api_typhoon", ReStr, id); + logger.LogInformation($"set niagara typhoon value success"); } } FolderFunction folderFunction = new FolderFunction(); @@ -346,6 +364,7 @@ namespace BackendWorkerService.Quartz.Jobs var ReStr = Fetch_PostWithJSONFormat($@"{obixApiConfig.ApiBase}obix/config/Arena/D2/CWB/L110/CAP/D2_CWB_L110_CAP_MET2/SeverityLEVL_Typhoon/set", observation.Alert.Info.Urgency); UpdatedNiagara("api_typhoon", ReStr, id); + logger.LogInformation($"set niagara typhoon value success"); } } FolderFunction folderFunction = new FolderFunction(); @@ -365,7 +384,7 @@ namespace BackendWorkerService.Quartz.Jobs { try { - await task_Detail.InsertWorkTime("WeatherAPI", "api_earthquake", null, new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 0, 25, 0)); + await task_Detail.InsertWorkTime("WeatherAPI", "api_earthquake"); var client = new HttpClient(); var UVUri = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/E-A0015-001?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&format=JSON&areaName=%E8%87%BA%E5%8C%97%E5%B8%82"; HttpResponseMessage response = client.GetAsync(UVUri).Result; @@ -421,10 +440,12 @@ namespace BackendWorkerService.Quartz.Jobs if (Nag.Contains("err")) { EarthquakeAPIdb.Add("@niagara", Nag); + logger.LogInformation($"set niagara earthquake value fail: {Nag}"); } else { EarthquakeAPIdb.Add("@niagara", "success"); + logger.LogInformation($"set niagara earthquake value success"); } @@ -448,10 +469,12 @@ namespace BackendWorkerService.Quartz.Jobs if (Nag.Contains("err")) { EarthquakeAPIdb.Add("@niagara", Nag); + logger.LogInformation($"set niagara earthquake value fail: {Nag}"); } else { EarthquakeAPIdb.Add("@niagara", "success"); + logger.LogInformation($"set niagara earthquake value success"); } EarthquakeAPIdbS.Add(EarthquakeAPIdb); } @@ -496,9 +519,11 @@ namespace BackendWorkerService.Quartz.Jobs var WxT = Fetch_PostWithJSONFormat($@"{obixApiConfig.ApiBase}obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/Wx/set", Wx.get_value); UpdatedNiagara("api_weateher", WxT, Wx.id); await task_Detail.InsertWorkTime_End("WeatherAPI", "set_weather"); + logger.LogInformation($"set niagara weather value success"); } catch (Exception ex) { + logger.LogInformation($"set niagara weather value fail"); await task_Detail.WorkFail("WeatherAPI", "set_weather", ex.Message.ToString()); } } @@ -562,5 +587,19 @@ namespace BackendWorkerService.Quartz.Jobs throw ex; } } + + public async Task ClearWeatherDataBef30() + { + try + { + string sql = $"delete from api_weateher WHERE created_at < DATE_SUB(NOW(), INTERVAL 1 MONTH)"; + await backgroundServiceRepository.ExecuteSql(sql); + } + catch (Exception e) + { + logger.LogError("[ClearWether] clear wether data error"); + throw e; + } + } } }