diff --git a/SolarPower/Controllers/StationOverviewController.cs b/SolarPower/Controllers/StationOverviewController.cs index 0556e85..8f4ee39 100644 --- a/SolarPower/Controllers/StationOverviewController.cs +++ b/SolarPower/Controllers/StationOverviewController.cs @@ -520,7 +520,7 @@ namespace SolarPower.Controllers a.TIMESTAMP = Convert.ToDateTime(a.TIMESTAMP).ToString("yyyy-MM"); break; case 3: - a.TOTALTIME = Convert.ToDateTime(a.TIMESTAMP).ToString("yyyy"); + a.TOTALTIME = ""; a.TIMESTAMP = Convert.ToDateTime(a.TIMESTAMP).ToString("yyyy"); break; } diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index 4ac3873..d8eea0c 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -1041,6 +1041,16 @@ ALTER TABLE `device` ADD COLUMN `InstallDate` timestamp NULL DEFAULT NULL COMMENT '安裝日期' AFTER `ColName`, DROP COLUMN `Remark`, +-- 新增模組溫度計溫度 20210712 +ALTER TABLE `pyrheliometer_history_day` + ADD COLUMN `Temperature` DECIMAL(5,2) NULL DEFAULT NULL COMMENT '每日平均模組溫度計溫度' AFTER `Irradiance`; + +ALTER TABLE `pyrheliometer_history_hour` + ADD COLUMN `Temperature` DECIMAL(5,2) NULL DEFAULT NULL COMMENT '即時模組溫度計溫度' AFTER `Irradiance`; + +ALTER TABLE `pyrheliometer_history_month` + ADD COLUMN `Temperature` DECIMAL(5,2) NULL DEFAULT NULL COMMENT '每月平均模組溫度計溫度' AFTER `Irradiance`; + /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; /*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */; diff --git a/SolarPower/Models/PowerStation.cs b/SolarPower/Models/PowerStation.cs index 7541f84..580ba7a 100644 --- a/SolarPower/Models/PowerStation.cs +++ b/SolarPower/Models/PowerStation.cs @@ -626,12 +626,14 @@ namespace SolarPower.Models.PowerStation public int PowerStationId { get; set; } public string Timestamp { get; set; } public double Irradiance { get; set; } + public double Temperature { get; set; } } public class AvgPyrheliometerHistory { public int PowerStationId { get; set; } public double AvgIrradiance { get; set; } + public double AvgTemperature { get; set; } } public class PyrheliometerHistoryDay diff --git a/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs index 5fd3b72..045b46d 100644 --- a/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcAvgPowerStationJob.cs @@ -259,7 +259,8 @@ namespace SolarPower.Quartz.Jobs { "PowerStationId", "Timestamp", - "Irradiance" + "Irradiance", + "Temperature" }; //每日 await powerStationRepository.AddPyrheliometerHistoryDayList(pyrheliometerHistoryDays, pyrheliometer_history_properties); diff --git a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs index 3302792..a0f2b31 100644 --- a/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs +++ b/SolarPower/Quartz/Jobs/CalcPowerStationJob.cs @@ -33,6 +33,7 @@ namespace SolarPower.Quartz.Jobs List powerStationHistoriesHour = new List(); List pyrheliometerHistoriesHour = new List(); + List TempHistoriesHour = new List(); List inverterHistories = new List(); List calcPowerStations = new List(); @@ -140,12 +141,14 @@ namespace SolarPower.Quartz.Jobs #endregion #region step2-2. 取得該電站的當前這小時的日照度歷史資料 - //1. 找出該電站所有日照計設備(包還共享 + //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); + var pyrheliometerHistory = await powerStationRepository.GetPyrheliometerHistoryPerHour(dateTime, deviceInfos,0); if (pyrheliometerHistory != null) { @@ -156,6 +159,19 @@ namespace SolarPower.Quartz.Jobs 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. 計算該電站所有逆變器每小時值 @@ -204,6 +220,15 @@ namespace SolarPower.Quartz.Jobs }; await powerStationRepository.AddPyrheliometerHistory(pyrheliometerHistoriesHour, pyrheliometer_history_properties); + + List Temp_history_properties = new List() + { + "PowerStationId", + "TIMESTAMP", + "Temperature" + }; + + await powerStationRepository.AddTempHistory(TempHistoriesHour, Temp_history_properties); #endregion #region step5. calcPowerStations UPDATE 到 power_station 資料表 diff --git a/SolarPower/Repository/Implement/OverviewRepository.cs b/SolarPower/Repository/Implement/OverviewRepository.cs index 0b4e46f..63cead5 100644 --- a/SolarPower/Repository/Implement/OverviewRepository.cs +++ b/SolarPower/Repository/Implement/OverviewRepository.cs @@ -527,17 +527,19 @@ namespace SolarPower.Repository.Implement ( SELECT DENSE_RANK() OVER(ORDER BY C.TIMESTAMP) AS ROWID , (DENSE_RANK() OVER(ORDER BY C.TIMESTAMP)) - 1 AS PreROWID, - C.TIMESTAMP,{kwh} AS KWH,C.SOLARHOUR,C.PR,P.Irradiance + C.TIMESTAMP,{kwh} AS KWH,C.SOLARHOUR,C.PR,P.Irradiance,P.Temperature AS Temp FROM {usedb} C - LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') WHERE C.PowerStationId = {post.PowerstationId} GROUP BY C.TIMESTAMP ORDER BY C.TIMESTAMP + LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') + WHERE C.PowerStationId = {post.PowerstationId} GROUP BY C.TIMESTAMP ORDER BY C.TIMESTAMP ) A RIGHT OUTER JOIN ( SELECT DENSE_RANK() OVER(ORDER BY C.TIMESTAMP) AS ROWID , (DENSE_RANK() OVER(ORDER BY C.TIMESTAMP)) - 1 AS PreROWID, - C.TIMESTAMP,{kwh} AS KWH,C.SOLARHOUR,C.PR,P.Irradiance + C.TIMESTAMP,{kwh} AS KWH,C.SOLARHOUR,C.PR,P.Irradiance,P.Temperature AS Temp FROM {usedb} C - LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') WHERE C.PowerStationId = {post.PowerstationId} GROUP BY C.TIMESTAMP ORDER BY C.TIMESTAMP + LEFT JOIN {pyrdb} P ON DATE_FORMAT(P.TIMESTAMP,'{timeGroup}') = DATE_FORMAT(C.TIMESTAMP,'{timeGroup}') + WHERE C.PowerStationId = {post.PowerstationId} GROUP BY C.TIMESTAMP ORDER BY C.TIMESTAMP ) B ON A.ROWID = B.PreROWID ) V {range}" ; diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index ebb35a3..136dd18 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -2176,7 +2176,6 @@ namespace SolarPower.Repository.Implement return count; } } - public async Task> GetListPyrheliometerByPowerStationId(int powerStationId, string db_name) { List result; @@ -2203,6 +2202,7 @@ namespace SolarPower.Repository.Implement return result; } } + //(有新增) public async Task> GetListTempByPowerStationId(int powerStationId, string db_name) { List result; @@ -2212,12 +2212,12 @@ namespace SolarPower.Repository.Implement { var sql = $@"SELECT * FROM {db_name}.device d - WHERE d.PowerStationId = @PowerStationId AND d.`Type` = 'PYR' AND d.Deleted = 0 + WHERE d.PowerStationId = @PowerStationId AND d.`Type` = 'MTR' AND d.Deleted = 0 UNION SELECT d.* FROM {db_name}.sharedevice sd LEFT JOIN {db_name}.device d ON sd.DeviceId = d.Id - WHERE sd.PowerStationId = @PowerStationId AND d.`Type` = 'PYR' AND d.Deleted = 0 + WHERE sd.PowerStationId = @PowerStationId AND d.`Type` = 'MTR' AND d.Deleted = 0 "; result = (await conn.QueryAsync(sql, new { PowerStationId = powerStationId })).ToList(); @@ -2229,9 +2229,18 @@ namespace SolarPower.Repository.Implement return result; } } - - public async Task GetPyrheliometerHistoryPerHour(string dateTime, List deviceInfos) + //(有修改) + public async Task GetPyrheliometerHistoryPerHour(string dateTime, List deviceInfos,int type) { + var typename = ""; + if(type == 1) + { + typename = "Temperature";//1為溫度計 + } + else if (type == 0) + { + typename = "Irradiance";//0為日照計 + } PyrheliometerHistory result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { @@ -2249,7 +2258,7 @@ namespace SolarPower.Repository.Implement sql_per_device.Add(str); } - var sql = @"SELECT a.TIMESTAMP, AVG(a.SENSOR) AS Irradiance FROM(" + string.Join(" UNION ", sql_per_device) + @") a GROUP BY TIMESTAMP"; + var sql = @$"SELECT a.TIMESTAMP, AVG(a.SENSOR) AS {typename} FROM(" + string.Join(" UNION ", sql_per_device) + @") a GROUP BY TIMESTAMP"; result = await conn.QueryFirstOrDefaultAsync(sql, new { DateTime = dateTime }); } @@ -2291,6 +2300,37 @@ namespace SolarPower.Repository.Implement return count; } } + //(有新增) + public async Task AddTempHistory(List entity, List properties) + { + int count; + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + string sql = GenerateUpdateQueryWithCustomTableAndWHERE(properties, "pyrheliometer_history_hour", "PowerStationId = @PowerStationId AND TIMESTAMP = @Timestamp"); + + count = await conn.ExecuteAsync(sql, entity, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + + return count; + } + } public async Task CalcAvgPyrheliometerHistory30day(string nowDay, int powerStationId) { @@ -2329,7 +2369,8 @@ namespace SolarPower.Repository.Implement var sql = $@"SELECT PowerStationId, DATE_FORMAT(p.TIMESTAMP, '%Y-%m-%d') AS TIMESTAMP, - AVG(p.Irradiance) AS AvgIrradiance + AVG(p.Irradiance) AS Irradiance, + AVG(p.Temperature) AS Temperature FROM pyrheliometer_history_hour p WHERE DATE_FORMAT(p.TIMESTAMP, '%Y-%m-%d') = @NowDay AND p.Irradiance != 0 @@ -2388,7 +2429,8 @@ namespace SolarPower.Repository.Implement var sql = $@"SELECT PowerStationId, DATE_FORMAT(p.TIMESTAMP, '%Y-%m') AS TIMESTAMP, - AVG(p.Irradiance) AS Irradiance + AVG(p.Irradiance) AS Irradiance, + AVG(p.Temperature) AS Temperature FROM pyrheliometer_history_hour p WHERE DATE_FORMAT(p.TIMESTAMP, '%Y-%m') = @Month AND p.Irradiance != 0 @@ -2467,10 +2509,11 @@ namespace SolarPower.Repository.Implement try { string sql = @"UPDATE pyrheliometer_history_month SET - Irradiance=@Irradiance + Irradiance=@Irradiance, + Temperature=@Temperature WHERE PowerStationId = @PowerStationId AND TIMESTAMP LIKE CONCAT(@TIMESTAMP, '%') -"; + "; count = await conn.ExecuteAsync(sql, entity, trans); diff --git a/SolarPower/Repository/Implement/RepositoryBase.cs b/SolarPower/Repository/Implement/RepositoryBase.cs index b5f44ff..cd1f6b2 100644 --- a/SolarPower/Repository/Implement/RepositoryBase.cs +++ b/SolarPower/Repository/Implement/RepositoryBase.cs @@ -555,6 +555,30 @@ namespace SolarPower.Repository.Implement return updateQuery.ToString(); } + /// + /// 產生Update語句,可選擇自己要加入資料表,填入想要的WHERE條件 + /// + /// + /// + /// + /// + protected string GenerateUpdateQueryWithCustomTableAndWHERE(List properties, string table_name,string WHERE) + { + var updateQuery = new StringBuilder($"UPDATE {table_name} SET "); + + properties.ForEach(property => + { + if (!property.Equals("Id")) + { + updateQuery.Append($"{property}=@{property},"); + } + }); + + updateQuery.Remove(updateQuery.Length - 1, 1); //remove last comma + updateQuery.Append($" WHERE {WHERE}"); + + return updateQuery.ToString(); + } protected string GenerateUpdateQueryWithCustomDBNameAndTable(List properties, string db_name, string table_name) { diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 6282057..beb608b 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -494,7 +494,9 @@ namespace SolarPower.Repository.Interface Task> GetPowerstationPyrheliometerAsync(string db_name, int stationId); Task> GetListPyrheliometerByPowerStationId(int powerStationId, string db_name); - Task GetPyrheliometerHistoryPerHour(string dateTime, List deviceInfos); + Task> GetListTempByPowerStationId(int powerStationId, string db_name); + Task GetPyrheliometerHistoryPerHour(string dateTime, List deviceInfos,int Type); + Task AddTempHistory(List entity, List properties); Task AddPyrheliometerHistory(List entity, List properties); Task CalcAvgPyrheliometerHistory30day(string nowDay, int powerStationId); Task CalcPyrheliometerHistoryDayDataByPowerStationId(string nowDay, int powerStationId); diff --git a/SolarPower/Startup.cs b/SolarPower/Startup.cs index bf2ae2a..31d6e1c 100644 --- a/SolarPower/Startup.cs +++ b/SolarPower/Startup.cs @@ -109,7 +109,7 @@ namespace SolarPower #region pqӶqBPRBkWP 30饭B(Cѭ2I) services.AddSingleton(); services.AddSingleton( - new JobSchedule(jobType: typeof(CalcAvgPowerStationJob), cronExpression: "0 0 2 * * ?") + new JobSchedule(jobType: typeof(CalcAvgPowerStationJob), cronExpression: "0 0 2 * * ?") //new JobSchedule(jobType: typeof(CalcAvgPowerStationJob), cronExpression: "0/10 * * * * ?") ); #endregion diff --git a/SolarPower/Views/StationOverview/StationOverviewInfo.cshtml b/SolarPower/Views/StationOverview/StationOverviewInfo.cshtml index 76f75d6..0cb00df 100644 --- a/SolarPower/Views/StationOverview/StationOverviewInfo.cshtml +++ b/SolarPower/Views/StationOverview/StationOverviewInfo.cshtml @@ -1985,14 +1985,19 @@ var diffSOLARHOURALL = 0; var irradianceALL = 0; var prALL; - var tempALL; + var tempALL = 0; + var listmonth = new Array(0); + var listkwh = new Array(0); + var listcolor = new Array(0); + var listirradiance = new Array(0); + var color = rgba(1); $.each(rel.data, function (index, val) { TimestampALL = val.totaltime; kwhALL += val.kwh; diffSOLARHOURALL += val.diffSOLARHOUR; irradianceALL += val.irradiance; prALL = val.pr; - tempALL = val.temp; + tempALL += val.temp; if (val.length != 0) { $('#HistoryDetailTable').append('' + '' + val.timestamp + '' + @@ -2002,8 +2007,49 @@ '' + val.pr + '' + '' + val.temp + '' + ''); + listmonth.push(val.timestamp); + listkwh.push(val.kwh); + listcolor.push(color); + listirradiance.push(val.irradiance); } }) + + + tempALL = tempALL / rel.data.length; + $('#history-kWh-convas-div').empty(); + $('#history-kWh-convas-div').append(''); + var ctx_history_kWh = document.getElementById('history-kWh').getContext('2d'); + var myhistorykwh = new Chart(ctx_history_kWh, { + type: 'bar', + data: { + labels: listmonth, + datasets: [{ + backgroundColor: listcolor, + borderColor: listcolor, + borderWidth: 1, + label: '發電量(kWh)', + data: listkwh + }, { + type: 'line', + label: '日照度(kWh/m2)', + fill: false, + data: listirradiance + }] + }, + options: { + title: { + display: true, + text: '發電量(kWh)' + }, + legend: { + display: true, + position: 'bottom' + } + } + }); + + + if (rel.data.length == 0) { $('#HistoryTotalTable').append('' + '' + '無資料' + '' + ''); $('#HistoryDetailTable').append('' + '' + '無資料' + '' + ''); diff --git a/SolarPower/Views/StationOverview/_History.cshtml b/SolarPower/Views/StationOverview/_History.cshtml index 97d8927..37c64ff 100644 --- a/SolarPower/Views/StationOverview/_History.cshtml +++ b/SolarPower/Views/StationOverview/_History.cshtml @@ -25,31 +25,33 @@
@* -
- - - *@ - + *@ + - + - +
@*
- -
*@ + + *@ + +
總結
@@ -65,12 +67,15 @@ -
+
+ +
+
詳細資訊
@@ -86,7 +91,6 @@ -