192 lines
10 KiB
C#
192 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using MySql.Data.MySqlClient;
|
|
using Dapper;
|
|
using solarApp.Model;
|
|
using System.Configuration;
|
|
|
|
namespace solarApp.Service
|
|
{
|
|
public class procSyncError
|
|
{
|
|
string Connection1 = string.Empty;
|
|
|
|
public procSyncError(string Connection_parame = null)
|
|
{
|
|
if (!string.IsNullOrEmpty(Connection_parame))
|
|
{
|
|
Connection1 = Connection_parame;
|
|
}
|
|
else
|
|
{
|
|
Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
|
|
}
|
|
}
|
|
|
|
public bool syncErrData()
|
|
{
|
|
bool result = false;
|
|
procArchiveLog arclog = new procArchiveLog();
|
|
try
|
|
{
|
|
using (MySqlConnection conn = new MySqlConnection(Connection1))
|
|
{
|
|
DateTime dt_start = DateTime.Now;
|
|
conn.Open();
|
|
#region 更新 Normal time
|
|
dt_start = DateTime.Now;
|
|
string sql = $@" update`err_main` a join alarmorion_orionalarmrecord b on a.id = b.id set
|
|
a.`sourcestate` = b.`sourcestate`,
|
|
a.`ackstate` = b.`ackstate`,
|
|
a.`priority` = b.`priority`,
|
|
a.`normaltime` = b.`normaltime`,
|
|
a.`acktime` = b.`acktime`,
|
|
a.`lastupdate` = b.`lastupdate`
|
|
where a.sourcestate = 1 and b.datestamp >= '{ System.DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd")}'";// and a.site_id is not null "; // ;";
|
|
int rowCount = conn.Execute(sql);
|
|
TimeSpan durationSec = DateTime.Now - dt_start;
|
|
arclog.insert_log("0", "syncError s0 normaltime", durationSec.TotalSeconds, "orionAlarmRecord", "err_main", "0", "", rowCount.ToString(), conn, null);
|
|
|
|
#endregion
|
|
|
|
#region 取得 昨天到今天的尚未同步的異常資料
|
|
sql = $@" select * from alarmorion_orionalarmrecord
|
|
where id not in (select id from err_main )
|
|
and datestamp >= '{System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}'";
|
|
//sql = $@"select * from err_main
|
|
// where site_id is null and datestamp >= '{System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}'";
|
|
var ds = conn.Query<errMain_model>(sql).AsList<errMain_model>();
|
|
StringBuilder sb = new StringBuilder();
|
|
bool isfirst = true;
|
|
foreach (var item in ds)
|
|
{
|
|
if (isfirst)
|
|
{
|
|
isfirst = false;
|
|
sb.Append(item.id);
|
|
}
|
|
else {
|
|
sb.Append($", {item.id}");
|
|
}
|
|
}
|
|
#endregion
|
|
if (ds.Count > 0)
|
|
{
|
|
#region 新增異常值 err_main
|
|
sql = "";
|
|
string sql_header = "INSERT INTO `err_main`(`id`, `timestamp`, `datestamp`, `uuidHash`, `uuid`, `isOpen`, `sourceState`, `ackState`, `ackRequired`, `alarmClass`, `priority`, `normalTime`, `ackTime`, `userAccount`, `alarmTransition`, `lastUpdate`) ";
|
|
foreach (var item in ds)
|
|
{ // //{item.uuidHash}, '{Convert.ToBase64String(item.uuid)}'
|
|
sql += sql_header + $@" VALUES ({item.id}, {item.timestamp}, '{DateTime.Parse(item.datestamp).ToString("yyyy-MM-dd")}', 0, 0
|
|
, {item.isOpen}, {item.sourceState}, {item.ackState}, {item.ackRequired}, {item.alarmClass}, {item.priority}, {item.normalTime}, {item.ackTime}
|
|
, '{item.userAccount}', {item.alarmTransition}, {item.lastUpdate}); ";
|
|
}
|
|
int rowCT = conn.Execute(sql);
|
|
TimeSpan duration = DateTime.Now - dt_start;
|
|
arclog.insert_log("0", "syncError s1", duration.TotalSeconds, "orionAlarmRecord", "err_main", "0", "", rowCT.ToString(), conn, null);
|
|
#endregion
|
|
|
|
#region update site_id, inverterID
|
|
dt_start = DateTime.Now;
|
|
sql = $@"update err_main a join alarmorion_orionalarmfacetvalue b on a.id = b.alarm
|
|
set err_valuekind = left(`value`, 1),
|
|
errValue = SUBSTRING_INDEX(
|
|
SUBSTRING_INDEX(`value`, '@', 1) # @前面所有string --> e:7
|
|
, ':', -1)
|
|
where facetName = 4 and alarm in ({sb.ToString()});";
|
|
rowCT = conn.Execute(sql);
|
|
duration = DateTime.Now - dt_start;
|
|
arclog.insert_log("0", "syncError s2 errValue", duration.TotalSeconds, "facetvalue4", "err_main", "0", "", rowCT.ToString(), conn, null);
|
|
#endregion
|
|
|
|
|
|
#region update site_id, device_id
|
|
dt_start = DateTime.Now;
|
|
sql = $@"update err_main a join alarmorion_orionalarmfacetvalue b on a.id = b.alarm
|
|
set err_valuekind = left(`value`, 1),
|
|
errDevice = SUBSTRING_INDEX(SUBSTRING_INDEX(`value`, ':', -1) # : 後面所有string
|
|
,'S', -1),
|
|
a.site_id = substring(
|
|
SUBSTRING_INDEX(SUBSTRING_INDEX(`value`, ':', -1) # : 後面所有string
|
|
,'S', -1)
|
|
, 1, 9)
|
|
where facetName = 8 and errDevice is null and a.datestamp >= '{System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}';";
|
|
rowCT = conn.Execute(sql);
|
|
duration = DateTime.Now - dt_start;
|
|
arclog.insert_log("0", "syncError s3 errDevice", duration.TotalSeconds, "facetValue8", "err_main", "0", "", rowCT.ToString(), conn, null);
|
|
|
|
#endregion
|
|
|
|
#region update 設備廠牌
|
|
dt_start = DateTime.Now;
|
|
sql = $@"update err_main a join v_company_inv b on a.errDevice = b.INVERTERID set
|
|
errDeviceBrand = b.brand,
|
|
errDeviceModel = b.Model
|
|
where errDevice is not null and datestamp >= '{System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")}';";
|
|
rowCT = conn.Execute(sql);
|
|
duration = DateTime.Now - dt_start;
|
|
arclog.insert_log("0", "syncError s4 site_id", duration.TotalSeconds, "v_company_inv", "err_main", "0", "", rowCT.ToString(), conn, null);
|
|
#endregion
|
|
|
|
#region 更新全部的燈號
|
|
dt_start = DateTime.Now;
|
|
sql = $@"DROP TABLE IF EXISTS `temp_healthStatus`;
|
|
create table temp_healthStatus
|
|
select v.siteName, z.* from (
|
|
select ROW_NUMBER() OVER (PARTITION BY site_id ORDER BY HealthStatus desc) row_num,
|
|
site_id, alarmID, datestamp, HealthStatus
|
|
from (
|
|
select site_id, datestamp, sourceState, id alarmID,
|
|
case when alarmClass = 4 then 3 # Network 控制盒斷線 - 紅燈
|
|
when alarmClass != 4 then 2 end HealthStatus # inv, sensor 異常 黃燈
|
|
from err_main
|
|
where sourceState = 1 and left(site_id , 1) = '0'
|
|
and datestamp > '{ System.DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd")}'
|
|
order by HealthStatus desc
|
|
limit 100
|
|
) x
|
|
)z join v_station v on z.site_id = v.siteid
|
|
where row_num = 1;";
|
|
conn.Execute(sql);
|
|
|
|
|
|
sql = $@"update power_station set healthstatus = 1;
|
|
update power_station a join temp_healthstatus b on a.code = b.site_id
|
|
set a.healthstatus = b.healthstatus;";
|
|
rowCT = conn.Execute(sql);
|
|
duration = DateTime.Now - dt_start;
|
|
arclog.insert_log("0", "syncError s5 site health", duration.TotalSeconds, "temp_healthstatus", "err_main", "0", "", rowCT.ToString(), conn, null);
|
|
|
|
#endregion
|
|
|
|
#region 更新 Inverter 燈號
|
|
dt_start = DateTime.Now;
|
|
sql = $@"update inv_status set status = 1;
|
|
update inv_status a join
|
|
(
|
|
select errDevice, 2 errStatus
|
|
from err_main -- 有資料 = 異常
|
|
where sourceState = 1 and left(errDevice, 1) = '0'
|
|
group by errDevice
|
|
)b on a.inverterid = b.errDevice
|
|
set status = errStatus;";
|
|
rowCT = conn.Execute(sql);
|
|
duration = DateTime.Now - dt_start;
|
|
arclog.insert_log("0", "syncError s6 inv health", duration.TotalSeconds, "", "err_main", "0", "", rowCT.ToString(), conn, null);
|
|
|
|
#endregion
|
|
}
|
|
conn.Close();
|
|
}
|
|
result = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|