444 lines
30 KiB
C#
444 lines
30 KiB
C#
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(string siteID, string date1) {
|
||
|
||
bool result = false;
|
||
try
|
||
{
|
||
_siteID = siteID;
|
||
_date1 = date1;
|
||
get_siteInfo();
|
||
clearData();
|
||
insert_sensor();
|
||
result = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清除指定期間、指定電站的 Sensor hour、day、month 資料
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool clearData()
|
||
{
|
||
bool result = false;
|
||
try
|
||
{
|
||
using (MySqlConnection conn = new MySqlConnection(Connection1))
|
||
{
|
||
conn.Open();
|
||
string sql = @"delete from sensor_history_hour where left(`TIMESTAMP`, 10) = @date1 and PowerStationID = @powerStationID;
|
||
delete from sensor_history_day where left(`TIMESTAMP`, 10) = @date1 and PowerStationID = @powerStationID;
|
||
delete from sensor_history_month where left(`TIMESTAMP`, 7) = left(@date1, 7) and PowerStationID = @powerStationID;
|
||
delete from sensoravg_history_hour where left(`TIMESTAMP`, 10) = @date1 and PowerStationID = @powerStationID;
|
||
delete from sensoravg_history_day where left(`TIMESTAMP`, 10) = @date1 and PowerStationID = @powerStationID;
|
||
delete from sensoravg_history_month where left(`TIMESTAMP`, 7) = left(@date1, 7) and PowerStationID = @powerStationID;";
|
||
var ds = conn.Execute(sql, new { date1 = _date1 , PowerStationID = _powerStationID });
|
||
conn.Close();
|
||
}
|
||
result = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public bool get_siteInfo()
|
||
{
|
||
bool result = false;
|
||
try
|
||
{
|
||
using (MySqlConnection conn = new MySqlConnection(Connection1))
|
||
{
|
||
conn.Open();
|
||
#region 取得 PowerStationID
|
||
string sql = @" select id , `code` siteID, siteDB, `name` siteName
|
||
from solar_master.power_station where `code` = @siteID";
|
||
var ds = conn.Query<station_list>(sql, new { siteID = _siteID }).AsList<station_list>();
|
||
foreach (var item in ds)
|
||
{
|
||
_powerStationID = item.id;
|
||
_siteDB = item.SiteDB;
|
||
_siteID01 = item.SiteID + "01";
|
||
}
|
||
#endregion
|
||
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();
|
||
string sql;
|
||
// init 關閉 group full column
|
||
sql = @"SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY,',''));";
|
||
conn.Execute(sql);
|
||
|
||
#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, EnvTemperature, Humidity, Vane, Dust)
|
||
select a.powerstationID, a.reportdate, ifnull(b.irrAvg, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust from
|
||
(
|
||
select @powerStationID powerstationID, FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') reportdate, avg(" + modelTempCol + @") Temperature,
|
||
avg(" + evnTempCol + @") envTemperature, avg(" + humCol + @") humidity, avg(" + windCol + @") Vane, avg(" + dustCol + @") Dust
|
||
from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg
|
||
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1
|
||
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 + @"), 6) irrAvg
|
||
from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg
|
||
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 " + 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 });
|
||
#endregion hour
|
||
|
||
#region day
|
||
sql = @"insert into sensor_history_day( `PowerStationId`, `TIMESTAMP`, Irradiance, Temperature, EnvTemperature, Humidity, Vane, Dust)
|
||
select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust from
|
||
(
|
||
select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(Temperature), 6) Temperature,
|
||
envTemperature, humidity, Vane, Dust
|
||
from solar_master.sensor_history_hour
|
||
where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1
|
||
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), 6) Irradiance
|
||
from solar_master.sensor_history_hour
|
||
where powerstationID = @powerstationID
|
||
and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1 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 });
|
||
#endregion day
|
||
|
||
#region month 如果跨月 會有2筆資料
|
||
|
||
sql = @"
|
||
delete from sensor_history_month where left(`TIMESTAMP`, 7) = @date1 and PowerStationID = @powerStationID;
|
||
|
||
insert into sensor_history_month( `PowerStationId`, `TIMESTAMP`, `Irradiance`, Temperature, EnvTemperature, Humidity, Vane, Dust)
|
||
select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust from
|
||
(
|
||
select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(Temperature), 6) Temperature,
|
||
envTemperature, humidity, Vane, Dust
|
||
from solar_master.sensor_history_day
|
||
where powerstationID = @powerstationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') = @date1
|
||
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m')
|
||
) a left join
|
||
(
|
||
select concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(Irradiance), 6) Irradiance
|
||
from solar_master.sensor_history_day
|
||
where powerstationID = @powerstationID
|
||
and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') = @date1 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) });
|
||
#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') = @date1
|
||
group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H');";
|
||
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 });
|
||
|
||
//日照計 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 + @"), 6) irrAvg
|
||
from " + _siteDB + ".s" + _siteID01 + @"_sensorAvg
|
||
where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 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 });
|
||
|
||
#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') = @date1
|
||
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d');";
|
||
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 });
|
||
|
||
//日照計 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 + @"), 6) irrAvg
|
||
from solar_master.sensoravg_history_hour
|
||
where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1 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 });
|
||
|
||
#endregion
|
||
|
||
#region Sensor 1~50 month
|
||
sql = @"
|
||
delete from sensoravg_history_month where left(`TIMESTAMP`, 7) = @date1 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') = @date1
|
||
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m');";
|
||
conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1.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 + @"), 6) irrAvg
|
||
from solar_master.sensoravg_history_day
|
||
where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') = @date1 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)});
|
||
|
||
#endregion
|
||
|
||
conn.Close();
|
||
result = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
bool insert_meter() {
|
||
bool result = false;
|
||
using (MySqlConnection conn = new MySqlConnection(Connection1))
|
||
{
|
||
conn.Open();
|
||
try
|
||
{
|
||
//先判斷是否存在 meter
|
||
string ss = $@"SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = '" + _siteDB + "' AND table_name = 's" + _siteID01 + "' ";
|
||
List<string> ds_meter = conn.Query<string>(ss, new { siteID = _siteID01 }).AsList<string>();
|
||
|
||
if (ds_meter.Count == 0) return false;
|
||
|
||
//每小時 table ex:s02202000101_meter
|
||
ss = @$"delete from solar_master.meter_history_hour where PowerStationId = " + _powerStationID + @" and left(`TIMESTAMP`, 10) = @date1;
|
||
INSERT INTO solar_master.meter_history_hour(PowerStationId, `TIMESTAMP`, METERID, V_AB, V_BC, V_CA, I_A, I_B, I_C, P, F, INPUT_KWH, OUTPUT_KWH)
|
||
SELECT " + _powerStationID + @" as PowerStationId, m1.`timestamp`, m1.METERID, m1.V_AB, m1.V_BC, m1.V_CA, m1.I_A, m1.I_B, m1.I_C, m1.P, m1.F, m2.INPUT_KWH, m2.OUTPUT_KWH
|
||
FROM ( -- 取得平均值
|
||
SELECT FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') AS `timestamp`, m.METERID, AVG(m.V_AB) AS V_AB,
|
||
AVG(m.V_BC) AS V_BC, AVG(m.V_CA) AS V_CA, AVG(m.I_A) AS I_A, AVG(m.I_B) AS I_B, AVG(m.I_C) AS I_C, AVG(m.P) AS P, AVG(m.F) AS F
|
||
FROM " + _siteDB + ".s" + _siteID01 + @"_meter m
|
||
WHERE FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') = @date1
|
||
GROUP BY FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), m.METERID
|
||
) m1
|
||
LEFT JOIN ( -- 取得整點值INPUT_KWH, OUTPUT_KWH
|
||
SELECT FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H') AS `timestamp`, m.METERID, m.INPUT_KWH AS INPUT_KWH, m.OUTPUT_KWH AS OUTPUT_KWH
|
||
FROM " + _siteDB + ".s" + _siteID01 + @" m
|
||
WHERE LEFT(FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), 10) = @date1
|
||
AND RIGHT(FROM_UNIXTIME(m.timestamp / 1000, '%Y-%m-%d %H'), 2) = '55' ) m2
|
||
ON m1.timestamp = m2.timestamp AND m1.METERID = m2.METERID;";
|
||
conn.Execute(ss, new { date1 = _date1 });
|
||
|
||
//每天
|
||
ss = @$" delete from solar_master.meter_history_day where PowerStationId = " + _powerStationID + @" and left(`TIMESTAMP`, 10) = @date1;
|
||
INSERT INTO solar_master.meter_history_day(PowerStationId, `TIMESTAMP`, METERID, V_AB, V_BC, V_CA, I_A, I_B, I_C, P, F, INPUT_KWH, OUTPUT_KWH)
|
||
SELECT m.PowerStationId, DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d') AS TIMESTAMP, m.METERID, AVG(m.V_AB) AS V_AB,
|
||
AVG(m.V_BC) AS V_BC, AVG(m.V_CA) AS V_CA, AVG(m.I_A) AS I_A, AVG(m.I_B) AS I_B, AVG(m.I_C) AS I_C,
|
||
AVG(m.P) AS P, AVG(m.F) AS F, AVG(m.INPUT_KWH) AS INPUT_KWH, AVG(m.OUTPUT_KWH) AS OUTPUT_KWH
|
||
FROM meter_history_hour m
|
||
WHERE DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d') = @date1 AND m.PowerStationId = @PowerStationId
|
||
GROUP BY DATE_FORMAT(m.TIMESTAMP, '%Y-%m-%d'), m.METERID;";
|
||
conn.Execute(ss, new { powerStationID = _powerStationID, date1 = _date1 });
|
||
|
||
//每月
|
||
ss = @$" delete from solar_master.meter_history_month where PowerStationId = " + _powerStationID + @" and left(`TIMESTAMP`, 10) = @date1;
|
||
INSERT INTO solar_master.meter_history_month(PowerStationId, `TIMESTAMP`, METERID, V_AB, V_BC, V_CA, I_A, I_B, I_C, P, F, INPUT_KWH, OUTPUT_KWH)
|
||
SELECT m.PowerStationId, DATE_FORMAT(m.TIMESTAMP, '%Y-%m') AS TIMESTAMP, m.METERID, AVG(m.V_AB) AS V_AB, AVG(m.V_BC) AS V_BC, AVG(m.V_CA) AS V_CA,
|
||
AVG(m.I_A) AS I_A, AVG(m.I_B) AS I_B, AVG(m.I_C) AS I_C, AVG(m.P) AS P,
|
||
AVG(m.F) AS F, AVG(m.INPUT_KWH) AS INPUT_KWH, AVG(m.OUTPUT_KWH) AS OUTPUT_KWH
|
||
FROM meter_history_day m
|
||
WHERE DATE_FORMAT(m.TIMESTAMP, '%Y-%m') = '2021-07'
|
||
AND m.PowerStationId = 1
|
||
GROUP BY DATE_FORMAT(m.TIMESTAMP, '%Y-%m'), m.METERID;";
|
||
conn.Execute(ss, new { powerStationID = _powerStationID, date1 = _date1 });
|
||
result = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
conn.Close();
|
||
}
|
||
return result;
|
||
}
|
||
|
||
}
|
||
}
|