FIC_Solar/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs
2021-07-14 14:21:37 +08:00

353 lines
17 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 Newtonsoft.Json;
using Quartz;
using SolarPower.Models.PowerStation;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
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();
string json = System.Text.Json.JsonSerializer.Serialize(powerStations);
logger.LogError("【{0}】{1}", "CalcPowerStationJob - step1", json);
#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>();
List<WeatherObservation> weatherObservations = new List<WeatherObservation>();
var DateTimeNow = DateTime.Now;
var count = 0;
#region ()
var client = new HttpClient();
var UVUri = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0003-001?Authorization=CWB-EA24220B-DDCC-4188-84E5-AD37A0E03F80";
HttpResponseMessage response = client.GetAsync(UVUri).Result;
String jsonUVs = response.Content.ReadAsStringAsync().Result.ToString();
Root2 observation = JsonConvert.DeserializeObject<Root2>(jsonUVs);
#endregion
#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");
#region step2-1.
var table_name = String.Format("`{0}`.{1}01_station", powerStation.SiteDB, powerStation.Code);
logger.LogError("【{0}】{1}", "CalcPowerStationJob - time & dataTable", dateTime + "======" + table_name);
var history = await powerStationRepository.GetPowerStationHistoryPerHour(dateTime, table_name);
json = System.Text.Json.JsonSerializer.Serialize(history);
logger.LogError("【{0}】{1}", "CalcPowerStationJob - history", json);
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
#region ()
if (powerStation.WeathersStationId == null)
{
var weatherStationId = "";
double shortLocation = 9999;
foreach (var Location in observation.Records.Location)
{
var powerLocation = powerStation.Coordinate.Split(',');
var nowLocation = Math.Sqrt(Math.Pow(Convert.ToDouble(powerLocation[0]) - Convert.ToDouble(Location.Lat), 2) + Math.Pow(Convert.ToDouble(powerLocation[1]) - Convert.ToDouble(Location.Lon), 2));
if (nowLocation < shortLocation)
{
shortLocation = nowLocation;
weatherStationId = Location.StationId;
calcPowerStation.TodayWeatherTemp = Convert.ToDouble(Location.WeatherElement[0].ElementValue);
}
}
calcPowerStation.WeathersStationId = weatherStationId;
}
#endregion
WeatherObservation weatherObservation = new WeatherObservation();
if (powerStation.WeathersStationId != null)
{
foreach(var Location in observation.Records.Location)
{
if(Location.StationId == powerStation.WeathersStationId)
{
calcPowerStation.TodayWeatherTemp = Convert.ToDouble(Location.WeatherElement[0].ElementValue);
weatherObservation.PowerStationId = powerStation.Id;
weatherObservation.Temp = Convert.ToDouble(Location.WeatherElement[0].ElementValue);
weatherObservation.ObsTime = Location.Time.ObsTime.ToString();
calcPowerStation.WeathersStationId = powerStation.WeathersStationId;
break;
}
}
}
var weather = await powerStationRepository.SelectNowWeather(powerStation.CityId);
calcPowerStation.TodayWeather = weather.WeatherKey;
calcPowerStation.RateOfRain = weather.PoP;
weatherObservations.Add(weatherObservation);
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"
};
json = System.Text.Json.JsonSerializer.Serialize(powerStationHistoriesHour);
logger.LogError("【{0}】{1}", "CalcPowerStationJob - step3", json);
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",
"WeathersStationId",
"TodayWeatherTemp",
"TodayWeather",
"RateOfRain"
};
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
List<string> weather_observation_properties = new List<string>()
{
"PowerStationId",
"ObsTime",
"Temp"
};
await powerStationRepository.AddWeatherObservation(weatherObservations, weather_observation_properties);
}
catch (Exception exception)
{
logger.LogError("【{0}】{1}", "CalcPowerStationJob", exception.Message);
}
}
}
}