FIC_Solar/solarApp/Service/procSensorSvc.cs
2021-08-09 01:25:18 +08:00

363 lines
25 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() {
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;
}
}
}