ibms-MCUT/BackendWorkerService/Quartz/Jobs/WeatherAPIJob.cs
2025-01-16 15:06:18 +08:00

290 lines
19 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<WeatherAPIJob> logger;
private readonly IBackgroundServicePostgresqlRepository backgroundServiceRepository;
private readonly ILogger<Task_Detail> loggers;
public WeatherAPIJob(ILogger<WeatherAPIJob> logger, IBackgroundServicePostgresqlRepository backgroundServiceRepository, ILogger<Task_Detail> 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<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
#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<Root3>(jsonUVs);
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;
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());
}
}
}
}