using Backend.Models; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Quartz; using Repository.BackendRepository.Implement; using Repository.BackendRepository.Interface; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; using System.Linq; using NCrontab; using BackendWorkerService.Services.Implement; namespace BackendWorkerService.Quartz.Jobs { [DisallowConcurrentExecution] class WeatherAPIJob : IJob { private readonly ILogger logger; private readonly IBackgroundServicePostgresqlRepository backgroundServiceRepository; private readonly ILogger loggers; public WeatherAPIJob(ILogger logger, IBackgroundServicePostgresqlRepository backgroundServiceRepository, ILogger loggers) { this.logger = logger; this.backgroundServiceRepository = backgroundServiceRepository; this.loggers = loggers; } public async Task Execute(IJobExecutionContext context) { Task_Detail task_Detail = new Task_Detail(loggers, backgroundServiceRepository); try { var obixApiConfig = new ObixApiConfig(); string encoded = string.Empty; #region 取得obix 設定 var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var variableObix = await backgroundServiceRepository.GetAllAsync(sqlObix); obixApiConfig.ApiBase = variableObix.Where(x => x.Name == "ApiBase").Select(x => x.Value).FirstOrDefault(); obixApiConfig.UserName = variableObix.Where(x => x.Name == "UserName").Select(x => x.Value).FirstOrDefault(); obixApiConfig.Password = variableObix.Where(x => x.Name == "Password").Select(x => x.Value).FirstOrDefault(); encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(obixApiConfig.UserName + ":" + obixApiConfig.Password)); #endregion 取得obix 設定 #region 取得氣象預報 if (await task_Detail.GetNeedWorkTask("WeatherAPI", "api_weateher")) { try { await task_Detail.InsertWorkTime("WeatherAPI", "api_weateher"); var client = new HttpClient(); var DataNO = "F-D0047-061"; var UVUri = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/F-D0047-061?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&format=JSON&locationName=%E4%BF%A1%E7%BE%A9%E5%8D%80&elementName=Wx,PoP12h,T,RH"; HttpResponseMessage response = client.GetAsync(UVUri).Result; String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString(); var observation = JsonConvert.DeserializeObject(jsonUVs); logger.LogInformation("【WeatherAPIJob】【取得成功氣象預報】"); if (observation.Success != "true") { logger.LogInformation("【WeatherAPIJob】【取得氣象預報資料不正確】"); } else { logger.LogInformation("【WeatherAPIJob】【開始存入氣象預報到資料庫】"); List> WeatherAPIdbS = new List>(); var Type_ALL = observation.Records.Locations[0].Location[0].WeatherElement; string sql = ""; if (Type_ALL.Any()) { foreach (var a in Type_ALL) { if (a.ElementName.Contains(ElementName.露點溫度.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.DewPoint}' WHERE start_time = '{b.DataTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.DataTime}', null, now(), '{a.ElementName}', '{DataNO}', '{c.DewPoint}', null WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.體感溫度.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.ApparentTemperature}' WHERE start_time = '{b.DataTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.DataTime}', null, now(), '{a.ElementName}', '{DataNO}', '{c.ApparentTemperature}', null WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.溫度.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.Temperature}' WHERE start_time = '{b.DataTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.DataTime}', null, now(), '{a.ElementName}', '{DataNO}', '{c.Temperature}', null WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.相對濕度.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.RelativeHumidity}' WHERE start_time = '{b.DataTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.DataTime}', null, now(), '{a.ElementName}', '{DataNO}', '{c.RelativeHumidity}', null WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.舒適度指數.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.ComfortIndex}', get_value2 = '{c.ComfortIndexDescription}' WHERE start_time = '{b.DataTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.DataTime}', null, now(), '{a.ElementName}', '{DataNO}', '{c.ComfortIndex}', '{c.ComfortIndexDescription}' WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.風速.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.WindSpeed}', get_value2 = '{c.BeaufortScale}' WHERE start_time = '{b.DataTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.DataTime}', null, now(), '{a.ElementName}', '{DataNO}', '{c.WindSpeed}', '{c.BeaufortScale}' WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.風向.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.WindDirection}' WHERE start_time = '{b.DataTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.DataTime}', null, now(), '{a.ElementName}', '{DataNO}', '{c.WindDirection}', null WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.小時降雨機率.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.ProbabilityOfPrecipitation}' WHERE start_time = '{b.StartTime}' and end_time = '{b.EndTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.StartTime}', '{b.EndTime}', now(), '{a.ElementName}', '{DataNO}', '{c.ProbabilityOfPrecipitation}', null WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.天氣現象.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.Weather}', get_value2 = '{c.WeatherCode}' WHERE start_time = '{b.StartTime}' and end_time = '{b.EndTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.StartTime}', '{b.EndTime}', now(), '{a.ElementName}', '{DataNO}', '{c.Weather}', '{c.WeatherCode}' WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } else if (a.ElementName.Contains(ElementName.天氣預報綜合描述.ToString())) { foreach (var b in a.Time) { foreach (var c in b.ElementValue) { sql += $@"WITH updated_rows AS ( UPDATE weather_forecast SET get_value = '{c.WeatherDescription}' WHERE start_time = '{b.StartTime}' and end_time = '{b.EndTime}' and weather_type = '{a.ElementName}' RETURNING * ) insert into weather_forecast (start_time, end_time, created_at, weather_type, data_no, get_value, get_value2) select '{b.StartTime}', '{b.EndTime}', now(), '{a.ElementName}', '{DataNO}', '{c.WeatherDescription}', null WHERE NOT EXISTS (SELECT 1 FROM updated_rows);"; } } } } } if (!string.IsNullOrWhiteSpace(sql)) { await backgroundServiceRepository.ExecuteSql(sql, null); } } await task_Detail.InsertWorkTime_End("WeatherAPI", "api_weateher"); } catch (Exception ex) { await task_Detail.WorkFail("WeatherAPI", "api_weateher", ex.Message.ToString()); } } #endregion } catch (Exception exception) { logger.LogError("【WeatherAPIJob】【任務失敗】"); logger.LogError("【WeatherAPIJob】【任務失敗】[Exception]:{0}", exception.ToString()); } } } }