1. 天氣 加入try catch

This commit is contained in:
Kai 2021-08-16 16:46:13 +08:00
parent 855fa4024d
commit ab0aa1ee1d

View File

@ -40,9 +40,11 @@ namespace SolarPower.Quartz.Jobs
var DateTimeNow = DateTime.Now; var DateTimeNow = DateTime.Now;
var dateTime = DateTimeNow.AddHours(-1).ToString("yyyy-MM-dd HH"); var dateTime = DateTimeNow.AddHours(-1).ToString("yyyy-MM-dd HH");
Root2 observation = null;
if (this.environment.IsDevelopment()) if (this.environment.IsDevelopment())
{ {
dateTime = "2021-08-09 10"; dateTime = "2021-08-16 09";
} }
logger.LogInformation("【CalcPowerStationJob】【任務開始】"); logger.LogInformation("【CalcPowerStationJob】【任務開始】");
@ -64,13 +66,23 @@ namespace SolarPower.Quartz.Jobs
List<WeatherObservation> weatherObservations = new List<WeatherObservation>(); List<WeatherObservation> weatherObservations = new List<WeatherObservation>();
#region () #region ()
try
{
logger.LogInformation("【CalcPowerStationJob】【開始取得氣象觀測】"); logger.LogInformation("【CalcPowerStationJob】【開始取得氣象觀測】");
var client = new HttpClient(); var client = new HttpClient();
var UVUri = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&elementName=TIME,TEMP"; var UVUri = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&elementName=TIME,TEMP";
HttpResponseMessage response = client.GetAsync(UVUri).Result; HttpResponseMessage response = client.GetAsync(UVUri).Result;
String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString(); String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString();
Root2 observation = JsonConvert.DeserializeObject<Root2>(jsonUVs); observation = JsonConvert.DeserializeObject<Root2>(jsonUVs);
logger.LogInformation("【CalcPowerStationJob】【取得成功氣象觀測】"); logger.LogInformation("【CalcPowerStationJob】【取得成功氣象觀測】");
}
catch (Exception ex)
{
logger.LogError("【CalcPowerStationJob】【取得失敗氣象觀測】");
logger.LogError("【{0}】{1}", "CalcPowerStationJob", ex.Message);
observation = null;
}
#endregion #endregion
#region step2. DB及電站編號找出該電站的控制器 #region step2. DB及電站編號找出該電站的控制器
@ -353,7 +365,7 @@ namespace SolarPower.Quartz.Jobs
#endregion #endregion
WeatherObservation weatherObservation = new WeatherObservation(); WeatherObservation weatherObservation = new WeatherObservation();
if (powerStation.WeathersStationId != null) if (powerStation.WeathersStationId != null && observation != null)
{ {
foreach (var Location in observation.Records.Location) foreach (var Location in observation.Records.Location)
{ {