From de8cde18e8dc5e02bb610c5d25407caecdf2bd76 Mon Sep 17 00:00:00 2001 From: "wanling040@gmail.com" Date: Sun, 7 Aug 2022 01:56:25 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=9B=BB=E7=AB=99=E8=B3=87=E8=A8=8A-?= =?UTF-8?q?=E9=80=86=E8=AE=8A=E5=99=A8=E5=88=86=E6=9E=90:=20=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E4=B8=8B=E6=96=B9=E7=9A=84=E7=9B=B4=E6=A2=9D=E5=9C=96?= =?UTF-8?q?=E6=B6=88=E5=A4=B1=E5=95=8F=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Views/StationOverview/StationOverviewInfo.cshtml | 2 +- SolarPower/Views/StationOverview/_Inverter.cshtml | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/SolarPower/Views/StationOverview/StationOverviewInfo.cshtml b/SolarPower/Views/StationOverview/StationOverviewInfo.cshtml index 718fde5..1c68f05 100644 --- a/SolarPower/Views/StationOverview/StationOverviewInfo.cshtml +++ b/SolarPower/Views/StationOverview/StationOverviewInfo.cshtml @@ -2359,7 +2359,7 @@ data: kwhBar.datasets[key], backgroundColor: bgColorlist[index], } - hideLoading(); + return dataset; }); $('#inverter-kWh-convas-div').empty(); diff --git a/SolarPower/Views/StationOverview/_Inverter.cshtml b/SolarPower/Views/StationOverview/_Inverter.cshtml index 935dbe3..6fd49a8 100644 --- a/SolarPower/Views/StationOverview/_Inverter.cshtml +++ b/SolarPower/Views/StationOverview/_Inverter.cshtml @@ -56,8 +56,7 @@ -
-
- -
-
\ No newline at end of file + +
+ +
From da507694faf6211a37def489f75512b586689d6b Mon Sep 17 00:00:00 2001 From: "wanling040@gmail.com" Date: Mon, 8 Aug 2022 11:31:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?CalcPowerStationJob:=20=E6=AF=8F=E5=B0=8F?= =?UTF-8?q?=E6=99=82=E6=9B=B4=E6=96=B0=E7=99=BC=E9=9B=BB=E9=87=8F=E3=80=81?= =?UTF-8?q?=E7=99=BC=E9=9B=BB=E9=87=91=E9=A1=8D=E7=AD=89=E8=B3=87=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SolarPower/Quartz/Jobs/CalcPowerStationJob.cs | 410 +++++++++++++----- .../Implement/PowerStationRepository.cs | 27 ++ .../Interface/IPowerStationRepository.cs | 1 + 3 files changed, 326 insertions(+), 112 deletions(-) diff --git a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs index 99a87bb..6d28052 100644 --- a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs @@ -93,6 +93,7 @@ namespace SolarPower.Quartz.Jobs { var calcPowerStation = new PowerStation(); calcPowerStation.Id = powerStation.Id; + string calcPowerStationTime = ""; #region step2-1. 取得該電站的當前這小時的歷史資料 var table_name = String.Format("s{1}01_station", powerStation.SiteDB, powerStation.Code); @@ -107,6 +108,8 @@ namespace SolarPower.Quartz.Jobs { logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); var history = await powerStationRepository.GetPowerStationHistoryPerHour(dateTime, full_table_name); + //await setHistoryPerHourForPowerStation(dateTime, full_table_name, powerStation); + // 當前用不到 // var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 0, dateTime); @@ -126,12 +129,12 @@ namespace SolarPower.Quartz.Jobs #region 計算單一電站每小時發電量、發電金額等資料 #region 發電量 - //每小時發電量(直接填寫 - calcPowerStation.kwh = history.KWH; - //今日發電量(直接填寫 - calcPowerStation.Today_kWh = history.TodayKWh; - //總發電量(直接填寫 - calcPowerStation.Total_kWh = history.TotalKWH; + ////每小時發電量(直接填寫 + //calcPowerStation.kwh = history.KWH; + ////今日發電量(直接填寫 + //calcPowerStation.Today_kWh = history.TodayKWh; + ////總發電量(直接填寫 + //calcPowerStation.Total_kWh = history.TotalKWH; #endregion #region 發電金額 @@ -141,96 +144,96 @@ namespace SolarPower.Quartz.Jobs history.TOTALMONEY = history.TotalKWH * powerStation.PowerRate; //即時發電金額 - switch (powerStation.SolarType) - { - case (int)SolarTypeEnum.SelfSold: //自建躉售 + //switch (powerStation.SolarType) + //{ + // case (int)SolarTypeEnum.SelfSold: //自建躉售 - //if (lastmoneyhistorybyhour != null) - //{ - // history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; - // history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; - //} - //else - //{ - // history.TODAYMONEY = history.KWH * powerStation.PowerRate; - // history.TOTALMONEY = history.KWH * powerStation.PowerRate; - //} + // //if (lastmoneyhistorybyhour != null) + // //{ + // // history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; + // // history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; + // //} + // //else + // //{ + // // history.TODAYMONEY = history.KWH * powerStation.PowerRate; + // // history.TOTALMONEY = history.KWH * powerStation.PowerRate; + // //} - //今日發電金額 計算方式:todaykWh * 授電費率 - calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; - //calcPowerStation.Today_Money = history.TODAYMONEY; + // //今日發電金額 計算方式:todaykWh * 授電費率 + // calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; + // //calcPowerStation.Today_Money = history.TODAYMONEY; - ////總發電金額 計算方式:totalkWh * 授電費率 - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; - //calcPowerStation.Total_Money = history.TOTALMONEY; - break; - case (int)SolarTypeEnum.HireSold: //租建躉售 - //找出該電站的所有土地房屋資訊 - var landBuildings = await powerStationRepository.GetAllLandBuildingInfoByPowerStationId(powerStation.Id, powerStation.SiteDB); - var sumLeaseRate = 0.00; - var avgLeaseRate = 0.00; + // ////總發電金額 計算方式:totalkWh * 授電費率 + // calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; + // //calcPowerStation.Total_Money = history.TOTALMONEY; + // 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(); + // foreach (var landBuilding in landBuildings) + // { + // sumLeaseRate += landBuilding.LeaseRate; + // } + // //avgLeaseRate = sumLeaseRate / landBuildings.Count(); - //if (lastmoneyhistorybyhour != null) - //{ - // history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; - // history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; + // //if (lastmoneyhistorybyhour != null) + // //{ + // // history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; + // // history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; - // //今日發電金額計算方式:todaykWh * 出借費率(各個土地房屋租借比率平均) - // calcPowerStation.Today_Money = lastmoneyhistorybyhour.TODAYMONEY + (history.KWH * powerStation.PowerRate * sumLeaseRate); - // //總發電金額 計算方式:totalkWh * 授電費率 - // calcPowerStation.Total_Money = lastmoneyhistorybyhour.TOTALMONEY + (history.KWH * powerStation.PowerRate * sumLeaseRate); - //} - //else - //{ - // history.TODAYMONEY = history.KWH * powerStation.PowerRate; - // history.TOTALMONEY = history.KWH * powerStation.PowerRate; + // // //今日發電金額計算方式:todaykWh * 出借費率(各個土地房屋租借比率平均) + // // calcPowerStation.Today_Money = lastmoneyhistorybyhour.TODAYMONEY + (history.KWH * powerStation.PowerRate * sumLeaseRate); + // // //總發電金額 計算方式:totalkWh * 授電費率 + // // calcPowerStation.Total_Money = lastmoneyhistorybyhour.TOTALMONEY + (history.KWH * powerStation.PowerRate * sumLeaseRate); + // //} + // //else + // //{ + // // history.TODAYMONEY = history.KWH * powerStation.PowerRate; + // // history.TOTALMONEY = history.KWH * powerStation.PowerRate; - // calcPowerStation.Today_Money = history.KWH * powerStation.PowerRate * sumLeaseRate; - // calcPowerStation.Total_Money = history.KWH * powerStation.PowerRate * sumLeaseRate; - //} + // // calcPowerStation.Today_Money = history.KWH * powerStation.PowerRate * sumLeaseRate; + // // calcPowerStation.Total_Money = history.KWH * powerStation.PowerRate * sumLeaseRate; + // //} - calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; + // calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; + // calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; - break; - case (int)SolarTypeEnum.SelfUse: //自建自用 + // break; + // case (int)SolarTypeEnum.SelfUse: //自建自用 - //if (lastmoneyhistorybyhour != null) - //{ - // history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; - // history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; - //} - //else - //{ - // history.TODAYMONEY = history.KWH * powerStation.PowerRate; - // history.TOTALMONEY = history.KWH * powerStation.PowerRate; - //} + // //if (lastmoneyhistorybyhour != null) + // //{ + // // history.TODAYMONEY = lastmoneyhistorybyhour.TODAYMONEY + history.KWH * powerStation.PowerRate; + // // history.TOTALMONEY = lastmoneyhistorybyhour.TOTALMONEY + history.KWH * powerStation.PowerRate; + // //} + // //else + // //{ + // // history.TODAYMONEY = history.KWH * powerStation.PowerRate; + // // history.TOTALMONEY = history.KWH * powerStation.PowerRate; + // //} - //今日發電金額 計算方式:todaykWh * 授電費率 - //calcPowerStation.Today_Money = history.TODAYMONEY; - calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; + // //今日發電金額 計算方式:todaykWh * 授電費率 + // //calcPowerStation.Today_Money = history.TODAYMONEY; + // calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; - //總發電金額 計算方式:totalkWh * 授電費率 - //calcPowerStation.Total_Money = history.TOTALMONEY; - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; - break; - } + // //總發電金額 計算方式:totalkWh * 授電費率 + // //calcPowerStation.Total_Money = history.TOTALMONEY; + // calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; + // break; + //} #endregion #region kWh/kWp - //直接填寫 - calcPowerStation.Today_kwhkwp = history.KWHKWP; + ////直接填寫 + //calcPowerStation.Today_kwhkwp = history.KWHKWP; #endregion #region PR //直接填寫 - calcPowerStation.Today_PR = history.PR; + //calcPowerStation.Today_PR = history.PR; #endregion #region 減碳量 @@ -252,19 +255,123 @@ namespace SolarPower.Quartz.Jobs //今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值] //calcPowerStation.Today_Carbon = history.TODAYCARBON; - calcPowerStation.Today_Carbon = history.TodayKWh * carbonRate; + //calcPowerStation.Today_Carbon = history.TodayKWh * carbonRate; //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] //calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; - calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; + //calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; #endregion #region 發電時間 - calcPowerStation.SolarHour = history.SolarHour; + //calcPowerStation.SolarHour = history.SolarHour; #endregion #endregion + #region 計算單一電站每小時發電量、發電金額等資料 for calcPowerStation + var dateTimeforPowerStation = DateTime.Now.ToString("yyyy-MM-dd");//取當天 + logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的每小時歷史資料 for calcPowerStation】", powerStation.Code, dateTimeforPowerStation); + var history_for_powerStation = await powerStationRepository.GetPowerStationHistoryPerHourForPowerStation(dateTimeforPowerStation, full_table_name); + if (history_for_powerStation == null) + { + logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料 for calcPowerStation;發電量、發電金額等資料設定為0】", powerStation.Code, dateTimeforPowerStation); + calcPowerStation.kwh = 0; + calcPowerStation.Today_kWh = 0; + calcPowerStation.Total_kWh = 0; + calcPowerStation.Today_Money = 0; + calcPowerStation.Total_Money = 0; + calcPowerStation.Today_kwhkwp = 0; + calcPowerStation.Today_PR = 0; + calcPowerStation.Today_Carbon = 0; + calcPowerStation.Total_Carbon = 0; + calcPowerStation.SolarHour = 0; + } + else + { + logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的每小時歷史資料 for calcPowerStation】", powerStation.Code, dateTimeforPowerStation); + #region 發電量 + //每小時發電量(直接填寫 + calcPowerStation.kwh = history_for_powerStation.KWH; + //今日發電量(直接填寫 + calcPowerStation.Today_kWh = history_for_powerStation.TodayKWh; + //總發電量(直接填寫 + calcPowerStation.Total_kWh = history_for_powerStation.TotalKWH; + #endregion + + #region 發電金額 + history_for_powerStation.MONEY = history_for_powerStation.KWH * powerStation.PowerRate; + history_for_powerStation.TODAYMONEY = history_for_powerStation.TodayKWh * powerStation.PowerRate; + history_for_powerStation.TOTALMONEY = history_for_powerStation.TotalKWH * powerStation.PowerRate; + + //即時發電金額 + switch (powerStation.SolarType) + { + case (int)SolarTypeEnum.SelfSold: //自建躉售 + //今日發電金額 計算方式:todaykWh * 授電費率 + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; + + ////總發電金額 計算方式:totalkWh * 授電費率 + calcPowerStation.Total_Money = history_for_powerStation.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; + } + + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; + + break; + case (int)SolarTypeEnum.SelfUse: //自建自用 + + //今日發電金額 計算方式:todaykWh * 授電費率 + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; + + //總發電金額 計算方式:totalkWh * 授電費率 + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate; + break; + } + #endregion + + #region kWh/kWp + ////直接填寫 + calcPowerStation.Today_kwhkwp = history_for_powerStation.KWHKWP; + #endregion + + #region PR + //直接填寫 + calcPowerStation.Today_PR = history_for_powerStation.PR; + #endregion + + #region 減碳量 + carbonRate = Convert.ToDouble(await powerStationRepository.GetOneVariableByName("CarbonRate")); + history_for_powerStation.CARBON = history_for_powerStation.KWH * carbonRate; + history_for_powerStation.TODAYCARBON = history_for_powerStation.TodayKWh * carbonRate; + history_for_powerStation.TOTALCARBON = history_for_powerStation.TotalKWH * carbonRate; + + //今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值] + calcPowerStation.Today_Carbon = history_for_powerStation.TodayKWh * carbonRate; + + //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] + calcPowerStation.Total_Carbon = history_for_powerStation.TotalKWH * carbonRate; + #endregion + + #region 發電時間 + calcPowerStation.SolarHour = history_for_powerStation.SolarHour; + #endregion + + #region 取拿到資料的時間 + calcPowerStationTime = history_for_powerStation.Timestamp; + #endregion + } + #endregion + powerStationHistoriesHour.Add(history); logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); @@ -299,10 +406,19 @@ namespace SolarPower.Quartz.Jobs { logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的日照計的平均值】", powerStation.Code, dateTime); var pyrheliometerHistory = await powerStationRepository.CalcSensorHistoryPerHour(dateTime, deviceInfos, Convert.ToInt32(SensorTypeEnum.PYR)); - + string dateTimeForCalc; + if (calcPowerStationTime != "") + { + dateTimeForCalc = calcPowerStationTime; + } + else + { + dateTimeForCalc = dateTime; + } + var pyrheliometerHistory_for_calcPowerStation = await powerStationRepository.CalcSensorHistoryPerHour(dateTimeForCalc, deviceInfos, Convert.ToInt32(SensorTypeEnum.PYR)); if (pyrheliometerHistory != null) { - calcPowerStation.Today_irradiance = pyrheliometerHistory.Irradiance; + calcPowerStation.Today_irradiance = pyrheliometerHistory_for_calcPowerStation.Irradiance; sensorHistory.Irradiance = pyrheliometerHistory.Irradiance; @@ -489,13 +605,13 @@ namespace SolarPower.Quartz.Jobs var TPYHistory = await powerStationRepository.CalcSensorHistoryPerHour(dateTime, TPYdeviceInfos, Convert.ToInt32(SensorTypeEnum.TPY)); if (TPYHistory != null) { - sensorHistory.IrrDay = TPYHistory.IrrDay; + sensorHistory.IrrDay = TPYHistory.IrrDay; logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的累計日照量的平均值】", powerStation.Code, dateTime); logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); - var ss = await powerStationRepository.CalcIrrDayHourHistoryPerHour(dateTime, powerStation.Id); + var ss = await powerStationRepository.CalcIrrDayHourHistoryPerHour(dateTime, powerStation.Id); if (ss != null) { - sensorHistory.IrrDayHour = double.Parse( ss); + sensorHistory.IrrDayHour = double.Parse(ss); logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); } @@ -508,30 +624,6 @@ namespace SolarPower.Quartz.Jobs logger.LogError("【CalcPowerStationJob】【失敗原因】- {0}", ex.Message); } - //9. 計算該電站 - 每小時累計日照量(IrrDayHour) - //try - //{ - // logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的累計日照量設備資訊】", powerStation.Code, dateTime); - // var TPYdeviceInfos = await powerStationRepository.GetDeviceListByPowerStationIdAndType(powerStation.Id, SensorTypeEnum.TPY.ToString(), powerStation.SiteDB); - // logger.LogInformation("【CalcPowerStationJob】【取得成功電站[{0}]在{1}的累計日照量備資訊】", powerStation.Code, dateTime); - // logger.LogInformation("【CalcPowerStationJob】【電站[{0}]在{1}的相減的累計日照量設備資訊】 - {2}", powerStation.Code, dateTime, System.Text.Json.JsonSerializer.Serialize(TPYdeviceInfos)); - // if (TPYdeviceInfos != null && TPYdeviceInfos.Count() > 0) - // { - // logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); - // var TPYHistory = await powerStationRepository.CalcIrrDayHourHistoryPerHour(dateTime, powerStation.Id); - // if (TPYHistory != null) - // { - // sensorHistory.IrrDayHour = TPYHistory.IrrDayHour; - // logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); - // } - // } - //} - //catch (Exception ex) - //{ - // logger.LogError("【CalcPowerStationJob】【計算失敗電站[{0}]在{1}的相減的累計日照量】", powerStation.Code, dateTime); - // logger.LogError("【CalcPowerStationJob】【失敗原因】- {0}", ex.Message); - //} - sensorHistoriesHour.Add(sensorHistory); } @@ -672,9 +764,9 @@ namespace SolarPower.Quartz.Jobs double p2 = Convert.ToDouble(powerLocation[1].Replace("°", string.Empty)); double dLat = Convert.ToDouble(Location.Lat); double dLon = Convert.ToDouble(Location.Lon); - double x = Math.Pow(p1 - dLat, 2) ; + double x = Math.Pow(p1 - dLat, 2); double y = Math.Pow(p2 - dLon, 2); - var nowLocation = Math.Sqrt(x + y); + var nowLocation = Math.Sqrt(x + y); //var nowLocation = Math.Sqrt(Math.Pow(Convert.ToDouble(powerLocation[0]) // - Convert.ToDouble(Location.Lat), 2) // + Math.Pow(Convert.ToDouble(powerLocation[1]) @@ -732,7 +824,7 @@ namespace SolarPower.Quartz.Jobs } } #endregion - + if (!this.environment.IsDevelopment()) { #region step3. 將historiers INSERT 到 power_station_history_hour 資料表 @@ -980,5 +1072,99 @@ namespace SolarPower.Quartz.Jobs } } } + + public async Task setHistoryPerHourForPowerStation(string dateTime, string full_table_name, PowerStation powerStation) + { + var calcPowerStation = new PowerStation(); + calcPowerStation.Id = powerStation.Id; + var history_for_powerStation = await powerStationRepository.GetPowerStationHistoryPerHourForPowerStation(dateTime, full_table_name); + if (history_for_powerStation == null) + { + //logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //每小時發電量(直接填寫 + calcPowerStation.kwh = 0; + //今日發電量(直接填寫 + calcPowerStation.Today_kWh = 0; + //總發電量(直接填寫 + calcPowerStation.Total_kWh = 0; + } + else + { + //每小時發電量(直接填寫 + calcPowerStation.kwh = history_for_powerStation.KWH; + //今日發電量(直接填寫 + calcPowerStation.Today_kWh = history_for_powerStation.TodayKWh; + //總發電量(直接填寫 + calcPowerStation.Total_kWh = history_for_powerStation.TotalKWH; + + #region 發電金額 + + history_for_powerStation.MONEY = history_for_powerStation.KWH * powerStation.PowerRate; + history_for_powerStation.TODAYMONEY = history_for_powerStation.TodayKWh * powerStation.PowerRate; + history_for_powerStation.TOTALMONEY = history_for_powerStation.TotalKWH * powerStation.PowerRate; + + //即時發電金額 + switch (powerStation.SolarType) + { + case (int)SolarTypeEnum.SelfSold: //自建躉售 + + //今日發電金額 計算方式:todaykWh * 授電費率 + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; + + ////總發電金額 計算方式:totalkWh * 授電費率 + calcPowerStation.Total_Money = history_for_powerStation.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; + } + + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; + + break; + case (int)SolarTypeEnum.SelfUse: //自建自用 + + //今日發電金額 計算方式:todaykWh * 授電費率 + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; + + //總發電金額 計算方式:totalkWh * 授電費率 + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate; + break; + } + #endregion + + #region PR + //直接填寫 + calcPowerStation.Today_PR = history_for_powerStation.PR; + #endregion + + #region 減碳量 + carbonRate = Convert.ToDouble(await powerStationRepository.GetOneVariableByName("CarbonRate")); + + history_for_powerStation.CARBON = history_for_powerStation.KWH * carbonRate; + history_for_powerStation.TODAYCARBON = history_for_powerStation.TodayKWh * carbonRate; + history_for_powerStation.TOTALCARBON = history_for_powerStation.TotalKWH * carbonRate; + + //今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值] + calcPowerStation.Today_Carbon = history_for_powerStation.TodayKWh * carbonRate; + + //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] + calcPowerStation.Total_Carbon = history_for_powerStation.TotalKWH * carbonRate; + + #endregion + + #region 發電時間 + calcPowerStation.SolarHour = history_for_powerStation.SolarHour; + #endregion + } + + } } } diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index ecb6390..48dddaf 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -2189,6 +2189,33 @@ namespace SolarPower.Repository.Implement } } + public async Task GetPowerStationHistoryPerHourForPowerStation(string dateTime, string table_name) + { + PowerStationHistory result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $@" + SELECT + DATE_FORMAT(FROM_UNIXTIME(timestamp / 1000), '%Y-%m-%d %H') AS TIMESTAMP, + SITEID, SiteType, KWH, TodayKWh, TotalKWH, + KWHKWP, PR, MP, SolarHour + FROM {table_name} + WHERE DATE_FORMAT(FROM_UNIXTIME(timestamp / 1000), '%Y-%m-%d') = @DateTime + AND kwh > 0 + order by timestamp desc"; + + result = await conn.QueryFirstOrDefaultAsync(sql, new { DateTime = dateTime }, commandTimeout: 600);//加上時間 + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + public async Task CalcAvgPowerStationHistory30day(string nowDay, string table_name) { AvgPowerStationHistory result; diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 7e9ff54..7ee71f8 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -633,5 +633,6 @@ namespace SolarPower.Repository.Interface Task> GetAllNoStatusAsync(); Task CalcIrrDayHourHistoryPerHour(string dateTime, int powerStationId); + Task GetPowerStationHistoryPerHourForPowerStation(string dateTime, string table_name); } } From 8133a69266e6e837eff42f8a4120e38f0a20ce78 Mon Sep 17 00:00:00 2001 From: "wanling040@gmail.com" Date: Mon, 8 Aug 2022 11:32:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?solar=5FApp:=20=E5=8D=B3=E6=99=82=E6=9B=B4?= =?UTF-8?q?=E6=96=B0(=E5=85=A8=E9=83=A8=E9=9B=BB=E7=AB=99=E7=9A=84?= =?UTF-8?q?=E7=99=BC=E9=9B=BB=E9=87=8F=E3=80=81=E7=99=BC=E9=9B=BB=E9=87=91?= =?UTF-8?q?=E9=A1=8D=E7=AD=89=E8=B3=87=E6=96=99)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solarApp/Service/getStationSvc.cs | 27 +++ solarApp/Service/procArchiveHourly.cs | 228 +++++++++++++++++++------- 2 files changed, 199 insertions(+), 56 deletions(-) diff --git a/solarApp/Service/getStationSvc.cs b/solarApp/Service/getStationSvc.cs index a1b6b08..92dca89 100644 --- a/solarApp/Service/getStationSvc.cs +++ b/solarApp/Service/getStationSvc.cs @@ -276,6 +276,33 @@ namespace solarApp.Service } } + public PowerStationHistory GetPowerStationHistoryPerHourForPowerStation(string dateTime, string table_name) + { + PowerStationHistory result; + using (MySqlConnection conn = new MySqlConnection(Connection1)) + { + try + { + var sql = $@" + SELECT + DATE_FORMAT(FROM_UNIXTIME(timestamp / 1000), '%Y-%m-%d %H') AS TIMESTAMP, + SITEID, SiteType, KWH, TodayKWh, TotalKWH, + KWHKWP, PR, MP, SolarHour + FROM {table_name} + WHERE DATE_FORMAT(FROM_UNIXTIME(timestamp / 1000), '%Y-%m-%d') = @DateTime + AND kwh > 0 + order by timestamp desc"; + + result = conn.QueryFirstOrDefault(sql, new { DateTime = dateTime }, commandTimeout: 600);//加上時間 + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + /// /// 透過電站編號,取得所有土地房屋資訊 /// diff --git a/solarApp/Service/procArchiveHourly.cs b/solarApp/Service/procArchiveHourly.cs index 3afe895..be0c5a0 100644 --- a/solarApp/Service/procArchiveHourly.cs +++ b/solarApp/Service/procArchiveHourly.cs @@ -80,6 +80,7 @@ namespace solarApp.Service { var calcPowerStation = new PowerStation(); calcPowerStation.Id = powerStation.Id; + string calcPowerStationTime = ""; #region step2-1. 取得該電站的當前這小時的歷史資料 var table_name = String.Format("s{1}01_station", powerStation.SiteDB, powerStation.Code); @@ -92,53 +93,167 @@ namespace solarApp.Service } else { - // logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); - var history = stationSvc.GetPowerStationHistoryPerHour(dateTime, full_table_name); - // 當前用不到 - // var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 0, dateTime); + //// logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //var history = stationSvc.GetPowerStationHistoryPerHour(dateTime, full_table_name); + //// 當前用不到 + //// var lastmoneyhistorybyhour = await powerStationRepository.GetLastMoneyAndCarbonInHour(powerStation.Id, 0, dateTime); - if (history == null) + //if (history == null) + //{ + // // logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //} + //else + //{ + // // logger.LogInformation("【CalcPowerStationJob】【取得成功電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + // // logger.LogInformation("【CalcPowerStationJob】【電站[{0}]在{1}的每小時歷史資料】 - {2}", powerStation.Code, dateTime, System.Text.Json.JsonSerializer.Serialize(history)); + + // // logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + // 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 發電金額 + + // history.MONEY = history.KWH * powerStation.PowerRate; + // history.TODAYMONEY = history.TodayKWh * powerStation.PowerRate; + // history.TOTALMONEY = history.TotalKWH * powerStation.PowerRate; + + // //即時發電金額 + // switch (powerStation.SolarType) + // { + // case (int)SolarTypeEnum.SelfSold: //自建躉售 + + // //今日發電金額 計算方式:todaykWh * 授電費率 + // calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; + // //calcPowerStation.Today_Money = history.TODAYMONEY; + + // ////總發電金額 計算方式:totalkWh * 授電費率 + // calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; + // //calcPowerStation.Total_Money = history.TOTALMONEY; + // break; + // case (int)SolarTypeEnum.HireSold: //租建躉售 + // //找出該電站的所有土地房屋資訊 + // var landBuildings = stationSvc.GetAllLandBuildingInfoByPowerStationId(powerStation.Id, powerStation.SiteDB); + // var sumLeaseRate = 0.00; + // var avgLeaseRate = 0.00; + + // foreach (var landBuilding in landBuildings) + // { + // sumLeaseRate += landBuilding.LeaseRate; + // } + + // calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; + // calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; + + // break; + // case (int)SolarTypeEnum.SelfUse: //自建自用 + + + // //今日發電金額 計算方式:todaykWh * 授電費率 + // //calcPowerStation.Today_Money = history.TODAYMONEY; + // calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; + + // //總發電金額 計算方式:totalkWh * 授電費率 + // //calcPowerStation.Total_Money = history.TOTALMONEY; + // calcPowerStation.Total_Money = 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(stationSvc.GetOneVariableByName("CarbonRate")); + + // history.CARBON = history.KWH * carbonRate; + // history.TODAYCARBON = history.TodayKWh * carbonRate; + // history.TOTALCARBON = history.TotalKWH * carbonRate; + + + // //今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值] + // //calcPowerStation.Today_Carbon = history.TODAYCARBON; + // calcPowerStation.Today_Carbon = history.TodayKWh * carbonRate; + + // //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] + // //calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; + // calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; + + // #endregion + + // #region 發電時間 + // calcPowerStation.SolarHour = history.SolarHour; + // #endregion + // #endregion + + // powerStationHistoriesHour.Add(history); + + // // logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //} + + #region 計算單一電站每小時發電量、發電金額等資料 for calcPowerStation + //logger.LogInformation("【CalcPowerStationJob】【開始取得電站[{0}]在{1}的每小時歷史資料 for calcPowerStation】", powerStation.Code, dateTime); + //var dateTimeforPowerStation = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"); + var dateTimeforPowerStation = DateTime.Now.ToString("yyyy-MM-dd"); + var history_for_powerStation = stationSvc.GetPowerStationHistoryPerHourForPowerStation(dateTimeforPowerStation, full_table_name); + + if (history_for_powerStation == null) { - // logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + //logger.LogWarning("【CalcPowerStationJob】【查無電站[{0}]在{1}的每小時歷史資料 for calcPowerStation;發電量、發電金額等資料設定為0】", powerStation.Code, dateTime); + calcPowerStation.kwh = 0; + calcPowerStation.Today_kWh = 0; + calcPowerStation.Total_kWh = 0; + calcPowerStation.Today_Money = 0; + calcPowerStation.Total_Money = 0; + calcPowerStation.Today_kwhkwp = 0; + calcPowerStation.Today_PR = 0; + calcPowerStation.Today_Carbon = 0; + calcPowerStation.Total_Carbon = 0; + calcPowerStation.SolarHour = 0; } else { - // logger.LogInformation("【CalcPowerStationJob】【取得成功電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); - // logger.LogInformation("【CalcPowerStationJob】【電站[{0}]在{1}的每小時歷史資料】 - {2}", powerStation.Code, dateTime, System.Text.Json.JsonSerializer.Serialize(history)); - - // logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); - history.PowerStationId = powerStation.Id; - history.Timestamp = Convert.ToDateTime(history.Timestamp + ":00:00").ToString("yyyy-MM-dd HH:mm:ss"); - - #region 計算單一電站每小時發電量、發電金額等資料 - + //logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的每小時歷史資料 for calcPowerStation】", powerStation.Code, dateTime); #region 發電量 //每小時發電量(直接填寫 - calcPowerStation.kwh = history.KWH; + calcPowerStation.kwh = history_for_powerStation.KWH; //今日發電量(直接填寫 - calcPowerStation.Today_kWh = history.TodayKWh; + calcPowerStation.Today_kWh = history_for_powerStation.TodayKWh; //總發電量(直接填寫 - calcPowerStation.Total_kWh = history.TotalKWH; + calcPowerStation.Total_kWh = history_for_powerStation.TotalKWH; #endregion #region 發電金額 - - history.MONEY = history.KWH * powerStation.PowerRate; - history.TODAYMONEY = history.TodayKWh * powerStation.PowerRate; - history.TOTALMONEY = history.TotalKWH * powerStation.PowerRate; + history_for_powerStation.MONEY = history_for_powerStation.KWH * powerStation.PowerRate; + history_for_powerStation.TODAYMONEY = history_for_powerStation.TodayKWh * powerStation.PowerRate; + history_for_powerStation.TOTALMONEY = history_for_powerStation.TotalKWH * powerStation.PowerRate; //即時發電金額 switch (powerStation.SolarType) { - case (int)SolarTypeEnum.SelfSold: //自建躉售 - - //今日發電金額 計算方式:todaykWh * 授電費率 - calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; - //calcPowerStation.Today_Money = history.TODAYMONEY; + case (int)SolarTypeEnum.SelfSold: //自建躉售 + //今日發電金額 計算方式:todaykWh * 授電費率 + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; ////總發電金額 計算方式:totalkWh * 授電費率 - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; - //calcPowerStation.Total_Money = history.TOTALMONEY; + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate; break; case (int)SolarTypeEnum.HireSold: //租建躉售 //找出該電站的所有土地房屋資訊 @@ -151,61 +266,53 @@ namespace solarApp.Service sumLeaseRate += landBuilding.LeaseRate; } - calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate * sumLeaseRate / 100; + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate * sumLeaseRate / 100; break; case (int)SolarTypeEnum.SelfUse: //自建自用 - //今日發電金額 計算方式:todaykWh * 授電費率 - //calcPowerStation.Today_Money = history.TODAYMONEY; - calcPowerStation.Today_Money = history.TodayKWh * powerStation.PowerRate; + calcPowerStation.Today_Money = history_for_powerStation.TodayKWh * powerStation.PowerRate; //總發電金額 計算方式:totalkWh * 授電費率 - //calcPowerStation.Total_Money = history.TOTALMONEY; - calcPowerStation.Total_Money = history.TotalKWH * powerStation.PowerRate; + calcPowerStation.Total_Money = history_for_powerStation.TotalKWH * powerStation.PowerRate; break; } #endregion #region kWh/kWp - //直接填寫 - calcPowerStation.Today_kwhkwp = history.KWHKWP; + ////直接填寫 + calcPowerStation.Today_kwhkwp = history_for_powerStation.KWHKWP; #endregion #region PR //直接填寫 - calcPowerStation.Today_PR = history.PR; + calcPowerStation.Today_PR = history_for_powerStation.PR; #endregion #region 減碳量 carbonRate = Convert.ToDouble(stationSvc.GetOneVariableByName("CarbonRate")); - - history.CARBON = history.KWH * carbonRate; - history.TODAYCARBON = history.TodayKWh * carbonRate; - history.TOTALCARBON = history.TotalKWH * carbonRate; - + history_for_powerStation.CARBON = history_for_powerStation.KWH * carbonRate; + history_for_powerStation.TODAYCARBON = history_for_powerStation.TodayKWh * carbonRate; + history_for_powerStation.TOTALCARBON = history_for_powerStation.TotalKWH * carbonRate; //今日減碳量( 今日發電量 * (0.554/1000)[抓資料庫值] - //calcPowerStation.Today_Carbon = history.TODAYCARBON; - calcPowerStation.Today_Carbon = history.TodayKWh * carbonRate; + calcPowerStation.Today_Carbon = history_for_powerStation.TodayKWh * carbonRate; //總減碳量(總發電量 * (0.554/1000)[抓資料庫值] - //calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; - calcPowerStation.Total_Carbon = history.TotalKWH * carbonRate; - + calcPowerStation.Total_Carbon = history_for_powerStation.TotalKWH * carbonRate; #endregion #region 發電時間 - calcPowerStation.SolarHour = history.SolarHour; - #endregion + calcPowerStation.SolarHour = history_for_powerStation.SolarHour; #endregion - powerStationHistoriesHour.Add(history); - - // logger.LogInformation("【CalcPowerStationJob】【計算完成電站[{0}]在{1}的每小時歷史資料】", powerStation.Code, dateTime); + #region 取拿到資料的時間 + calcPowerStationTime = history_for_powerStation.Timestamp; + #endregion } + #endregion } #endregion @@ -236,7 +343,16 @@ namespace solarApp.Service { // logger.LogInformation("【CalcPowerStationJob】【開始計算電站[{0}]在{1}的日照計的平均值】", powerStation.Code, dateTime); var pyrheliometerHistory = stationSvc.CalcSensorHistoryPerHour(dateTime, deviceInfos, Convert.ToInt32(SensorTypeEnum.PYR)); - + string dateTimeForCalc; + if (calcPowerStationTime != "") + { + dateTimeForCalc = calcPowerStationTime; + } + else + { + dateTimeForCalc = dateTime; + } + var pyrheliometerHistory_for_calcPowerStation = stationSvc.CalcSensorHistoryPerHour(dateTimeForCalc, deviceInfos, Convert.ToInt32(SensorTypeEnum.PYR)); if (pyrheliometerHistory != null) { calcPowerStation.Today_irradiance = pyrheliometerHistory.Irradiance; @@ -314,7 +430,7 @@ namespace solarApp.Service "SolarHour" }; // logger.LogInformation("【update Power_Station 】【update Power_Station】", dateTime); - //stationSvc.UpdateList(calcPowerStations, power_station_properties); + stationSvc.UpdateList(calcPowerStations, power_station_properties); #endregion //}