FIC_Solar/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs
2021-07-06 20:12:00 +08:00

193 lines
8.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<PowerStation> calcPowerStations = new List<PowerStation>();
var DateTimeNow = DateTime.Now;
#region step2. DB及電站編號找出該電站的控制器
var count = 0;
foreach (var powerStation in powerStations)
{
if(count > 0)
{
break;
}
#region step2-1.
var table_name = String.Format("`{0}`.{1}01_ficstationhistory", powerStation.SiteDB, powerStation.Code);
var dateTime = DateTimeNow.AddHours(-1).ToString("yyyy-MM-dd HH");
dateTime = "2021-06-24 18";
var history = await powerStationRepository.GetPowerStationHistoryPerHour(dateTime, table_name);
if (history != null)
{
history.PowerStationId = powerStation.Id;
#region
var calcPowerStation = new PowerStation();
calcPowerStation.Id = powerStation.Id;
calcPowerStation.kwh = history.KWH;
#region
//今日發電量(直接填寫
calcPowerStation.Today_kWh = history.TodayKWh;
//總發電量(直接填寫
calcPowerStation.Total_kWh = history.TotalKWH;
#endregion
//TODO
#region
//1. 找出該電站所有日照計設備(包還共享
//2. 透過該DB、col取得到是哪一個sensor
#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);
calcPowerStations.Add(calcPowerStation);
}
#endregion
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
//TODO 日照
#region step4. 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",
};
await powerStationRepository.UpdateList(calcPowerStations, power_station_properties);
#endregion
}
catch (Exception exception)
{
logger.LogError("【{0}】{1}", "CalcPowerStationJob", exception.Message);
}
}
}
}