2022-10-14 16:08:54 +08:00
|
|
|
|
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;
|
2023-05-04 16:45:05 +08:00
|
|
|
|
using BackendWorkerService.Services.Implement;
|
2022-10-14 16:08:54 +08:00
|
|
|
|
|
|
|
|
|
namespace BackendWorkerService.Quartz.Jobs
|
|
|
|
|
{
|
|
|
|
|
[DisallowConcurrentExecution]
|
|
|
|
|
class WeatherAPIJob : IJob
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<WeatherAPIJob> logger;
|
2025-01-16 15:06:18 +08:00
|
|
|
|
private readonly IBackgroundServicePostgresqlRepository backgroundServiceRepository;
|
2022-10-14 16:08:54 +08:00
|
|
|
|
private readonly ILogger<Task_Detail> loggers;
|
2025-01-16 15:06:18 +08:00
|
|
|
|
public WeatherAPIJob(ILogger<WeatherAPIJob> logger, IBackgroundServicePostgresqlRepository backgroundServiceRepository, ILogger<Task_Detail> loggers)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.backgroundServiceRepository = backgroundServiceRepository;
|
|
|
|
|
this.loggers = loggers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Execute(IJobExecutionContext context)
|
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
Task_Detail task_Detail = new Task_Detail(loggers, backgroundServiceRepository);
|
2022-10-14 16:08:54 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-05 16:46:26 +08:00
|
|
|
|
var obixApiConfig = new ObixApiConfig();
|
|
|
|
|
string encoded = string.Empty;
|
2025-01-16 15:06:18 +08:00
|
|
|
|
|
2023-05-05 16:46:26 +08:00
|
|
|
|
#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<KeyValue>(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 設定
|
2025-01-16 15:06:18 +08:00
|
|
|
|
|
|
|
|
|
#region 取得氣象預報
|
2022-10-14 16:08:54 +08:00
|
|
|
|
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";
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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";
|
2022-10-14 16:08:54 +08:00
|
|
|
|
HttpResponseMessage response = client.GetAsync(UVUri).Result;
|
|
|
|
|
String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString();
|
|
|
|
|
|
2025-01-16 15:06:18 +08:00
|
|
|
|
var observation = JsonConvert.DeserializeObject<Root3>(jsonUVs);
|
2022-10-14 16:08:54 +08:00
|
|
|
|
logger.LogInformation("【WeatherAPIJob】【取得成功氣象預報】");
|
|
|
|
|
|
|
|
|
|
if (observation.Success != "true")
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("【WeatherAPIJob】【取得氣象預報資料不正確】");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("【WeatherAPIJob】【開始存入氣象預報到資料庫】");
|
|
|
|
|
List<Dictionary<string, object>> WeatherAPIdbS = new List<Dictionary<string, object>>();
|
|
|
|
|
var Type_ALL = observation.Records.Locations[0].Location[0].WeatherElement;
|
2025-01-16 15:06:18 +08:00
|
|
|
|
string sql = "";
|
2022-10-14 16:08:54 +08:00
|
|
|
|
|
2025-01-16 15:06:18 +08:00
|
|
|
|
if (Type_ALL.Any())
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
foreach (var a in Type_ALL)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
if (a.ElementName.Contains(ElementName.露點溫度.ToString()))
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
foreach (var b in a.Time)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
else if (a.ElementName.Contains(ElementName.體感溫度.ToString()))
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
foreach (var b in a.Time)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
else if (a.ElementName.Contains(ElementName.溫度.ToString()))
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
else if (a.ElementName.Contains(ElementName.相對濕度.ToString()))
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
else if (a.ElementName.Contains(ElementName.舒適度指數.ToString()))
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
foreach (var b in a.Time)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
else if (a.ElementName.Contains(ElementName.風速.ToString()))
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
foreach (var b in a.Time)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
}
|
|
|
|
|
else if (a.ElementName.Contains(ElementName.風向.ToString()))
|
|
|
|
|
{
|
|
|
|
|
foreach (var b in a.Time)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
else if (a.ElementName.Contains(ElementName.小時降雨機率.ToString()))
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
foreach (var b in a.Time)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
}
|
|
|
|
|
else if (a.ElementName.Contains(ElementName.天氣預報綜合描述.ToString()))
|
|
|
|
|
{
|
|
|
|
|
foreach (var b in a.Time)
|
2022-10-14 16:08:54 +08:00
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
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);";
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-16 15:06:18 +08:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(sql))
|
|
|
|
|
{
|
|
|
|
|
await backgroundServiceRepository.ExecuteSql(sql, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
|
2025-01-16 15:06:18 +08:00
|
|
|
|
await task_Detail.InsertWorkTime_End("WeatherAPI", "api_weateher");
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-01-16 15:06:18 +08:00
|
|
|
|
await task_Detail.WorkFail("WeatherAPI", "api_weateher", ex.Message.ToString());
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
#endregion
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
logger.LogError("【WeatherAPIJob】【任務失敗】");
|
|
|
|
|
logger.LogError("【WeatherAPIJob】【任務失敗】[Exception]:{0}", exception.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-16 15:06:18 +08:00
|
|
|
|
}
|