using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Quartz; using SolarPower.Models.PowerStation; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; namespace SolarPower.Quartz.Jobs { [DisallowConcurrentExecution] public class CalcAvgPowerStationJob : IJob { private readonly ILogger logger; private readonly IPowerStationRepository powerStationRepository; public CalcAvgPowerStationJob(ILogger logger, IPowerStationRepository powerStationRepository) { this.logger = logger; this.powerStationRepository = powerStationRepository; } public async Task Execute(IJobExecutionContext context) { try { #region step1. 找出所有電站 var powerStations = await powerStationRepository.GetAllAsync(); #endregion List calcAvgPowerStations = new List(); List powerStationHistoryDays = new List(); List insertPowerStationHistoryMonths = new List(); List updatePowerStationHistoryMonths = new List(); List pyrheliometerHistoryDays = new List(); List insertPyrheliometerHistoryMonths = new List(); List updatePyrheliometerHistoryMonths = new List(); List allofInverterHistorDays = new List(); List insertInverterHistoryMonths = new List(); List updateInverterHistoryMonths = new List(); List weatherForecasts = new List(); var DateTimeNow = DateTime.Now; var count = 0; #region 氣象觀測(取資料) var client = new HttpClient(); var UVUri = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/F-C0032-001?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80&elementName=Wx,PoP&sort=time"; HttpResponseMessage response = client.GetAsync(UVUri).Result; String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString(); Root2 observation = JsonConvert.DeserializeObject(jsonUVs); #endregion foreach(var location in observation.Records.Location) { WeatherForecast weatherForecast = new WeatherForecast(); weatherForecast.LocationName = location.LocationName; List SubweatherForecasts = new List(); foreach (var a in location.WeatherElement) { if (a.ElementName == "Wx") { foreach (var time in a.Time) { int index = 0; if (location.WeatherElement[0].ElementName == "Wx") { weatherForecast = new WeatherForecast(); weatherForecast.LocationName = location.LocationName; weatherForecast.StartTime = time.StartTime.ToString(); weatherForecast.EndTime = time.EndTime.ToString(); weatherForecast.Wx = time.Parameter.ParameterName; weatherForecast.WxValue = time.Parameter.ParameterValue; SubweatherForecasts.Add(weatherForecast); } else { SubweatherForecasts[index].StartTime = time.StartTime.ToString(); SubweatherForecasts[index].EndTime = time.EndTime.ToString(); SubweatherForecasts[index].Wx = time.Parameter.ParameterName; SubweatherForecasts[index].WxValue = time.Parameter.ParameterValue; //SubweatherForecasts.Add(weatherForecast); index++; } //weatherForecasts.Add(weatherForecast); } } if (a.ElementName == "PoP") { int index = 0; foreach (var time in a.Time) { if (location.WeatherElement[0].ElementName == "PoP") { weatherForecast = new WeatherForecast(); weatherForecast.LocationName = location.LocationName; weatherForecast.PoP = time.Parameter.ParameterName; SubweatherForecasts.Add(weatherForecast); } else { SubweatherForecasts[index].PoP = time.Parameter.ParameterName; //SubweatherForecasts.Add(weatherForecast); index++; } } } } weatherForecasts.AddRange(SubweatherForecasts); } List weather_forecast_properties = new List() { "LocationName", "StartTime", "EndTime", "Wx", "WxValue", "PoP" }; await powerStationRepository.AddWeatherForecast(weatherForecasts, weather_forecast_properties); #region step2. 從電站的DB及電站編號找出該電站的控制器 foreach (var powerStation in powerStations) { if (count > 0) { break; } var calcPowerStation = new PowerStation(); calcPowerStation.Id = powerStation.Id; var dateNowDay = DateTimeNow.AddDays(-1).ToString("yyyy-MM-dd"); dateNowDay = "2021-07-08"; #region step2-1. 計算該電站的30天平均資料 var table_name = String.Format("`{0}`.`{1}01_station`", powerStation.SiteDB, powerStation.Code); var history = await powerStationRepository.CalcAvgPowerStationHistory30day(dateNowDay, table_name); if (history != null) { history.PowerStationId = powerStation.Id; #region 計算電站30天的 kWh/kWp 與 PR 值 #region 平均kWh/kWp //直接填寫 calcPowerStation.Avg_kwhkwp = history.AvgKWHKWP; #endregion #region 平均PR //直接填寫 calcPowerStation.Avg_PR = history.AvgPR; #endregion #endregion } #endregion #region step2-2 計算電站30天的日照計平均值 var avgPyrheliometerHistory = await powerStationRepository.CalcAvgPyrheliometerHistory30day(dateNowDay, powerStation.Id); if (avgPyrheliometerHistory != null) { calcPowerStation.Avg_irradiance = avgPyrheliometerHistory.AvgIrradiance; } calcAvgPowerStations.Add(calcPowerStation); #endregion #region step2-3. 計算昨天的所有值總和 //電站資訊 var historyDay = await powerStationRepository.GetLastOnePowerStationHistoryByDay(dateNowDay, table_name); if (historyDay != null) { historyDay.PowerStationId = powerStation.Id; powerStationHistoryDays.Add(historyDay); } //日照計 var pyrheliometerHistorDay = await powerStationRepository.CalcPyrheliometerHistoryDayDataByPowerStationId(dateNowDay, powerStation.Id); if (pyrheliometerHistorDay != null) { pyrheliometerHistoryDays.Add(pyrheliometerHistorDay); } //逆變器 var inverterHistoriesDay = await powerStationRepository.CalcInverterHistoryDayDataByPowerStationId(dateNowDay, powerStation.SiteDB, powerStation.Id); if (inverterHistoriesDay != null) { foreach(var inverterHistoryDay in inverterHistoriesDay) { allofInverterHistorDays.Add(inverterHistoryDay); } } #endregion #region step2-4. 計算這個月的所有值總和 //判斷這個月是否已存在 var dateNowMonth = DateTimeNow.ToString("yyyy-MM"); //電站該月份的歷史資料 var exist_history = await powerStationRepository.GetOnePowerStationHistoryByPowerStationIdAndMonth(powerStation.Id, dateNowMonth); if (exist_history == null) { //新增 var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth); if (historyMonth != null) { historyMonth.Timestamp = Convert.ToDateTime(historyMonth.Timestamp).ToString("yyyy-MM-dd"); insertPowerStationHistoryMonths.Add(historyMonth); } } else { //修改 var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth); if (historyMonth != null) { updatePowerStationHistoryMonths.Add(historyMonth); } } //電站該月份的的日照度歷史資料 var exist_pyrheliometer_history = await powerStationRepository.GetOnePyrheliometerHistoryByMonth(dateNowMonth, powerStation.Id); if (exist_pyrheliometer_history == null) { //新增 var pyrheliometerHistoryMonth = await powerStationRepository.CalcPyrheliometerHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id); if (pyrheliometerHistoryMonth != null) { pyrheliometerHistoryMonth.Timestamp = Convert.ToDateTime(pyrheliometerHistoryMonth.Timestamp).ToString("yyyy-MM-dd"); insertPyrheliometerHistoryMonths.Add(pyrheliometerHistoryMonth); } } else { //修改 var pyrheliometerHistoryMonth = await powerStationRepository.CalcPyrheliometerHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id); if (pyrheliometerHistoryMonth != null) { updatePyrheliometerHistoryMonths.Add(pyrheliometerHistoryMonth); } } //電站該月份的的逆變器歷史資料 var exist_inverter_histories = await powerStationRepository.GetInverterHistoryByPowerStationIdAndMonth(dateNowMonth, powerStation.Id); if (exist_inverter_histories.Count == 0 ) { //新增 var inverterHistoriesMonth = await powerStationRepository.CalcInverterHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.SiteDB, powerStation.Id); if (inverterHistoriesMonth.Count > 0) { foreach(var inverterHistoryMonth in inverterHistoriesMonth) { inverterHistoryMonth.TIMESTAMP = Convert.ToDateTime(inverterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd"); insertInverterHistoryMonths.Add(inverterHistoryMonth); } } } else { //修改 var inverterHistoriesMonth = await powerStationRepository.CalcInverterHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.SiteDB, powerStation.Id); if (inverterHistoriesMonth.Count > 0) { foreach (var inverterHistoryMonth in inverterHistoriesMonth) { inverterHistoryMonth.TIMESTAMP = Convert.ToDateTime(inverterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd"); updateInverterHistoryMonths.Add(inverterHistoryMonth); } } } #endregion count++; } #endregion #region step3. calcPowerStations UPDATE 到 power_station 資料表 List power_station_properties = new List() { "Id", "avg_kwhkwp", "avg_PR", "avg_irradiance" }; await powerStationRepository.UpdateList(calcAvgPowerStations, power_station_properties); #endregion #region step4. 將各電站的每日及月的資料insert or update 各資料表 //每日 List history_properties_day = new List() { "PowerStationId", "TIMESTAMP", "SITEID", "SITETYPE", "TODAYKWH", "TOTALKWH", "KWHKWP", "PR", "MP", "SolarHour" }; await powerStationRepository.AddPowerStationHistoryDayList(powerStationHistoryDays, history_properties_day); //每月 List history_properties_month = new List() { "PowerStationId", "TIMESTAMP", "SITEID", "SITETYPE", "MonthKWh", "TOTALKWH", "KWHKWP", "PR", "MP", "SolarHour" }; if (insertPowerStationHistoryMonths.Count > 0) { await powerStationRepository.AddPowerStationHistoryMonthList(insertPowerStationHistoryMonths, history_properties_month); } if (updatePowerStationHistoryMonths.Count > 0) { await powerStationRepository.UpdatePowerStationHistoryMonthList(updatePowerStationHistoryMonths); } #endregion #region step5. 將各電站的每日及月的日照度資料insert or update 各資料表 List pyrheliometer_history_properties = new List() { "PowerStationId", "Timestamp", "Irradiance", "Temperature" }; //每日 await powerStationRepository.AddPyrheliometerHistoryDayList(pyrheliometerHistoryDays, pyrheliometer_history_properties); //每月 if (insertPyrheliometerHistoryMonths.Count > 0) { await powerStationRepository.AddPyrheliometerHistoryMonthList(insertPyrheliometerHistoryMonths, pyrheliometer_history_properties); } if (updatePyrheliometerHistoryMonths.Count > 0) { await powerStationRepository.UpdatePyrheliometerHistoryMonthList(updatePyrheliometerHistoryMonths); } #endregion #region step6. 將各電站的每日及月的逆變器資料insert or update 各資料表 List inverter_history_properties = new List() { "PowerStationId", "TIMESTAMP", "INVERTERID", "KWH", "TODAYKWH", "KWHKWP", }; //每日 await powerStationRepository.AddInverterHistoryDayList(allofInverterHistorDays, inverter_history_properties); //每月 if (insertInverterHistoryMonths.Count > 0) { await powerStationRepository.AddInverterHistoryMonthList(insertInverterHistoryMonths, inverter_history_properties); } if (updateInverterHistoryMonths.Count > 0) { await powerStationRepository.UpdateInverterHistoryMonthList(updateInverterHistoryMonths); } #endregion } catch (Exception exception) { logger.LogError("【{0}】{1}", "CalcAvgPowerStationJob", exception.Message); } } } }