391 lines
18 KiB
C#
391 lines
18 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using SolarPower.Models;
|
|
using SolarPower.Models.PowerStation;
|
|
using SolarPower.Repository.Interface;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SolarPower.Controllers
|
|
{
|
|
public class DataBackFillController : MyBaseController<DataBackFillController>
|
|
{
|
|
private readonly IPowerStationRepository powerStationRepository;
|
|
private double carbonRate;
|
|
|
|
public DataBackFillController(IPowerStationRepository powerStationRepository) : base()
|
|
{
|
|
this.powerStationRepository = powerStationRepository;
|
|
}
|
|
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
public async Task<ApiResult<string>> BackFillHistory()
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
var dateTimeFormat = "yyyy-MM-dd";
|
|
|
|
try
|
|
{
|
|
var targetDay = 7; //預計範圍天數
|
|
|
|
var now = DateTime.Now;
|
|
var endDate = now.AddDays(-1);
|
|
var endDateStr = endDate.ToString(dateTimeFormat);
|
|
var startDate = endDate.AddDays(-1 * targetDay);
|
|
var startDateStr = startDate.ToString(dateTimeFormat);
|
|
var timeArray = new List<string>()
|
|
{
|
|
"00:05",
|
|
"01:05",
|
|
"02:05",
|
|
"03:05",
|
|
"04:05",
|
|
"05:05",
|
|
"06:05",
|
|
"07:05",
|
|
"08:05",
|
|
"09:05",
|
|
"10:05",
|
|
"11:05",
|
|
"12:05",
|
|
"13:05",
|
|
"14:05",
|
|
"15:05",
|
|
"16:05",
|
|
"17:05",
|
|
"18:05",
|
|
"19:05",
|
|
"20:05",
|
|
"21:05",
|
|
"22:05",
|
|
"23:05"
|
|
};
|
|
|
|
#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>();
|
|
|
|
//5分鐘Sensor平均歷史資料
|
|
List<SensorAvgHistory> sensorAvgHistoryHour = new List<SensorAvgHistory>();
|
|
|
|
#region 每小時歷史資料回填
|
|
for (var i = 0; i < targetDay; i++)
|
|
{
|
|
var currentDateStr = startDate.AddDays(i).ToString(dateTimeFormat);
|
|
for (var j = 0; j < 24; j++)
|
|
{
|
|
var time = string.Format("{0}", j.ToString().PadLeft(2, '0'));
|
|
|
|
var all_time = string.Format("{0} {1}", currentDateStr, time);
|
|
|
|
foreach (var powerStation in powerStations)
|
|
{
|
|
#region step2. 電站歷史資料彙整(每小時)
|
|
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))
|
|
{
|
|
#region step2-1. 電站歷史資料
|
|
var history = await powerStationRepository.GetPowerStationHistoryPerHour(all_time, full_table_name);
|
|
var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 0, all_time);
|
|
|
|
if (history != null)
|
|
{
|
|
history.PowerStationId = powerStation.Id;
|
|
history.Timestamp = Convert.ToDateTime(history.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
carbonRate = Convert.ToDouble(await powerStationRepository.GetOneVariableByName("CarbonRate"));
|
|
|
|
history.CARBON = history.KWH * carbonRate;
|
|
history.TODAYCARBON = lastmoneyhistorybyhour.TODAYCARBON + history.KWH * carbonRate;
|
|
history.TOTALCARBON = lastmoneyhistorybyhour.TOTALCARBON + history.KWH * carbonRate;
|
|
history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate;
|
|
history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate;
|
|
|
|
powerStationHistoriesHour.Add(history);
|
|
}
|
|
#endregion
|
|
|
|
#region step2-2. 取得該電站的當前這小時的日照度歷史資料
|
|
//step2-2 1. 找出該電站所有日照計設備(包含共享
|
|
var deviceInfos = await powerStationRepository.GetListPyrheliometerByPowerStationId(powerStation.Id, powerStation.SiteDB);
|
|
if (deviceInfos != null)
|
|
{
|
|
//step2-2 2. 計算該電站所有日照計設的每小時的平均在依照日照計數量平均
|
|
var pyrheliometerHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(all_time, deviceInfos, 0);
|
|
|
|
if (pyrheliometerHistory != null)
|
|
{
|
|
pyrheliometerHistory.Timestamp = Convert.ToDateTime(pyrheliometerHistory.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
|
pyrheliometerHistory.PowerStationId = powerStation.Id;
|
|
pyrheliometerHistoriesHour.Add(pyrheliometerHistory);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region step2-3. 取得該電站的當前這小時的溫度計歷史資料
|
|
//step2-3 1. 找出該電站所有溫度計設備(包含共享
|
|
var tempdeviceInfos = await powerStationRepository.GetListTempByPowerStationId(powerStation.Id, powerStation.SiteDB);
|
|
if (tempdeviceInfos != null)
|
|
{
|
|
//step2-3 2. 計算該電站所有溫度計設的每小時的平均在依照溫度計數量平均
|
|
var tempHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(all_time, 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
|
|
}
|
|
#endregion
|
|
|
|
#region step3. 逆變器歷史資料彙整(每小時)
|
|
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();
|
|
|
|
var inverter_table_name = String.Format("s{0}01_inv", powerStation.Code);
|
|
var full_inverter_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, inverter_table_name);
|
|
var exist_inverter_table = await powerStationRepository.ExistTable(powerStation.SiteDB, inverter_table_name);
|
|
|
|
if (!string.IsNullOrEmpty(exist_inverter_table))
|
|
{
|
|
inverterHistories = await powerStationRepository.CalcInverterHisyortHourData(all_time, powerStation.SiteDB, full_inverter_table_name, inverterIds);
|
|
//取得日照計要找的欄位資訊
|
|
var pyrheliometer = await powerStationRepository.GetFirstPyrheliometerInfo(powerStation.Id, powerStation.SiteDB);
|
|
var pyrheliometerValue = await powerStationRepository.GetFirstPyrheliometerValue(all_time, pyrheliometer.DBName, pyrheliometer.TableName, pyrheliometer.ColName);
|
|
foreach (var inverterHistory in inverterHistories)
|
|
{
|
|
inverterHistory.Irradiance = pyrheliometerValue;
|
|
inverterHistory.DC1KW = inverterHistory.DC1W / 1000;
|
|
inverterHistory.DC2KW = inverterHistory.DC2W / 1000;
|
|
inverterHistory.DC3KW = inverterHistory.DC3W / 1000;
|
|
inverterHistory.DC4KW = inverterHistory.DC4W / 1000;
|
|
inverterHistory.DC5KW = inverterHistory.DC5W / 1000;
|
|
|
|
inverterHistory.DCKW = (inverterHistory.DC1W + inverterHistory.DC2W + inverterHistory.DC3W + inverterHistory.DC4W + inverterHistory.DC5W) / 1000;
|
|
inverterHistory.ACKW = (inverterHistory.AC1W + inverterHistory.AC2W + inverterHistory.AC3W) / 1000;
|
|
|
|
inverterHistory.TIMESTAMP = Convert.ToDateTime(inverterHistory.TIMESTAMP + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
|
inverterHistory.PowerStationId = powerStation.Id;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region step4. 計算該電站所有sensoravg
|
|
var seneoravg_table_name = String.Format("s{0}01_sensoravg", powerStation.Code);
|
|
var full_seneoravg_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, seneoravg_table_name);
|
|
var exist_seneoravg_table = await powerStationRepository.ExistTable(powerStation.SiteDB, seneoravg_table_name);
|
|
if (!string.IsNullOrEmpty(exist_seneoravg_table))
|
|
{
|
|
var sensorAvgHistory = await powerStationRepository.CalcSensorAvgHistory(all_time, full_seneoravg_table_name);
|
|
|
|
if (sensorAvgHistory != null)
|
|
{
|
|
sensorAvgHistory.PowerStationId = powerStation.Id;
|
|
sensorAvgHistory.TIMESTAMP = Convert.ToDateTime(sensorAvgHistory.TIMESTAMP + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
sensorAvgHistoryHour.Add(sensorAvgHistory);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
}
|
|
|
|
#region 刪除後新增該時間區間的電站歷史紀錄
|
|
List<string> history_properties = new List<string>()
|
|
{
|
|
"PowerStationId",
|
|
"TIMESTAMP",
|
|
"SITEID",
|
|
"SITETYPE",
|
|
"KWH",
|
|
"TODAYKWH",
|
|
"TOTALKWH",
|
|
"KWHKWP",
|
|
"PR",
|
|
"MP",
|
|
"SolarHour",
|
|
"MONEY",
|
|
"CARBON",
|
|
"TODAYMONEY",
|
|
"TOTALMONEY",
|
|
"TODAYCARBON",
|
|
"TOTALCARBON"
|
|
};
|
|
await powerStationRepository.AddAfterPurgePowerStationHistoryHour(startDateStr, endDateStr, powerStationHistoriesHour, history_properties);
|
|
#endregion
|
|
|
|
#region 刪除後新增該時間區間的日照溫度歷史紀錄
|
|
List<string> pyrheliometer_history_properties = new List<string>()
|
|
{
|
|
"PowerStationId",
|
|
"TIMESTAMP",
|
|
"Irradiance"
|
|
};
|
|
await powerStationRepository.AddAfterPurgePyrheliometerHistoryHour(startDateStr, endDateStr, pyrheliometerHistoriesHour, pyrheliometer_history_properties);
|
|
|
|
List<string> Temp_history_properties = new List<string>()
|
|
{
|
|
"PowerStationId",
|
|
"TIMESTAMP",
|
|
"Temperature"
|
|
};
|
|
await powerStationRepository.AddTempHistory(TempHistoriesHour, Temp_history_properties);
|
|
#endregion
|
|
|
|
#region 刪除後新增該時間區間的逆變器歷史紀錄
|
|
List<string> inverter_history_properties = new List<string>()
|
|
{
|
|
"PowerStationId",
|
|
"INVERTERID",
|
|
"TIMESTAMP",
|
|
"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",
|
|
"TOTALKWH",
|
|
"KWHKWP",
|
|
};
|
|
await powerStationRepository.AddAfterPurgeInverterHistoryHour(startDateStr, endDateStr, inverterHistories, inverter_history_properties);
|
|
#endregion
|
|
|
|
#region 刪除後新增該時間區間的SensorAvg歷史紀錄
|
|
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",
|
|
};
|
|
await powerStationRepository.AddAfterPurgeSensorAvgHistoryHour(startDateStr, endDateStr, sensorAvgHistoryHour, sensoravg_history_properties);
|
|
#endregion
|
|
|
|
#endregion
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
|
|
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
|
return apiResult;
|
|
}
|
|
}
|
|
}
|