diff --git a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs index ec69123..c8a016e 100644 --- a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs @@ -40,9 +40,11 @@ namespace SolarPower.Quartz.Jobs var DateTimeNow = DateTime.Now; var dateTime = DateTimeNow.AddHours(-1).ToString("yyyy-MM-dd HH"); + Root2 observation = null; + if (this.environment.IsDevelopment()) { - dateTime = "2021-08-09 10"; + dateTime = "2021-08-16 09"; } logger.LogInformation("【CalcPowerStationJob】【任務開始】"); @@ -64,13 +66,23 @@ namespace SolarPower.Quartz.Jobs List weatherObservations = new List(); #region 氣象觀測(取資料) - logger.LogInformation("【CalcPowerStationJob】【開始取得氣象觀測】"); - 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"; - HttpResponseMessage response = client.GetAsync(UVUri).Result; - String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString(); - Root2 observation = JsonConvert.DeserializeObject(jsonUVs); - logger.LogInformation("【CalcPowerStationJob】【取得成功氣象觀測】"); + try + { + logger.LogInformation("【CalcPowerStationJob】【開始取得氣象觀測】"); + 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"; + HttpResponseMessage response = client.GetAsync(UVUri).Result; + String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString(); + observation = JsonConvert.DeserializeObject(jsonUVs); + logger.LogInformation("【CalcPowerStationJob】【取得成功氣象觀測】"); + } + catch (Exception ex) + { + logger.LogError("【CalcPowerStationJob】【取得失敗氣象觀測】"); + logger.LogError("【{0}】{1}", "CalcPowerStationJob", ex.Message); + observation = null; + } + #endregion #region step2. 從電站的DB及電站編號找出該電站的控制器 @@ -195,7 +207,7 @@ namespace SolarPower.Quartz.Jobs #endregion powerStationHistoriesHour.Add(history); - + logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); } #endregion @@ -353,7 +365,7 @@ namespace SolarPower.Quartz.Jobs #endregion WeatherObservation weatherObservation = new WeatherObservation(); - if (powerStation.WeathersStationId != null) + if (powerStation.WeathersStationId != null && observation != null) { foreach (var Location in observation.Records.Location) { @@ -363,7 +375,7 @@ namespace SolarPower.Quartz.Jobs calcPowerStation.TodayWeatherTemp = Convert.ToDouble(Location.WeatherElement[0].ElementValue); weatherObservation.PowerStationId = powerStation.Id; weatherObservation.Temp = Convert.ToDouble(Location.WeatherElement[0].ElementValue); - weatherObservation.ObsTime = !string.IsNullOrEmpty(Location.Time.ObsTime)? Convert.ToInt32(Location.Time.ObsTime.Substring(0, 4)) >= 1971 ? Location.Time.ObsTime : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + weatherObservation.ObsTime = !string.IsNullOrEmpty(Location.Time.ObsTime) ? Convert.ToInt32(Location.Time.ObsTime.Substring(0, 4)) >= 1971 ? Location.Time.ObsTime : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); calcPowerStation.WeathersStationId = powerStation.WeathersStationId; break; } @@ -380,8 +392,8 @@ namespace SolarPower.Quartz.Jobs calcPowerStation.RateOfRain = weather.PoP; } - - + + } #endregion @@ -581,7 +593,7 @@ namespace SolarPower.Quartz.Jobs #endregion #region step8. 將 meter INSERT 到 meter_history_hour 資料表 - List memter_history_properties = new List() + List memter_history_properties = new List() { "PowerStationId", "TIMESTAMP",