[WebApi][BGService]氣象局資料改版對應調整

This commit is contained in:
張家睿 2025-02-05 10:52:52 +08:00
parent a794808d49
commit 60003dd89f
3 changed files with 259 additions and 102 deletions

View File

@ -266,7 +266,119 @@ namespace Backend.Models
#endregion
#region weather api
public class Root3
{
public string Success { get; set; }
public Records3 Records { get; set; }
}
public class Records3
{
public string DatasetDescription { get; set; }
public string LocationsName { get; set; }
public string Dataid { get; set; }
public List<Locations3> Locations { get; set; }
}
public class Locations3
{
public List<Location3> Location { get; set; }
}
public class Location3
{
public string LocationName { get; set; }
public string Geocode { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public List<WeatherElement3> WeatherElement { get; set; }
}
public class WeatherElement3
{
public string ElementName { get; set; }
public List<Time> Time { get; set; }
}
public class Time
{
public string DataTime { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public List<ElementValue> ElementValue { get; set; }
}
public class ElementValue
{
/// <summary>
/// 溫度
/// </summary>
public string Temperature { get; set; }
/// <summary>
/// 露點溫度
/// </summary>
public string DewPoint { get; set; }
/// <summary>
/// 相對溼度
/// </summary>
public string RelativeHumidity { get; set; }
/// <summary>
/// 體感溫度
/// </summary>
public string ApparentTemperature { get; set; }
/// <summary>
/// 舒適度指數
/// </summary>
public string ComfortIndex { get; set; }
/// <summary>
/// 舒適度指數描述
/// </summary>
public string ComfortIndexDescription { get; set; }
/// <summary>
/// 風速
/// </summary>
public string WindSpeed { get; set; }
/// <summary>
/// 蒲福氏風級
/// </summary>
public string BeaufortScale { get; set; }
/// <summary>
/// 風向
/// </summary>
public string WindDirection { get; set; }
/// <summary>
/// 降雨機率
/// </summary>
public string ProbabilityOfPrecipitation { get; set; }
/// <summary>
/// 天氣現象
/// </summary>
public string Weather { get; set; }
/// <summary>
/// 天氣現象代號
/// </summary>
public string WeatherCode { get; set; }
/// <summary>
/// 天氣預報綜合描述
/// </summary>
public string WeatherDescription { get; set; }
}
public enum ElementName
{
,
,
,
,
,
,
,
,
,
}
#endregion

View File

@ -129,7 +129,8 @@ namespace BackendWorkerService.Quartz.Jobs
HttpResponseMessage response = client.GetAsync(UVUri).Result;
String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString();
var observation = JsonConvert.DeserializeObject<Root>(jsonUVs);
var observation = JsonConvert.DeserializeObject<Root3>(jsonUVs); // Use Root3 here
logger.LogInformation("【WeatherAPIJob】【取得成功氣象預報】");
if (observation.Success != "true")
@ -140,67 +141,35 @@ namespace BackendWorkerService.Quartz.Jobs
{
logger.LogInformation("【WeatherAPIJob】【開始存入氣象預報到資料庫】");
List<Dictionary<string, object>> WeatherAPIdbS = new List<Dictionary<string, object>>();
var Type_ALL = observation.Records.Locations[0].Location[0].WeatherElement;
var Type_ALL = observation.Records.Locations[0].Location[1].WeatherElement; // Location[1]是信義區
foreach (var a in Type_ALL)
{
foreach (var b in a.Time)
{
if (a.ElementName == "PoP12h" || a.ElementName == "Wx")
{
if (Convert.ToDateTime(b.StartTime) > DateTime.Now.AddDays(1))
{
break;
}
}
else
{
if (Convert.ToDateTime(b.DataTime) > DateTime.Now.AddDays(1))
{
break;
}
}
Dictionary<string, object> WeatherAPIdb = new Dictionary<string, object>()
{
{ "@weather_type", a.ElementName},
{ "@data_no", DataNO},
{ "@get_value", b.ElementValue[0].Value},
{ "@measures", b.ElementValue[0].Measures},
{ "@created_by", "system"},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
};
if (a.ElementName == "PoP12h" || a.ElementName == "Wx")
{
WeatherAPIdb.Add("@start_time", b.StartTime);
WeatherAPIdb.Add("@end_time", b.EndTime);
}
else
{
WeatherAPIdb.Add("@start_time", b.DataTime);
WeatherAPIdb.Add("@end_time", null);
}
WeatherAPIdbS.Add(WeatherAPIdb);
if (a.ElementName == "Wx")
{
Dictionary<string, object> TWeatherAPIdb = new Dictionary<string, object>()
{
{ "@weather_type", "WxV"},
{ "@weather_type", a.ElementName},
{ "@data_no", DataNO},
{ "@get_value", b.ElementValue[1].Value},
{ "@measures", b.ElementValue[1].Measures},
{ "@start_time", b.StartTime},
{ "@end_time", b.EndTime},
{ "@created_by", "system"},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}
};
WeatherAPIdbS.Add(TWeatherAPIdb);
}
ProcessElementValue(a, b, WeatherAPIdb, WeatherAPIdbS);
}
}
await backendRepository.AddMutiByCustomTable(WeatherAPIdbS, "api_weateher");
string sql = @"
INSERT INTO api_weateher (weather_type, data_no, get_value, get_value2, created_by, created_at, start_time, end_time)
VALUES (@weather_type, @data_no, @get_value, @get_value2, @created_by, @created_at, @start_time, @end_time)
ON DUPLICATE KEY UPDATE
get_value = VALUES(get_value),
get_value2 = VALUES(get_value2);";
foreach (var item in WeatherAPIdbS)
{
await backendRepository.ExecuteSql(sql, item);
}
}
await task_Detail.InsertWorkTime_End("WeatherAPI", "api_weateher");
@ -246,16 +215,16 @@ namespace BackendWorkerService.Quartz.Jobs
.FirstOrDefault();
Dictionary<string, object> RainAPIdb = new Dictionary<string, object>()
{
{ "@msgType", observation.Alert.MsgType},
{ "@headline", taipeiInfo.Headline},
{ "@severity_level", severity_level },
{ "@areaDesc", area},
{ "@onset", taipeiInfo.Onset.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@expires", taipeiInfo.Expires.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@created_by", "system"},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
};
{
{ "@msgType", observation.Alert.MsgType},
{ "@headline", taipeiInfo.Headline},
{ "@severity_level", severity_level },
{ "@areaDesc", area},
{ "@onset", taipeiInfo.Onset.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@expires", taipeiInfo.Expires.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@created_by", "system"},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
};
var id = await backendRepository.AddOneByCustomTableReturnId(RainAPIdb, "api_rain");
var val = RainValue(severity_level, taipeiInfo.Headline);
@ -388,17 +357,17 @@ namespace BackendWorkerService.Quartz.Jobs
var area = taipeiInfo.Area.Where(a => a.AreaDesc == "臺北市").Select(x => x.AreaDesc).FirstOrDefault();
Dictionary<string, object> EarthquakeAPIdb = new Dictionary<string, object>()
{
{ "@msgType", observation.Alert.MsgType},
{ "@headline", taipeiInfo.Headline},
{ "@areaDesc", area},
{ "@urgency",taipeiInfo.Urgency},
{ "@severity",taipeiInfo.Severity},
{ "@onset", taipeiInfo.Onset.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@expires", taipeiInfo.Expires.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@created_by", "system"},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
};
{
{ "@msgType", observation.Alert.MsgType},
{ "@headline", taipeiInfo.Headline},
{ "@areaDesc", area},
{ "@urgency",taipeiInfo.Urgency},
{ "@severity",taipeiInfo.Severity},
{ "@onset", taipeiInfo.Onset.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@expires", taipeiInfo.Expires.ToString("yyyy-MM-dd HH:mm:ss")},
{ "@created_by", "system"},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
};
var id = await backendRepository.AddOneByCustomTableReturnId(EarthquakeAPIdb, "api_typhoon");
if (taipeiInfo.Urgency != null && taipeiInfo.Urgency != "Expected")
{
@ -532,16 +501,16 @@ namespace BackendWorkerService.Quartz.Jobs
if (NeedCallApi == 0)
{
Dictionary<string, object> EarthquakeAPIdb = new Dictionary<string, object>()
{
{ "@earthquakeNo", eq.EarthquakeNo},
{ "@newEarthquake", 1},
{ "@originTime", DateTime.Parse(eq.EarthquakeInfo.OriginTime.ToString(), System.Globalization.CultureInfo.CurrentCulture)},
{ "@magnitudeValue", eq.EarthquakeInfo.EarthquakeMagnitude.MagnitudeValue},
{ "@areaName", exist.CountyName},
{ "@areaIntensity", exist.AreaIntensity},
{ "@created_by", "system"},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
};
{
{ "@earthquakeNo", eq.EarthquakeNo},
{ "@newEarthquake", 1},
{ "@originTime", DateTime.Parse(eq.EarthquakeInfo.OriginTime.ToString(), System.Globalization.CultureInfo.CurrentCulture)},
{ "@magnitudeValue", eq.EarthquakeInfo.EarthquakeMagnitude.MagnitudeValue},
{ "@areaName", exist.CountyName},
{ "@areaIntensity", exist.AreaIntensity},
{ "@created_by", "system"},
{ "@created_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
};
var Nag = Fetch_PostWithJSONFormat($@"{obixApiConfig.ApiBase}obix/config/Arena/D2/CWB/L110/CAP/D2_CWB_L110_CAP_GEO/ALMLEVL_LMI/set", Regex.Match(exist.AreaIntensity, @"\d+").ToString());
@ -669,25 +638,25 @@ namespace BackendWorkerService.Quartz.Jobs
await task_Detail.InsertWorkTime("WeatherAPI", "set_weather");
var sql = @$"SELECT
id,
weather_type,
get_value
FROM api_weateher
WHERE id IN (
SELECT MAX(id)
FROM api_weateher
WHERE start_time < NOW()
GROUP BY weather_type
)
ORDER BY start_time DESC";
id,
weather_type,
get_value
FROM api_weateher
WHERE id IN (
SELECT MAX(id)
FROM api_weateher
WHERE start_time < NOW()
GROUP BY weather_type
)
ORDER BY start_time DESC";
var types = await backendRepository.GetAllAsync<ShowWeather>(sql);
var urlMapping = new Dictionary<string, string>
{
{ "T", "obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/T/set" },
{ "RH", "obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/RH/set" },
{ "PoP12h", "obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/PoP6h/set" },
{ "Wx", "obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/Wx/set" }
};
{
{ "溫度", "obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/T/set" },
{ "相對濕度", "obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/RH/set" },
{ "3小時降雨機率", "obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/PoP6h/set" },
{ "天氣現象", "obix/config/Arena/D2/CWB/L110/OPD/D2_CWB_L110_OPD_element/Wx/set" }
};
// 建立批次請求
var batchRequests = types
@ -880,6 +849,82 @@ namespace BackendWorkerService.Quartz.Jobs
}
}
//Helper Method to process Element Values
private void ProcessElementValue(WeatherElement3 a, Time b, Dictionary<string, object> WeatherAPIdb, List<Dictionary<string, object>> WeatherAPIdbS)
{
//Common parameters for element value
if (a.ElementName.Contains(ElementName..ToString()) || a.ElementName.Contains(ElementName..ToString()) || a.ElementName.Contains(ElementName..ToString()))
{
if (Convert.ToDateTime(b.StartTime) > DateTime.Now.AddDays(1))
{
return; // Skip to next element
}
WeatherAPIdb.Add("@start_time", Convert.ToDateTime(b.StartTime).ToString("yyyy-MM-dd HH:mm:ss"));
WeatherAPIdb.Add("@end_time", Convert.ToDateTime(b.EndTime).ToString("yyyy-MM-dd HH:mm:ss"));
}
else
{
if (Convert.ToDateTime(b.DataTime) > DateTime.Now.AddDays(1))
{
return; // Skip to next element
}
WeatherAPIdb.Add("@start_time", Convert.ToDateTime(b.DataTime).ToString("yyyy-MM-dd HH:mm:ss"));
WeatherAPIdb.Add("@end_time", null);
}
//Switch statement to handle different element names and their values
switch (a.ElementName)
{
case "溫度":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].Temperature);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "露點溫度":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].DewPoint);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "相對濕度":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].RelativeHumidity);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "體感溫度":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].ApparentTemperature);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "舒適度指數":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].ComfortIndex);
WeatherAPIdb.Add("@get_value2", b.ElementValue[0].ComfortIndexDescription);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "風速":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].WindSpeed);
WeatherAPIdb.Add("@get_value2", b.ElementValue[0].BeaufortScale);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "風向":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].WindDirection);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "3小時降雨機率":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].ProbabilityOfPrecipitation);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "天氣現象":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].Weather);
WeatherAPIdb.Add("@get_value2", b.ElementValue[0].WeatherCode);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
case "天氣預報綜合描述":
WeatherAPIdb.Add("@get_value", b.ElementValue[0].WeatherDescription);
WeatherAPIdbS.Add(new Dictionary<string, object>(WeatherAPIdb));
break;
}
}
public async void UpdatedNiagara(string DBTableName, string ResponseStr, int CheckNumId)
{

View File

@ -47,27 +47,27 @@ namespace FrontendWebApi.ApiControllers
(
SELECT w.get_value
FROM api_weateher w
WHERE w.weather_type = 'Wx' AND @DateNow BETWEEN w.start_time AND w.end_time ORDER BY created_at DESC, w.id desc
WHERE w.weather_type = '' AND @DateNow BETWEEN w.start_time AND w.end_time ORDER BY created_at DESC, w.id desc
limit 1
) AS WxText,
(
SELECT wd.WeatherKey
FROM api_weateher w
LEFT JOIN weather_description wd ON w.get_value = wd.WeatherValue
WHERE w.weather_type = 'WxV' AND @DateNow BETWEEN w.start_time AND w.end_time ORDER BY created_at DESC, w.id desc
LEFT JOIN weather_description wd ON w.get_value2 = wd.WeatherValue
WHERE w.weather_type = '' AND @DateNow BETWEEN w.start_time AND w.end_time ORDER BY created_at DESC, w.id desc
limit 1
) AS Wx,
(
SELECT w.get_value
FROM api_weateher w
WHERE w.weather_type = 'T' AND @DateNow >= w.start_time
WHERE w.weather_type = '' AND @DateNow >= w.start_time
order by created_at desc, id desc
limit 1
) AS Temp,
(
SELECT w.get_value
FROM api_weateher w
WHERE w.weather_type = 'RH' AND @DateNow >= w.start_time
WHERE w.weather_type = '' AND @DateNow >= w.start_time
order by created_at desc, id desc
limit 1
) AS RH