275 lines
12 KiB
C#
275 lines
12 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 CalcPowerStationJob : IJob
|
||
{
|
||
private readonly ILogger<CalcPowerStationJob> logger;
|
||
private readonly IPowerStationRepository powerStationRepository;
|
||
|
||
private double carbonRate;
|
||
|
||
public CalcPowerStationJob(ILogger<CalcPowerStationJob> 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<PowerStationHistory> powerStationHistoriesHour = new List<PowerStationHistory>();
|
||
List<PyrheliometerHistory> pyrheliometerHistoriesHour = new List<PyrheliometerHistory>();
|
||
List<PyrheliometerHistory> TempHistoriesHour = new List<PyrheliometerHistory>();
|
||
List<InverterHistory> inverterHistories = new List<InverterHistory>();
|
||
List<PowerStation> calcPowerStations = new List<PowerStation>();
|
||
|
||
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 dateTime = DateTimeNow.AddHours(-1).ToString("yyyy-MM-dd HH");
|
||
|
||
dateTime = "2021-07-08 12";
|
||
|
||
#region step2-1. 取得該電站的當前這小時的歷史資料
|
||
var table_name = String.Format("`{0}`.{1}01_station", powerStation.SiteDB, powerStation.Code);
|
||
|
||
var history = await powerStationRepository.GetPowerStationHistoryPerHour(dateTime, table_name);
|
||
if (history != null)
|
||
{
|
||
history.PowerStationId = powerStation.Id;
|
||
history.Timestamp = Convert.ToDateTime(history.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
||
#region 計算單一電站每小時發電量、發電金額等資料
|
||
|
||
#region 發電量
|
||
//每小時發電量(直接填寫
|
||
calcPowerStation.kwh = history.KWH;
|
||
//今日發電量(直接填寫
|
||
calcPowerStation.Today_kWh = history.TodayKWh;
|
||
//總發電量(直接填寫
|
||
calcPowerStation.Total_kWh = history.TotalKWH;
|
||
#endregion
|
||
|
||
#region 發電金額
|
||
//發電金額
|
||
switch (powerStation.SolarType)
|
||
{
|
||
case (int)SolarTypeEnum.SelfSold: //自建躉售
|
||
//今日發電金額 計算方式:todaykWh * 授電費率
|
||
calcPowerStation.Today_Monery = history.TodayKWh * powerStation.PowerRate;
|
||
|
||
//總發電金額 計算方式:totalkWh * 授電費率
|
||
calcPowerStation.Total_Monery = history.TotalKWH * powerStation.PowerRate;
|
||
break;
|
||
case (int)SolarTypeEnum.HireSold: //租建躉售
|
||
//找出該電站的所有土地房屋資訊
|
||
var landBuildings = await powerStationRepository.GetAllLandBuildingInfoByPowerStationId(powerStation.Id, powerStation.SiteDB);
|
||
var sumLeaseRate = 0.00;
|
||
var avgLeaseRate = 0.00;
|
||
|
||
foreach (var landBuilding in landBuildings)
|
||
{
|
||
sumLeaseRate += landBuilding.LeaseRate;
|
||
}
|
||
avgLeaseRate = sumLeaseRate / landBuildings.Count();
|
||
|
||
//今日發電金額計算方式:todaykWh * 出借費率(各個土地房屋租借比率平均)
|
||
calcPowerStation.Today_Monery = history.TodayKWh * avgLeaseRate;
|
||
|
||
//總發電金額 計算方式:totalkWh * 授電費率
|
||
calcPowerStation.Total_Monery = history.TotalKWH * avgLeaseRate;
|
||
break;
|
||
case (int)SolarTypeEnum.SelfUse: //自建自用
|
||
//今日發電金額 計算方式:todaykWh * 授電費率
|
||
calcPowerStation.Today_Monery = history.TodayKWh * powerStation.PowerRate;
|
||
|
||
//總發電金額 計算方式:totalkWh * 授電費率
|
||
calcPowerStation.Total_Monery = history.TotalKWH * powerStation.PowerRate;
|
||
break;
|
||
}
|
||
#endregion
|
||
|
||
#region kWh/kWp
|
||
//直接填寫
|
||
calcPowerStation.Today_kwhkwp = history.KWHKWP;
|
||
#endregion
|
||
|
||
#region PR
|
||
//直接填寫
|
||
calcPowerStation.Today_PR = history.PR;
|
||
#endregion
|
||
|
||
#region 減碳量
|
||
carbonRate = Convert.ToDouble(await powerStationRepository.GetOneVariableByName("CarbonRate"));
|
||
|
||
//今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值]
|
||
calcPowerStation.Today_Carbon = history.TodayKWh * carbonRate;
|
||
//總減碳量(總發電量 * (0.554/1000)[抓資料庫值]
|
||
calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate;
|
||
#endregion
|
||
|
||
#endregion
|
||
|
||
powerStationHistoriesHour.Add(history);
|
||
}
|
||
#endregion
|
||
|
||
#region step2-2. 取得該電站的當前這小時的日照度歷史資料
|
||
//1. 找出該電站所有日照計設備(包含共享
|
||
var deviceInfos = await powerStationRepository.GetListPyrheliometerByPowerStationId(powerStation.Id, powerStation.SiteDB);
|
||
// 找出該電站所有溫度計設備(包含共享
|
||
var tempdeviceInfos = await powerStationRepository.GetListTempByPowerStationId(powerStation.Id, powerStation.SiteDB);
|
||
if (deviceInfos != null)
|
||
{
|
||
//2. 計算該電站所有日照計設的每小時的平均在依照日照計數量平均
|
||
var pyrheliometerHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(dateTime, deviceInfos,0);
|
||
|
||
if (pyrheliometerHistory != null)
|
||
{
|
||
calcPowerStation.Today_irradiance = pyrheliometerHistory.Irradiance;
|
||
|
||
pyrheliometerHistory.Timestamp = Convert.ToDateTime(pyrheliometerHistory.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
||
pyrheliometerHistory.PowerStationId = powerStation.Id;
|
||
pyrheliometerHistoriesHour.Add(pyrheliometerHistory);
|
||
}
|
||
}
|
||
//2. 計算該電站所有溫度計設的每小時的平均在依照溫度計數量平均
|
||
if (tempdeviceInfos != null)
|
||
{
|
||
var tempHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(dateTime, tempdeviceInfos, 1);
|
||
if (tempHistory != null)
|
||
{
|
||
tempHistory.Timestamp = Convert.ToDateTime(tempHistory.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
||
tempHistory.PowerStationId = powerStation.Id;
|
||
TempHistoriesHour.Add(tempHistory);
|
||
}
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region step2-3. 計算該電站所有逆變器每小時值
|
||
var inverter_table_name = String.Format("`{0}`.{1}01_inv", powerStation.SiteDB, powerStation.Code);
|
||
|
||
inverterHistories = await powerStationRepository.CalcInverterHisyortHourData(dateTime, powerStation.SiteDB, inverter_table_name);
|
||
foreach(var inverterHistory in inverterHistories)
|
||
{
|
||
inverterHistory.TIMESTAMP = Convert.ToDateTime(inverterHistory.TIMESTAMP + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
||
inverterHistory.PowerStationId = powerStation.Id;
|
||
}
|
||
|
||
#endregion
|
||
|
||
calcPowerStations.Add(calcPowerStation);
|
||
|
||
count++;
|
||
}
|
||
#endregion
|
||
|
||
#region step3. 將historiers INSERT 到 power_station_history_hour 資料表
|
||
List<string> history_properties = new List<string>()
|
||
{
|
||
"PowerStationId",
|
||
"TIMESTAMP",
|
||
"SITEID",
|
||
"SITETYPE",
|
||
"KWH",
|
||
"TODAYKWH",
|
||
"TOTALKWH",
|
||
"KWHKWP",
|
||
"PR",
|
||
"MP",
|
||
"SolarHour"
|
||
};
|
||
|
||
await powerStationRepository.AddPowerStationHistory(powerStationHistoriesHour, history_properties);
|
||
#endregion
|
||
|
||
#region step4. 將Pyrheliometer History INSERT 到 sensor_history_hour 資料表
|
||
List<string> pyrheliometer_history_properties = new List<string>()
|
||
{
|
||
"PowerStationId",
|
||
"TIMESTAMP",
|
||
"Irradiance"
|
||
};
|
||
|
||
await powerStationRepository.AddPyrheliometerHistory(pyrheliometerHistoriesHour, pyrheliometer_history_properties);
|
||
|
||
List<string> Temp_history_properties = new List<string>()
|
||
{
|
||
"PowerStationId",
|
||
"TIMESTAMP",
|
||
"Temperature"
|
||
};
|
||
|
||
await powerStationRepository.AddTempHistory(TempHistoriesHour, Temp_history_properties);
|
||
#endregion
|
||
|
||
#region step5. calcPowerStations UPDATE 到 power_station 資料表
|
||
List<string> power_station_properties = new List<string>()
|
||
{
|
||
"Id",
|
||
"kwh",
|
||
"Today_kwh",
|
||
"Total_kwh",
|
||
"today_kwhkwp",
|
||
"today_monery",
|
||
"total_monery",
|
||
"today_PR",
|
||
"today_carbon",
|
||
"total_carbon",
|
||
"today_irradiance"
|
||
};
|
||
|
||
await powerStationRepository.UpdateList(calcPowerStations, power_station_properties);
|
||
#endregion
|
||
|
||
#region step6. 將 inverter INSERT 到 inverter_history_hour 資料表
|
||
List<string> inverter_history_properties = new List<string>()
|
||
{
|
||
"PowerStationId",
|
||
"INVERTERID",
|
||
"TIMESTAMP",
|
||
"KWH",
|
||
"TODAYKWH",
|
||
"KWHKWP",
|
||
};
|
||
|
||
await powerStationRepository.AddInverterHisyort(inverterHistories, inverter_history_properties);
|
||
#endregion
|
||
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
logger.LogError("【{0}】{1}", "CalcPowerStationJob", exception.Message);
|
||
}
|
||
}
|
||
}
|
||
}
|