diff --git a/SolarPower/Controllers/AnalysisStationInfoController.cs b/SolarPower/Controllers/AnalysisStationInfoController.cs index b502bc0..64fe37f 100644 --- a/SolarPower/Controllers/AnalysisStationInfoController.cs +++ b/SolarPower/Controllers/AnalysisStationInfoController.cs @@ -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(); diff --git a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs index 4652e68..7c25789 100644 --- a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs @@ -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 inverterHistoriesHour = new List(); List calcPowerStations = new List(); List sensorAvgHistoryHour = new List(); + List meterHistoriesHour = new List(); List weatherObservations = new List(); #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; @@ -185,7 +186,7 @@ namespace SolarPower.Quartz.Jobs history.TODAYMONEY = history.KWH * powerStation.PowerRate; history.TOTALMONEY = history.KWH * powerStation.PowerRate; } - + #endregion #region 發電時間 @@ -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 memter_history_properties = new List() + { + "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 weather_observation_properties = new List() { "PowerStationId", diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index c6233f1..a7b86e6 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -3710,6 +3710,122 @@ namespace SolarPower.Repository.Implement } } + public async Task 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(sql, new { DateTime = dateTime }); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return result; + } + } + + public async Task AddMeterHistory(List entity, List 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 CalcMeterDayDataByPowerStationId(string nowDay, int powerStationId) + { + MeterHistory result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + + var sql = $@""; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { PowerStationId = powerStationId, NowDay = nowDay }); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return result; + } + } + + public async Task> GetPowerStationsByCompanyId(MyUser myUser) { List result; diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index bf48ec1..ce13052 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -546,6 +546,9 @@ namespace SolarPower.Repository.Interface Task CalcSensorAvgHistoryMonthDataByPowerStationId(string month, int powerStationId); Task AddSensorAvgHistoryMonthList(List entity, List properties); Task UpdateSensorAvgHistoryMonthList(List entity); + Task CalcMeterHistory(string dateTime, string table_name); + Task AddMeterHistory(List entity, List properties); + Task CalcMeterDayDataByPowerStationId(string nowDay, int powerStationId); Task> GetPowerStationsByCompanyId(MyUser myUser); Task> GetPowerStationInverter(Dictionary> dic, string filter); Task> GetPowerStationDevice(Dictionary> dic, string filter);