FIC_Solar/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs
2021-11-04 11:17:05 +08:00

1077 lines
60 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Quartz;
using solarApp.Service;
using SolarPower.Models;
using SolarPower.Models.PowerStation;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace SolarPower.Quartz.Jobs
{
[DisallowConcurrentExecution]
public class CalcAvgPowerStationJob : IJob
{
private readonly ILogger<CalcAvgPowerStationJob> logger;
private readonly IPowerStationRepository powerStationRepository;
private readonly IUserRepository userRepository;
private readonly INoticeScheduleRepository noticeScheduleRepository;
private readonly IStationReportRepository stationReportRepository;
private readonly IOperationRepository operationRepository;
private readonly IConfiguration Configuration;
public CalcAvgPowerStationJob(
ILogger<CalcAvgPowerStationJob> logger,
IPowerStationRepository powerStationRepository,
IUserRepository userRepository,
INoticeScheduleRepository noticeScheduleRepository,
IStationReportRepository stationReportRepository,
IOperationRepository operationRepository,
IConfiguration Configuration)
{
this.logger = logger;
this.powerStationRepository = powerStationRepository;
this.userRepository = userRepository;
this.noticeScheduleRepository = noticeScheduleRepository;
this.stationReportRepository = stationReportRepository;
this.operationRepository = operationRepository;
this.Configuration = Configuration;
}
public async Task Execute(IJobExecutionContext context)
{
try
{
logger.LogInformation("【CalcAvgPowerStationJob】【任務開始】");
#region step1.
logger.LogInformation("【CalcAvgPowerStationJob】【開始取得電站資料】");
var powerStations = await powerStationRepository.GetAllAsync();
logger.LogInformation("【CalcAvgPowerStationJob】【取得成功電站資料】");
logger.LogInformation("【CalcAvgPowerStationJob】【電站資料】 - {0}", System.Text.Json.JsonSerializer.Serialize(powerStations));
#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<string> pyrheliometer_history_properties = new List<string>()
{
"PowerStationId",
"Timestamp",
"Irradiance",
"Temperature"
};
//逆變器
List<InverterHistory> allofInverterHistorDays = new List<InverterHistory>();
List<InverterHistory> insertInverterHistoryMonths = new List<InverterHistory>();
List<InverterHistory> updateInverterHistoryMonths = new List<InverterHistory>();
List<string> inverter_history_properties = new List<string>()
{
"PowerStationId",
"TIMESTAMP",
"INVERTERID",
"Irradiance",
"AC1V",
"AC1A",
"AC1W",
"AC1F",
"AC1WH",
"AC2V",
"AC2A",
"AC2W",
"AC2F",
"AC2WH",
"AC3V",
"AC3A",
"AC3W",
"AC3F",
"AC3WH",
"DC1V",
"DC1A",
"DC1W",
"DC1KW",
"DC1WH",
"DC2V",
"DC2A",
"DC2W",
"DC2KW",
"DC2WH",
"DC3V",
"DC3A",
"DC3W",
"DC3KW",
"DC3WH",
"DC4V",
"DC4A",
"DC4W",
"DC4KW",
"DC4WH",
"DC5V",
"DC5A",
"DC5W",
"DC5KW",
"DC5WH",
"PR",
"RA1",
"RA2",
"RA3",
"RA4",
"RA5",
"DCKW",
"ACKW",
"KWH",
"TODAYKWH",
"KWHKWP",
};
//sensor
List<SensorAvgHistory> sensorAvgHistoryDays = new List<SensorAvgHistory>();
List<SensorAvgHistory> insertSensorAvgHistoryMonths = new List<SensorAvgHistory>();
List<SensorAvgHistory> updateSensorAvgHistoryMonths = new List<SensorAvgHistory>();
List<string> sensoravg_history_properties = new List<string>()
{
"PowerStationId",
"TIMESTAMP",
"SENSORAVG01",
"SENSORAVG02",
"SENSORAVG03",
"SENSORAVG04",
"SENSORAVG05",
"SENSORAVG06",
"SENSORAVG07",
"SENSORAVG08",
"SENSORAVG09",
"SENSORAVG10",
"SENSORAVG11",
"SENSORAVG12",
"SENSORAVG13",
"SENSORAVG14",
"SENSORAVG15",
"SENSORAVG16",
"SENSORAVG17",
"SENSORAVG18",
"SENSORAVG19",
"SENSORAVG20",
"SENSORAVG21",
"SENSORAVG22",
"SENSORAVG23",
"SENSORAVG24",
"SENSORAVG25",
"SENSORAVG26",
"SENSORAVG27",
"SENSORAVG28",
"SENSORAVG29",
"SENSORAVG30",
"SENSORAVG31",
"SENSORAVG32",
"SENSORAVG33",
"SENSORAVG34",
"SENSORAVG35",
"SENSORAVG36",
"SENSORAVG37",
"SENSORAVG38",
"SENSORAVG39",
"SENSORAVG40",
"SENSORAVG41",
"SENSORAVG42",
"SENSORAVG43",
"SENSORAVG44",
"SENSORAVG45",
"SENSORAVG46",
"SENSORAVG47",
"SENSORAVG48",
"SENSORAVG49",
"SENSORAVG50",
};
//電錶
List<MeterHistory> meterHistoriesDays = new List<MeterHistory>();
List<MeterHistory> insertmeterHistoryMonths = new List<MeterHistory>();
List<MeterHistory> updatemeterHistoryMonths = new List<MeterHistory>();
List<string> meter_history_properties = new List<string>()
{
"PowerStationId",
"TIMESTAMP",
"METERID",
"V_AB",
"V_BC",
"V_CA",
"I_A",
"I_B",
"I_C",
"I_C",
"P",
"F",
"INPUT_KWH",
"OUTPUT_KWH"
};
List<WeatherForecast> weatherForecasts = new List<WeatherForecast>();
var DateTimeNow = DateTime.Now;
var dateNowDay = DateTimeNow.AddDays(-1).ToString("yyyy-MM-dd");
var dateNowMonth = DateTimeNow.AddDays(-1).ToString("yyyy-MM");
Root2 observation = null;
#region ()
try
{
logger.LogInformation("【CalcAvgPowerStationJob】【開始取得氣象觀測】");
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();
observation = JsonConvert.DeserializeObject<Root2>(jsonUVs);
logger.LogInformation("【CalcAvgPowerStationJob】【取得成功氣象觀測】");
}
catch (Exception ex)
{
logger.LogError("【CalcAvgPowerStationJob】【取得失敗氣象觀測】");
logger.LogError("【{0}】{1}", "CalcPowerStationJob", ex.Message);
observation = null;
}
#endregion
if (observation != null)
{
foreach (var location in observation.Records.Location)
{
WeatherForecast weatherForecast = new WeatherForecast();
weatherForecast.LocationName = location.LocationName;
List<WeatherForecast> SubweatherForecasts = new List<WeatherForecast>();
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<string> weather_forecast_properties = new List<string>()
{
"LocationName",
"StartTime",
"EndTime",
"Wx",
"WxValue",
"PoP"
};
await powerStationRepository.AddWeatherForecast(weatherForecasts, weather_forecast_properties);
#region 60operation_record
var OperationDeletes = await powerStationRepository.GetAllDataList<OperationRecord>("operation_record", "Deleted = 1");
List<int> deleteoperations = new List<int>();
foreach(var deletes in OperationDeletes)
{
if(DateTime.Now.AddDays(-60) > Convert.ToDateTime(deletes.UpdatedAt))
{
deleteoperations.Add(deletes.Id);
}
}
await operationRepository.DeleteRecord(deleteoperations);
#endregion
#region step2.
foreach (var powerStation in powerStations)
{
var calcPowerStation = new PowerStation();
calcPowerStation.Id = powerStation.Id;
#region step2-1. 30
var table_name = String.Format("s{1}01_station", powerStation.SiteDB, powerStation.Code);
var full_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, table_name);
var exist = await powerStationRepository.ExistTable(powerStation.SiteDB, table_name);
if (!string.IsNullOrEmpty(exist))
{
logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}的30天平均資料】", powerStation.Code, dateNowDay);
var history = await powerStationRepository.CalcAvgPowerStationHistory30day(dateNowDay, full_table_name);
logger.LogInformation("【CalcAvgPowerStationJob】【取得成功電站[{0}]在{1}的30天平均資料】", powerStation.Code, dateNowDay);
logger.LogInformation("【CalcAvgPowerStationJob】【電站[{0}]在{1}的30天平均資料】 - {2}", powerStation.Code, dateNowDay, System.Text.Json.JsonSerializer.Serialize(history));
if (history != null)
{
logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}的30天平均資料】", powerStation.Code, dateNowDay);
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
logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}的30天平均資料】", powerStation.Code, dateNowDay);
}
#region 30
logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}的30天日照計平均資料】", powerStation.Code, dateNowDay);
var avgPyrheliometerHistory = await powerStationRepository.CalcAvgPyrheliometerHistory30day(dateNowDay, powerStation.Id);
logger.LogInformation("【CalcAvgPowerStationJob】【取得成功電站[{0}]在{1}的30天日照計平均資料】", powerStation.Code, dateNowDay);
logger.LogInformation("【CalcAvgPowerStationJob】【電站[{0}]在{1}的30天日照計平均資料】 - {2}", powerStation.Code, dateNowDay, System.Text.Json.JsonSerializer.Serialize(avgPyrheliometerHistory));
if (avgPyrheliometerHistory != null)
{
calcPowerStation.Avg_irradiance = avgPyrheliometerHistory.AvgIrradiance;
}
calcAvgPowerStations.Add(calcPowerStation);
#endregion
}
#endregion
#region step2-2.
/*因修改為補償 而註解*/
//if (!string.IsNullOrEmpty(exist))
//{
// //電站資訊
// logger.LogInformation("【CalcAvgPowerStationJob】【開始取得電站[{0}]在{1}的所有值的總和】", powerStation.Code, dateNowDay);
// var historyDay = await powerStationRepository.GetLastOnePowerStationHistoryByDay(dateNowDay, full_table_name);
// logger.LogInformation("【CalcAvgPowerStationJob】【取得成功電站[{0}]在{1}的所有值的總和】", powerStation.Code, dateNowDay);
// logger.LogInformation("【CalcAvgPowerStationJob】【電站[{0}]在{1}的所有值的總和】 - {2}", powerStation.Code, dateNowDay, System.Text.Json.JsonSerializer.Serialize(historyDay));
// var moneyandcarbon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, dateNowDay, 1);
// var lastmoneyhistory = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 1, "");
// if (historyDay != null)
// {
// historyDay.PowerStationId = powerStation.Id;
// historyDay.CARBON = moneyandcarbon.CARBON;
// historyDay.MONEY = moneyandcarbon.MONEY;
// //historyDay.TODAYCARBON = lastmoneyhistory.TODAYCARBON;
// //historyDay.TODAYMONEY = lastmoneyhistory.TODAYMONEY ;
// if (lastmoneyhistory != null)
// {
// historyDay.TOTALCARBON = lastmoneyhistory.TOTALCARBON + moneyandcarbon.CARBON;
// historyDay.TOTALMONEY = lastmoneyhistory.TOTALMONEY + moneyandcarbon.MONEY;
// }
// else
// {
// historyDay.TOTALCARBON = moneyandcarbon.CARBON;
// historyDay.TOTALMONEY = moneyandcarbon.MONEY;
// }
// //日期轉換
// historyDay.Timestamp = Convert.ToDateTime(historyDay.Timestamp + ":00:00").ToString("yyyy-MM-dd");
// powerStationHistoryDays.Add(historyDay);
// }
//}
////日照計、溫度
//logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}的日照計所有值的平均】", powerStation.Code, dateNowDay);
//var pyrheliometerHistorDay = await powerStationRepository.CalcPyrheliometerHistoryDayDataByPowerStationId(dateNowDay, powerStation.Id);
//logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}的日照計所有值的平均】", powerStation.Code, dateNowDay);
//if (pyrheliometerHistorDay != null)
//{
// pyrheliometerHistoryDays.Add(pyrheliometerHistorDay);
//}
////逆變器
//logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}的逆變器所有值的平均】", powerStation.Code, dateNowDay);
//var inverterHistoriesDay = await powerStationRepository.CalcInverterHistoryDayDataByPowerStationId(dateNowDay, powerStation.SiteDB, powerStation.Id);
//logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}的逆變器所有值的平均】", powerStation.Code, dateNowDay);
//if (inverterHistoriesDay != null && inverterHistoriesDay.Count() > 0)
//{
// foreach (var inverterHistoryDay in inverterHistoriesDay)
// {
// inverterHistoryDay.DC1KW = inverterHistoryDay.DC1W / 1000;
// inverterHistoryDay.DC2KW = inverterHistoryDay.DC2W / 1000;
// inverterHistoryDay.DC3KW = inverterHistoryDay.DC3W / 1000;
// inverterHistoryDay.DC4KW = inverterHistoryDay.DC4W / 1000;
// inverterHistoryDay.DC5KW = inverterHistoryDay.DC5W / 1000;
// inverterHistoryDay.DCKW = (inverterHistoryDay.DC1W + inverterHistoryDay.DC2W + inverterHistoryDay.DC3W + inverterHistoryDay.DC4W + inverterHistoryDay.DC5W) / 1000;
// inverterHistoryDay.ACKW = (inverterHistoryDay.AC1W + inverterHistoryDay.AC2W + inverterHistoryDay.AC3W) / 1000;
// allofInverterHistorDays.Add(inverterHistoryDay);
// }
//}
////sensor avg
//logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}的Sensor Avg table所有值的平均】", powerStation.Code, dateNowDay);
//var sensorAvgHistoryDay = await powerStationRepository.CalcSensorAvgDayDataByPowerStationId(dateNowDay, powerStation.Id);
//logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}的Sensor Avg table所有值的平均】", powerStation.Code, dateNowDay);
//if (sensorAvgHistoryDay != null)
//{
// sensorAvgHistoryDays.Add(sensorAvgHistoryDay);
//}
//meter
logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}的meter_hour所有值的平均】", powerStation.Code, dateNowDay);
var meterHistoriesDay = await powerStationRepository.CalcMeterDayDataByPowerStationId(dateNowDay, powerStation.Id);
logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}的meter_hour所有值的平均】", powerStation.Code, dateNowDay);
if (meterHistoriesDay != null && meterHistoriesDay.Count() > 0)
{
meterHistoriesDays.AddRange(meterHistoriesDay);
}
#endregion
}
#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資料表
//每日 - 電站歷史資料
List<string> history_properties_day = new List<string>()
{
"PowerStationId",
"TIMESTAMP",
"SITEID",
"SITETYPE",
"TODAYKWH",
"TOTALKWH",
"KWHKWP",
"PR",
"MP",
"SolarHour",
"MONEY",
"CARBON",
"TOTALMONEY",
"TOTALCARBON"
};
/*因修改為補償 而註解*/
//await powerStationRepository.AddPowerStationHistoryDayList(powerStationHistoryDays, history_properties_day);
////每日 - 日照溫度歷史資料
//await powerStationRepository.AddPyrheliometerHistoryDayList(pyrheliometerHistoryDays, pyrheliometer_history_properties);
////每日 - 逆變器歷史資料
//await powerStationRepository.AddInverterHistoryDayList(allofInverterHistorDays, inverter_history_properties);
////每日 - sensor avg
//await powerStationRepository.AddSensorAvgHistoryDayList(sensorAvgHistoryDays, sensoravg_history_properties);
////每日 - meter
//await powerStationRepository.AddMeterHistoryDayList(meterHistoriesDays, meter_history_properties);
#endregion
#region step5.
foreach (var powerStation in powerStations)
{
////電站該月份的歷史資料
//logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}月份的歷史資料總和】", powerStation.Code, dateNowMonth);
//var exist_history = await powerStationRepository.GetOnePowerStationHistoryByPowerStationIdAndMonth(powerStation.Id, dateNowMonth);
//if (exist_history == null)
//{ //新增
// var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth);
// var moneyandcarbonMon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, dateNowMonth, 0);
// var lastmoneyhistorymonth = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 2, "");
// if (historyMonth != null)
// {
// historyMonth.Timestamp = Convert.ToDateTime(historyMonth.Timestamp).ToString("yyyy-MM-dd");
// historyMonth.MONEY = moneyandcarbonMon.MONEY;
// historyMonth.CARBON = moneyandcarbonMon.CARBON;
// if (lastmoneyhistorymonth != null)
// {
// historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON;
// historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY;
// }
// else
// {
// historyMonth.TOTALCARBON = moneyandcarbonMon.CARBON;
// historyMonth.TOTALMONEY = moneyandcarbonMon.MONEY;
// }
// insertPowerStationHistoryMonths.Add(historyMonth);
// }
// logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的歷史資料總和】", powerStation.Code, dateNowMonth);
//}
//else
//{ //修改
// var historyMonth = await powerStationRepository.ClacPowerStationHistoryMonthDataByPowerStationId(powerStation.Id, dateNowMonth);
// var moneyandcarbonMon = await powerStationRepository.GetMoneyAndCarbonWithHistoryHour(powerStation.Id, dateNowMonth, 0);
// var lastmoneyhistorymonth = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 2, "");
// if (historyMonth != null)
// {
// historyMonth.MONEY = historyMonth.MONEY + moneyandcarbonMon.MONEY;
// historyMonth.CARBON = historyMonth.CARBON + moneyandcarbonMon.CARBON;
// if (lastmoneyhistorymonth != null)
// {
// historyMonth.TOTALCARBON = lastmoneyhistorymonth.TOTALCARBON + moneyandcarbonMon.CARBON;
// historyMonth.TOTALMONEY = lastmoneyhistorymonth.TOTALMONEY + moneyandcarbonMon.MONEY;
// }
// else
// {
// historyMonth.TOTALCARBON = moneyandcarbonMon.CARBON;
// historyMonth.TOTALMONEY = moneyandcarbonMon.MONEY;
// }
// updatePowerStationHistoryMonths.Add(historyMonth);
// }
// logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的歷史資料總和】", powerStation.Code, dateNowMonth);
//}
////電站該月份的的日照度歷史資料
//logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}月份的日照度歷史資料】", powerStation.Code, dateNowMonth);
//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);
// }
// logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的日照度歷史資料】", powerStation.Code, dateNowMonth);
//}
//else
//{ //修改
// var pyrheliometerHistoryMonth = await powerStationRepository.CalcPyrheliometerHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id);
// if (pyrheliometerHistoryMonth != null)
// {
// updatePyrheliometerHistoryMonths.Add(pyrheliometerHistoryMonth);
// }
// logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的日照度歷史資料】", powerStation.Code, dateNowMonth);
//}
////電站該月份的的逆變器歷史資料
//logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}月份的逆變器歷史資料】", powerStation.Code, dateNowMonth);
//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.DC1KW = inverterHistoryMonth.DC1W / 1000;
// inverterHistoryMonth.DC2KW = inverterHistoryMonth.DC2W / 1000;
// inverterHistoryMonth.DC3KW = inverterHistoryMonth.DC3W / 1000;
// inverterHistoryMonth.DC4KW = inverterHistoryMonth.DC4W / 1000;
// inverterHistoryMonth.DC5KW = inverterHistoryMonth.DC5W / 1000;
// inverterHistoryMonth.DCKW = (inverterHistoryMonth.DC1W + inverterHistoryMonth.DC2W + inverterHistoryMonth.DC3W + inverterHistoryMonth.DC4W + inverterHistoryMonth.DC5W) / 1000;
// inverterHistoryMonth.ACKW = (inverterHistoryMonth.AC1W + inverterHistoryMonth.AC2W + inverterHistoryMonth.AC3W) / 1000;
// inverterHistoryMonth.TIMESTAMP = Convert.ToDateTime(inverterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd");
// insertInverterHistoryMonths.Add(inverterHistoryMonth);
// }
// }
// logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的逆變器歷史資料】", powerStation.Code, dateNowMonth);
//}
//else
//{ //修改
// var inverterHistoriesMonth = await powerStationRepository.CalcInverterHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.SiteDB, powerStation.Id);
// if (inverterHistoriesMonth.Count > 0)
// {
// foreach (var inverterHistoryMonth in inverterHistoriesMonth)
// {
// inverterHistoryMonth.DC1KW = inverterHistoryMonth.DC1W / 1000;
// inverterHistoryMonth.DC2KW = inverterHistoryMonth.DC2W / 1000;
// inverterHistoryMonth.DC3KW = inverterHistoryMonth.DC3W / 1000;
// inverterHistoryMonth.DC4KW = inverterHistoryMonth.DC4W / 1000;
// inverterHistoryMonth.DC5KW = inverterHistoryMonth.DC5W / 1000;
// inverterHistoryMonth.DCKW = (inverterHistoryMonth.DC1W + inverterHistoryMonth.DC2W + inverterHistoryMonth.DC3W + inverterHistoryMonth.DC4W + inverterHistoryMonth.DC5W) / 1000;
// inverterHistoryMonth.ACKW = (inverterHistoryMonth.AC1W + inverterHistoryMonth.AC2W + inverterHistoryMonth.AC3W) / 1000;
// inverterHistoryMonth.TIMESTAMP = Convert.ToDateTime(inverterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd");
// updateInverterHistoryMonths.Add(inverterHistoryMonth);
// }
// }
// logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的逆變器歷史資料】", powerStation.Code, dateNowMonth);
//}
////電站該月份的的sensoravg歷史資料
//logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}月份的Sensor Avg table所有值的平均資料】", powerStation.Code, dateNowMonth);
//var exist_sensoravg_history = await powerStationRepository.GetSensorAvgHistoryByPowerStationIdAndMonth(dateNowMonth, powerStation.Id);
//if (exist_sensoravg_history == null)
//{ //新增
// var sensorAvgHistoryMonth = await powerStationRepository.CalcSensorAvgHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id);
// if (sensorAvgHistoryMonth != null)
// {
// sensorAvgHistoryMonth.TIMESTAMP = Convert.ToDateTime(sensorAvgHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd");
// insertSensorAvgHistoryMonths.Add(sensorAvgHistoryMonth);
// }
// logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的Sensor Avg table所有值的平均資料】", powerStation.Code, dateNowMonth);
//}
//else
//{ //修改
// var sensorAvgHistoryMonth = await powerStationRepository.CalcSensorAvgHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id);
// if (sensorAvgHistoryMonth != null)
// {
// sensorAvgHistoryMonth.TIMESTAMP = Convert.ToDateTime(sensorAvgHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd");
// updateSensorAvgHistoryMonths.Add(sensorAvgHistoryMonth);
// }
// logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的Sensor Avg table所有值的平均資料】", powerStation.Code, dateNowMonth);
//}
//電站該月份的的meter歷史資料
logger.LogInformation("【CalcAvgPowerStationJob】【開始計算電站[{0}]在{1}月份的meter table所有值的平均資料】", powerStation.Code, dateNowMonth);
var exist_meter_history = await powerStationRepository.GetMeterHistoryByPowerStationIdAndMonth(dateNowMonth, powerStation.Id);
if (exist_meter_history.Count() == 0)
{ //新增
var meterHistoriesMonth = await powerStationRepository.CalcMeterHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id);
if (meterHistoriesMonth != null && meterHistoriesMonth.Count() > 0)
{
foreach (var meterHistoryMonth in meterHistoriesMonth)
{
meterHistoryMonth.TIMESTAMP = Convert.ToDateTime(meterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd");
insertmeterHistoryMonths.Add(meterHistoryMonth);
}
}
logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的meter table所有值的平均資料】", powerStation.Code, dateNowMonth);
}
else
{ //修改
var meterHistoriesMonth = await powerStationRepository.CalcMeterHistoryMonthDataByPowerStationId(dateNowMonth, powerStation.Id);
if (meterHistoriesMonth != null && meterHistoriesMonth.Count() > 0)
{
foreach (var meterHistoryMonth in meterHistoriesMonth)
{
meterHistoryMonth.TIMESTAMP = Convert.ToDateTime(meterHistoryMonth.TIMESTAMP).ToString("yyyy-MM-dd");
updatemeterHistoryMonths.Add(meterHistoryMonth);
}
}
logger.LogInformation("【CalcAvgPowerStationJob】【計算完成電站[{0}]在{1}月份的meter table所有值的平均資料】", powerStation.Code, dateNowMonth);
}
}
#endregion
//每月
List<string> history_properties_month = new List<string>()
{
"PowerStationId",
"TIMESTAMP",
"SITEID",
"SITETYPE",
"MonthKWh",
"TOTALKWH",
"KWHKWP",
"PR",
"MP",
"SolarHour",
"MONEY",
"CARBON",
"TOTALMONEY",
"TOTALCARBON"
};
//if (insertPowerStationHistoryMonths.Count > 0)
//{
// await powerStationRepository.AddPowerStationHistoryMonthList(insertPowerStationHistoryMonths, history_properties_month);
//}
//if (updatePowerStationHistoryMonths.Count > 0)
//{
// await powerStationRepository.UpdatePowerStationHistoryMonthList(updatePowerStationHistoryMonths);
//}
//#region step5. 將各電站的每月的日照度資料insert or update 各資料表
////每月
//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 各資料表
////每月
//if (insertInverterHistoryMonths.Count > 0)
//{
// await powerStationRepository.AddInverterHistoryMonthList(insertInverterHistoryMonths, inverter_history_properties);
//}
//if (updateInverterHistoryMonths.Count > 0)
//{
// await powerStationRepository.UpdateInverterHistoryMonthList(updateInverterHistoryMonths);
//}
//#endregion
//#region step7. 將各電站的每月的Sensor Avg資料insert or update 各資料表
////每月
//if (insertSensorAvgHistoryMonths.Count > 0)
//{
// await powerStationRepository.AddSensorAvgHistoryMonthList(insertSensorAvgHistoryMonths, sensoravg_history_properties);
//}
//if (updateSensorAvgHistoryMonths.Count > 0)
//{
// await powerStationRepository.UpdateSensorAvgHistoryMonthList(updateSensorAvgHistoryMonths);
//}
//#endregion
#region step8. meter資料insert or update
//每月
if (insertmeterHistoryMonths.Count > 0)
{
await powerStationRepository.AddMeterHistoryMonthList(insertmeterHistoryMonths, meter_history_properties);
}
if (updatemeterHistoryMonths.Count > 0)
{
await powerStationRepository.UpdateMeterHistoryMonthList(updatemeterHistoryMonths);
}
#endregion
#region
var gobackDay = this.Configuration.GetValue<int>("GoBackDay"); //回推天數
var Connection_string = Configuration.GetValue<string>("mySql");
var start_date = DateTimeNow.AddDays(-1 * gobackDay);
var end_date = DateTimeNow.AddDays(-1);
logger.LogInformation("【CalcAvgPowerStationJob】【開始執行補償機制】");
procSensorSvc sensorSvc = new procSensorSvc(Connection_string);
procInvSvc invSvc = new procInvSvc(Connection_string, logger);
procStationSvc siteSvc = new procStationSvc(Connection_string);
foreach (var powerStation in powerStations)
{
start_date = DateTimeNow.AddDays(-1 * gobackDay);
for (; start_date <= end_date; start_date = start_date.AddDays(1))
{
var day_str = start_date.ToString("yyyy-MM-dd");
try
{
logger.LogInformation("【CalcAvgPowerStationJob】【開始執行電站[{0}]在{1}的Sensor補償機制】", powerStation.Code, day_str);
sensorSvc.archiveData(powerStation.Code, day_str);
logger.LogInformation("【CalcAvgPowerStationJob】【執行完成電站[{0}]在{1}的Sensor補償機制】", powerStation.Code, day_str);
}
catch (Exception exception)
{
logger.LogInformation("【CalcAvgPowerStationJob】【執行失敗電站[{0}]在{1}的Sensor補償機制】", powerStation.Code, day_str);
logger.LogError("【CalcAvgPowerStationJob】[Exception] - {0}", exception.Message);
if (exception.InnerException != null)
{
logger.LogError("【CalcAvgPowerStationJob】[InnerException] - {0}", exception.InnerException.Message);
}
}
try
{
logger.LogInformation("【CalcAvgPowerStationJob】【開始執行電站[{0}]在{1}的Meter補償機制】", powerStation.Code, day_str);
sensorSvc.archiveMeterData(powerStation.Code, day_str);
logger.LogInformation("【CalcAvgPowerStationJob】【執行完成電站[{0}]在{1}的Meter補償機制】", powerStation.Code, day_str);
}
catch (Exception exception)
{
logger.LogInformation("【CalcAvgPowerStationJob】【執行失敗電站[{0}]在{1}的Meter補償機制】", powerStation.Code, day_str);
logger.LogError("【CalcAvgPowerStationJob】[Exception] - {0}", exception.Message);
if (exception.InnerException != null)
{
logger.LogError("【CalcAvgPowerStationJob】[InnerException] - {0}", exception.InnerException.Message);
}
}
try
{
logger.LogInformation("【CalcAvgPowerStationJob】【開始執行電站[{0}]在{1}的Inverter補償機制】", powerStation.Code, day_str);
invSvc.archiveData(powerStation.Code, day_str);
logger.LogInformation("【CalcAvgPowerStationJob】【執行完成電站[{0}]在{1}的Inverter補償機制】", powerStation.Code, day_str);
}
catch (Exception exception)
{
logger.LogInformation("【CalcAvgPowerStationJob】【執行失敗電站[{0}]在{1}的Inverter補償機制】", powerStation.Code, day_str);
logger.LogError("【CalcAvgPowerStationJob】[Exception] - {0}", exception.Message);
if (exception.InnerException != null)
{
logger.LogError("【CalcAvgPowerStationJob】[InnerException] - {0}", exception.InnerException.Message);
}
}
try
{
logger.LogInformation("【CalcAvgPowerStationJob】【開始執行電站[{0}]在{1}的Site補償機制】", powerStation.Code, day_str);
siteSvc.archiveData(powerStation.Code, day_str);
logger.LogInformation("【CalcAvgPowerStationJob】【執行完成電站[{0}]在{1}的Site補償機制】", powerStation.Code, day_str);
}
catch (Exception exception)
{
logger.LogInformation("【CalcAvgPowerStationJob】【執行失敗電站[{0}]在{1}的Site補償機制】", powerStation.Code, day_str);
logger.LogError("【CalcAvgPowerStationJob】[Exception] - {0}", exception.Message);
if (exception.InnerException != null)
{
logger.LogError("【CalcAvgPowerStationJob】[InnerException] - {0}", exception.InnerException.Message);
}
}
}
}
logger.LogInformation("【CalcAvgPowerStationJob】【執行完成補償機制】");
#endregion
#region
var users = userRepository.GetAllAsync();
var ttt = new List<string>() {
"s506488@gmail.com",
"cesarliuc@gmail.com"
};
foreach (var user in users.Result)
{
try
{
logger.LogInformation("【CalcAvgPowerStationJob】【開始產生使用者[{0}({1})]的日月報】", user.Account, user.Name);
List<OperationPersonnel> powerstations = new List<OperationPersonnel>();
powerstations = await noticeScheduleRepository.GetPowerStationOperationPersonnel(user.Id);
if (powerstations.Count == 0)
{
continue;
}
List<Excelpowerstation> sentdaypowerstations = powerstations.Where(x => x.EmailDayReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
List<Excelpowerstation> sentMaxpowerstations = powerstations.Where(x => x.EmailComplexReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
Controllers.StationReportController stationReportController = new Controllers.StationReportController(powerStationRepository, stationReportRepository);
//日報表
if (sentdaypowerstations.Count != 0)
{
Excel dayexcel = new Excel()
{
FormType = 0,
PowerStation = sentdaypowerstations,
SearchType = 0,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"),
Userid = user.Id
};
var stationReportName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(dayexcel, Formatting.Indented));
if (stationReportName != "")
{
NoticeSchedule DaySchedule = new NoticeSchedule()
{
UserId = user.Id,
EmailType = 0,
RecipientEmail = user.Email,
Subject = "日報表",
Attachment = stationReportName,
RecipientName = user.Name,
Type = 1
};
List<string> properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
}
}
//綜合報表 每日
if (sentMaxpowerstations.Count != 0)
{
Select_table2 maxdayexcel = new Select_table2()
{
FormType = 0,
PowerStation = sentMaxpowerstations,
SearchType = 0,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"),
Userid = user.Id
};
var stationMaxReportName = stationReportController.ExportExcelmaxtableBackDownload(JsonConvert.SerializeObject(maxdayexcel, Formatting.Indented));
NoticeSchedule MaxSchedule = new NoticeSchedule()
{
UserId = user.Id,
EmailType = 2,
RecipientEmail = user.Email,
Subject = "綜合報表",
Attachment = stationMaxReportName,
RecipientName = user.Name,
Type = 1
};
List<string> properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties);
if (DateTime.Now.ToString("dd") == "01")
{
Select_table2 maxmonthexcel = new Select_table2()
{
FormType = 0,
PowerStation = sentMaxpowerstations,
SearchType = 0,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM"),
Userid = user.Id
};
var stationReportmaxmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(maxmonthexcel, Formatting.Indented));
NoticeSchedule MaxmonthSchedule = new NoticeSchedule()
{
RecipientEmail = user.Email,
Subject = "綜合報表",
Attachment = stationReportmaxmonthName,
RecipientName = user.Name,
Type = 1,
UserId = user.Id,
EmailType = 2
};
List<string> properties2 = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2);
}
}
if (DateTime.Now.ToString("dd") == "01")
{
List<Excelpowerstation> sentmonthpowerstations = powerstations.Where(x => x.EmailMonthReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
if (sentmonthpowerstations.Count == 0)
{
break;
}
Excel monthexcel = new Excel()
{
FormType = 1,
PowerStation = sentmonthpowerstations,
SearchType = 2,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM"),
Userid = user.Id
};
var stationReportmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(monthexcel, Formatting.Indented));
if (stationReportmonthName != "")
{
NoticeSchedule MonthSchedule = new NoticeSchedule()
{
RecipientEmail = user.Email,
Subject = "月報表",
Attachment = stationReportmonthName,
RecipientName = user.Name,
Type = 1,
UserId = user.Id,
EmailType = 1
};
List<string> properties2 = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2);
}
}
logger.LogInformation("【CalcAvgPowerStationJob】【產生完成使用者[{0}({1})]的日月報】", user.Account, user.Name);
}
catch (Exception exception)
{
logger.LogError("【CalcAvgPowerStationJob】【產生失敗使用者[{0}({1})]的日月報】", user.Account, user.Name);
logger.LogError("【CalcAvgPowerStationJob】[Exception] - {0}", exception.Message);
if (exception.InnerException != null)
{
logger.LogError("【CalcAvgPowerStationJob】[InnerException] - {0}", exception.InnerException.Message);
}
var line = new StackTrace(exception, true).GetFrame(0).GetFileLineNumber();
logger.LogError("【CalcAvgPowerStationJob】[錯誤行數] - {0}", line);
}
}
#endregion
logger.LogInformation("【CalcAvgPowerStationJob】【任務完成】");
}
catch (Exception exception)
{
logger.LogError("【CalcAvgPowerStationJob - main】[Exception] - {0}", exception.Message);
if(exception.InnerException != null)
{
logger.LogError("【CalcAvgPowerStationJob - main】[InnerException] - {0}", exception.InnerException.Message);
}
}
}
}
}