1069 lines
70 KiB
C#
1069 lines
70 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 = string.Empty;
|
||
|
||
public procSensorSvc(string Connection_parame = null)
|
||
{
|
||
if (!string.IsNullOrEmpty(Connection_parame))
|
||
{
|
||
Connection1 = Connection_parame;
|
||
}
|
||
else
|
||
{
|
||
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 isFirst { 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;
|
||
}
|
||
|
||
public bool archiveSensorHistoryHourData(string siteID, string date1)
|
||
{
|
||
bool result = false;
|
||
_siteID = siteID;
|
||
_date1 = date1;
|
||
get_siteInfo();
|
||
insert_sensor_for_history_hour();
|
||
result = true;
|
||
return result;
|
||
}
|
||
|
||
public bool archiveData_test(string siteID, string date1)
|
||
{
|
||
|
||
bool result = false;
|
||
//try
|
||
//{
|
||
_siteID = siteID;
|
||
_date1 = date1;
|
||
get_siteInfo();
|
||
// clearData();
|
||
update_invdayhour();
|
||
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 });
|
||
MySqlCommand cmd = new MySqlCommand();
|
||
cmd.Connection = conn;
|
||
cmd.CommandTimeout = 720;
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
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()
|
||
{
|
||
procArchiveLog arclog = new procArchiveLog();
|
||
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 ";
|
||
sql = @$"select a.id, CONCAT( {_siteID} ,'01') SiteID, a.`name` sensorName, type, DBName, TableName, colname
|
||
from {_siteDB}.device a left join {_siteDB}.sharedevice b on a.id = b.deviceID
|
||
where (left(UID, 11) = @siteID or b.PowerStationID = {_powerStationID}) 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"}]}
|
||
// { "Name":"風向計","EName":"WIN"}]}
|
||
// { "Name":" 累計日照計","EName":"TPY"}]}
|
||
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 vandlst = ds_sensor.FindAll(x => x.type.Contains("VAN"));
|
||
var dustlst = ds_sensor.FindAll(x => x.type.Contains("DST")); //落塵計 add @ 2021-12-12
|
||
var winlst = ds_sensor.FindAll(x => x.type.Contains("WIN")); //風向計 add @ 2021-12-12
|
||
var irrDaylst = ds_sensor.FindAll(x => x.type.Contains("TPY")); //累計日照計 add @ 2021-12-12
|
||
//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 vanCol = string.Empty; string meterCol = string.Empty; string dustCol = string.Empty; string winCol = string.Empty; string irrDayCol = string.Empty;
|
||
irrCol = ConcatColumn(irrlst);//日照計
|
||
modelTempCol = ConcatColumn(modelTemplst);
|
||
evnTempCol = ConcatColumn(envTemplst);
|
||
humCol = ConcatColumn(humlst);
|
||
vanCol = ConcatColumn(vandlst);
|
||
dustCol = ConcatColumn(dustlst);
|
||
winCol = ConcatColumn(winlst);
|
||
irrDayCol = ConcatColumn(irrDaylst);
|
||
|
||
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
|
||
|
||
//電表
|
||
DateTime dt_start = DateTime.Now;
|
||
#endregion 獲取 Sensor 類別
|
||
try
|
||
{
|
||
#region hour
|
||
// 暫時解法 儘針對整個 Device 都是共享的情況 ---------------
|
||
string tableName = "s" + _siteID01 + "_sensorAvg";
|
||
foreach (var item in ds_sensor)
|
||
{
|
||
tableName = item.TableName;
|
||
break;
|
||
}
|
||
|
||
sql = @"insert into sensor_history_hour( `PowerStationId`, `TIMESTAMP`, Irradiance, Temperature, EnvTemperature, Humidity, Vane, Dust, WingDirection, irrDay, irrDayHour)
|
||
select a.powerstationID, a.reportdate, ifnull(b.irrAvg, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust, WingDirection, irrDay, 0 from
|
||
(
|
||
select @powerStationID powerstationID, concat(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00') reportdate, round(avg(" + modelTempCol + @"), 4) Temperature,
|
||
avg(" + evnTempCol + @") envTemperature, avg(" + humCol + @") humidity, avg(" + vanCol + @") Vane, avg(" + dustCol + @") Dust, avg(" + winCol + @") WingDirection, max(" + irrDayCol + @") irrDay
|
||
from " + _siteDB + "." + tableName + @"
|
||
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 + "." + tableName + @"
|
||
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 });
|
||
MySqlCommand cmd = new MySqlCommand();
|
||
cmd.Connection = conn;
|
||
cmd.CommandTimeout = 720;
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
int rowCT = cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
DateTime dt_start2 = DateTime.Now;
|
||
TimeSpan duration = DateTime.Now - dt_start2;
|
||
arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor hour", "0", "", rowCT.ToString(), conn, cmd);
|
||
|
||
// irrDayHour計算
|
||
//sql = $@"update sensor_history_hour a join (
|
||
// select LEFT(DATE_ADD(`TIMESTAMP`, INTERVAL +1 hour), 13) hour_1, irrday
|
||
// from sensor_history_hour a
|
||
// where powerstationID = {_powerStationID} and LEFT(a.TIMESTAMP, 10 ) = '{_date1}'
|
||
|
||
// )b on LEFT(a.`TIMESTAMP`, 13) = hour_1
|
||
// Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end
|
||
// where a.powerstationID = {_powerStationID}";
|
||
//if (isFirst)
|
||
//{
|
||
// sql = $@"DROP TEMPORARY TABLE IF EXISTS `temp_s1`;
|
||
// create TEMPORARY TABLE temp_s1
|
||
// select powerstationID, `TIMESTAMP`, irrday
|
||
// from sensor_history_hour a
|
||
// where LEFT(a.TIMESTAMP, 10 ) = '{_date1}';
|
||
// ALTER TABLE `temp_s1` ADD INDEX `temp_indexs1` (powerstationID, `TIMESTAMP`);";
|
||
// dt_start2 = DateTime.Now;
|
||
// cmd.CommandText = sql;
|
||
// int updateCT = cmd.ExecuteNonQuery();
|
||
// cmd.Parameters.Clear();
|
||
// duration = DateTime.Now - dt_start2;
|
||
// arclog.insert_log(_powerStationID, "sensor step0 irrDayHour init", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", updateCT.ToString(), conn, cmd);
|
||
//}
|
||
|
||
//sql = $@" update sensor_history_hour a join
|
||
// (select powerstationID, LEFT(DATE_ADD(`TIMESTAMP`, INTERVAL +1 hour), 13) hour_1, irrday from temp_s1 ) b
|
||
// on LEFT(a.`TIMESTAMP`, 13) = hour_1 and a.powerstationID = b.powerstationID
|
||
// Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end
|
||
// where LEFT(a.TIMESTAMP, 10 ) = '{_date1}'
|
||
// and a.powerstationID = {_powerStationID} ";
|
||
//dt_start2 = DateTime.Now;
|
||
//cmd.CommandText = sql;
|
||
//int rowCT2 = cmd.ExecuteNonQuery();
|
||
//cmd.Parameters.Clear();
|
||
//duration = DateTime.Now - dt_start2;
|
||
//arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd);
|
||
|
||
|
||
//sql = $@"update sensor_history_hour a join
|
||
// (
|
||
// select powerstationID, `TIMESTAMP`, irrday -
|
||
// (select irrday from sensor_history_hour where powerstationID = {_powerStationID} and LEFT(`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(_date1).AddHours(-1).ToString("yyyy-MM-dd HH")}' limit 1) irrdayhour
|
||
// from sensor_history_hour z
|
||
// where powerstationID = {_powerStationID} and LEFT(z.`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(_date1).ToString("yyyy-MM-dd HH")}'
|
||
// ) b on a.powerstationID = b.powerstationID and a.`TIMESTAMP` = b.`TIMESTAMP`
|
||
// Set a.irrdayhour = b.irrdayhour where a.powerstationID = {_powerStationID}";
|
||
|
||
//dt_start2 = DateTime.Now;
|
||
//cmd.CommandText = sql;
|
||
//int rowCT2 = cmd.ExecuteNonQuery();
|
||
//cmd.Parameters.Clear();
|
||
//duration = DateTime.Now - dt_start2;
|
||
//arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd);
|
||
#endregion hour
|
||
|
||
#region day
|
||
sql = @"insert into sensor_history_day( `PowerStationId`, `TIMESTAMP`, Irradiance, Temperature, EnvTemperature, Humidity, Vane, Dust, WingDirection, irrDay, irrDayHour)
|
||
select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust, a.WingDirection, a.irrDay, a.irrDayHour from
|
||
(
|
||
select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(Temperature), 6) Temperature,
|
||
envTemperature, humidity, Vane, Dust, avg(WingDirection) WingDirection, max(irrDay) irrDay, max(irrDay) irrDayHour
|
||
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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
#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, WingDirection, irrDay, irrDayHour)
|
||
select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust, WingDirection, irrDay, irrDayHour from
|
||
(
|
||
select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(Temperature), 6) Temperature,
|
||
avg(envTemperature) envTemperature, avg(humidity) humidity, avg(Vane) Vane, avg(Dust) Dust, avg(WingDirection) WingDirection, max(irrDay) irrDay, sum(irrDayHour) irrDayHour
|
||
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) });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1.Substring(0, 7));
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
#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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
|
||
//日照計 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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
}
|
||
|
||
#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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
|
||
//日照計 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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
}
|
||
|
||
#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) });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1.Substring(0, 7));
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
|
||
//日照計 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)});
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1.Substring(0, 7));
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
}
|
||
#endregion
|
||
|
||
conn.Close();
|
||
result = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
bool insert_sensor_for_history_hour()
|
||
{
|
||
procArchiveLog arclog = new procArchiveLog();
|
||
bool result = false;
|
||
using (MySqlConnection conn = new MySqlConnection(Connection1))
|
||
{
|
||
conn.Open();
|
||
string sql;
|
||
//電表
|
||
DateTime dt_start = DateTime.Now;
|
||
try
|
||
{
|
||
#region hour
|
||
MySqlCommand cmd = new MySqlCommand();
|
||
cmd.Connection = conn;
|
||
cmd.CommandTimeout = 720;
|
||
|
||
DateTime dt_start2 = DateTime.Now;
|
||
TimeSpan duration = DateTime.Now - dt_start2;
|
||
if (isFirst)
|
||
{
|
||
sql = $@"DROP TEMPORARY TABLE IF EXISTS `temp_s1`;
|
||
create TEMPORARY TABLE temp_s1
|
||
select powerstationID, `TIMESTAMP`, irrday
|
||
from sensor_history_hour a
|
||
where LEFT(a.TIMESTAMP, 10 ) = '{_date1}';
|
||
ALTER TABLE `temp_s1` ADD INDEX `temp_indexs1` (powerstationID, `TIMESTAMP`);";
|
||
dt_start2 = DateTime.Now;
|
||
cmd.CommandText = sql;
|
||
int updateCT = cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
duration = DateTime.Now - dt_start2;
|
||
arclog.insert_log(_powerStationID, "sensor step0 irrDayHour init", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", updateCT.ToString(), conn, cmd);
|
||
}
|
||
|
||
sql = $@" update sensor_history_hour a join
|
||
(select powerstationID, LEFT(DATE_ADD(`TIMESTAMP`, INTERVAL +1 hour), 13) hour_1, irrday from temp_s1 ) b
|
||
on LEFT(a.`TIMESTAMP`, 13) = hour_1 and a.powerstationID = b.powerstationID
|
||
Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end
|
||
where LEFT(a.TIMESTAMP, 10 ) = '{_date1}'
|
||
and a.powerstationID = {_powerStationID} ";
|
||
dt_start2 = DateTime.Now;
|
||
cmd.CommandText = sql;
|
||
int rowCT2 = cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
duration = DateTime.Now - dt_start2;
|
||
arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd);
|
||
#endregion hour
|
||
|
||
conn.Close();
|
||
result = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public bool archiveMeterData(string siteID, string date1)
|
||
{
|
||
|
||
bool result = false;
|
||
try
|
||
{
|
||
_siteID = siteID;
|
||
_date1 = date1;
|
||
get_siteInfo();
|
||
//clearMeterData();
|
||
insert_meter();
|
||
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 + "_meter' ";
|
||
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 + @"_meter 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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 當天-前一小時 累計日照相減值
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
bool update_invdayhour()
|
||
{
|
||
bool result = false;
|
||
try
|
||
{
|
||
MySqlCommand cmd = new MySqlCommand();
|
||
procArchiveLog arclog = new procArchiveLog();
|
||
using (MySqlConnection conn = new MySqlConnection(Connection1))
|
||
{
|
||
conn.Open();
|
||
string sql = $@"update sensor_history_hour a join
|
||
(
|
||
select powerstationID, `TIMESTAMP`, irrday -
|
||
(select irrday from sensor_history_hour where powerstationID = {_powerStationID} and LEFT(`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(_date1).AddHours(-1).ToString("yyyy-MM-dd HH")}' limit 1) irrdayhour
|
||
from sensor_history_hour z
|
||
where powerstationID = {_powerStationID} and LEFT(z.`TIMESTAMP`, 13 ) = '{(Convert.ToDateTime(_date1)).ToString("yyyy-MM-dd HH")}'
|
||
) b on a.powerstationID = b.powerstationID and a.`TIMESTAMP` = b.`TIMESTAMP`
|
||
Set a.irrdayhour = case when b.irrdayhour < 0 then 0 else b.irrdayhour end
|
||
where a.powerstationID = {_powerStationID}";
|
||
cmd.Connection = conn;
|
||
cmd.CommandTimeout = 720;
|
||
DateTime dt_start2 = DateTime.Now;
|
||
cmd.CommandText = sql;
|
||
int rowCT2 = cmd.ExecuteNonQuery();
|
||
dt_start2 = DateTime.Now;
|
||
TimeSpan duration = DateTime.Now - dt_start2;
|
||
arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd);
|
||
}
|
||
result = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
return result;
|
||
|
||
}
|
||
|
||
bool insert_sensor_irrdayHour()
|
||
{
|
||
procArchiveLog arclog = new procArchiveLog();
|
||
bool result = false;
|
||
bool isFirst = true;
|
||
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 ";
|
||
sql = @$"select a.id, CONCAT( {_siteID} ,'01') SiteID, a.`name` sensorName, type, DBName, TableName, colname
|
||
from {_siteDB}.device a left join {_siteDB}.sharedevice b on a.id = b.deviceID
|
||
where (left(UID, 11) = @siteID or b.PowerStationID = {_powerStationID}) 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"}]}
|
||
// { "Name":"風向計","EName":"WIN"}]}
|
||
// { "Name":" 累計日照計","EName":"TPY"}]}
|
||
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 vandlst = ds_sensor.FindAll(x => x.type.Contains("VAN"));
|
||
var dustlst = ds_sensor.FindAll(x => x.type.Contains("DST")); //落塵計 add @ 2021-12-12
|
||
var winlst = ds_sensor.FindAll(x => x.type.Contains("WIN")); //風向計 add @ 2021-12-12
|
||
var irrDaylst = ds_sensor.FindAll(x => x.type.Contains("TPY")); //累計日照計 add @ 2021-12-12
|
||
//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 vanCol = string.Empty; string meterCol = string.Empty; string dustCol = string.Empty; string winCol = string.Empty; string irrDayCol = string.Empty;
|
||
irrCol = ConcatColumn(irrlst);//日照計
|
||
modelTempCol = ConcatColumn(modelTemplst);
|
||
evnTempCol = ConcatColumn(envTemplst);
|
||
humCol = ConcatColumn(humlst);
|
||
vanCol = ConcatColumn(vandlst);
|
||
dustCol = ConcatColumn(dustlst);
|
||
winCol = ConcatColumn(winlst);
|
||
irrDayCol = ConcatColumn(irrDaylst);
|
||
|
||
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
|
||
|
||
//電表
|
||
DateTime dt_start = DateTime.Now;
|
||
#endregion 獲取 Sensor 類別
|
||
try
|
||
{
|
||
#region hour
|
||
// 暫時解法 儘針對整個 Device 都是共享的情況 ---------------
|
||
string tableName = "s" + _siteID01 + "_sensorAvg";
|
||
foreach (var item in ds_sensor)
|
||
{
|
||
tableName = item.TableName;
|
||
break;
|
||
}
|
||
|
||
sql = @"insert into sensor_history_hour( `PowerStationId`, `TIMESTAMP`, Irradiance, Temperature, EnvTemperature, Humidity, Vane, Dust, WingDirection, irrDay, irrDayHour)
|
||
select a.powerstationID, a.reportdate, ifnull(b.irrAvg, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust, WingDirection, irrDay, 0 from
|
||
(
|
||
select @powerStationID powerstationID, concat(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H'), ':00') reportdate, round(avg(" + modelTempCol + @"), 4) Temperature,
|
||
avg(" + evnTempCol + @") envTemperature, avg(" + humCol + @") humidity, avg(" + vanCol + @") Vane, avg(" + dustCol + @") Dust, avg(" + winCol + @") WingDirection, max(" + irrDayCol + @") irrDay
|
||
from " + _siteDB + "." + tableName + @"
|
||
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 + "." + tableName + @"
|
||
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 });
|
||
MySqlCommand cmd = new MySqlCommand();
|
||
cmd.Connection = conn;
|
||
cmd.CommandTimeout = 720;
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
int rowCT = cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
DateTime dt_start2 = DateTime.Now;
|
||
TimeSpan duration = DateTime.Now - dt_start2;
|
||
arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor hour", "0", "", rowCT.ToString(), conn, cmd);
|
||
|
||
// irrDayHour計算
|
||
//sql = $@"update sensor_history_hour a join (
|
||
// select LEFT(DATE_ADD(`TIMESTAMP`, INTERVAL +1 hour), 13) hour_1, irrday
|
||
// from sensor_history_hour a
|
||
// where powerstationID = {_powerStationID} and LEFT(a.TIMESTAMP, 10 ) = '{_date1}'
|
||
|
||
// )b on LEFT(a.`TIMESTAMP`, 13) = hour_1
|
||
// Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end
|
||
// where a.powerstationID = {_powerStationID}";
|
||
if (isFirst)
|
||
{
|
||
sql = $@"DROP TEMPORARY TABLE IF EXISTS `temp_s1`;
|
||
create TEMPORARY TABLE temp_s1
|
||
select powerstationID, `TIMESTAMP`, irrday
|
||
from sensor_history_hour a
|
||
where LEFT(a.TIMESTAMP, 10 ) = '{_date1}';
|
||
ALTER TABLE `temp_s1` ADD INDEX `temp_indexs1` (powerstationID, `TIMESTAMP`);";
|
||
dt_start2 = DateTime.Now;
|
||
cmd.CommandText = sql;
|
||
int updateCT = cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
duration = DateTime.Now - dt_start2;
|
||
arclog.insert_log(_powerStationID, "sensor step0 irrDayHour init", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", updateCT.ToString(), conn, cmd);
|
||
}
|
||
|
||
sql = $@" update sensor_history_hour a join
|
||
(select LEFT(DATE_ADD(`TIMESTAMP`, INTERVAL +1 hour), 13) hour_1, irrday from temp_s1 ) b
|
||
on LEFT(a.`TIMESTAMP`, 13) = hour_1 and a.powerstationID = {_powerStationID}
|
||
Set irrDayhour = case when a.`irrday` - b.`irrday` < 0 then 0 else a.`irrday` - b.`irrday` end
|
||
where LEFT(a.TIMESTAMP, 10 ) = '{_date1}'
|
||
and a.powerstationID = {_powerStationID} ";
|
||
dt_start2 = DateTime.Now;
|
||
cmd.CommandText = sql;
|
||
int rowCT2 = cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
duration = DateTime.Now - dt_start2;
|
||
arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd);
|
||
|
||
|
||
//sql = $@"update sensor_history_hour a join
|
||
// (
|
||
// select powerstationID, `TIMESTAMP`, irrday -
|
||
// (select irrday from sensor_history_hour where powerstationID = {_powerStationID} and LEFT(`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(_date1).AddHours(-1).ToString("yyyy-MM-dd HH")}' limit 1) irrdayhour
|
||
// from sensor_history_hour z
|
||
// where powerstationID = {_powerStationID} and LEFT(z.`TIMESTAMP`, 13 ) = '{Convert.ToDateTime(_date1).ToString("yyyy-MM-dd HH")}'
|
||
// ) b on a.powerstationID = b.powerstationID and a.`TIMESTAMP` = b.`TIMESTAMP`
|
||
// Set a.irrdayhour = b.irrdayhour where a.powerstationID = {_powerStationID}";
|
||
|
||
//dt_start2 = DateTime.Now;
|
||
//cmd.CommandText = sql;
|
||
//int rowCT2 = cmd.ExecuteNonQuery();
|
||
//cmd.Parameters.Clear();
|
||
//duration = DateTime.Now - dt_start2;
|
||
//arclog.insert_log(_powerStationID, "sensor step1 for irrDayHour", duration.TotalSeconds, "sensor_history_hour", "update sensor hour", "0", "", rowCT2.ToString(), conn, cmd);
|
||
#endregion hour
|
||
|
||
#region day
|
||
sql = @"insert into sensor_history_day( `PowerStationId`, `TIMESTAMP`, Irradiance, Temperature, EnvTemperature, Humidity, Vane, Dust, WingDirection, irrDay, irrDayHour)
|
||
select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust, a.WingDirection, a.irrDay, a.irrDayHour from
|
||
(
|
||
select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d'), ' 00:00') reportdate, round(avg(Temperature), 6) Temperature,
|
||
envTemperature, humidity, Vane, Dust, avg(WingDirection) WingDirection, max(irrDay) irrDay, max(irrDay) irrDayHour
|
||
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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
#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, WingDirection, irrDay, irrDayHour)
|
||
select a.powerstationID, a.reportdate, ifnull(b.Irradiance, 0) irrAvg, a.Temperature, a.envTemperature, a.humidity, a.Vane, a.Dust, WingDirection, irrDay, irrDayHour from
|
||
(
|
||
select powerStationID , concat(DATE_FORMAT(`TIMESTAMP`,'%Y-%m'), '-01 00:00') reportdate, round(avg(Temperature), 6) Temperature,
|
||
avg(envTemperature) envTemperature, avg(humidity) humidity, avg(Vane) Vane, avg(Dust) Dust, avg(WingDirection) WingDirection, max(irrDay) irrDay, sum(irrDay) irrDayHour
|
||
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) });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1.Substring(0, 7));
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
#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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
|
||
//日照計 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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
}
|
||
|
||
#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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
|
||
//日照計 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 });
|
||
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
|
||
cmd.Parameters.AddWithValue("@date1", _date1);
|
||
cmd.CommandText = sql;
|
||
cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
}
|
||
|
||
#endregion
|
||
|
||
conn.Close();
|
||
result = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
}
|
||
}
|