This commit is contained in:
Kai 2021-08-09 10:46:52 +08:00
commit c92142d4e2
22 changed files with 2782 additions and 1039 deletions

View File

@ -1212,5 +1212,298 @@ namespace SolarPower.Controllers
return apiResult; return apiResult;
} }
public FileResult ExportExcelmaxtable(string post)
{
var postObject = JsonConvert.DeserializeObject<Select_table2>(post);
var workbook = new XSSFWorkbook();
#region excel設定
IFont font12 = workbook.CreateFont();
font12.FontName = "新細明體";
font12.FontHeightInPoints = 12;
ICellStyle style12 = workbook.CreateCellStyle();
style12.SetFont(font12);
style12.Alignment = HorizontalAlignment.Center;
style12.VerticalAlignment = VerticalAlignment.Center;
IFont font12Times = workbook.CreateFont();
font12Times.FontName = "Times New Roman";
font12Times.FontHeightInPoints = 12;
IFont font18 = workbook.CreateFont();
font18.FontName = "新細明體";
font18.FontHeightInPoints = 18;
font18.IsBold = true;
ICellStyle styleTitle18 = workbook.CreateCellStyle();
styleTitle18.SetFont(font18);
styleTitle18.Alignment = HorizontalAlignment.Center;
styleTitle18.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLeft12 = workbook.CreateCellStyle();
styleLeft12.SetFont(font12);
styleLeft12.Alignment = HorizontalAlignment.Left;
styleLeft12.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLine12 = workbook.CreateCellStyle();
styleLine12.SetFont(font12);
styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
styleLine12.VerticalAlignment = VerticalAlignment.Center;
styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
ICellStyle stylein12 = workbook.CreateCellStyle();
stylein12.SetFont(font12Times);
stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
stylein12.VerticalAlignment = VerticalAlignment.Center;
stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
var Formbody = GetMaxForm(postObject);
var sheet = workbook.CreateSheet("綜合報表");
int RowPosition = 0;
int index = 0;
var citycount = Formbody.Result.Data.GroupBy(a => a.CityName).Count();
RowPosition = citycount + 6;
IRow row;
ICell cell;
double kwhkwp = 0;
double kwp = 0;
CellRangeAddress region;
List<CityArray> cityArrays = new List<CityArray>();
foreach (var form in Formbody.Result.Data)
{
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue(form.CityName+form.AreaName);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(form.PowerstationName);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(form.Kwh,2));
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(form.SolarHour,2));
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(form.AvgIrradiance,2));
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(form.AvgPR,2));
cell.CellStyle = styleLine12;
kwp += form.Kwh;
kwhkwp += form.AvgKWHKWP;
RowPosition++;
CityArray cityinfo = new CityArray
{
City = form.CityName,
Count = 1,
Kwh = form.Kwh,
SolarHour = form.SolarHour
};
if( cityArrays.Where(a=>a.City == form.CityName).Count() > 0)
{
var city = cityArrays.Where(a => a.City == form.CityName).FirstOrDefault();
city.Count += cityinfo.Count;
city.Kwh += cityinfo.Kwh;
city.SolarHour += cityinfo.SolarHour;
}
else
{
cityArrays.Add(cityinfo);
}
}
RowPosition = citycount + 6;
RowPosition--;
row = sheet.CreateRow(RowPosition);
index = 0;
List<string> lasthead = new List<string>()
{
"區域",
"電站名稱",
"發電量",
"發電小時",
"平均日照",
"PR"
};
foreach (var head in lasthead)
{
sheet.SetColumnWidth(index, 4 * 160 * 8);
cell = row.CreateCell(index);
cell.SetCellValue(head);
cell.CellStyle = styleLine12;
index++;
}
RowPosition--;
RowPosition -= citycount;
var cityRowPosition = RowPosition;
index = 0;
foreach(var cityArray in cityArrays)
{
index = 0;
row = sheet.CreateRow(cityRowPosition);
cell = row.CreateCell(index);
cell.SetCellValue(cityArray.City);
cell.CellStyle = styleLine12;
region = new CellRangeAddress(cityRowPosition, cityRowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round((cityArray.Kwh / cityArray.Count), 2));
cell.CellStyle = styleLine12;
region = new CellRangeAddress(cityRowPosition, cityRowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round((cityArray.SolarHour / cityArray.Count), 2));
cell.CellStyle = styleLine12;
region = new CellRangeAddress(cityRowPosition, cityRowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
cityRowPosition++;
}
index = 0;
RowPosition--;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("縣市");
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("平均發電量(kWp)");
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("發電時間(小時)");
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
RowPosition = 0;
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("時間");
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
index ++;
cell = row.CreateCell(index);
cell.SetCellValue("發電量");
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("有效日照時數");
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
RowPosition++;
row = sheet.CreateRow(RowPosition);
index = 0;
cell = row.CreateCell(index);
cell.SetCellValue(postObject.Time);
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(kwp,2));
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
index ++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(kwhkwp, 2));
cell.CellStyle = styleLine12;
region = new CellRangeAddress(RowPosition, RowPosition, index, index + 1);
sheet.AddMergedRegion(region);
index++;
cell = row.CreateCell(index);
cell.CellStyle = styleLine12;
var ms = new NpoiMemoryStream
{
AllowClose = false
};
workbook.Write(ms);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
var Datename = postObject.Time.Replace("-", "");
return File(ms, "application/vnd.ms-excel", "FIC太陽能監控平台" + "_" + "綜合報表" + "_" + postObject.Userid + Datename + ".xlsx");
}
} }
} }

View File

@ -14,6 +14,7 @@ namespace SolarPower.Models
public string EndAt { get { return Convert.ToDateTime(endAt).ToString("yyyy-MM-dd"); } set { endAt = value; } } //結束時間 public string EndAt { get { return Convert.ToDateTime(endAt).ToString("yyyy-MM-dd"); } set { endAt = value; } } //結束時間
public int Kwh { get; set; }//購電度數 public int Kwh { get; set; }//購電度數
public float Money { get; set; }//售出金額 public float Money { get; set; }//售出金額
public int Deleted { get; set; }
} }
public class ElectricitySoldRecordTable : ElectricitySoldRecord public class ElectricitySoldRecordTable : ElectricitySoldRecord

View File

@ -85,13 +85,22 @@ namespace SolarPower.Models
public string AreaName { get; set; } public string AreaName { get; set; }
public string PowerstationName { get; set; } public string PowerstationName { get; set; }
public int PowerstationId { get; set; } public int PowerstationId { get; set; }
public int PowerstationType { get; set; }
public double Kwh { get; set; } public double Kwh { get; set; }
public string PowerstationDB { get; set; } public string PowerstationDB { get; set; }
public double SolarHour { get; set; } public double SolarHour { get; set; }
public double AvgIrradiance { get; set; } public double AvgIrradiance { get; set; }
public double AvgPR { get; set; } public double AvgPR { get; set; }
public double AvgKWHKWP { get; set; } public double AvgKWHKWP { get; set; }
public double TodayMoney { get; set; }
}
public class CityArray
{
public string City { get; set; }
public double Kwh { get; set; }
public double SolarHour { get; set; }
public int Count { get; set; }
} }

View File

@ -34,7 +34,7 @@ namespace SolarPower.Repository.Implement
post.Time = post.Time.Replace("-", "~"); post.Time = post.Time.Replace("-", "~");
post.Time = post.Time.Replace("/", "-"); post.Time = post.Time.Replace("/", "-");
var time = post.Time.Split("~"); var time = post.Time.Split("~");
sql = @$"SELECT es.`*`,ps.Name AS PowerStationName FROM electricity_sold_record es sql = @$"SELECT es.*,ps.Name AS PowerStationName FROM electricity_sold_record es
LEFT JOIN power_station ps ON es.PowerstationId = ps.Id LEFT JOIN power_station ps ON es.PowerstationId = ps.Id
WHERE es.StartAt BETWEEN '{time[0]}' AND '{time[1]}' AND es.PowerstationId IN @ids AND es.Deleted = 0"; WHERE es.StartAt BETWEEN '{time[0]}' AND '{time[1]}' AND es.PowerstationId IN @ids AND es.Deleted = 0";
break; break;
@ -43,18 +43,17 @@ namespace SolarPower.Repository.Implement
post.Time = post.Time.Replace("-", "~"); post.Time = post.Time.Replace("-", "~");
post.Time = post.Time.Replace("/", "-"); post.Time = post.Time.Replace("/", "-");
var time1 = post.Time.Split("~"); var time1 = post.Time.Split("~");
sql = @$"SELECT es.`*`,ps.Name AS PowerStationName FROM electricity_sold_record es sql = @$"SELECT es.*,ps.Name AS PowerStationName FROM electricity_sold_record es
LEFT JOIN power_station ps ON es.PowerstationId = ps.Id LEFT JOIN power_station ps ON es.PowerstationId = ps.Id
WHERE es.StartAt BETWEEN '{time1[0]}' AND '{time1[1]}' AND es.PowerstationId IN @ids AND es.Deleted = 0"; WHERE es.StartAt BETWEEN '{time1[0]}' AND '{time1[1]}' AND es.PowerstationId IN @ids AND es.Deleted = 0";
sql = "";
break; break;
case 2: case 2:
sql = @$"SELECT es.`*`,ps.Name AS PowerStationName FROM electricity_sold_record es sql = @$"SELECT es.*,ps.Name AS PowerStationName FROM electricity_sold_record es
LEFT JOIN power_station ps ON es.PowerstationId = ps.Id LEFT JOIN power_station ps ON es.PowerstationId = ps.Id
WHERE DATE_FORMAT(es.StartAt , '%Y-%m') = '{post.Time}' AND es.PowerstationId IN @ids AND es.Deleted = 0"; WHERE DATE_FORMAT(es.StartAt , '%Y-%m') = '{post.Time}' AND es.PowerstationId IN @ids AND es.Deleted = 0";
break; break;
case 3: case 3:
sql = @$"SELECT es.`*`,ps.Name AS PowerStationName FROM electricity_sold_record es sql = @$"SELECT es.*,ps.Name AS PowerStationName FROM electricity_sold_record es
LEFT JOIN power_station ps ON es.PowerstationId = ps.Id LEFT JOIN power_station ps ON es.PowerstationId = ps.Id
WHERE DATE_FORMAT(es.StartAt , '%Y') = '{post.Time}' AND es.PowerstationId IN @ids AND es.Deleted = 0"; WHERE DATE_FORMAT(es.StartAt , '%Y') = '{post.Time}' AND es.PowerstationId IN @ids AND es.Deleted = 0";
break; break;

View File

@ -265,13 +265,15 @@ namespace SolarPower.Repository.Implement
a.Name AS 'AreaName', a.Name AS 'AreaName',
ps.Name AS 'PowerstationName', ps.Name AS 'PowerstationName',
ps.SiteDB AS 'PowerstationDB', ps.SiteDB AS 'PowerstationDB',
ps.SolarType AS 'PowerstationType',
ps.Id as 'PowerStationId',
n.* n.*
FROM power_station ps FROM power_station ps
LEFT JOIN city c ON c.Id = ps.CityId LEFT JOIN city c ON c.Id = ps.CityId
LEFT JOIN area a ON a.Id = ps.AreaId LEFT JOIN area a ON a.Id = ps.AreaId
left JOIN left JOIN
( (
SELECT SUM(a.SolarHour) SolarHour,SUM(a.Kwh) Kwh,AVG(b.PR) AvgPR,AVG(b.KWHKWP) AvgKWHKWP,AVG(c.Irradiance) AvgIrradiance,a.PowerStationId FROM SELECT SUM(a.SolarHour) SolarHour,SUM(a.Kwh) Kwh,AVG(b.PR) AvgPR,AVG(b.KWHKWP) AvgKWHKWP,AVG(c.Irradiance) AvgIrradiance,SUM(b.TodayMoney) as 'TodayMoney',a.PowerStationId FROM
( (
SELECT Max(ps.SOLARHOUR) AS 'SolarHour',MAX(ps.TODAYKWH) 'Kwh' ,DATE_FORMAT(ps.TIMESTAMP,'%Y-%m-%d ') AS 'time', ps.PowerStationId SELECT Max(ps.SOLARHOUR) AS 'SolarHour',MAX(ps.TODAYKWH) 'Kwh' ,DATE_FORMAT(ps.TIMESTAMP,'%Y-%m-%d ') AS 'time', ps.PowerStationId
FROM power_station_history_hour ps FROM power_station_history_hour ps
@ -280,7 +282,7 @@ namespace SolarPower.Repository.Implement
) a ) a
left JOIN left JOIN
( (
SELECT B.PR ,B.KWHKWP, DATE_FORMAT(A.bb,'%Y-%m-%d') time FROM SELECT B.PR ,B.KWHKWP,B.TODAYMONEY AS 'TodayMoney', DATE_FORMAT(A.bb,'%Y-%m-%d') time FROM
( (
SELECT Max(ps.TIMESTAMP) AS bb FROM power_station_history_hour ps SELECT Max(ps.TIMESTAMP) AS bb FROM power_station_history_hour ps
WHERE ps.PowerStationId IN @ids {wheretime} WHERE ps.PowerStationId IN @ids {wheretime}

View File

@ -594,7 +594,7 @@
} }
function Searchform() { function Searchform() {
console.log(selecterd_powerstationId); /* console.log(selecterd_powerstationId);*/
if (searchType == 0 || searchType == 1) { if (searchType == 0 || searchType == 1) {
timerange = $('#DateGettext').val(); timerange = $('#DateGettext').val();
} }

View File

@ -301,6 +301,7 @@
var nowpowerstation = null;//選擇電站 var nowpowerstation = null;//選擇電站
var haveinvertName = []; var haveinvertName = [];
var nowform; var nowform;
var xxx;
$(function () { $(function () {
//#region 預設初始值 //#region 預設初始值
@ -946,8 +947,16 @@
FormType: nowform, FormType: nowform,
PowerStation: selecterd_invert PowerStation: selecterd_invert
} }
if (send_data.FormType != null && send_data.PowerStation.length != 0) { if (send_data.FormType != null && send_data.PowerStation.length != 0 && send_data.FormType != 2) {
window.location = "/StationReport/ExportExcel?post=" + JSON.stringify(send_data); window.location = "/StationReport/ExportExcel?post=" + JSON.stringify(send_data);
} else {
if (send_data.FormType != 2) {
toast_warning("請先選擇電站及報表類型");
}
}
if (send_data.PowerStation.length != 0 && send_data.FormType == 2) {
window.location = "/StationReport/ExportExcelmaxtable?post=" + JSON.stringify(send_data);
} else { } else {
toast_warning("請先選擇電站及報表類型"); toast_warning("請先選擇電站及報表類型");
} }
@ -977,6 +986,12 @@
} }
//下方 //下方
$.each(rel.data, function (index, data) { $.each(rel.data, function (index, data) {
var hirerate = 0;
if (data.powerstationType == 1)
{
hirerate = hire(data.powerstationDB, data.powerstationId);
}
var cityinfo = { var cityinfo = {
city: data.cityName, city: data.cityName,
kwp: data.kwh, kwp: data.kwh,
@ -1010,6 +1025,16 @@
StrInfoBody += "<td>" + data.solarHour.toFixed(2) + "</td>"; StrInfoBody += "<td>" + data.solarHour.toFixed(2) + "</td>";
StrInfoBody += "<td>" + data.avgIrradiance.toFixed(2) + "</td>"; StrInfoBody += "<td>" + data.avgIrradiance.toFixed(2) + "</td>";
StrInfoBody += "<td>" + data.avgPR.toFixed(2) + "</td>"; StrInfoBody += "<td>" + data.avgPR.toFixed(2) + "</td>";
if (showmoney == 1) {
StrInfoBody += "<td>" + data.todayMoney.toFixed(2) + "</td>";
StrInfoBody += "<td>" + (data.todayMoney * hirerate * 0.01).toFixed(2)+ "</td>";
StrInfoBody += "<td>" + data.todayMoney.toFixed(2) + "</td>";
}
StrInfoBody += "</tr>"; StrInfoBody += "</tr>";
kwp += data.kwh; kwp += data.kwh;
kwhkwp += data.avgKWHKWP; kwhkwp += data.avgKWHKWP;
@ -1054,5 +1079,61 @@
nowform = 2; nowform = 2;
}) })
} }
function hire(db, id)
{
var hirerate = 0;
var dataTosent =
{
Sitedb: db,
PowerstationId: id
}
var posturl = "/StationReport/GetHireInfo";
$.ajax({
url: posturl,
type: "POST",//'GET',
dataType: 'json',
data: dataTosent, // 輸入的資料
cache: false,
async: false,
success: function (response) {
$.each(response.data, function (index, value) {
hirerate += Number(value.leaseRate);
})
}
});
//await $.post(posturl, dataTosent, function (rel) {
// if (rel.code != "0000") {
// out_reject();
// toast_error(rel.data.msg);
// return;
// }
//}).promise();
return hirerate;
//return new Promise(function (out_resolve, out_reject) {
// $.post(posturl, dataTosent, function (rel) {
// if (rel.code != "0000") {
// out_reject();
// toast_error(rel.data.msg);
// return;
// }
// $.each(rel.data, function (index, value) {
// hirerate += Number(value.leaseRate);
// })
// out_resolve(hirerate);
// })
//});
}
</script> </script>
} }

View File

@ -2,8 +2,7 @@
<configuration> <configuration>
<connectionStrings> <connectionStrings>
<!--<add name="mySql" connectionString="server=172.16.251.248;database=healthdep;user=idafenweb;password=P@ssw0rd;charset=utf8;" />-->
<!--<add name="mySql" connectionString="server=127.0.0.1;database=healthdep;user=root;password=zxcvb123;charset=utf8;" />-->
<add name="mySql" connectionString="server=60.251.164.103;user=webuser;Database=solar_master;Port=11306;password=FICadmin99;charset='utf8';pooling=true;sslmode=none;" providerName="MySql.Data.MySqlClient" /> <add name="mySql" connectionString="server=60.251.164.103;user=webuser;Database=solar_master;Port=11306;password=FICadmin99;charset='utf8';pooling=true;sslmode=none;" providerName="MySql.Data.MySqlClient" />
<!--<add name="mySql" connectionString="server=localhost;user=root;Database=solar_master;Port=3306;password=zxcvb123;charset='utf8';pooling=true;sslmode=none;" providerName="MySql.Data.MySqlClient" />-->
</connectionStrings> </connectionStrings>
</configuration> </configuration>

View File

@ -87,6 +87,16 @@ namespace solarApp.Model
public int count { get; set; } public int count { get; set; }
} }
public class web_inv_15min
{
public string siteID { get; set; }
public string INVERTERID { get; set; }
public string reportdate { get; set; }
public double KWH { get; set; }
public double TODAYKWH { get; set; }
public double kWhKWP { get; set; }
}
public class web_inv_hour public class web_inv_hour
{ {
public string INVERTERID { get; set; } public string INVERTERID { get; set; }

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace solarApp.Model
{
public class sensor_model
{
public string id { get; set; }
public string SiteID { get; set; }
public string sensorName { get; set; }
public string type { get; set; }
public string DBName { get; set; }
public string TableName { get; set; }
public string colname { get; set; }
}
public class sensor_raw
{
public string SiteID { get; set; }
public string reportdate { get; set; }
public double sensorAvg01 { get; set; }
public double sensorAvg02 { get; set; }
public double sensorAvg03 { get; set; }
public double sensorAvg04 { get; set; }
public double sensorAvg05 { get; set; }
public double sensorAvg06 { get; set; }
}
public class sensor_raw_V2
{
public string SiteID { get; set; }
public string reportdate { get; set; }
public double irrAvg { get; set; }
public double modelTempAvg { get; set; }
public double envTempAvg { get; set; }
public double humidityAvg { get; set; }
public double windAvg { get; set; }
public double dustAvg { get; set; }
}
public class sensor_hour
{
public string SiteID { get; set; }
public string reportdate { get; set; }
public double irrAvg { get; set; }
public double modelTempAvg { get; set; }
public double envTempAvg { get; set; }
public double humidityAvg { get; set; }
public double windAvg { get; set; }
public double dustAvg { get; set; }
public int count { get; set; }
}
}

View File

@ -40,10 +40,12 @@ namespace solarApp.Model
public double PR { get; set; } public double PR { get; set; }
public double SOLARHOUR { get; set; } public double SOLARHOUR { get; set; }
public double kwhkwp { get; set; } public double kwhkwp { get; set; }
//public double MONEY { get; set; } public double MONEY { get; set; }
//public double TODAYMONEY { get; set; } public double todayMoney { get; set; }
//public double CARBON { get; set; } public double totalMoney { get; set; }
//public double TODAYCARBON { get; set; } public double CARBON { get; set; }
public double todayCarbon { get; set; }
public double totalCarbon { get; set; }
} }
public class web_station_day public class web_station_day

View File

@ -17,7 +17,8 @@ namespace solarApp
Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new fmMain()); Application.Run(new fmArchive());
//Application.Run(new fmMain());
} }
} }
} }

View File

@ -67,6 +67,21 @@ namespace solarApp.Service
} }
} }
public List<web_inv_15min> get_web_Inv_15min(string reportDate, string invID, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"select `code` siteID, DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H:%i') reportdate, inverterid, a.KWH, a.TODAYKWH, round(a.kwhkwp, 3) kwhkwp
from inverter_history_15min a join power_station b on a.powerstationID = b.id
where left(`TIMESTAMP`, 10) = '" + reportDate + "' and inverterid = '" + invID + @"' and `code` = @siteID
order by cast(reportdate as datetime)";
List<web_inv_15min> ds = conn.Query<web_inv_15min>(sql, new { siteID = siteID }).AsList<web_inv_15min>();
conn.Close();
return ds;
}
}
/// <summary> /// <summary>
/// web 呈現值 每小時平均值 inverter - hour /// web 呈現值 每小時平均值 inverter - hour
/// </summary> /// </summary>
@ -78,7 +93,7 @@ namespace solarApp.Service
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
string sql = @"select `TIMESTAMP` reportdate, inverterid, KWH, TODAYKWH, TOTALKWH, round(PR, 2) PR string sql = @"select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H:%i') reportdate, inverterid, KWH, TODAYKWH, TOTALKWH, round(PR, 2) PR
from inverter_history_hour where left(`TIMESTAMP`, 10) = '" + reportDate + "' and inverterid = '" + invID + "' "; from inverter_history_hour where left(`TIMESTAMP`, 10) = '" + reportDate + "' and inverterid = '" + invID + "' ";
List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>(); List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>();
conn.Close(); conn.Close();
@ -86,26 +101,26 @@ namespace solarApp.Service
} }
} }
public List<web_inv_hour> get_web_Inv_day(string reportDate, string invID) public List<web_inv_hour> get_web_Inv_day(string date1, string date2, string invID)
{ {
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
string sql = @"select `TIMESTAMP` reportdate, inverterid, KWH, TODAYKWH, TOTALKWH, round(PR, 2) PR string sql = @"select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, inverterid, round(KWH, 2) KWH, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, round(PR, 2) PR
from inverter_history_day where left(`TIMESTAMP`, 10) = '" + reportDate + "' and inverterid = '" + invID + "' "; from inverter_history_day where left(`TIMESTAMP`, 10) between '" + date1 + "' and '"+ date2 +"' and inverterid = '" + invID + "' ";
List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>(); List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>();
conn.Close(); conn.Close();
return ds; return ds;
} }
} }
public List<web_inv_hour> get_web_Inv_month(string reportDate, string invID) public List<web_inv_hour> get_web_Inv_month(string date1, string date2, string invID)
{ {
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
string sql = @"select `TIMESTAMP` reportdate, inverterid, KWH, TODAYKWH, TOTALKWH, round(PR, 2) PR string sql = @"select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, inverterid, KWH, TODAYKWH, TOTALKWH, round(PR, 2) PR
from inverter_history_month where left(`TIMESTAMP`, 7) = '" + reportDate + "' and inverterid = '" + invID + "' "; from inverter_history_month where left(`TIMESTAMP`, 7) between '" + date1 + "' and '" + date2 + "' and inverterid = '" + invID + "' ";
List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>(); List<web_inv_hour> ds = conn.Query<web_inv_hour>(sql).AsList<web_inv_hour>();
conn.Close(); conn.Close();
return ds; return ds;

View File

@ -0,0 +1,220 @@
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Dapper;
using solarApp.Model;
using System.Configuration;
namespace solarApp.Service
{
public class getSensorSvc
{
string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
/// <summary>
/// 電站 Raw Data
/// </summary>
/// <param name="reportDate"></param>
/// <param name="siteDB"></param>
/// <param name="siteID"></param>
/// <returns></returns>
public List<sensor_raw> get_sensor_raw(string reportDate, string siteDB, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"select siteid , FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, round(sensorAvg01, 3) sensorAvg01, round(sensorAvg02, 3) sensorAvg02,
round(sensorAvg03, 3) sensorAvg03, round(sensorAvg04, 3) sensorAvg04, round(sensorAvg05, 3) sensorAvg05, round(sensorAvg06, 3) sensorAvg06
from " + siteDB + ".s" + siteID + @"_sensorAvg
where left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10) = @reportDate";
List<sensor_raw> ds = conn.Query<sensor_raw>(sql, new { reportDate = reportDate }).AsList<sensor_raw>();
conn.Close();
return ds;
}
}
/// <summary>
/// Sensor 欄位需要平均時 欄位串接 (s1 + s2) / 2
/// </summary>
/// <param name="lstData"></param>
/// <returns></returns>
string ConcatColumn(List<sensor_model> lstData)
{
string ss = string.Empty; string result = string.Empty;
if (lstData.Count > 0)
{
foreach (var item in lstData)
{
ss += (ss == string.Empty) ? item.colname : "+" + item.colname; // 2 個以上欄位需要相加
}
if (lstData.Count >= 2 && lstData[0].type == "PYR") result = "round( avg( " + ss + ") / " + lstData.Count.ToString() + " , 2) "; //日照計 2個以上 需要除以個數 (平均值)
else
result = "round( avg(" + ss + " ), 2) ";
}
else result = "0";
return result;
}
public List<sensor_raw_V2> get_sensor_raw_hour(string reportDate, string siteDB, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
// init 關閉 group full column
string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));";
conn.Execute(ss);
#region Sensor
string sql = @"select a.id, CONCAT( left(UID, 9) ,'01') SiteID, a.`name` sensorName, type, DBName, TableName, colname
from " + siteDB + @".device a
where left(UID, 11) = @siteID";
List<sensor_model> ds_sensor = conn.Query<sensor_model>(sql, new { siteID = siteID}).AsList<sensor_model>();
//欄位處理
// { "Type":[
// { "Name":"日照計","EName":"PYR"},
// { "Name":"模組溫度計","EName":"MTR"},
// { "Name":"環境溫度計","EName":"ETR"},
// { "Name":"環境濕度計","EName":"EMM"},
// { "Name":"風速計","EName":"VAN"},
// { "Name":" 電表","EName":"PWR"}]}
var irrlst = ds_sensor.FindAll(x => x.type.Contains("PYR"));
var modelTemplst = ds_sensor.FindAll(x => x.type.Contains("MTR"));
var envTemplst = ds_sensor.FindAll(x => x.type.Contains("ETR"));
var humlst = ds_sensor.FindAll(x => x.type.Contains("EMM"));
var windlst = ds_sensor.FindAll(x => x.type.Contains("VAN"));
var dustlst = ds_sensor.FindAll(x => x.type.Contains("DST")); //需要新增於DB
//var meterlst = ds_sensor.FindAll(x => x.type.Contains("PWR")); 電錶暫不處理
string irrCol = string.Empty; string modelTempCol = string.Empty; string evnTempCol = string.Empty; string humCol = string.Empty; string windCol = string.Empty; string meterCol = string.Empty; string dustCol = string.Empty;
irrCol = ConcatColumn(irrlst);//日照計
modelTempCol = ConcatColumn(modelTemplst);
evnTempCol = ConcatColumn(envTemplst);
humCol = ConcatColumn(humlst);
windCol = ConcatColumn(windlst);
dustCol = ConcatColumn(dustlst);
string irrNot0 = string.Empty; // and 日照1 <> 0 and 日照2 <> 0
# region 0
if (irrlst.Count > 0)
{
foreach (var item in irrlst)
irrNot0 += " and " + item.colname + " <> 0 "; // and S1 <> 0 and S2 <> 0
}
#endregion
//電表
#endregion Sensor
sql = @"select a.siteID, a.reportdate, a.modelTempAvg, ifnull(b.irrAvg, 0) irrAvg, a.envTempAvg, humidityAvg, windAvg, dustAvg from
(
select @siteID siteID, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, "+ modelTempCol+ @" modelTempAvg
," + evnTempCol + @" envTempAvg, " + humCol + @" humidityAvg, " + windCol + @" windAvg, " + dustCol + @" dustAvg
from " + siteDB + ".s" + siteID + @"_sensorAvg
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H')
) a left join
(
select FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate , " + irrCol + @" irrAvg
from " + siteDB + ".s" + siteID + @"_sensorAvg
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate "+ irrNot0 + @" # 0
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H')
)b on a.reportdate = b.reportdate";
List<sensor_raw_V2> ds = conn.Query<sensor_raw_V2>(sql, new { siteID = siteID, reportDate = reportDate }).AsList<sensor_raw_V2>();
conn.Close();
return ds;
}
}
/// <summary>
/// web 呈現值 station - hour
/// </summary>
/// <param name="reportDate"></param>
/// <returns></returns>
public List<sensor_hour> get_web_sensor_hour(string reportDate, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, round(Irradiance, 2) irrAvg, round(Temperature, 2) modelTempAvg
from sensor_history_hour a join power_station b on a.PowerStationId = b.id
where left(`TIMESTAMP`, 10) = '" + reportDate + "' and b.`code` = @siteID";
List<sensor_hour> ds = conn.Query<sensor_hour>(sql, new {siteID = siteID }).AsList<sensor_hour>();
conn.Close();
return ds;
}
}
public List<sensor_hour> get_web_sensor_day(string date1, string date2, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, round(Irradiance, 2) irrAvg, round(Temperature, 2) modelTempAvg
from sensor_history_day a join power_station b on a.PowerStationId = b.id
where left(`TIMESTAMP`, 10) between '" + date1 + "' and '"+date2+"' and b.`code` = @siteID";
List<sensor_hour> ds = conn.Query<sensor_hour>(sql, new { siteID = siteID }).AsList<sensor_hour>();
conn.Close();
return ds;
}
}
public List<sensor_hour> get_web_sensor_month(string date1, string date2, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, round(Irradiance, 2) irrAvg, round(Temperature, 2) modelTempAvg
from sensor_history_month a join power_station b on a.PowerStationId = b.id
where left(`TIMESTAMP`, 7) between '" + date1 + "' and '" + date2 + "' and b.`code` = @siteID";
List<sensor_hour> ds = conn.Query<sensor_hour>(sql, new { siteID = siteID}).AsList<sensor_hour>();
conn.Close();
return ds;
}
}
public List<sensor_raw> get_web_sensor50_hour(string reportDate, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, sensorAVG01, sensorAVG02, sensorAVG03, sensorAVG04, sensorAVG05, sensorAVG06
from sensoravg_history_hour a join power_station b on a.PowerStationId = b.id
where left(`TIMESTAMP`, 10) = '" + reportDate + "' and b.`code` = @siteID";
List<sensor_raw> ds = conn.Query<sensor_raw>(sql, new { siteID = siteID }).AsList<sensor_raw>();
conn.Close();
return ds;
}
}
public List<sensor_raw> get_web_sensor50_day(string date1, string date2, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, sensorAVG01, sensorAVG02, sensorAVG03, sensorAVG04, sensorAVG05, sensorAVG06
from sensoravg_history_day a join power_station b on a.PowerStationId = b.id
where left(`TIMESTAMP`, 10) between @date1 and @date2 and b.`code` = @siteID";
List<sensor_raw> ds = conn.Query<sensor_raw>(sql, new { siteID = siteID, date1 = date1, date2 = date2 }).AsList<sensor_raw>();
conn.Close();
return ds;
}
}
public List<sensor_raw> get_web_sensor50_month(string date1, string date2, string siteID)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, b.`code` siteid, sensorAVG01, sensorAVG02, sensorAVG03, sensorAVG04, sensorAVG05, sensorAVG06
from sensoravg_history_month a join power_station b on a.PowerStationId = b.id
where left(`TIMESTAMP`, 7) between @date1 and @date2 and b.`code` = @siteID";
List<sensor_raw> ds = conn.Query<sensor_raw>(sql, new { siteID = siteID, date1 = date1, date2 = date2 }).AsList<sensor_raw>();
conn.Close();
return ds;
}
}
}
}

View File

@ -29,7 +29,7 @@ namespace solarApp.Service
string sql = @"select id , FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, siteid, round(KWH, 3) KWH, round(TODAYKWH, 3) TODAYKWH, string sql = @"select id , FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, siteid, round(KWH, 3) KWH, round(TODAYKWH, 3) TODAYKWH,
round(TOTALKWH, 3)TOTALKWH, round(PR, 3) PR, round(TODAYKWH, 3)SOLARHOUR , round(kwhkwp, 3) kwhkwp round(TOTALKWH, 3)TOTALKWH, round(PR, 3) PR, round(TODAYKWH, 3)SOLARHOUR , round(kwhkwp, 3) kwhkwp
from " + siteDB + ".s" + siteID + @"_station from " + siteDB + ".s" + siteID + @"_station
where left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10) = @reportDate"; where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @reportDate";
List<raw_statino> ds = conn.Query<raw_statino>(sql, new { reportDate = reportDate }).AsList<raw_statino>(); List<raw_statino> ds = conn.Query<raw_statino>(sql, new { reportDate = reportDate }).AsList<raw_statino>();
conn.Close(); conn.Close();
return ds; return ds;
@ -69,11 +69,11 @@ namespace solarApp.Service
// from v_station_temp // from v_station_temp
// where left(reportdate, 10) between @date1 and @date2 // where left(reportdate, 10) between @date1 and @date2
// group by siteid, left(reportdate, 10)"; // group by siteid, left(reportdate, 10)";
string sql = @" select siteid, left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10) reportdate, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH, string sql = @" select siteid, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') reportdate, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH,
round((max(PR)), 2) PR, round((max(SOLARHOUR)), 2) SOLARHOUR, round((max(KWHKWP)), 2) KWHKWP, count(*) count round((max(PR)), 2) PR, round((max(SOLARHOUR)), 2) SOLARHOUR, round((max(KWHKWP)), 2) KWHKWP, count(*) count
from " + siteDB+ ".s"+ siteID + @"_station from " + siteDB+ ".s"+ siteID + @"_station
where left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10) between @date1 and @date2 where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2
group by siteid, left(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i'), 10)"; group by siteid, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d')";
List<raw_station_day> ds = conn.Query<raw_station_day>(sql, new { date1 = date1, date2 = date2 }).AsList<raw_station_day>(); List<raw_station_day> ds = conn.Query<raw_station_day>(sql, new { date1 = date1, date2 = date2 }).AsList<raw_station_day>();
conn.Close(); conn.Close();
return ds; return ds;
@ -87,15 +87,17 @@ namespace solarApp.Service
/// </summary> /// </summary>
/// <param name="reportDate"></param> /// <param name="reportDate"></param>
/// <returns></returns> /// <returns></returns>
public List<web_station_hour> get_web_station_hour(string reportDate) public List<web_station_hour> get_web_station_hour(string reportDate, string siteID)
{ {
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, siteid, round(KWH, 2) KWH, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H') reportdate, siteid, round(KWH, 2) KWH, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH,
round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, round(money, 3) money, round(todaymoney, 3) todaymoney, round(totalmoney, 3) totalmoney,
from power_station_history_hour where left(`TIMESTAMP`, 10) = '" + reportDate + "' "; round(carbon, 3) carbon, round(todayCarbon, 3) todayCarbon, round(totalCarbon, 3) totalCarbon
List<web_station_hour> ds = conn.Query<web_station_hour>(sql).AsList<web_station_hour>(); from power_station_history_hour
where siteID = @siteID and left(`TIMESTAMP`, 10) = '" + reportDate + "' ";
List<web_station_hour> ds = conn.Query<web_station_hour>(sql, new { siteID = siteID.Substring(0, 9) }).AsList<web_station_hour>();
conn.Close(); conn.Close();
return ds; return ds;
} }
@ -107,28 +109,28 @@ namespace solarApp.Service
/// <param name="reportDate"></param> /// <param name="reportDate"></param>
/// <param name="invID"></param> /// <param name="invID"></param>
/// <returns></returns> /// <returns></returns>
public List<web_station_hour> get_web_station_day(string date1, string date2) public List<web_station_hour> get_web_station_day(string date1, string date2, string siteID)
{ {
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, siteid, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, siteid, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH,
round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money
from power_station_history_day where left(`TIMESTAMP`, 10) between @date1 and @date2 "; from power_station_history_day where left(`TIMESTAMP`, 10) between @date1 and @date2 and siteid = @siteID";
List<web_station_hour> ds = conn.Query<web_station_hour>(sql, new { date1 = date1, date2 = date2 }).AsList<web_station_hour>(); List<web_station_hour> ds = conn.Query<web_station_hour>(sql, new { date1 = date1, date2 = date2 , siteID = siteID}).AsList<web_station_hour>();
conn.Close(); conn.Close();
return ds; return ds;
} }
} }
public List<web_station_hour> get_web_station_month(string date1, string date2) public List<web_station_hour> get_web_station_month(string date1, string date2, string siteID)
{ {
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, siteid, round(TOTALKWH, 2) TOTALKWH, round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money string sql = @" select DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, siteid, round(TOTALKWH, 2) TOTALKWH, round(PR, 3) PR, round(KWHKWP, 3) KWHKWP, money
from power_station_history_month where left(`TIMESTAMP`, 7) between @date1 and @date2 "; from power_station_history_month where left(`TIMESTAMP`, 7) between @date1 and @date2 and siteid = @siteID";
List<web_station_hour> ds = conn.Query<web_station_hour>(sql, new { date1 = date1, date2 = date2 }).AsList<web_station_hour>(); List<web_station_hour> ds = conn.Query<web_station_hour>(sql, new { date1 = date1, date2 = date2, siteID = siteID }).AsList<web_station_hour>();
conn.Close(); conn.Close();
return ds; return ds;
} }

View File

@ -12,211 +12,240 @@ namespace solarApp.Service
public class procInvSvc public class procInvSvc
{ {
string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
/// <summary>
/// 每小時平均值 inverter 原始資料 - hour public string _siteID { get; set; }
/// </summary> public string _siteDB { get; set; }
/// <param name="reportDate"></param> public string _siteID01 { get; set; }
/// <param name="invID"></param> public string _date1 { get; set; }
/// <returns></returns> public string _date2 { get; set; }
public List<raw_inv_hour> proc_Inv_hour(string reportDate) public string _powerStationID { get; set; }
public bool clearData()
{
bool result = false;
try
{
#region PowerStationID
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID";
var ds = conn.Query<string>(sql, new { siteID = _siteID }).AsList<string>();
_powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0";
conn.Close();
}
#endregion
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"
delete from inverter_history_15min where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1;
delete from inverter_history_hour where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1;
delete from inverter_history_day where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1;
delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;";
var ds = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID });
conn.Close();
}
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
public bool archiveData()
{
bool result = false;
try
{
clearData();
insert_inv();
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
bool insert_inv()
{
bool result = false;
try
{ {
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
//string sql = @" select a.reportdate, a.inverterid, a.KWH, b.TODAYKWH, b.TOTALKWH, b.PR, a.count from string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));";
// ( conn.Execute(ss);
// select left(reportdate, 13)reportdate, inverterid
// ,round((sum(WH)/1000), 2) KWH, count(*) count
// from v_inv_temp
// where left(reportdate, 10) = '" + reportDate + "' and inverterid = '" + invID + @"'
// group by left(reportdate, 13), inverterid #region 15 min
// )a join (
// select reportdate, round(TODAYKWH, 2) TODAYKWH, round(TOTALKWH, 2) TOTALKWH, inverterid, round(PR, 2) PR
// from v_inv_temp
// where left(reportdate, 10) = '" + reportDate + @"' and right(reportdate, 2) = '55' and inverterid = '" + invID + @"'
// )b on a.reportdate = left(b.reportdate, 13) and a.inverterid = b.inverterid ";
string sql = @" string sql = @"
INSERT INTO `solar_master`.`inverter_history_hour`(`PowerStationId`, `INVERTERID`, `TIMESTAMP`, Irradiance, `AC1V`, `AC1A`, `AC1W`, `AC1F`, `AC1WH`, `AC2V`, `AC2A`, `AC2W`, `AC2F`, `AC2WH`, INSERT INTO solar_master.inverter_history_15min( PowerStationId, TIMESTAMP, INVERTERID, KWH, TODAYKWH, KWHKWP)
`AC3V`, `AC3A`, `AC3W`, `AC3F`, `AC3WH`, `DC1V`, `DC1A`, `DC1W`, `DC1WH`, `DC2V`, `DC2A`, `DC2W`, `DC2WH`, `DC3V`, `DC3A`, `DC3W`, select PowerStationId, reportdate, a.inverterid, KWH, TODAYKWH, round((kwh/capacity), 3) kwpkwp from
`DC3WH`, `DC4V`, `DC4A`, `DC4W`, `DC4WH`, `DC5V`, `DC5A`, `DC5W`, `DC5WH`, `PR`, `RA1`, `RA2`, `RA3`, `RA4`, `RA5`, `ACKW`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`) (
SELECT 1 powerStationID, a.INVERTERID, concat(a.reportDate,':00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00:00')reportdate, inverterid,
round((sum(WH)/1000), 3) KWH, round(max(TODAYKWH), 3) TODAYKWH
from " + _siteDB + ".s" + _siteID01 + @"_inv
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '00' and '10'
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid
union
select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':15:00')reportdate, inverterid,
round((sum(WH)/1000), 3) KWH, round(max(TODAYKWH), 3) TODAYKWH
from " + _siteDB + ".s" + _siteID01 + @"_inv
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '15' and '25'
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid
union
select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':30:00')reportdate, inverterid,
round((sum(WH)/1000), 3) KWH, round(max(TODAYKWH), 3) TODAYKWH
from " + _siteDB + ".s" + _siteID01 + @"_inv
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '30' and '40'
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid
union
select @PowerStationId PowerStationId, CONCAT(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':45:00')reportdate, inverterid,
round((sum(WH)/1000), 3) KWH, round(max(TODAYKWH), 3) TODAYKWH
from " + _siteDB + ".s" + _siteID01 + @"_inv
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '45' and '55'
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid
) a join " + _siteDB + @".inverter b on a.inverterid = b.inverterid";
var ds = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID });
#endregion
#region hour
sql = @"
insert into solar_master.inverter_history_hour (PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH,
AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W, DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH,
PR, RA1, RA2, RA3, RA4, RA5, DCKW, ACKW, KWH, TODAYKWH, TOTALKWH, KWHKWP)
SELECT @powerstationID powerStationID, a.INVERTERID, a.reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH,
a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W,
a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, inv_pr.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, a.KWH, a.TODAYKWH, a.TOTALKWH, a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH,
i.Capacity, (a.KWH / i.Capacity) AS KWHKWP inv_pr.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, ((DC1W + DC2W + DC3W + DC4W+ DC5W) / 1000) DCKW,
from ((AC1W + AC2W + AC3W) / 1000) ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH, (a.KWH / i.Capacity) AS KWHKWP
( -- KWH from
SELECT FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') AS reportDate, a.INVERTERID, ( -- KWH
AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, SELECT concat(FROM_UNIXTIME(a.TIMESTAMP/1000,'%Y-%m-%d %H'), ':00:00') reportDate, a.INVERTERID,
AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V,
AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A,
SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W,
SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V,
AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W,
SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH,
SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V,
AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH,
AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W,
SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A,
SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, #inv_pr.PR AS PR,
AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3,
AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, # a.KWH, AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH
MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH FROM " + _siteDB + ".s" + _siteID01 + @"_inv a
FROM s02202000101_inv a WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = @date1
WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04'
GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d %H'), 13), a.INVERTERID GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d %H'), 13), a.INVERTERID
) a ) a
LEFT JOIN( -- PR LEFT JOIN( -- PR
SELECT FROM_UNIXTIME(inv.TIMESTAMP / 1000, '%Y-%m-%d %H') AS reportDate, inv.INVERTERID, inv.PR SELECT concat(FROM_UNIXTIME(inv.TIMESTAMP/1000,'%Y-%m-%d %H'), ':00:00') AS reportDate, inv.INVERTERID, inv.PR
FROM s02202000101_inv inv FROM " + _siteDB + ".s" + _siteID01 + @"_inv inv
WHERE left(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 10) = '2021-08-04' WHERE left(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 10) = @date1
and right(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 2) = '55' and right(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 2) = '55'
GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/ 1000, '%Y-%m-%d %H'), inv.INVERTERID GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/ 1000, '%Y-%m-%d %H'), inv.INVERTERID
) inv_pr ) inv_pr
ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID
-- --
LEFT JOIN solar_com0002.inverter i ON a.INVERTERID = i.InverterId LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId
-- ---------------------- -- ---------------------
left join ( left join (
select FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') AS reportDate, round(avg(SENSORAVG01),3) Irradiance select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d %H'), ':00:00') AS reportDate, Irradiance
from s02202000101_sensoravg a from sensor_history_hour a
where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0 where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1
group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H') )irr on a.reportDate = irr.reportDate
)irr on a.reportDate = irr.reportDate ;"; order by a.INVERTERID, a.reportDate;";
List<raw_inv_hour> ds = conn.Query<raw_inv_hour>(sql).AsList<raw_inv_hour>(); int ct = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID });
conn.Close();
return ds;
}
}
public List<raw_inv_hour> proc_Inv_day(string reportDate) #endregion hour
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"
INSERT INTO `solar_master`.`inverter_history_day`(`PowerStationId`, `INVERTERID`, `TIMESTAMP`, Irradiance, `AC1V`, `AC1A`, `AC1W`, `AC1F`, `AC1WH`, `AC2V`, `AC2A`, `AC2W`, `AC2F`, `AC2WH`,
`AC3V`, `AC3A`, `AC3W`, `AC3F`, `AC3WH`, `DC1V`, `DC1A`, `DC1W`, `DC1WH`, `DC2V`, `DC2A`, `DC2W`, `DC2WH`, `DC3V`, `DC3A`, `DC3W`,
`DC3WH`, `DC4V`, `DC4A`, `DC4W`, `DC4WH`, `DC5V`, `DC5A`, `DC5W`, `DC5WH`, `PR`, `RA1`, `RA2`, `RA3`, `RA4`, `RA5`, `ACKW`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`)
SELECT 1 powerStationID, a.INVERTERID, concat(a.reportDate,':00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH, #region day
sql = @"
INSERT INTO solar_master.inverter_history_day(PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH,
AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W,
DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, PR, RA1, RA2, RA3, RA4, RA5, `DCKW`, `ACKW`, KWH, TODAYKWH, TOTALKWH, KWHKWP)
SELECT @powerStationID powerStationID, a.INVERTERID, concat(a.reportDate,' 00:00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH,
a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W, a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W,
a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, a.KWH, a.TODAYKWH, a.TOTALKWH, a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5,
i.Capacity, (a.KWH / i.Capacity) AS KWHKWP DCKW, ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH, (a.KWH / i.Capacity) AS KWHKWP
from from
( -- KWH ( -- KWH
SELECT FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d') AS reportDate, a.INVERTERID, SELECT DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') AS reportDate, a.INVERTERID,
AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V,
AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V, AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A,
AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W,
SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A, SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V,
SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W,
AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W, SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, max(PR) AS PR,
SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, avg(DCKW) as DCKW, avg(ACKW) as ACKW,
SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V, MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, SUM(a.KWH) AS KWH
AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, FROM solar_master.inverter_history_hour a
AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W, WHERE powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') = @date1
SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, GROUP BY DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d'), a.INVERTERID
SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, #inv_pr.PR AS PR, ) a
AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId
AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, # a.KWH, -- ----------------------
AVG(a.PR) as PR, left join (
MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, (SUM(a.WH)/1000) AS KWH select DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') AS reportDate, Irradiance
FROM solar_com0002.s02202000101_inv a from sensor_history_day a
WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' where powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m-%d') = @date1 and Irradiance <> 0 limit 1
GROUP BY left(FROM_UNIXTIME(a.TIMESTAMP/ 1000, '%Y-%m-%d'), 10), a.INVERTERID )irr on a.reportDate = irr.reportDate ;";
) a conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID });
-- LEFT JOIN( -- PR #endregion day
-- SELECT FROM_UNIXTIME(inv.TIMESTAMP / 1000, '%Y-%m-%d') AS reportDate, inv.INVERTERID, inv.PR
-- FROM s02202000101_inv inv #region month
-- WHERE left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' sql = @"
-- and right(DATE_FORMAT(FROM_UNIXTIME(inv.TIMESTAMP / 1000), '%Y-%m-%d %H:%i'), 2) = '55' delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;
-- GROUP BY FROM_UNIXTIME(inv.TIMESTAMP/ 1000, '%Y-%m-%d %H'), inv.INVERTERID
-- ) inv_pr INSERT INTO solar_master.inverter_history_month (PowerStationId, INVERTERID, TIMESTAMP, Irradiance, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH,
-- ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID AC3V, AC3A, AC3W, AC3F, AC3WH, DC1V, DC1A, DC1W, DC1WH, DC2V, DC2A, DC2W, DC2WH, DC3V, DC3A, DC3W,
-- DC3WH, DC4V, DC4A, DC4W, DC4WH, DC5V, DC5A, DC5W, DC5WH, PR, RA1, RA2, RA3, RA4, RA5, `DCKW`, `ACKW`, KWH, TODAYKWH, TOTALKWH, KWHKWP)
LEFT JOIN solar_com0002.inverter i ON a.INVERTERID = i.InverterId
-- ---------------------- SELECT @powerStationID powerStationID, a.INVERTERID, concat(a.reportDate,'-01 00:00:00') reportDate, IFNULL(Irradiance, 0), a.AC1V, a.AC1A, a.AC1W, a.AC1F, a.AC1WH, a.AC2V, a.AC2A, a.AC2W, a.AC2F, a.AC2WH,
left join ( a.AC3V, a.AC3A, a.AC3W, a.AC3F, a.AC3WH, a.DC1V, a.DC1A, a.DC1W, a.DC1WH, a.DC2V, a.DC2A, a.DC2W, a.DC2WH, a.DC3V, a.DC3A, a.DC3W,
select FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d') AS reportDate, round(avg(SENSORAVG01),3) Irradiance a.DC3WH, a.DC4V, a.DC4A, a.DC4W, a.DC4WH, a.DC5V, a.DC5A, a.DC5W, a.DC5WH, a.PR, a.RA1, a.RA2, a.RA3, a.RA4, a.RA5, DCKW, ACKW, a.KWH, a.TODAYKWH, a.TOTALKWH,
from s02202000101_sensoravg a (a.KWH / i.Capacity) AS KWHKWP
where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0 from
group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d') ( -- KWH
)irr on a.reportDate = irr.reportDate ;"; SELECT DATE_FORMAT(a.TIMESTAMP, '%Y-%m') AS reportDate, a.INVERTERID,
List<raw_inv_hour> ds = conn.Query<raw_inv_hour>(sql).AsList<raw_inv_hour>(); AVG(a.AC1V) AS AC1V, AVG(a.AC1A) AS AC1A, SUM(a.AC1W) AS AC1W, AVG(a.AC1F) AS AC1F, SUM(a.AC1WH) AS AC1WH, AVG(a.AC2V) AS AC2V,
AVG(a.AC2A) AS AC2A, SUM(a.AC2W) AS AC2W, AVG(a.AC2F) AS AC2F, SUM(a.AC2WH) AS AC2WH,AVG(a.AC3V) AS AC3V, AVG(a.AC3A) AS AC3A,
SUM(a.AC3W) AS AC3W, AVG(a.AC3F) AS AC3F, SUM(a.AC3WH) AS AC3WH, AVG(a.DC1V) AS DC1V, AVG(a.DC1A) AS DC1A, SUM(a.DC1W) AS DC1W,
SUM(a.DC1WH) AS DC1WH, AVG(a.DC2V) AS DC2V, AVG(a.DC2A) AS DC2A, SUM(a.DC2W) AS DC2W, SUM(a.DC2WH) AS DC2WH, AVG(a.DC3V) AS DC3V,
AVG(a.DC3A) AS DC3A, AVG(a.DC3W) AS DC3W, AVG(a.DC3WH) AS DC3WH, AVG(a.DC4V) AS DC4V, AVG(a.DC4A) AS DC4A, SUM(a.DC4W) AS DC4W,
SUM(a.DC4WH) AS DC4WH,AVG(a.DC5V) AS DC5V, AVG(a.DC5A) AS DC5A, SUM(a.DC5W) AS DC5W, SUM(a.DC5WH) AS DC5WH, max(PR) AS PR,
AVG(a.RA1) AS RA1, AVG(a.RA2) AS RA2, AVG(a.RA3) AS RA3, AVG(a.RA4) AS RA4, AVG(a.RA5) AS RA5, avg(DCKW) as DCKW, avg(ACKW) as ACKW,
MAX(a.TODAYKWH) AS TODAYKWH, MAX(a.TOTALKWH) AS TOTALKWH, SUM(a.KWH) AS KWH
FROM solar_master.inverter_history_day a
WHERE powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m') = @date1
GROUP BY DATE_FORMAT(a.TIMESTAMP, '%Y-%m'), a.INVERTERID
) a
LEFT JOIN " + _siteDB + @".inverter i ON a.INVERTERID = i.InverterId
-- ----------------------
left join (
select DATE_FORMAT(a.TIMESTAMP, '%Y-%m') AS reportDate, Irradiance
from sensor_history_month a
where powerstationID = @powerstationID and DATE_FORMAT(a.TIMESTAMP, '%Y-%m') = @date1 and Irradiance <> 0
)irr on a.reportDate = irr.reportDate ;";
conn.Execute(sql, new { date1 = _date1.Substring(0, 7), PowerStationID = _powerStationID });
#endregion month
conn.Close(); conn.Close();
return ds;
} }
result = true;
} }
catch (Exception ex)
public List<raw_inv_hour> proc_station_hour(string reportDate)
{ {
using (MySqlConnection conn = new MySqlConnection(Connection1)) throw ex;
{
conn.Open();
string sql = @"
INSERT INTO `solar_master`.`power_station_history_hour`(`PowerStationId`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `MONEY`, `TODAYMONEY`, `TOTALMONEY`, `CARBON`, `TODAYCARBON`, `TOTALCARBON`)
select 1 PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') reportdate, siteid, 0 SITETYPE, round((sum(KWH)), 2) KWH, round((max(TODAYKWH)), 2) TODAYKWH, round((max(TOTALKWH)), 2) TOTALKWH, round((max(KWHKWP)), 2) KWHKWP, round((max(PR)), 2) PR, 0 as MP, round((max(SOLARHOUR)), 2) SOLARHOUR,
0, 0 , 0, 0, 0 , 0
from solar_com0002.s02202000101_station
where FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d') = '2021-08-04'
group by siteid, FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H')";
List<raw_inv_hour> ds = conn.Query<raw_inv_hour>(sql).AsList<raw_inv_hour>();
conn.Close();
return ds;
} }
return result;
} }
public List<raw_inv_hour> proc_station_day(string reportDate)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"
insert into solar_master.power_station_history_day(`PowerStationId`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `MONEY`, `TOTALMONEY`, `CARBON`, `TOTALCARBON`)
SELECT 1 as PowerStationId, FROM_UNIXTIME(s.TIMESTAMP/1000, '%Y-%m-%d %H') AS reportDate, s.SITEID, s.SITETYPE,
s.TodayKWh, s.TotalKWH, s.KWHKWP, s.PR, s.MP, s.SolarHour, 0, 0, 0, 0
FROM solar_com0002.s02202000101_station s
WHERE DATE_FORMAT(FROM_UNIXTIME(s.TIMESTAMP/1000), '%Y-%m-%d') = '2021-08-04'
ORDER BY TIMESTAMP DESC
LIMIT 1 ";
List<raw_inv_hour> ds = conn.Query<raw_inv_hour>(sql).AsList<raw_inv_hour>();
conn.Close();
return ds;
}
}
public List<raw_inv_hour> proc_sensor_hour(string reportDate)
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"
INSERT INTO `solar_master`.`sensor_history_hour`(`PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`)
select a.PowerStationId, a.reportDate, ifnull(b.Irradiance, 0) Irradiance, a.temperature
from
(#
select 1 as PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') AS reportDate,
0 Irradiance, round(avg(SENSORAVG02),3) temperature
from s02202000101_sensoravg a
where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04'
group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H')
)a left join (
#
select 1 as PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') AS reportDate,
round(avg(SENSORAVG01),3) Irradiance, round(avg(SENSORAVG02),3) temperature
from s02202000101_sensoravg a
where left(FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d'), 10) = '2021-08-04' and SENSORAVG01 <> 0
group by FROM_UNIXTIME(a.TIMESTAMP / 1000, '%Y-%m-%d %H')
)b on a.reportDate = b.reportDate ;";
List<raw_inv_hour> ds = conn.Query<raw_inv_hour>(sql).AsList<raw_inv_hour>();
conn.Close();
return ds;
}
}
} }
} }

View File

@ -0,0 +1,362 @@
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Dapper;
using solarApp.Model;
using System.Configuration;
using System.Threading.Tasks;
namespace solarApp.Service
{
public class procSensorSvc
{
string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
public string _siteID { get; set; }
public string _siteDB { get; set; }
public string _siteID01 { get; set; }
public string _date1 { get; set; }
public string _date2 { get; set; }
public string _powerStationID { get; set; }
//public procSensorSvc(string siteDB, string siteID, string date1, string date2)
//{
// if (!string.IsNullOrEmpty(siteDB)) _siteDB = siteDB;
// if (!string.IsNullOrEmpty(siteID)) _siteID = siteID.Substring(0, 9);
// if (!string.IsNullOrEmpty(siteID)) _siteID01 = siteID;
// if (!string.IsNullOrEmpty(_date1)) _date1 = date1;
// if (!string.IsNullOrEmpty(_date2)) _date2 = date2;
// #region 取得 PowerStationID
// using (MySqlConnection conn = new MySqlConnection(Connection1))
// {
// conn.Open();
// string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID";
// var ds = conn.Query<string>(sql, new { siteID = _siteID }).AsList<string>();
// _powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0";
// conn.Close();
// }
// #endregion
// archiveData();
//}
public bool archiveData() {
bool result = false;
try
{
clearData();
insert_sensor();
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
/// <summary>
/// 清除指定期間、指定電站的 Sensor hour、day、month 資料
/// </summary>
/// <returns></returns>
private bool clearData()
{
bool result = false;
try
{
#region PowerStationID
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID";
var ds = conn.Query<string>(sql, new { siteID = _siteID }).AsList<string>();
_powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0";
conn.Close();
}
#endregion
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"delete from sensor_history_hour where left(`TIMESTAMP`, 10) between @date1 and @date2 and PowerStationID = @powerStationID;
delete from sensor_history_day where left(`TIMESTAMP`, 10) between @date1 and @date2 and PowerStationID = @powerStationID;
delete from sensor_history_month where left(`TIMESTAMP`, 7) between left(@date1, 7) and left(@date2, 7) and PowerStationID = @powerStationID;
delete from sensoravg_history_hour where left(`TIMESTAMP`, 10) between @date1 and @date2 and PowerStationID = @powerStationID;
delete from sensoravg_history_day where left(`TIMESTAMP`, 10) between @date1 and @date2 and PowerStationID = @powerStationID;
delete from sensoravg_history_month where left(`TIMESTAMP`, 7) between left(@date1, 7) and left(@date2, 7) and PowerStationID = @powerStationID;";
var ds = conn.Execute(sql, new { date1 = _date1, date2 = _date2, PowerStationID = _powerStationID });
conn.Close();
}
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
/// <summary>
/// Sensor 欄位需要平均時 欄位串接 (s1 + s2) / 2
/// </summary>
/// <param name="lstData"></param>
/// <returns></returns>
string ConcatColumn(List<sensor_model> lstData)
{
string ss = string.Empty; string result = string.Empty;
if (lstData.Count > 0)
{
foreach (var item in lstData)
{
ss += (ss == string.Empty) ? item.colname : "+" + item.colname; // 2 個以上欄位需要相加
}
if (lstData.Count >= 2) result = "(" + ss + ") / " + lstData.Count.ToString(); // 2個以上 需要除以個數 (平均值)
else
result = ss;
}
else result = "0";
return result;
}
bool insert_sensor()
{
bool result = false;
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
#region PowerStationID
string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID";
var ds = conn.Query<string>(sql, new { siteID = _siteID }).AsList<string>();
_powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0";
#endregion
// init 關閉 group full column
string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));";
conn.Execute(ss);
#region Sensor
//沒有被刪除、啟用中、狀態正常的 才可列入計算
sql = @"select a.id, CONCAT( left(UID, 9) ,'01') SiteID, a.`name` sensorName, type, DBName, TableName, colname
from " + _siteDB + @".device a
where left(UID, 11) = @siteID and deleted = 0 and enabled = 1 and `status` = 1 ";
List<sensor_model> ds_sensor = conn.Query<sensor_model>(sql, new { siteID = _siteID01 }).AsList<sensor_model>();
//欄位處理
// { "Type":[
// { "Name":"日照計","EName":"PYR"},
// { "Name":"模組溫度計","EName":"MTR"},
// { "Name":"環境溫度計","EName":"ETR"},
// { "Name":"環境濕度計","EName":"EMM"},
// { "Name":"風速計","EName":"VAN"},
// { "Name":" 電表","EName":"PWR"}]}
var irrlst = ds_sensor.FindAll(x => x.type.Contains("PYR"));
var modelTemplst = ds_sensor.FindAll(x => x.type.Contains("MTR"));
var envTemplst = ds_sensor.FindAll(x => x.type.Contains("ETR"));
var humlst = ds_sensor.FindAll(x => x.type.Contains("EMM"));
var windlst = ds_sensor.FindAll(x => x.type.Contains("VAN"));
var dustlst = ds_sensor.FindAll(x => x.type.Contains("DST")); //需要新增於DB
//var meterlst = ds_sensor.FindAll(x => x.type.Contains("PWR")); 電錶暫不處理
string irrCol = string.Empty; string modelTempCol = string.Empty; string evnTempCol = string.Empty; string humCol = string.Empty; string windCol = string.Empty; string meterCol = string.Empty; string dustCol = string.Empty;
irrCol = ConcatColumn(irrlst);//日照計
modelTempCol = ConcatColumn(modelTemplst);
evnTempCol = ConcatColumn(envTemplst);
humCol = ConcatColumn(humlst);
windCol = ConcatColumn(windlst);
dustCol = ConcatColumn(dustlst);
string irrNot0 = string.Empty; // and 日照1 <> 0 and 日照2 <> 0
# region 0
if (irrlst.Count > 0)
{
foreach (var item in irrlst)
irrNot0 += " and " + item.colname + " <> 0 "; // and S1 <> 0 and S2 <> 0
}
#endregion
//電表
#endregion Sensor
try
{
#region hour
sql = @"insert into sensor_history_hour( `PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`)
select a.powerstationID, a.reportdate, ifnull(b.irrAvg, 0) irrAvg, a.modelTempAvg from
(
select @powerStationID powerstationID, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, round(avg(" + modelTempCol + @"), 2) modelTempAvg,
round(avg(" + evnTempCol + @"), 2) envTempAvg, round(avg(" + humCol + @"), 2) humidityAvg, round(avg(" + windCol + @"), 2) windAvg,
round(avg(" + dustCol + @"), 2) dustAvg
from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H')
) a left join
(
select concat(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00') reportdate, round(avg(" + irrCol + @"), 2) irrAvg
from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2 " + irrNot0 + @" # 0
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H')
)b on a.reportdate = b.reportdate";
//MySqlCommand cmd = new MySqlCommand(sql, conn);
//cmd.Parameters()
//cmd.ExecuteNonQuery();
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 });
#endregion hour
#region day
sql = @"insert into sensor_history_day( `PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`)
select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.modelTempAvg from
(
select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(Temperature), 2) modelTempAvg
from solar_master.sensor_history_hour
where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') between @date1 and @date2
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d')
) a left join
(
select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(Irradiance), 2) Irradiance
from solar_master.sensor_history_hour
where powerstationID = @powerstationID
and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') between @date1 and @date2 and Irradiance <> 0 # 0
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d')
)b on a.reportdate = b.reportdate";
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 });
#endregion day
#region month 2
sql = @"
delete from sensor_history_month where left(`TIMESTAMP`, 7) between left(@date1, 7) and left(@date2, 7) and PowerStationID = @powerStationID;
insert into sensor_history_month( `PowerStationId`, `TIMESTAMP`, `Irradiance`, `Temperature`)
select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.modelTempAvg from
(
select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(Temperature), 2) modelTempAvg
from solar_master.sensor_history_day
where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') between @date1 and @date2
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m')
) a left join
(
select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(Irradiance), 2) Irradiance
from solar_master.sensor_history_day
where powerstationID = @powerstationID
and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') between @date1 and @date2 and Irradiance <> 0 # 0
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m')
)b on a.reportdate = b.reportdate";
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1.Substring(0,7), date2 = _date2.Substring(0, 7) });
#endregion month
#region Sensor 1~50 hour
sql = @"
INSERT INTO solar_master.sensoravg_history_hour(PowerStationId, `TIMESTAMP`, SENSORAVG01, SENSORAVG02, SENSORAVG03, SENSORAVG04, SENSORAVG05, SENSORAVG06, SENSORAVG07, SENSORAVG08, SENSORAVG09, SENSORAVG10, SENSORAVG11, SENSORAVG12, SENSORAVG13, SENSORAVG14, SENSORAVG15, SENSORAVG16, SENSORAVG17, SENSORAVG18, SENSORAVG19, SENSORAVG20, SENSORAVG21, SENSORAVG22, SENSORAVG23, SENSORAVG24, SENSORAVG25, SENSORAVG26, SENSORAVG27, SENSORAVG28, SENSORAVG29, SENSORAVG30, SENSORAVG31, SENSORAVG32, SENSORAVG33, SENSORAVG34, SENSORAVG35, SENSORAVG36, SENSORAVG37, SENSORAVG38, SENSORAVG39, SENSORAVG40, SENSORAVG41, SENSORAVG42, SENSORAVG43, SENSORAVG44, SENSORAVG45, SENSORAVG46, SENSORAVG47, SENSORAVG48, SENSORAVG49, SENSORAVG50)
select @powerStationID powerstationID, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, avg(sensorAvg01), avg(sensorAvg02),
avg(sensorAvg03), avg(sensorAvg04), avg(sensorAvg05), avg(sensorAvg06), avg(sensorAvg07), avg(sensorAvg08), avg(sensorAvg09),
avg(sensorAvg10), avg(sensorAvg11), avg(sensorAvg12), avg(sensorAvg13), avg(sensorAvg14), avg(sensorAvg15), avg(sensorAvg16),
avg(sensorAvg17), avg(sensorAvg18), avg(sensorAvg19), avg(sensorAvg20), avg(sensorAvg21), avg(sensorAvg22), avg(sensorAvg23),
avg(sensorAvg24), avg(sensorAvg25), avg(sensorAvg26), avg(sensorAvg27), avg(sensorAvg28), avg(sensorAvg29), avg(sensorAvg30),
avg(sensorAvg31), avg(sensorAvg32), avg(sensorAvg33), avg(sensorAvg34), avg(sensorAvg35), avg(sensorAvg36), avg(sensorAvg37),
avg(sensorAvg38), avg(sensorAvg39), avg(sensorAvg40), avg(sensorAvg41), avg(sensorAvg42), avg(sensorAvg43), avg(sensorAvg44),
avg(sensorAvg45), avg(sensorAvg46), avg(sensorAvg47), avg(sensorAvg48), avg(sensorAvg49), avg(sensorAvg50)
from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H');";
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 });
//日照計 update
sql = string.Empty;
foreach (var item in irrlst)
{
sql += @" update sensoravg_history_hour a join
(
select @powerstationID powerstationID, concat(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00') reportdate, round(avg(" + item.colname + @"), 2) irrAvg
from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') between @date1 and @date2 and " + item.colname + @" <> 0 # 0
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H')
)b on a.powerStationID = b.powerStationID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d %H:%i') = b.reportdate
set a." + item.colname + " = b.irrAvg;";
}
if (!string.IsNullOrEmpty(sql)) conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 });
#endregion
#region Sensor 1~50 day
sql = @"
INSERT INTO solar_master.sensoravg_history_day(PowerStationId, `TIMESTAMP`, SENSORAVG01, SENSORAVG02, SENSORAVG03, SENSORAVG04, SENSORAVG05, SENSORAVG06, SENSORAVG07, SENSORAVG08, SENSORAVG09, SENSORAVG10, SENSORAVG11, SENSORAVG12, SENSORAVG13, SENSORAVG14, SENSORAVG15, SENSORAVG16, SENSORAVG17, SENSORAVG18, SENSORAVG19, SENSORAVG20, SENSORAVG21, SENSORAVG22, SENSORAVG23, SENSORAVG24, SENSORAVG25, SENSORAVG26, SENSORAVG27, SENSORAVG28, SENSORAVG29, SENSORAVG30, SENSORAVG31, SENSORAVG32, SENSORAVG33, SENSORAVG34, SENSORAVG35, SENSORAVG36, SENSORAVG37, SENSORAVG38, SENSORAVG39, SENSORAVG40, SENSORAVG41, SENSORAVG42, SENSORAVG43, SENSORAVG44, SENSORAVG45, SENSORAVG46, SENSORAVG47, SENSORAVG48, SENSORAVG49, SENSORAVG50)
select @powerStationID powerstationID, DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') reportdate, avg(sensorAvg01), avg(sensorAvg02),
avg(sensorAvg03), avg(sensorAvg04), avg(sensorAvg05), avg(sensorAvg06), avg(sensorAvg07), avg(sensorAvg08), avg(sensorAvg09),
avg(sensorAvg10), avg(sensorAvg11), avg(sensorAvg12), avg(sensorAvg13), avg(sensorAvg14), avg(sensorAvg15), avg(sensorAvg16),
avg(sensorAvg17), avg(sensorAvg18), avg(sensorAvg19), avg(sensorAvg20), avg(sensorAvg21), avg(sensorAvg22), avg(sensorAvg23),
avg(sensorAvg24), avg(sensorAvg25), avg(sensorAvg26), avg(sensorAvg27), avg(sensorAvg28), avg(sensorAvg29), avg(sensorAvg30),
avg(sensorAvg31), avg(sensorAvg32), avg(sensorAvg33), avg(sensorAvg34), avg(sensorAvg35), avg(sensorAvg36), avg(sensorAvg37),
avg(sensorAvg38), avg(sensorAvg39), avg(sensorAvg40), avg(sensorAvg41), avg(sensorAvg42), avg(sensorAvg43), avg(sensorAvg44),
avg(sensorAvg45), avg(sensorAvg46), avg(sensorAvg47), avg(sensorAvg48), avg(sensorAvg49), avg(sensorAvg50)
from solar_master.sensoravg_history_hour
where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') between @date1 and @date2
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d');";
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 });
//日照計 update
sql = string.Empty;
foreach (var item in irrlst)
{
sql += @" update solar_master.sensoravg_history_day a join
(
select @powerstationID powerstationID, concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(" + item.colname + @"), 2) irrAvg
from solar_master.sensoravg_history_hour
where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') between @date1 and @date2 and " + item.colname + @" <> 0 # 0
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d')
)b on a.powerStationID = b.powerStationID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d') = b.reportdate
set a." + item.colname + " = b.irrAvg;";
}
if (!string.IsNullOrEmpty(sql)) conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1, date2 = _date2 });
#endregion
#region Sensor 1~50 month
sql = @"
delete from sensoravg_history_month where left(`TIMESTAMP`, 7) between left(@date1, 7) and left(@date2, 7) and PowerStationID = @powerStationID;
INSERT INTO solar_master.sensoravg_history_month (PowerStationId, `TIMESTAMP`, SENSORAVG01, SENSORAVG02, SENSORAVG03, SENSORAVG04, SENSORAVG05, SENSORAVG06, SENSORAVG07, SENSORAVG08, SENSORAVG09, SENSORAVG10, SENSORAVG11, SENSORAVG12, SENSORAVG13, SENSORAVG14, SENSORAVG15, SENSORAVG16, SENSORAVG17, SENSORAVG18, SENSORAVG19, SENSORAVG20, SENSORAVG21, SENSORAVG22, SENSORAVG23, SENSORAVG24, SENSORAVG25, SENSORAVG26, SENSORAVG27, SENSORAVG28, SENSORAVG29, SENSORAVG30, SENSORAVG31, SENSORAVG32, SENSORAVG33, SENSORAVG34, SENSORAVG35, SENSORAVG36, SENSORAVG37, SENSORAVG38, SENSORAVG39, SENSORAVG40, SENSORAVG41, SENSORAVG42, SENSORAVG43, SENSORAVG44, SENSORAVG45, SENSORAVG46, SENSORAVG47, SENSORAVG48, SENSORAVG49, SENSORAVG50)
select @powerStationID powerstationID, concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, avg(sensorAvg01), avg(sensorAvg02),
avg(sensorAvg03), avg(sensorAvg04), avg(sensorAvg05), avg(sensorAvg06), avg(sensorAvg07), avg(sensorAvg08), avg(sensorAvg09),
avg(sensorAvg10), avg(sensorAvg11), avg(sensorAvg12), avg(sensorAvg13), avg(sensorAvg14), avg(sensorAvg15), avg(sensorAvg16),
avg(sensorAvg17), avg(sensorAvg18), avg(sensorAvg19), avg(sensorAvg20), avg(sensorAvg21), avg(sensorAvg22), avg(sensorAvg23),
avg(sensorAvg24), avg(sensorAvg25), avg(sensorAvg26), avg(sensorAvg27), avg(sensorAvg28), avg(sensorAvg29), avg(sensorAvg30),
avg(sensorAvg31), avg(sensorAvg32), avg(sensorAvg33), avg(sensorAvg34), avg(sensorAvg35), avg(sensorAvg36), avg(sensorAvg37),
avg(sensorAvg38), avg(sensorAvg39), avg(sensorAvg40), avg(sensorAvg41), avg(sensorAvg42), avg(sensorAvg43), avg(sensorAvg44),
avg(sensorAvg45), avg(sensorAvg46), avg(sensorAvg47), avg(sensorAvg48), avg(sensorAvg49), avg(sensorAvg50)
from solar_master.sensoravg_history_day
where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') between @date1 and @date2
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m');";
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1.Substring(0, 7), date2 = _date2.Substring(0, 7) });
//日照計 update
sql = string.Empty;
foreach (var item in irrlst)
{
sql += @" update solar_master.sensoravg_history_month a join
(
select @powerstationID powerstationID, concat( DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(" + item.colname + @"), 2) irrAvg
from solar_master.sensoravg_history_day
where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') between @date1 and @date2 and " + item.colname + @" <> 0 # 0
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d')
)b on a.powerStationID = b.powerStationID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m') = b.reportdate
set a." + item.colname + " = b.irrAvg;";
}
if (!string.IsNullOrEmpty(sql)) conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1.Substring(0, 7), date2 = _date2.Substring(0, 7) });
#endregion
conn.Close();
result = true;
}
catch (Exception ex)
{
throw ex;
}
}
return result;
}
}
}

View File

@ -0,0 +1,190 @@
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using Dapper;
using solarApp.Model;
using System.Configuration;
namespace solarApp.Service
{
public class procStationSvc
{
string Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
public string _siteID { get; set; }
public string _siteDB { get; set; }
public string _siteID01 { get; set; }
public string _date1 { get; set; }
public string _date2 { get; set; }
public string _powerStationID { get; set; }
public bool clearData()
{
bool result = false;
try
{
#region PowerStationID
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID";
var ds = conn.Query<string>(sql, new { siteID = _siteID }).AsList<string>();
_powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0";
conn.Close();
}
#endregion
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string sql = @"
delete from power_station_history_hour where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1;
delete from power_station_history_day where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1;
delete from power_station_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;";
var ds = conn.Execute(sql, new { date1 = _date1, powerStationID = _powerStationID });
conn.Close();
}
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
public bool archiveData()
{
bool result = false;
try
{
clearData();
insert_station();
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
bool insert_station()
{
bool result = false;
try
{
using (MySqlConnection conn = new MySqlConnection(Connection1))
{
conn.Open();
string ss = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));";
conn.Execute(ss);
string sql = @" select id as PowerStationID from solar_master.power_station where `code` = @siteID";
var ds = conn.Query<string>(sql, new { siteID = _siteID }).AsList<string>();
_powerStationID = (ds.Count > 0) ? ds[0][0].ToString() : "0";
#region hour -- solar_type 0 1 2 ------------ 1
//select b.id PowerStationId, a.reportdate, b.siteID, a.siteType, a.KWH, a.TODAYKWH, a.TOTALKWH, a.KWHKWP, a.PR, a.MP, a.SOLARHOUR,
// (KWH * PowerRate) MONEY, (a.TODAYKWH * PowerRate) TODAYMONEY, (a.TOTALKWH * PowerRate) TOTALMONEY,
// (KWH * CarbonRate) CARBON, (a.TODAYKWH * CarbonRate) TODAYCARBON, round((a.TOTALKWH * CarbonRate), 4) TOTALCARBON
// from
// (
// select siteid, siteType, concat(FROM_UNIXTIME(`TIMESTAMP`/ 1000, '%Y-%m-%d %H'), ':00:00') reportdate,
// round((sum(KWH)), 4) KWH, round((max(TODAYKWH)), 4) TODAYKWH, round((max(TOTALKWH)), 4) TOTALKWH, round((max(KWHKWP)), 4) KWHKWP,
// round((max(PR)), 4) PR, 0 as MP, round((max(SOLARHOUR)), 4) SOLARHOUR
// from " + _siteDB + ".s" + _siteID01 + @"_station
// where FROM_UNIXTIME(`TIMESTAMP`/ 1000, '%Y-%m-%d') = @date1
// group by siteid, FROM_UNIXTIME(`TIMESTAMP`/ 1000, '%Y-%m-%d %H')
// )a join
// (
// select id, `code` siteID, PowerRate, (select `value` from solar_master.`variable` where `name` = 'CarbonRate') as CarbonRate, SolarType
// from solar_master.power_station
// where `code` = @siteID
// ) b on a.siteID = b.siteID
sql = @"insert into solar_master.power_station_history_hour(PowerStationId, `TIMESTAMP`, SITEID, SITETYPE, KWH, TODAYKWH, TOTALKWH, KWHKWP, PR, MP, SOLARHOUR, MONEY, TODAYMONEY, TOTALMONEY, CARBON, TODAYCARBON, TOTALCARBON)
select b.id PowerStationId, concat(FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d %H'), ':00:00') reportdate, a.siteid, a.siteType,
a.KWH, TODAYKWH, TOTALKWH, KWHKWP , PR, MP, a.SOLARHOUR ,
(a.KWH * PowerRate) MONEY, (a.TODAYKWH * PowerRate) TODAYMONEY, (a.TOTALKWH * PowerRate) TOTALMONEY,
(a.KWH * CarbonRate) CARBON, (a.TODAYKWH * CarbonRate) TODAYCARBON, round((a.TOTALKWH * CarbonRate), 3) TOTALCARBON
from " + _siteDB + ".s" + _siteID01 + @"_station a join
(select id, `code` siteID, PowerRate, (select `value` from solar_master.`variable` where `name` = 'CarbonRate') as CarbonRate, SolarType
from solar_master.power_station where `code` = @siteID
) b on a.siteID = b.siteID
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and a.SITEID = @siteID ";
int ct = conn.Execute(sql, new { date1 = _date1, siteID = _siteID });
#endregion hour
#region day
sql = @" INSERT INTO solar_master.power_station_history_day( PowerStationId, `TIMESTAMP`, SITEID, SITETYPE, TODAYKWH, TOTALKWH, KWHKWP,
PR, MP, SOLARHOUR, MONEY, TOTALMONEY, CARBON, TOTALCARBON)
select b.id PowerStationId, a.reportdate, b.siteID, a.siteType, a.TODAYKWH, a.TOTALKWH, a.KWHKWP, a.PR, a.MP, a.SOLARHOUR,
(KWH * PowerRate) MONEY, (a.TOTALKWH * PowerRate) TOTALMONEY,
(KWH * CarbonRate) CARBON, round((a.TOTALKWH * CarbonRate), 4) TOTALCARBON
from
(
select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00:00') reportdate, siteid, siteType, round((sum(KWH)), 4) KWH,
round((max(TODAYKWH)), 4) TODAYKWH, round((max(TOTALKWH)), 4) TOTALKWH, round((max(KWHKWP)), 4) KWHKWP,
round((max(PR)), 4) PR, round((max(MP)), 4) as MP, round((max(SOLARHOUR)), 4) SOLARHOUR
from solar_master.power_station_history_hour a
where SITEID = @siteID and DATE_FORMAT(a.`TIMESTAMP`,'%Y-%m-%d') = @date1
group by a.siteid, DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d')
)a join
(
select id, `code` siteID, PowerRate, (select `value` from solar_master.`variable` where `name` = 'CarbonRate') as CarbonRate, SolarType
from solar_master.power_station
where `code` = @siteID
) b on a.siteID = b.siteID ";
conn.Execute(sql, new { date1 = _date1, siteID = _siteID });
#endregion day
#region month
sql = @"
delete from power_station_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;
INSERT INTO solar_master.power_station_history_month(PowerStationId, `TIMESTAMP`, SITEID, SITETYPE, TOTALKWH, KWHKWP,
PR, MP, SOLARHOUR, MONEY, TOTALMONEY, CARBON, TOTALCARBON)
select b.id PowerStationId, a.reportdate, b.siteID, a.siteType, a.TOTALKWH, a.KWHKWP, a.PR, a.MP, a.SOLARHOUR,
(KWH * PowerRate) MONEY, (a.TOTALKWH * PowerRate) TOTALMONEY,
(KWH * CarbonRate) CARBON, round((a.TOTALKWH * CarbonRate), 4) TOTALCARBON
from
(
select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00:00') reportdate, siteid, siteType, round((sum(KWH)), 4) KWH,
round((max(TODAYKWH)), 4) TODAYKWH, round((max(TOTALKWH)), 4) TOTALKWH, round((max(KWHKWP)), 4) KWHKWP,
round((max(PR)), 4) PR, round((max(MP)), 4) as MP, round((max(SOLARHOUR)), 4) SOLARHOUR
from solar_master.power_station_history_hour a
where SITEID = @siteID and DATE_FORMAT(a.`TIMESTAMP`, '%Y-%m') = @date1
group by a.siteid, DATE_FORMAT(`TIMESTAMP`, '%Y-%m')
)a join
(
select id, `code` siteID, PowerRate, (select `value` from solar_master.`variable` where `name` = 'CarbonRate') as CarbonRate, SolarType
from solar_master.power_station
where `code` = @siteID
) b on a.siteID = b.siteID ";
conn.Execute(sql, new { date1 = _date1.Substring(0, 7), siteID = _siteID, powerStationID = _powerStationID });
#endregion month
conn.Close();
}
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -5,11 +5,13 @@ using System.Data;
using System.Drawing; using System.Drawing;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using solarApp.Service;
namespace solarApp namespace solarApp
{ {
public partial class fmArchive : Form public partial class fmArchive : Form
{ {
getStationSvc stationSvc = new getStationSvc();
public fmArchive() public fmArchive()
{ {
InitializeComponent(); InitializeComponent();
@ -19,5 +21,133 @@ namespace solarApp
{ {
} }
private void bt_Sensor_Click(object sender, EventArgs e)
{
string date1 = dtSelect.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procSensorSvc sensorSvc = new procSensorSvc();
sensorSvc._siteDB = lbSiteDB_sensor.Text;
sensorSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9);
sensorSvc._siteID01 = lbSiteID_sensor.Text;
sensorSvc._date1 = date1;
sensorSvc._date2 = date1;
sensorSvc.archiveData();
//sensorSvc.archiveData();
MessageBox.Show("OK");
}
private void fmArchive_Load(object sender, EventArgs e)
{
#region
int i = 0;
var site_list = stationSvc.get_station_list();
foreach (var item in site_list)
{
RadioButton rb = new RadioButton();
rb.Name = item.SiteID;
rb.Text = item.SiteName;
rb.Tag = item.SiteDB;
rb.Font = new Font(Font.FontFamily, 22);
rb.AutoSize = true;
rb.CheckedChanged += new EventHandler(rb_site_CheckedChanged);
fp_site.Controls.Add(rb);
if (i == 0) rb.Checked = true;
i++;
}
#endregion
}
private void rb_site_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked)
{
lbSiteName_sensor.Text = rb.Text;
lbSiteDB_sensor.Text = rb.Tag.ToString();
lbSiteID_sensor.Text = rb.Name;
//顯示 table name on label
//lbSiteRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Station";
//lbInvRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Inv";
//lbSensorRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_SensorAVG";
//add_inv_list(lbSiteDB_inv.Text, lbSiteID_inv.Text.Substring(0, 9));
}
}
private void btVerifyData_Click(object sender, EventArgs e)
{
fmMain fm = new fmMain();
fm.Show();
}
private void bt_Inv_Click(object sender, EventArgs e)
{
string date1 = dtSelect.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procInvSvc invSvc = new procInvSvc();
invSvc._siteDB = lbSiteDB_sensor.Text;
invSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9);
invSvc._siteID01 = lbSiteID_sensor.Text;
invSvc._date1 = date1;
invSvc._date2 = date1;
invSvc.archiveData();
//sensorSvc.archiveData();
MessageBox.Show("OK");
}
private void dtSelect_ValueChanged(object sender, EventArgs e)
{
bt_clear_inv.Enabled = true;
bt_clear_station.Enabled = true;
bt_clear_inv.Text = "clear " + dtSelect.Value.ToString("MM-dd");
bt_clear_station.Text = "clear " + dtSelect.Value.ToString("MM-dd");
}
private void bt_clear_inv_Click(object sender, EventArgs e)
{
string date1 = dtSelect.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procInvSvc invSvc = new procInvSvc();
invSvc._siteDB = lbSiteDB_sensor.Text;
invSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9);
invSvc._siteID01 = lbSiteID_sensor.Text;
invSvc._date1 = date1;
invSvc._date2 = date1;
invSvc.clearData();
//sensorSvc.archiveData();
MessageBox.Show("OK");
}
private void bt_clear_station_Click(object sender, EventArgs e)
{
string date1 = dtSelect.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procStationSvc siteSvc = new procStationSvc();
siteSvc._siteDB = lbSiteDB_sensor.Text;
siteSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9);
siteSvc._siteID01 = lbSiteID_sensor.Text;
siteSvc._date1 = date1;
siteSvc._date2 = date1;
siteSvc.clearData();
//sensorSvc.archiveData();
MessageBox.Show("OK");
}
private void bt_site_Click(object sender, EventArgs e)
{
string date1 = dtSelect.Value.ToString("yyyy-MM-dd");
//procSensorSvc sensorSvc = new procSensorSvc(lbSiteDB_sensor.Text, lbSiteID_sensor.Text, date1, date1);
procStationSvc siteSvc = new procStationSvc();
siteSvc._siteDB = lbSiteDB_sensor.Text;
siteSvc._siteID = lbSiteID_sensor.Text.Substring(0, 9);
siteSvc._siteID01 = lbSiteID_sensor.Text;
siteSvc._date1 = date1;
siteSvc._date2 = date1;
siteSvc.archiveData();
//sensorSvc.archiveData();
MessageBox.Show("OK");
}
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@ namespace solarApp
{ {
get_inv_svc inv_svc = new get_inv_svc(); get_inv_svc inv_svc = new get_inv_svc();
getStationSvc stationSvc = new getStationSvc(); getStationSvc stationSvc = new getStationSvc();
getSensorSvc sensorSvc = new getSensorSvc();
public fmMain() public fmMain()
{ {
InitializeComponent(); InitializeComponent();
@ -23,28 +24,44 @@ namespace solarApp
private async void button1_Click(object sender, EventArgs e) private async void button1_Click(object sender, EventArgs e)
{ {
lbMsg_inv.Text = "loading ... "; lbMsg_inv.Text = "loading ... ";
string date1 = dtselect_inv.Value.ToString("yyyy-MM-dd"); string date2 = dtselect_inv.Value.ToString("yyyy-MM-dd");
gv_fic_inv_raw.DataSource = inv_svc.Get_rawInv(date1, lbInverterID.Text, lbSiteDB_inv.Text, lbSiteID_inv.Text); //string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd");
string date1 = dtselect_inv.Value.AddDays(-7).ToString("yyyy-MM-dd");
gv_fic_inv_hour.DataSource = inv_svc.get_Inv_rawAvg(date1, lbInverterID.Text, lbSiteDB_inv.Text, lbSiteID_inv.Text);
gv_web_inv_hour.DataSource = inv_svc.get_web_Inv_hour(date1, lbInverterID.Text); gv_fic_inv_raw.DataSource = inv_svc.Get_rawInv(date2, lbInverterID.Text, lbSiteDB_inv.Text, lbSiteID_inv.Text);
gv_web_inv_day.DataSource = inv_svc.get_web_Inv_day(date1, lbInverterID.Text); gv_fic_inv_hour.DataSource = inv_svc.get_Inv_rawAvg(date2, lbInverterID.Text, lbSiteDB_inv.Text, lbSiteID_inv.Text);
gv_web_inv_month.DataSource = inv_svc.get_web_Inv_month( date1.Substring(0, 7), lbInverterID.Text); gv_web_inv_hour.DataSource = inv_svc.get_web_Inv_hour(date2, lbInverterID.Text);
gv_web_inv_day.DataSource = inv_svc.get_web_Inv_day(date1, date2, lbInverterID.Text);
gv_web_inv_month.DataSource = inv_svc.get_web_Inv_month( date1.Substring(0, 7), date2.Substring(0, 7), lbInverterID.Text);
lbMsg_inv.Text = " done " + System.DateTime.Now.ToShortTimeString(); lbMsg_inv.Text = " done " + System.DateTime.Now.ToShortTimeString();
} }
private void btInv_15min_Click(object sender, EventArgs e)
{
lbMsg_inv.Text = "loading ... ";
string date2 = dtselect_inv.Value.ToString("yyyy-MM-dd");
//string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd");
string date1 = dtselect_inv.Value.AddDays(-7).ToString("yyyy-MM-dd");
gv_web_inv_hour.DataSource = inv_svc.get_web_Inv_15min(date2, lbInverterID.Text , lbSiteID_inv.Text.Substring(0, 9));
}
private void fmMain_Load(object sender, EventArgs e) private void fmMain_Load(object sender, EventArgs e)
{ {
dtselect_station1.Value = DateTime.Today.AddDays(-7);
dtselect_station2.Value = DateTime.Today.AddDays(-1);
dtselect_station1.Value = DateTime.Today.AddDays(-1);
dtselect_inv.Value = DateTime.Today.AddDays(-1); dtselect_inv.Value = DateTime.Today.AddDays(-1);
// Cursor.Current = Cursors.Default; dtSelect_sensor1.Value = DateTime.Today.AddDays(-1);
tabControl1.SelectedTab = tabControl1.TabPages[1]; tabControl1.SelectedTab = tabControl1.TabPages[1];
@ -81,9 +98,16 @@ namespace solarApp
lbSiteDB_inv.Text = rb.Tag.ToString(); lbSiteDB_inv.Text = rb.Tag.ToString();
lbSiteName_inv.Text = rb.Text; lbSiteName_inv.Text = rb.Text;
lbSiteID_inv.Text = rb.Name; lbSiteID_inv.Text = rb.Name;
lbSiteName_sensor.Text = lbSiteName_inv.Text;
lbSiteDB_sensor.Text = lbSiteDB_inv.Text;
lbSiteID_sensor.Text = lbSiteID_inv.Text;
//顯示 table name on label //顯示 table name on label
lbSiteRaw.Text = lbSiteName_inv.Text + " "+ lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Station"; lbSiteRaw.Text = lbSiteName_inv.Text + " "+ lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Station";
lbInvRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Inv"; lbInvRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Inv";
lbSensorRaw.Text = lbSiteName_inv.Text + " " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_SensorAVG";
add_inv_list(lbSiteDB_inv.Text, lbSiteID_inv.Text.Substring(0, 9)); add_inv_list(lbSiteDB_inv.Text, lbSiteID_inv.Text.Substring(0, 9));
} }
} }
@ -153,21 +177,51 @@ namespace solarApp
{ {
lbMsg_station.Text = "loading ... "; lbMsg_station.Text = "loading ... ";
string date1 = dtselect_station1.Value.ToString("yyyy-MM-dd"); string date2 = dtselect_station1.Value.ToString("yyyy-MM-dd");
string date2 = dtselect_station2.Value.ToString("yyyy-MM-dd"); //string date2 = dtselect_station2.Value.ToString("yyyy-MM-dd");
string date1 = dtselect_station1.Value.AddDays(-7).ToString("yyyy-MM-dd");
gv_fic_station_raw.DataSource = stationSvc.get_station_raw(date1, lbSiteDB_inv.Text, lbSiteID_inv.Text); gv_fic_station_raw.DataSource = stationSvc.get_station_raw(date2, lbSiteDB_inv.Text, lbSiteID_inv.Text);
gv_fic_station_day.DataSource = stationSvc.get_station_rawAvg(date1, date2, lbSiteDB_inv.Text, lbSiteID_inv.Text); gv_fic_station_day.DataSource = stationSvc.get_station_rawAvg(date1, date2, lbSiteDB_inv.Text, lbSiteID_inv.Text);
gv_web_station_hour.DataSource = stationSvc.get_web_station_hour( date1 ); gv_web_station_hour.DataSource = stationSvc.get_web_station_hour(date2, lbSiteID_inv.Text);
gv_web_station_day.DataSource = stationSvc.get_web_station_day(date1, date2); gv_web_station_day.DataSource = stationSvc.get_web_station_day(date1, date2, lbSiteID_inv.Text.Substring(0, 9));
gv_web_station_month.DataSource = stationSvc.get_web_station_month(date1.Substring(0, 7), date2.Substring(0, 7));
gv_web_station_month.DataSource = stationSvc.get_web_station_month(date1.Substring(0, 7), date2.Substring(0, 7), lbSiteID_inv.Text.Substring(0, 9));
lbMsg_station.Text = " done " + System.DateTime.Now.ToShortTimeString(); lbMsg_station.Text = " done " + System.DateTime.Now.ToShortTimeString();
} }
private void btSearch_sensor_Click(object sender, EventArgs e)
{
string date2 = dtSelect_sensor1.Value.ToString("yyyy-MM-dd");
//string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd");
string date1 = dtSelect_sensor1.Value.AddDays(-7).ToString("yyyy-MM-dd");
gv_fic_sensor_raw.DataSource = sensorSvc.get_sensor_raw(date2, lbSiteDB_inv.Text, lbSiteID_inv.Text);
gv_fic_sensor_hour.DataSource = sensorSvc.get_sensor_raw_hour(date2, lbSiteDB_inv.Text, lbSiteID_inv.Text.Substring(0, 11));
gv_web_sensor_hour.DataSource = sensorSvc.get_web_sensor_hour(date2, lbSiteID_inv.Text.Substring(0, 09));
gv_web_sensor_day.DataSource = sensorSvc.get_web_sensor_day(date1, date2, lbSiteID_inv.Text.Substring(0, 09));
gv_web_sensor_month.DataSource = sensorSvc.get_web_sensor_month(date1.Substring(0, 7), date2.Substring(0, 7), lbSiteID_inv.Text.Substring(0, 09));
}
private void btSensor_switch_Click(object sender, EventArgs e)
{
string date2 = dtSelect_sensor1.Value.ToString("yyyy-MM-dd");
//string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd");
string date1 = dtSelect_sensor1.Value.AddDays(-7).ToString("yyyy-MM-dd");
gv_web_sensor_hour.DataSource = sensorSvc.get_web_sensor50_hour(date2, lbSiteID_inv.Text.Substring(0, 09));
gv_web_sensor_day.DataSource = sensorSvc.get_web_sensor50_day(date1, date2, lbSiteID_inv.Text.Substring(0, 09));
gv_web_sensor_month.DataSource = sensorSvc.get_web_sensor50_month(date1.Substring(0, 7), date2.Substring(0, 7), lbSiteID_inv.Text.Substring(0, 09));
}
} }
} }