101 lines
4.6 KiB
C#
101 lines
4.6 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 CalcInverter15minJob : IJob
|
|
{
|
|
private readonly ILogger<CalcInverter15minJob> logger;
|
|
private readonly IPowerStationRepository powerStationRepository;
|
|
|
|
public CalcInverter15minJob(ILogger<CalcInverter15minJob> logger, IPowerStationRepository powerStationRepository)
|
|
{
|
|
this.logger = logger;
|
|
this.powerStationRepository = powerStationRepository;
|
|
}
|
|
|
|
public async Task Execute(IJobExecutionContext context)
|
|
{
|
|
try
|
|
{
|
|
#region step1. 找出所有電站
|
|
logger.LogInformation("【CalcInverter15minJob】【開始取得電站資料】");
|
|
var powerStations = await powerStationRepository.GetAllAsync();
|
|
logger.LogInformation("【CalcPowerStationJob】【取得成功電站資料】");
|
|
logger.LogInformation("【CalcPowerStationJob】【電站資料】 - {0}", System.Text.Json.JsonSerializer.Serialize(powerStations));
|
|
#endregion
|
|
|
|
List<InverterHistory> calcInverter15mins = new List<InverterHistory>();
|
|
|
|
var DateTimeNow = DateTime.Now;
|
|
|
|
var count = 0;
|
|
|
|
#region step2. 從電站的DB及電站編號找出該電站的控制器
|
|
foreach (var powerStation in powerStations)
|
|
{
|
|
if (count > 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
var dateNowTime = DateTimeNow.ToString("yyyy-MM-dd HH:mm");
|
|
|
|
//取得所有該電站的逆變器
|
|
logger.LogInformation("【CalcInverter15minJob】【開始取得電站[{0}]在{1}的逆變器設備資訊】", powerStation.Code, dateNowTime);
|
|
var controllers = await powerStationRepository.GetAllDeviceControllerId(powerStation.Id, powerStation.SiteDB);
|
|
var inverters = await powerStationRepository.InverterTable(controllers, powerStation.SiteDB);
|
|
var inverterIds = inverters.Where(x => x.Enabled == 1 && x.Status != 0).Select(x => x.InverterId).ToList();
|
|
logger.LogInformation("【CalcInverter15minJob】【取得成功電站[{0}]在{1}的逆變器設備資訊】", powerStation.Code, dateNowTime);
|
|
|
|
var calcInverter15min = new InverterHistory();
|
|
|
|
#region step2-1. 計算該電站逆變器每15min 的值
|
|
var table_name = String.Format("`{0}`.`s{1}01_inv`", powerStation.SiteDB, powerStation.Code);
|
|
logger.LogInformation("【CalcInverter15minJob】【開始計算電站[{0}]在{1}的每15分鐘逆變器資訊】", powerStation.Code, dateNowTime);
|
|
calcInverter15mins = await powerStationRepository.CalcInverterHisyort15minData(dateNowTime, powerStation.SiteDB, table_name, inverterIds);
|
|
logger.LogInformation("【CalcInverter15minJob】【計算完成電站[{0}]在{1}的每15分鐘逆變器資訊】", powerStation.Code, dateNowTime);
|
|
if (calcInverter15mins.Count() > 0)
|
|
{
|
|
foreach (var inverterHistory in calcInverter15mins)
|
|
{
|
|
inverterHistory.TIMESTAMP = Convert.ToDateTime(inverterHistory.TIMESTAMP + ":00").ToString("yyyy-MM-dd HH:mm:ss");
|
|
inverterHistory.PowerStationId = powerStation.Id;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
count++;
|
|
}
|
|
#endregion
|
|
|
|
#region step3. 將 inverter INSERT 到 inverter_history_hour 資料表
|
|
List<string> inverter_history_properties = new List<string>()
|
|
{
|
|
"PowerStationId",
|
|
"INVERTERID",
|
|
"TIMESTAMP",
|
|
"KWH",
|
|
"TODAYKWH",
|
|
"KWHKWP",
|
|
};
|
|
|
|
await powerStationRepository.AddInverter15minHistory(calcInverter15mins, inverter_history_properties);
|
|
#endregion
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
logger.LogError("【{0}】{1}", "CalcInverter15minJob", exception.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|