1. 加入 電錶每小時歸檔
2. 交叉分析bug fix
This commit is contained in:
parent
c92142d4e2
commit
6a5cf5a332
@ -124,7 +124,11 @@ namespace SolarPower.Controllers
|
||||
var powerStation = await powerStationRepository.GetOneAsync(psId_Group.Key);
|
||||
|
||||
//區分電站總覽or設備
|
||||
selected_powerStationIds = psId_Group.Where(x => x.DeviceType == "PWS").Select(x => Convert.ToInt32(x.DeviceId)).ToList();
|
||||
var temp_psIds = psId_Group.Where(x => x.DeviceType == "PWS").Select(x => Convert.ToInt32(x.DeviceId)).ToList();
|
||||
if(temp_psIds.Count() > 0)
|
||||
{
|
||||
selected_powerStationIds.AddRange(temp_psIds);
|
||||
}
|
||||
var selected_meter = psId_Group.Where(x => x.DeviceType == "PWR").Select(x => x.DeviceId).ToList();
|
||||
var selected_device = psId_Group.Where(x => x.DeviceType != "PWS" && x.DeviceType != "PWR").Select(x => x.DeviceId).ToList();
|
||||
|
||||
|
||||
@ -42,7 +42,7 @@ namespace SolarPower.Quartz.Jobs
|
||||
|
||||
if (this.environment.IsDevelopment())
|
||||
{
|
||||
dateTime = "2021-08-06 09";
|
||||
dateTime = "2021-08-09 10";
|
||||
}
|
||||
|
||||
logger.LogInformation("【CalcPowerStationJob】【任務開始】");
|
||||
@ -60,6 +60,7 @@ namespace SolarPower.Quartz.Jobs
|
||||
List<InverterHistory> inverterHistoriesHour = new List<InverterHistory>();
|
||||
List<PowerStation> calcPowerStations = new List<PowerStation>();
|
||||
List<SensorAvgHistory> sensorAvgHistoryHour = new List<SensorAvgHistory>();
|
||||
List<MeterHistory> meterHistoriesHour = new List<MeterHistory>();
|
||||
List<WeatherObservation> weatherObservations = new List<WeatherObservation>();
|
||||
|
||||
#region 氣象觀測(取資料)
|
||||
@ -171,7 +172,7 @@ namespace SolarPower.Quartz.Jobs
|
||||
//總減碳量(總發電量 * (0.554/1000)[抓資料庫值]
|
||||
calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate;
|
||||
|
||||
if(lastmoneyhistorybyhour != null)
|
||||
if (lastmoneyhistorybyhour != null)
|
||||
{
|
||||
history.TODAYCARBON = lastmoneyhistorybyhour.TODAYCARBON + history.KWH * carbonRate;
|
||||
history.TOTALCARBON = lastmoneyhistorybyhour.TOTALCARBON + history.KWH * carbonRate;
|
||||
@ -256,7 +257,7 @@ namespace SolarPower.Quartz.Jobs
|
||||
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) && inverterIds !=null && inverterIds.Count() > 0)
|
||||
if (!string.IsNullOrEmpty(exist_inverter_table) && inverterIds != null && inverterIds.Count() > 0)
|
||||
{
|
||||
logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的逆變器的資訊】", powerStation.Code, dateTime);
|
||||
var inverterHistories = await powerStationRepository.CalcInverterHisyortHourData(dateTime, powerStation.SiteDB, full_inverter_table_name, inverterIds);
|
||||
@ -284,7 +285,7 @@ namespace SolarPower.Quartz.Jobs
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region step2-3. 計算該電站所有sensoravg
|
||||
#region step2-4. 計算該電站所有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);
|
||||
@ -304,6 +305,26 @@ namespace SolarPower.Quartz.Jobs
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region step2-5. 計算該電站meter
|
||||
var meter_table_name = String.Format("s{0}01_meter", powerStation.Code);
|
||||
var full_meter_table_name = String.Format("`{0}`.`{1}`", powerStation.SiteDB, seneoravg_table_name);
|
||||
var exist_meter_table = await powerStationRepository.ExistTable(powerStation.SiteDB, meter_table_name);
|
||||
if (!string.IsNullOrEmpty(exist_seneoravg_table))
|
||||
{
|
||||
logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的meter的資訊】", powerStation.Code, dateTime);
|
||||
var meterHistory = await powerStationRepository.CalcMeterHistory(dateTime, full_meter_table_name);
|
||||
logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的meter的資訊】", powerStation.Code, dateTime);
|
||||
|
||||
if (meterHistory != null)
|
||||
{
|
||||
meterHistory.PowerStationId = powerStation.Id;
|
||||
meterHistory.TIMESTAMP = Convert.ToDateTime(meterHistory.TIMESTAMP + ":00:00").ToString("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
meterHistoriesHour.Add(meterHistory);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 確認是否有觀測站(沒有則新增)
|
||||
if (powerStation.WeathersStationId == null)
|
||||
{
|
||||
@ -312,7 +333,7 @@ namespace SolarPower.Quartz.Jobs
|
||||
foreach (var Location in observation.Records.Location)
|
||||
{
|
||||
|
||||
if(powerStation.Coordinate != null)
|
||||
if (powerStation.Coordinate != null)
|
||||
{
|
||||
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));
|
||||
@ -340,7 +361,7 @@ namespace SolarPower.Quartz.Jobs
|
||||
calcPowerStation.TodayWeatherTemp = Convert.ToDouble(Location.WeatherElement[0].ElementValue);
|
||||
weatherObservation.PowerStationId = powerStation.Id;
|
||||
weatherObservation.Temp = Convert.ToDouble(Location.WeatherElement[0].ElementValue);
|
||||
weatherObservation.ObsTime = Convert.ToInt32(Location.Time.ObsTime.Substring(0, 4)) < 1970? null : Location.Time.ObsTime;
|
||||
weatherObservation.ObsTime = Convert.ToInt32(Location.Time.ObsTime.Substring(0, 4)) < 1970 ? null : Location.Time.ObsTime;
|
||||
calcPowerStation.WeathersStationId = powerStation.WeathersStationId;
|
||||
break;
|
||||
}
|
||||
@ -556,7 +577,29 @@ namespace SolarPower.Quartz.Jobs
|
||||
await powerStationRepository.AddSensorAvgHistory(sensorAvgHistoryHour, sensoravg_history_properties);
|
||||
#endregion
|
||||
|
||||
#region step8. 新增天氣資訊
|
||||
#region step8. 將 meter INSERT 到 meter_history_hour 資料表
|
||||
List<string> memter_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"
|
||||
};
|
||||
|
||||
await powerStationRepository.AddMeterHistory(meterHistoriesHour, memter_history_properties);
|
||||
#endregion
|
||||
|
||||
#region step9. 新增天氣資訊
|
||||
List<string> weather_observation_properties = new List<string>()
|
||||
{
|
||||
"PowerStationId",
|
||||
|
||||
@ -3710,6 +3710,122 @@ namespace SolarPower.Repository.Implement
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MeterHistory> CalcMeterHistory(string dateTime, string table_name)
|
||||
{
|
||||
MeterHistory result;
|
||||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||||
{
|
||||
try
|
||||
{
|
||||
var sql = @$"SELECT
|
||||
m1.timestamp,
|
||||
m1.METERID,
|
||||
m1.V_AB,
|
||||
m1.V_BC,
|
||||
m1.V_CA,
|
||||
m1.I_A,
|
||||
m1.I_B,
|
||||
m1.I_C,
|
||||
m1.P,
|
||||
m1.F,
|
||||
m2.INPUT_KWH,
|
||||
m2.OUTPUT_KWH
|
||||
FROM ( -- 取得平均值
|
||||
SELECT
|
||||
FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') AS timestamp
|
||||
AVG(m.V_AB) AS V_AB,
|
||||
AVG(m.V_BC) AS V_BC,
|
||||
AVG(m.V_CA) AS V_CA,
|
||||
AVG(m.I_A) AS I_A,
|
||||
AVG(m.I_B) AS I_B,
|
||||
AVG(m.I_C) AS I_C,
|
||||
AVG(m.P) AS P,
|
||||
AVG(m.F) AS F
|
||||
FROM {table_name} m
|
||||
WHERE FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') = @DateTime
|
||||
GROUP BY FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), m.METERID
|
||||
) m1
|
||||
LEFT JOIN ( -- 取得整點值INPUT_KWH, OUTPUT_KWH
|
||||
SELECT
|
||||
FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') AS timestamp,
|
||||
m.METERID,
|
||||
m.INPUT_KWH AS INPUT_KWH,
|
||||
m.OUTPUT_KWH AS OUTPUT_KWH
|
||||
FROM {table_name} m
|
||||
WHERE LEFT(FROM_UNIXTIME(b.timestamp / 1000, '%Y-%m-%d %H'), 10) = @DateTime
|
||||
AND RIGHT(FROM_UNIXTIME(b.timestamp / 1000, '%Y-%m-%d %H'), 2) = '55' ) m2
|
||||
ON m1.timestamp = m2.timestamp AND m1.METERID = m2.METERID";
|
||||
result = await conn.QueryFirstOrDefaultAsync<MeterHistory>(sql, new { DateTime = dateTime });
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
finally
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> AddMeterHistory(List<MeterHistory> entity, List<string> properties)
|
||||
{
|
||||
int count;
|
||||
using (IDbConnection conn = _databaseHelper.GetConnection())
|
||||
{
|
||||
conn.Open();
|
||||
using (var trans = conn.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
string sql = GenerateInsertQueryWithCustomTable(properties, "meter_history_hour");
|
||||
|
||||
count = await conn.ExecuteAsync(sql, entity, trans);
|
||||
|
||||
trans.Commit();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
trans.Rollback();
|
||||
throw exception;
|
||||
}
|
||||
finally
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
//TODO: 電表每日彙整
|
||||
public async Task<MeterHistory> CalcMeterDayDataByPowerStationId(string nowDay, int powerStationId)
|
||||
{
|
||||
MeterHistory result;
|
||||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var sql = $@"";
|
||||
|
||||
result = await conn.QueryFirstOrDefaultAsync<MeterHistory>(sql, new { PowerStationId = powerStationId, NowDay = nowDay });
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
finally
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<PowerStation>> GetPowerStationsByCompanyId(MyUser myUser)
|
||||
{
|
||||
List<PowerStation> result;
|
||||
|
||||
@ -546,6 +546,9 @@ namespace SolarPower.Repository.Interface
|
||||
Task<SensorAvgHistory> CalcSensorAvgHistoryMonthDataByPowerStationId(string month, int powerStationId);
|
||||
Task<int> AddSensorAvgHistoryMonthList(List<SensorAvgHistory> entity, List<string> properties);
|
||||
Task<int> UpdateSensorAvgHistoryMonthList(List<SensorAvgHistory> entity);
|
||||
Task<MeterHistory> CalcMeterHistory(string dateTime, string table_name);
|
||||
Task<int> AddMeterHistory(List<MeterHistory> entity, List<string> properties);
|
||||
Task<MeterHistory> CalcMeterDayDataByPowerStationId(string nowDay, int powerStationId);
|
||||
Task<List<PowerStation>> GetPowerStationsByCompanyId(MyUser myUser);
|
||||
Task<List<PowerStationInverter>> GetPowerStationInverter(Dictionary<string, List<int>> dic, string filter);
|
||||
Task<List<PowerStationDevice>> GetPowerStationDevice(Dictionary<string, List<int>> dic, string filter);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user