This commit is contained in:
b110212000 2021-07-12 16:05:11 +08:00
parent 7a1fcddc3c
commit c320e282b1
12 changed files with 195 additions and 36 deletions

View File

@ -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;
}

View File

@ -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) */;

View File

@ -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

View File

@ -259,7 +259,8 @@ namespace SolarPower.Quartz.Jobs
{
"PowerStationId",
"Timestamp",
"Irradiance"
"Irradiance",
"Temperature"
};
//每日
await powerStationRepository.AddPyrheliometerHistoryDayList(pyrheliometerHistoryDays, pyrheliometer_history_properties);

View File

@ -33,6 +33,7 @@ namespace SolarPower.Quartz.Jobs
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>();
@ -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<string> Temp_history_properties = new List<string>()
{
"PowerStationId",
"TIMESTAMP",
"Temperature"
};
await powerStationRepository.AddTempHistory(TempHistoriesHour, Temp_history_properties);
#endregion
#region step5. calcPowerStations UPDATE power_station

View File

@ -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}" ;

View File

@ -2176,7 +2176,6 @@ namespace SolarPower.Repository.Implement
return count;
}
}
public async Task<List<DeviceInfo>> GetListPyrheliometerByPowerStationId(int powerStationId, string db_name)
{
List<DeviceInfo> result;
@ -2203,6 +2202,7 @@ namespace SolarPower.Repository.Implement
return result;
}
}
//(有新增)
public async Task<List<DeviceInfo>> GetListTempByPowerStationId(int powerStationId, string db_name)
{
List<DeviceInfo> 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<DeviceInfo>(sql, new { PowerStationId = powerStationId })).ToList();
@ -2229,9 +2229,18 @@ namespace SolarPower.Repository.Implement
return result;
}
}
public async Task<PyrheliometerHistory> GetPyrheliometerHistoryPerHour(string dateTime, List<DeviceInfo> deviceInfos)
//(有修改)
public async Task<PyrheliometerHistory> GetPyrheliometerHistoryPerHour(string dateTime, List<DeviceInfo> 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<PyrheliometerHistory>(sql, new { DateTime = dateTime });
}
@ -2291,6 +2300,37 @@ namespace SolarPower.Repository.Implement
return count;
}
}
//(有新增)
public async Task<int> AddTempHistory(List<PyrheliometerHistory> entity, List<string> 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<AvgPyrheliometerHistory> 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);

View File

@ -555,6 +555,30 @@ namespace SolarPower.Repository.Implement
return updateQuery.ToString();
}
/// <summary>
/// 產生Update語句可選擇自己要加入資料表填入想要的WHERE條件
/// </summary>
/// <param name="properties"></param>
/// <param name="table_name"></param>
/// <param name="WHERE"></param>
/// <returns></returns>
protected string GenerateUpdateQueryWithCustomTableAndWHERE(List<string> 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<string> properties, string db_name, string table_name)
{

View File

@ -494,7 +494,9 @@ namespace SolarPower.Repository.Interface
Task<List<PowerstationOption>> GetPowerstationPyrheliometerAsync(string db_name, int stationId);
Task<List<DeviceInfo>> GetListPyrheliometerByPowerStationId(int powerStationId, string db_name);
Task<PyrheliometerHistory> GetPyrheliometerHistoryPerHour(string dateTime, List<DeviceInfo> deviceInfos);
Task<List<DeviceInfo>> GetListTempByPowerStationId(int powerStationId, string db_name);
Task<PyrheliometerHistory> GetPyrheliometerHistoryPerHour(string dateTime, List<DeviceInfo> deviceInfos,int Type);
Task<int> AddTempHistory(List<PyrheliometerHistory> entity, List<string> properties);
Task<int> AddPyrheliometerHistory(List<PyrheliometerHistory> entity, List<string> properties);
Task<AvgPyrheliometerHistory> CalcAvgPyrheliometerHistory30day(string nowDay, int powerStationId);
Task<PyrheliometerHistory> CalcPyrheliometerHistoryDayDataByPowerStationId(string nowDay, int powerStationId);

View File

@ -109,7 +109,7 @@ namespace SolarPower
#region PRkWP 30(2)
services.AddSingleton<CalcAvgPowerStationJob>();
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

View File

@ -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('<tr>' +
'<td>' + val.timestamp + '</td>' +
@ -2002,8 +2007,49 @@
'<td>' + val.pr + '</td>' +
'<td>' + val.temp + '</td>' +
'</tr>');
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('<canvas id="history-kWh"></canvas>');
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('<tr>' + '<td colspan="6" style="text-align:center;">' + '無資料' + '</td>' + '</tr>');
$('#HistoryDetailTable').append('<tr>' + '<td colspan="6" style="text-align:center;">' + '無資料' + '</td>' + '</tr>');

View File

@ -25,31 +25,33 @@
<div class="btn-group" id="js-demo-nesting" role="group" aria-label="Button group with nested dropdown">
@*<button type="button" class="btn btn-secondary waves-effect waves-themed"> &lt; </button>
<div class="btn-group" role="group">
<button type="button" class="btn btn-secondary dropdown-toggle waves-effect waves-themed" data-toggle="dropdown">2021 五月 </button>
<div class="dropdown-menu">
<a class="dropdown-item" href="javascript:void(0)">2021 四月</a>
<a class="dropdown-item" href="javascript:void(0)">2021 三月</a>
<div class="btn-group" role="group">
<button type="button" class="btn btn-secondary dropdown-toggle waves-effect waves-themed" data-toggle="dropdown">2021 五月 </button>
<div class="dropdown-menu">
<a class="dropdown-item" href="javascript:void(0)">2021 四月</a>
<a class="dropdown-item" href="javascript:void(0)">2021 三月</a>
</div>
</div>
</div>
<button type="button" class="btn btn-secondary waves-effect waves-themed"> &gt; </button>*@
<input type="date" class="form-control" id="DateGet"/>
<button type="button" class="btn btn-secondary waves-effect waves-themed"> &gt; </button>*@
<input type="date" class="form-control" id="DateGet" />
<input type="month" class="form-control" id="MonthGet" style="display:none"/>
<input type="month" class="form-control" id="MonthGet" style="display:none" />
<input type="number" class="form-control" min="1900" max="2099" step="1" id="YearGet" style="display:none"/>
<input type="number" class="form-control" min="1900" max="2099" step="1" id="YearGet" style="display:none" />
</div>
</div>
@*<div class="pr-3">
<button type="button" class="btn btn-secondary waves-effect waves-themed">列印報告</button>
</div>*@
<button type="button" class="btn btn-secondary waves-effect waves-themed">列印報告</button>
</div>*@
</div>
<div class="row mb-5">
<div class="card p-3 w-100">
<h5 class="font-weight-bold mb-3 pl-5 pb-3">總結</h5>
@ -65,12 +67,15 @@
</tr>
</thead>
<tbody id="HistoryTotalTable">
</tbody>
</table>
</div>
</div>
<div id="history-kWh-convas-div">
<canvas id="history-kWh"></canvas>
</div>
<div class="row mb-5">
<div class="card p-3 w-100">
<h5 class="font-weight-bold mb-3 pl-5 pb-3">詳細資訊</h5>
@ -86,7 +91,6 @@
</tr>
</thead>
<tbody id="HistoryDetailTable">
</tbody>
</table>
</div>