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 ()
logger.LogInformation("【CalcPowerStationJob】【開始取得氣象觀測】"); try
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"; logger.LogInformation("【CalcPowerStationJob】【開始取得氣象觀測】");
HttpResponseMessage response = client.GetAsync(UVUri).Result; var client = new HttpClient();
String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString(); var UVUri = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&elementName=TIME,TEMP";
Root2 observation = JsonConvert.DeserializeObject<Root2>(jsonUVs); HttpResponseMessage response = client.GetAsync(UVUri).Result;
logger.LogInformation("【CalcPowerStationJob】【取得成功氣象觀測】"); String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString();
observation = JsonConvert.DeserializeObject<Root2>(jsonUVs);
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及電站編號找出該電站的控制器
@ -195,7 +207,7 @@ namespace SolarPower.Quartz.Jobs
#endregion #endregion
powerStationHistoriesHour.Add(history); powerStationHistoriesHour.Add(history);
logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime);
} }
#endregion #endregion
@ -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)
{ {
@ -363,7 +375,7 @@ namespace SolarPower.Quartz.Jobs
calcPowerStation.TodayWeatherTemp = Convert.ToDouble(Location.WeatherElement[0].ElementValue); calcPowerStation.TodayWeatherTemp = Convert.ToDouble(Location.WeatherElement[0].ElementValue);
weatherObservation.PowerStationId = powerStation.Id; weatherObservation.PowerStationId = powerStation.Id;
weatherObservation.Temp = Convert.ToDouble(Location.WeatherElement[0].ElementValue); 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; calcPowerStation.WeathersStationId = powerStation.WeathersStationId;
break; break;
} }
@ -380,8 +392,8 @@ namespace SolarPower.Quartz.Jobs
calcPowerStation.RateOfRain = weather.PoP; calcPowerStation.RateOfRain = weather.PoP;
} }
} }
#endregion #endregion
@ -581,7 +593,7 @@ namespace SolarPower.Quartz.Jobs
#endregion #endregion
#region step8. meter INSERT meter_history_hour #region step8. meter INSERT meter_history_hour
List<string> memter_history_properties = new List<string>() List<string> memter_history_properties = new List<string>()
{ {
"PowerStationId", "PowerStationId",
"TIMESTAMP", "TIMESTAMP",