257 lines
11 KiB
C#
257 lines
11 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Quartz;
|
|
using SolarPower.Models.PowerStation;
|
|
using SolarPower.Repository.Interface;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SolarPower.Quartz.Jobs
|
|
{
|
|
[DisallowConcurrentExecution]
|
|
public class CalcAvgPowerStationJob : IJob
|
|
{
|
|
private readonly ILogger<CalcAvgPowerStationJob> logger;
|
|
private readonly IPowerStationRepository powerStationRepository;
|
|
|
|
public CalcAvgPowerStationJob(ILogger<CalcAvgPowerStationJob> 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<PowerStation> calcAvgPowerStations = new List<PowerStation>();
|
|
List<PowerStationHistoryDay> powerStationHistoryDays = new List<PowerStationHistoryDay>();
|
|
List<PowerStationHistoryMonth> insertPowerStationHistoryMonths = new List<PowerStationHistoryMonth>();
|
|
List<PowerStationHistoryMonth> updatePowerStationHistoryMonths = new List<PowerStationHistoryMonth>();
|
|
|
|
List<PyrheliometerHistory> pyrheliometerHistoryDays = new List<PyrheliometerHistory>();
|
|
List<PyrheliometerHistory> insertPyrheliometerHistoryMonths = new List<PyrheliometerHistory>();
|
|
List<PyrheliometerHistory> updatePyrheliometerHistoryMonths = new List<PyrheliometerHistory>();
|
|
|
|
List<InverterHistory> inverterHistorDays = new List<InverterHistory>();
|
|
List<InverterHistory> insertInverterHistoryMonths = new List<InverterHistory>();
|
|
List<InverterHistory> updateInverterHistoryMonths = new List<InverterHistory>();
|
|
|
|
var DateTimeNow = DateTime.Now;
|
|
|
|
var count = 0;
|
|
|
|
#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 inverterHistorDay = await powerStationRepository.CalcPyrheliometerHistoryDayDataByPowerStationId(dateNowDay, powerStation.Id);
|
|
if (inverterHistorDay != null)
|
|
{
|
|
inverterHistorDays.Add(inverterHistorDay);
|
|
}
|
|
#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);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
count++;
|
|
}
|
|
#endregion
|
|
|
|
#region step3. calcPowerStations UPDATE 到 power_station 資料表
|
|
List<string> power_station_properties = new List<string>()
|
|
{
|
|
"Id",
|
|
"avg_kwhkwp",
|
|
"avg_PR",
|
|
"avg_irradiance"
|
|
};
|
|
|
|
await powerStationRepository.UpdateList(calcAvgPowerStations, power_station_properties);
|
|
#endregion
|
|
|
|
#region step4. 將各電站的每日及月的資料insert or update 各資料表
|
|
//每日
|
|
List<string> history_properties_day = new List<string>()
|
|
{
|
|
"PowerStationId",
|
|
"TIMESTAMP",
|
|
"SITEID",
|
|
"SITETYPE",
|
|
"TODAYKWH",
|
|
"TOTALKWH",
|
|
"KWHKWP",
|
|
"PR",
|
|
"MP",
|
|
"SolarHour"
|
|
};
|
|
|
|
await powerStationRepository.AddPowerStationHistoryDayList(powerStationHistoryDays, history_properties_day);
|
|
|
|
//每月
|
|
List<string> history_properties_month = new List<string>()
|
|
{
|
|
"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<string> pyrheliometer_history_properties = new List<string>()
|
|
{
|
|
"PowerStationId",
|
|
"Timestamp",
|
|
"Irradiance"
|
|
};
|
|
//每日
|
|
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
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
logger.LogError("【{0}】{1}", "CalcAvgPowerStationJob", exception.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|