[後台-後端] 調整後台同步記錄流程

[前台-前端] 修改判斷名稱
This commit is contained in:
dev02 2023-11-16 11:32:32 +08:00
parent 3a0ead386b
commit fd6657a6d4
6 changed files with 510 additions and 39 deletions

View File

@ -14,6 +14,7 @@ using NPOI.SS.Formula.Functions;
using Repository.Helper; using Repository.Helper;
using Microsoft.AspNetCore.Routing.Matching; using Microsoft.AspNetCore.Routing.Matching;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Diagnostics;
namespace Backend.Controllers namespace Backend.Controllers
{ {
@ -23,6 +24,10 @@ namespace Backend.Controllers
//private readonly IDeviceImportRepository deviceImportRepository; //private readonly IDeviceImportRepository deviceImportRepository;
private readonly INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository; private readonly INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository;
private readonly IBackEndConfigHelper backEndConfig; private readonly IBackEndConfigHelper backEndConfig;
private Stopwatch stopwatch = new Stopwatch();
private Stopwatch stopwatchSection = new Stopwatch();
private Stopwatch syncStopWatch = new Stopwatch();
public NiagaraDataSynchronizeController(IBackendRepository backendRepository, INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository,IBackEndConfigHelper backEndConfig) public NiagaraDataSynchronizeController(IBackendRepository backendRepository, INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository,IBackEndConfigHelper backEndConfig)
{ {
this.backendRepository = backendRepository; this.backendRepository = backendRepository;
@ -43,8 +48,9 @@ namespace Backend.Controllers
[HttpPost] [HttpPost]
public Task<ApiResult<List<Device_value>>> RawDataList() public Task<ApiResult<List<Device_value>>> RawDataList()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
ApiResult<List<Device_value>> apiResult = new ApiResult<List<Device_value>>(); ApiResult<List<Device_value>> apiResult = new ApiResult<List<Device_value>>();
try try
{ {
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'";
@ -62,6 +68,8 @@ namespace Backend.Controllers
string top100 = "";//" top 100 "; string top100 = "";//" top 100 ";
webRequestService svc = new webRequestService(); webRequestService svc = new webRequestService();
var urlSlots = backendRepository.GetAllAsync<string>("select obixSlot from building where deleted = 0").Result; var urlSlots = backendRepository.GetAllAsync<string>("select obixSlot from building where deleted = 0").Result;
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
foreach (var us in urlSlots) foreach (var us in urlSlots)
{ {
string bql = us + "bql:select " + top100 + " * from baja:Folder "; string bql = us + "bql:select " + top100 + " * from baja:Folder ";
@ -69,7 +77,8 @@ namespace Backend.Controllers
//ds = svc.obixQuery("http://localhost:8080/obix/config/Program/ObixQuery/query/", bql); //ds = svc.obixQuery("http://localhost:8080/obix/config/Program/ObixQuery/query/", bql);
ds.AddRange(svc.obixQuery(obixApiConfig.ApiBase, bql, obixApiConfig.TagQuantity, obixApiConfig.UserName, obixApiConfig.Password));//三菱 ds.AddRange(svc.obixQuery(obixApiConfig.ApiBase, bql, obixApiConfig.TagQuantity, obixApiConfig.UserName, obixApiConfig.Password));//三菱
} }
stopwatchSection.Stop();
niagaraDataSynchronizeRepository.KeepTimeLog("GetDataFromNiagara", stopwatchSection.ElapsedMilliseconds);
apiResult.Code = "0000"; apiResult.Code = "0000";
apiResult.Data = ds; apiResult.Data = ds;
} }
@ -82,6 +91,8 @@ namespace Backend.Controllers
} }
//return apiResult; //return apiResult;
stopwatchSection.Stop();
niagaraDataSynchronizeRepository.KeepTimeLog("RawDataList", stopwatch.ElapsedMilliseconds);
return Task.FromResult(apiResult); return Task.FromResult(apiResult);
} }
@ -92,8 +103,10 @@ namespace Backend.Controllers
[HttpPost] [HttpPost]
public async Task<ApiResult<bool>> CompareData([FromBody] List<Device_value> ds) public async Task<ApiResult<bool>> CompareData([FromBody] List<Device_value> ds)
{ {
syncStopWatch.Start();
stopwatch = new Stopwatch();
stopwatch.Start();
bool result = false; bool result = false;
ApiResult<bool> apiResult = new ApiResult<bool>(); ApiResult<bool> apiResult = new ApiResult<bool>();
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'";
var variableObix = await backendRepository.GetAllAsync<Backend.Models.KeyValue>(sqlObix); var variableObix = await backendRepository.GetAllAsync<Backend.Models.KeyValue>(sqlObix);
@ -101,6 +114,8 @@ namespace Backend.Controllers
try try
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
string ss = ds.Where(x => x.tag_name != "").FirstOrDefault().tag_name; string ss = ds.Where(x => x.tag_name != "").FirstOrDefault().tag_name;
var data = ds.Where(x => x.tag_name != ""); var data = ds.Where(x => x.tag_name != "");
List<string> building = null; List<string> building = null;
@ -114,16 +129,62 @@ namespace Backend.Controllers
//building.Add(ss.Split("_")[1]); //building.Add(ss.Split("_")[1]);
building = data.GroupBy(x => x.tag_name.Split("_")[1]).Select(x => x.Key).ToList(); building = data.GroupBy(x => x.tag_name.Split("_")[1]).Select(x => x.Key).ToList();
} }
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("filter building tag", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
string LightSwitchLevel = await niagaraDataSynchronizeRepository.getLightSwitchLevel(); //獲取照明開關 是否在 device or device_node string LightSwitchLevel = await niagaraDataSynchronizeRepository.getLightSwitchLevel(); //獲取照明開關 是否在 device or device_node
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("getLightSwitchLevel", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building, tag_quantity); // 匯入 MySQL table: import_niagara_tag await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building, tag_quantity); // 匯入 MySQL table: import_niagara_tag
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("InsertNiagaraTagList", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.DeviceComparison(LightSwitchLevel); await niagaraDataSynchronizeRepository.DeviceComparison(LightSwitchLevel);
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("DeviceComparison", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.CheckDiffFullNameAndCover(); // * await niagaraDataSynchronizeRepository.CheckDiffFullNameAndCover(); // *
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("CheckDiffFullNameAndCover", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.CheckFullNameEmptyReplaceByDeviceName(); await niagaraDataSynchronizeRepository.CheckFullNameEmptyReplaceByDeviceName();
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("CheckFullNameEmptyReplaceByDeviceName", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.InsertBuildingMenu(); await niagaraDataSynchronizeRepository.InsertBuildingMenu();
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("InsertBuildingMenu", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.InsertSubSystemFloor(); await niagaraDataSynchronizeRepository.InsertSubSystemFloor();
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("InsertSubSystemFloor", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.InsertFloor(); await niagaraDataSynchronizeRepository.InsertFloor();
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("InsertFloor", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await this.DeviceDisasterAsync(); await this.DeviceDisasterAsync();
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("DeviceDisasterAsync", stopwatchSection.ElapsedMilliseconds);
result = true; result = true;
apiResult.Code = "0000"; apiResult.Code = "0000";
@ -136,6 +197,8 @@ namespace Backend.Controllers
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message + Environment.NewLine + exception.StackTrace); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message + Environment.NewLine + exception.StackTrace);
} }
stopwatch.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("CompareData", stopwatch.ElapsedMilliseconds);
return apiResult; return apiResult;
} }
@ -147,13 +210,15 @@ namespace Backend.Controllers
public Task<ApiResult<List<ImpNiaItem>>> RawDataDevItemList([FromBody] Device_value building) public Task<ApiResult<List<ImpNiaItem>>> RawDataDevItemList([FromBody] Device_value building)
{ {
ApiResult<List<ImpNiaItem>> apiResult = new ApiResult<List<ImpNiaItem>>(); ApiResult<List<ImpNiaItem>> apiResult = new ApiResult<List<ImpNiaItem>>();
stopwatch = new Stopwatch();
stopwatch.Start();
try try
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
var ds = new List<ImpNiaItem>(); var ds = new List<ImpNiaItem>();
webRequestService svc = new webRequestService(); webRequestService svc = new webRequestService();
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'";
var variableObix = backendRepository.GetAllAsync<Backend.Models.KeyValue>(sqlObix).Result; var variableObix = backendRepository.GetAllAsync<Backend.Models.KeyValue>(sqlObix).Result;
var obixApiConfig = new Backend.Models.ObixApiConfig(); var obixApiConfig = new Backend.Models.ObixApiConfig();
@ -179,6 +244,8 @@ namespace Backend.Controllers
{ {
niagaraDataSynchronizeRepository.DeviceControlPoint(data2); niagaraDataSynchronizeRepository.DeviceControlPoint(data2);
} }
stopwatchSection.Stop();
niagaraDataSynchronizeRepository.KeepTimeLog("get device_item from niagara and record all device point", stopwatchSection.ElapsedMilliseconds);
var buildings = backendRepository.GetAllAsync<string>("select building_tag from building where deleted = 0").Result; var buildings = backendRepository.GetAllAsync<string>("select building_tag from building where deleted = 0").Result;
apiResult.Code = "0000"; apiResult.Code = "0000";
apiResult.Data = ds.Where(x => buildings.Contains(x.device_building_tag)).ToList(); apiResult.Data = ds.Where(x => buildings.Contains(x.device_building_tag)).ToList();
@ -191,6 +258,8 @@ namespace Backend.Controllers
} }
//return apiResult; //return apiResult;
stopwatch.Stop();
niagaraDataSynchronizeRepository.KeepTimeLog("RawDataDevItemList", stopwatch.ElapsedMilliseconds);
return Task.FromResult(apiResult); return Task.FromResult(apiResult);
} }
@ -201,6 +270,8 @@ namespace Backend.Controllers
[HttpPost] [HttpPost]
public async Task<ApiResult<bool>> DevIteComData([FromBody] List<ImpNiaItem> ds) public async Task<ApiResult<bool>> DevIteComData([FromBody] List<ImpNiaItem> ds)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
bool result = false; bool result = false;
var oldDs = JsonConvert.DeserializeObject<List<ImpNiaItem>>(JsonConvert.SerializeObject(ds)); var oldDs = JsonConvert.DeserializeObject<List<ImpNiaItem>>(JsonConvert.SerializeObject(ds));
ApiResult<bool> apiResult = new ApiResult<bool>(); ApiResult<bool> apiResult = new ApiResult<bool>();
@ -223,21 +294,59 @@ namespace Backend.Controllers
obixApiConfig.Password = variableObix.Where(x => x.Name == "Password").Select(x => x.Value).FirstOrDefault(); obixApiConfig.Password = variableObix.Where(x => x.Name == "Password").Select(x => x.Value).FirstOrDefault();
//obixApiConfig.UrlSlot = variableObix.Where(x => x.Name == "url_slot").Select(x => x.Value).FirstOrDefault(); //obixApiConfig.UrlSlot = variableObix.Where(x => x.Name == "url_slot").Select(x => x.Value).FirstOrDefault();
webRequestService svc = new webRequestService(); webRequestService svc = new webRequestService();
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
var data = svc.obixHisQuery_v2(obixApiConfig.ApiBase + "obix/config/Program/ObixQuery/query/", obixApiConfig.ApiBase + "obix/histories", "", var data = svc.obixHisQuery_v2(obixApiConfig.ApiBase + "obix/config/Program/ObixQuery/query/", obixApiConfig.ApiBase + "obix/histories", "",
obixApiConfig.UserName, obixApiConfig.Password); obixApiConfig.UserName, obixApiConfig.Password);
ds.AddRange(data); ds.AddRange(data);
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("Get history device point from niagara", stopwatchSection.ElapsedMilliseconds);
// save to history // save to history
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.InsertItemFromNiagara_History(ds, building); // insert 暫存table import_niagara_item_history await niagaraDataSynchronizeRepository.InsertItemFromNiagara_History(ds, building); // insert 暫存table import_niagara_item_history
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("InsertItemFromNiagara_History", stopwatchSection.ElapsedMilliseconds);
if (!string.IsNullOrEmpty(isDome) && isDome.Contains("ibms_dome")) if (!string.IsNullOrEmpty(isDome) && isDome.Contains("ibms_dome"))
{
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.RecoverSysTag("import_niagara_item_history"); // recover missing deivce_system_tag await niagaraDataSynchronizeRepository.RecoverSysTag("import_niagara_item_history"); // recover missing deivce_system_tag
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("RecoverSysTag import_niagara_item_history", stopwatchSection.ElapsedMilliseconds);
}
#endregion #endregion
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.InsertItemFromNiagara(oldDs, building); // insert 暫存table import_niagara_item await niagaraDataSynchronizeRepository.InsertItemFromNiagara(oldDs, building); // insert 暫存table import_niagara_item
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("InsertItemFromNiagara", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.DeviceItemComparison(); //insert device_item await niagaraDataSynchronizeRepository.DeviceItemComparison(); //insert device_item
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("DeviceItemComparison", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.CheckItemDiffFullNameAndCover(); // update device_item.fullname await niagaraDataSynchronizeRepository.CheckItemDiffFullNameAndCover(); // update device_item.fullname
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("CheckItemDiffFullNameAndCover", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.ItemCheckFullNameEmptyReplaceByDeviceName(); // 檢查device_item內FullName為空的值以points取代 await niagaraDataSynchronizeRepository.ItemCheckFullNameEmptyReplaceByDeviceName(); // 檢查device_item內FullName為空的值以points取代
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("ItemCheckFullNameEmptyReplaceByDeviceName", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await niagaraDataSynchronizeRepository.CheckItemIsShowHistory(); await niagaraDataSynchronizeRepository.CheckItemIsShowHistory();
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("CheckItemIsShowHistory", stopwatchSection.ElapsedMilliseconds);
result = true; result = true;
@ -253,11 +362,18 @@ namespace Backend.Controllers
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message + Environment.NewLine + exception.StackTrace); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message + Environment.NewLine + exception.StackTrace);
} }
stopwatch.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("DevIteComData", stopwatch.ElapsedMilliseconds);
syncStopWatch.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("Full sync usage record", syncStopWatch.ElapsedMilliseconds);
return apiResult; return apiResult;
} }
private async Task DeviceDisasterAsync() private async Task DeviceDisasterAsync()
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'";
var variableObix = await backendRepository.GetAllAsync<Backend.Models.KeyValue>(sqlObix); var variableObix = await backendRepository.GetAllAsync<Backend.Models.KeyValue>(sqlObix);
var obixApiConfig = new Models.ObixApiConfig(); var obixApiConfig = new Models.ObixApiConfig();
@ -281,6 +397,8 @@ namespace Backend.Controllers
await niagaraDataSynchronizeRepository.DeviceDisasterAysnc(dv); await niagaraDataSynchronizeRepository.DeviceDisasterAysnc(dv);
} }
stopwatchSection.Stop();
await niagaraDataSynchronizeRepository.KeepTimeLog("DeviceDisasterAsync", stopwatchSection.ElapsedMilliseconds);
} }
} }
} }

View File

@ -7,13 +7,27 @@ using Repository.Models;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Xml.Linq; using System.Xml.Linq;
using System.Diagnostics;
using Repository.BackendRepository.Implement;
using Repository.Helper;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Components.Routing;
namespace Backend.Services.Implement namespace Backend.Services.Implement
{ {
public class webRequestService public class webRequestService
{ {
private Stopwatch stopwatch = new Stopwatch();
private Stopwatch stopwatchSection = new Stopwatch();
public List<Device_value> obixQuery(string urlString, string bql, string tag_quantity, string acc, string pass) public List<Device_value> obixQuery(string urlString, string bql, string tag_quantity, string acc, string pass)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
List<Device_value> result = new List<Device_value>(); List<Device_value> result = new List<Device_value>();
//String username = "obixUser"; //String username = "obixUser";
//String password = "Admin123456"; //String password = "Admin123456";
@ -24,7 +38,8 @@ namespace Backend.Services.Implement
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
String API_Url = urlString; String API_Url = urlString;
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url); HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url);
Postrequest.Method = "POST"; Postrequest.Method = "POST";
Postrequest.Headers.Add("Authorization", "Basic " + encoded); Postrequest.Headers.Add("Authorization", "Basic " + encoded);
@ -38,7 +53,11 @@ namespace Backend.Services.Implement
} }
HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse(); HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
stopwatchSection.Stop();
KeepTimeLog("request data from niagara", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
XmlDocument xmlDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(responseString); xmlDoc.LoadXml(responseString);
//xmlDoc.Load("N4v1021.xml");//N4v1021 //xmlDoc.Load("N4v1021.xml");//N4v1021
@ -113,7 +132,11 @@ namespace Backend.Services.Implement
} }
} }
} }
stopwatchSection.Stop();
KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
KeepTimeLog("obixQuery", stopwatch.ElapsedMilliseconds);
return result; return result;
} }
@ -126,6 +149,8 @@ namespace Backend.Services.Implement
/// <returns></returns> /// <returns></returns>
public List<ImpNiaItem> obixHisQuery(string bqlUrlString, string urlString, string slot, string acc, string pass, string AreaTag) public List<ImpNiaItem> obixHisQuery(string bqlUrlString, string urlString, string slot, string acc, string pass, string AreaTag)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
//String username = "obixUser"; //String username = "obixUser";
//String password = "Admin123456"; //String password = "Admin123456";
//String username = "stanGG"; //String username = "stanGG";
@ -139,6 +164,8 @@ namespace Backend.Services.Implement
String cp_API_Url = bqlUrlString; String cp_API_Url = bqlUrlString;
String building_tag = slot.Split("/")[2].Replace("|", "_").Trim(); //Ex: D2_, M_ String building_tag = slot.Split("/")[2].Replace("|", "_").Trim(); //Ex: D2_, M_
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
HttpWebRequest cp_Postrequest = (HttpWebRequest)WebRequest.Create(cp_API_Url); HttpWebRequest cp_Postrequest = (HttpWebRequest)WebRequest.Create(cp_API_Url);
cp_Postrequest.Method = "POST"; cp_Postrequest.Method = "POST";
cp_Postrequest.Headers.Add("Authorization", "Basic " + encoded); cp_Postrequest.Headers.Add("Authorization", "Basic " + encoded);
@ -153,7 +180,11 @@ namespace Backend.Services.Implement
cp_Postrequest.Timeout = System.Threading.Timeout.Infinite; cp_Postrequest.Timeout = System.Threading.Timeout.Infinite;
HttpWebResponse cp_response = (HttpWebResponse)cp_Postrequest.GetResponse(); HttpWebResponse cp_response = (HttpWebResponse)cp_Postrequest.GetResponse();
var cp_responseString = new StreamReader(cp_response.GetResponseStream()).ReadToEnd(); var cp_responseString = new StreamReader(cp_response.GetResponseStream()).ReadToEnd();
stopwatchSection.Stop();
KeepTimeLog("request data from niagara", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
XmlDocument cp_xmlDoc = new XmlDocument(); XmlDocument cp_xmlDoc = new XmlDocument();
cp_xmlDoc.LoadXml(cp_responseString); cp_xmlDoc.LoadXml(cp_responseString);
@ -316,12 +347,18 @@ namespace Backend.Services.Implement
} }
} }
} }
stopwatchSection.Stop();
KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
KeepTimeLog("obixHisQuery", stopwatch.ElapsedMilliseconds);
return result.ToList(); return result.ToList();
} }
public List<Device_value_disaster> obixDevDisaster(string urlString, string bql, string tag_quantity, string acc, string pass, string dt) public List<Device_value_disaster> obixDevDisaster(string urlString, string bql, string tag_quantity, string acc, string pass, string dt)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
List<Device_value_disaster> result = new List<Device_value_disaster>(); List<Device_value_disaster> result = new List<Device_value_disaster>();
try try
{ {
@ -330,6 +367,8 @@ namespace Backend.Services.Implement
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(acc + ":" + pass)); String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(acc + ":" + pass));
String API_Url = urlString; String API_Url = urlString;
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url); HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url);
Postrequest.Method = "POST"; Postrequest.Method = "POST";
Postrequest.Headers.Add("Authorization", "Basic " + encoded); Postrequest.Headers.Add("Authorization", "Basic " + encoded);
@ -344,7 +383,11 @@ namespace Backend.Services.Implement
HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse(); HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
stopwatchSection.Stop();
KeepTimeLog("get data from niagara", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
XmlDocument xmlDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(responseString); xmlDoc.LoadXml(responseString);
@ -428,8 +471,12 @@ namespace Backend.Services.Implement
} }
catch(Exception e) catch(Exception e)
{ {
var a = e;
} }
stopwatchSection.Stop();
KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
KeepTimeLog("obixHisQuery", stopwatch.ElapsedMilliseconds);
return result; return result;
} }
@ -442,6 +489,8 @@ namespace Backend.Services.Implement
/// <returns></returns> /// <returns></returns>
public List<ImpNiaItem> obixHisQuery_v2(string bqlUrlString, string urlString, string slot, string acc, string pass) public List<ImpNiaItem> obixHisQuery_v2(string bqlUrlString, string urlString, string slot, string acc, string pass)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
//String username = "obixUser"; //String username = "obixUser";
//String password = "Admin123456"; //String password = "Admin123456";
//String username = "stanGG"; //String username = "stanGG";
@ -494,6 +543,8 @@ namespace Backend.Services.Implement
List<ImpNiaItem> result = new List<ImpNiaItem>(); List<ImpNiaItem> result = new List<ImpNiaItem>();
String API_Url = urlString; String API_Url = urlString;
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url); HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url);
Postrequest.Method = "GET"; Postrequest.Method = "GET";
Postrequest.Headers.Add("Authorization", "Basic " + encoded); Postrequest.Headers.Add("Authorization", "Basic " + encoded);
@ -502,12 +553,16 @@ namespace Backend.Services.Implement
HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse(); HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
stopwatchSection.Stop();
KeepTimeLog("get data from niagara", stopwatchSection.ElapsedMilliseconds);
XmlDocument xmlDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(responseString); xmlDoc.LoadXml(responseString);
// 1.循環所有的 station // 1.循環所有的 station
//ref https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c //ref https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
{ {
var stationName = node.Attributes["name"].InnerText; // WSP_Supervisor var stationName = node.Attributes["name"].InnerText; // WSP_Supervisor
@ -573,7 +628,12 @@ namespace Backend.Services.Implement
} }
} }
} }
stopwatchSection.Stop();
KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds);
#endregion #endregion
stopwatch.Stop();
KeepTimeLog("obixHisQuery_v2", stopwatch.ElapsedMilliseconds);
return result.ToList(); return result.ToList();
//return result.Where(x => x.full_name != null).ToList(); //return result.Where(x => x.full_name != null).ToList();
} }
@ -739,6 +799,8 @@ namespace Backend.Services.Implement
/// <returns></returns> /// <returns></returns>
public List<DeviceControlPoint> obixAllDevicePoint(string bqlUrlString, string urlString, string slot, string acc, string pass, string AreaTag) public List<DeviceControlPoint> obixAllDevicePoint(string bqlUrlString, string urlString, string slot, string acc, string pass, string AreaTag)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
String username = acc; String username = acc;
String password = pass; String password = pass;
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
@ -748,6 +810,8 @@ namespace Backend.Services.Implement
String cp_API_Url = bqlUrlString; String cp_API_Url = bqlUrlString;
String building_tag = slot.Split("/")[2].Replace("|", "_").Trim(); //Ex: D2_, M_ String building_tag = slot.Split("/")[2].Replace("|", "_").Trim(); //Ex: D2_, M_
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
HttpWebRequest cp_Postrequest = (HttpWebRequest)WebRequest.Create(cp_API_Url); HttpWebRequest cp_Postrequest = (HttpWebRequest)WebRequest.Create(cp_API_Url);
cp_Postrequest.Method = "POST"; cp_Postrequest.Method = "POST";
cp_Postrequest.Headers.Add("Authorization", "Basic " + encoded); cp_Postrequest.Headers.Add("Authorization", "Basic " + encoded);
@ -762,7 +826,11 @@ namespace Backend.Services.Implement
cp_Postrequest.Timeout = System.Threading.Timeout.Infinite; cp_Postrequest.Timeout = System.Threading.Timeout.Infinite;
HttpWebResponse cp_response = (HttpWebResponse)cp_Postrequest.GetResponse(); HttpWebResponse cp_response = (HttpWebResponse)cp_Postrequest.GetResponse();
var cp_responseString = new StreamReader(cp_response.GetResponseStream()).ReadToEnd(); var cp_responseString = new StreamReader(cp_response.GetResponseStream()).ReadToEnd();
stopwatchSection.Stop();
KeepTimeLog("get data from niagara", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
XmlDocument cp_xmlDoc = new XmlDocument(); XmlDocument cp_xmlDoc = new XmlDocument();
cp_xmlDoc.LoadXml(cp_responseString); cp_xmlDoc.LoadXml(cp_responseString);
@ -833,7 +901,29 @@ namespace Backend.Services.Implement
} }
} }
} }
stopwatchSection.Stop();
KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
KeepTimeLog("obixHisQuery", stopwatch.ElapsedMilliseconds);
return result.ToList(); return result.ToList();
} }
public void KeepTimeLog(string functionName, long millisecond, [CallerLineNumber] int line = 0, [CallerMemberName] string name = "")
{
string path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Logs");
string filepath = System.IO.Path.Combine(path, "SyncLog" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine($"Log current time {DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}, Log function name: {functionName}, Log function usage time: {TimeSpan.FromMilliseconds(millisecond)}, Log function record line: {line}, Log called function name: {name}");
sw.WriteLine(Environment.NewLine);
sw.Flush();
sw.Close();
fs.Close();
}
}
} }
} }

View File

@ -14,7 +14,7 @@
}, },
"DBConfig": { "DBConfig": {
"MySqlDBConfig": { "MySqlDBConfig": {
"Server": "jbgI3Q4uFL4Kd9bbvToklA==", //0.201 "Server": "CYGthbCeGtAXT4s1NOSJHQ==", //0.201
"Port": "mkF51jVbg40V5K5eTh2Ckw==", //3306 "Port": "mkF51jVbg40V5K5eTh2Ckw==", //3306
//"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30 //"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30
//"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp //"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp

View File

@ -141,7 +141,7 @@
} }
@if (ViewBag.role.Contains("RescueDeviceAED")) @if (ViewBag.role.Contains("WarningValueIndex"))
{ {
<li class=""> <li class="">
<a href="#" title="警戒值設定" data-filter-tags="category" id="warningValueBtn"> <a href="#" title="警戒值設定" data-filter-tags="category" id="warningValueBtn">

View File

@ -16,11 +16,16 @@ using Org.BouncyCastle.Utilities.Collections;
using System.Reflection; using System.Reflection;
using System.Drawing; using System.Drawing;
using Org.BouncyCastle.Asn1.Mozilla; using Org.BouncyCastle.Asn1.Mozilla;
using System.IO;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Repository.BackendRepository.Implement namespace Repository.BackendRepository.Implement
{ {
public class NiagaraDataSynchronizeRepository : BackendRepository, INiagaraDataSynchronizeRepository public class NiagaraDataSynchronizeRepository : BackendRepository, INiagaraDataSynchronizeRepository
{ {
private Stopwatch stopwatch = new Stopwatch();
private Stopwatch stopwatchSection = new Stopwatch();
public NiagaraDataSynchronizeRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) public NiagaraDataSynchronizeRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
{ {
} }
@ -33,13 +38,17 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task InsertNiagaraTagList(List<Device_value> ds, List<string> building, string tag_quantity) public async Task InsertNiagaraTagList(List<Device_value> ds, List<string> building, string tag_quantity)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try try
{ {
foreach(var b in building) stopwatchSection = new Stopwatch();
stopwatchSection.Start();
foreach (var b in building)
{ {
string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_tag` ( string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_tag` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
@ -68,6 +77,8 @@ namespace Repository.BackendRepository.Implement
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
} }
stopwatchSection.Stop();
await KeepTimeLog("Create or delete related building import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
//N4資料groupBy後放入import_niagara_tag資料表 //N4資料groupBy後放入import_niagara_tag資料表
var ds2 = ds.GroupBy(x => new var ds2 = ds.GroupBy(x => new
@ -83,6 +94,8 @@ namespace Repository.BackendRepository.Implement
// StringBuilder sb = new StringBuilder(); // StringBuilder sb = new StringBuilder();
bool isDome = false; //是否為巨蛋案 bool isDome = false; //是否為巨蛋案
int count = 0; int count = 0;
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
foreach (var row in ds2) foreach (var row in ds2)
{ {
if (string.IsNullOrEmpty(row.tag_name)) continue; if (string.IsNullOrEmpty(row.tag_name)) continue;
@ -140,18 +153,27 @@ namespace Repository.BackendRepository.Implement
} }
} }
} }
stopwatchSection.Stop();
await KeepTimeLog("Insert into import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
if (sb.Length > 0) if (sb.Length > 0)
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
var temp = sb.ToString(); var temp = sb.ToString();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{ {
await conn.ExecuteAsync(temp); await conn.ExecuteAsync(temp);
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("Insert into import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
//巨蛋特殊處理 //巨蛋特殊處理
if (isDome) if (isDome)
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" -- tag 第二碼為小類(除了安全系統之外) sb.Append($@" -- tag 第二碼為小類(除了安全系統之外)
update import_niagara_tag set device_name_tag = device_system_tag update import_niagara_tag set device_name_tag = device_system_tag
where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B' and where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B' and
@ -167,8 +189,12 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
stopwatchSection.Stop();
await KeepTimeLog("巨蛋特殊處理", stopwatchSection.ElapsedMilliseconds);
} }
} }
stopwatch.Stop();
await KeepTimeLog("InsertNiagaraTagList", stopwatch.ElapsedMilliseconds);
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -188,12 +214,16 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task<string> getLightSwitchLevel() public async Task<string> getLightSwitchLevel()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
var sql = "select system_value from variable where system_type = 'module_light_switch' "; var sql = "select system_value from variable where system_type = 'module_light_switch' ";
var module_light_switch = conn.QueryAsync<string>(sql).Result.FirstOrDefault(); var module_light_switch = conn.QueryAsync<string>(sql).Result.FirstOrDefault();
conn.Close(); conn.Close();
stopwatch.Stop();
await KeepTimeLog("getLightSwitchLevel", stopwatch.ElapsedMilliseconds);
return module_light_switch; return module_light_switch;
} }
} }
@ -203,16 +233,19 @@ namespace Repository.BackendRepository.Implement
/// </summary> /// </summary>
/// <param name="ds"></param> /// <param name="ds"></param>
/// <param name="building"></param> /// <param name="building"></param>
/// <returns></returns> /// <returns></returns>
public async Task InsertItemFromNiagara(List<ImpNiaItem> ds, List<string> building) public async Task InsertItemFromNiagara(List<ImpNiaItem> ds, List<string> building)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
try try
{ {
#region import_niagara_item資料表中選取的棟別 #region import_niagara_item資料表中選取的棟別
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
foreach(var b in building) foreach(var b in building)
{ {
string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_item` ( string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_item` (
@ -237,7 +270,12 @@ namespace Repository.BackendRepository.Implement
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
} }
stopwatchSection.Stop();
await KeepTimeLog("Create or delete related building import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
#endregion #endregion
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
ds = ds.GroupBy(x => new { ds = ds.GroupBy(x => new {
device_area_tag = x.device_area_tag, device_area_tag = x.device_area_tag,
device_building_tag = x.device_building_tag, device_building_tag = x.device_building_tag,
@ -293,9 +331,13 @@ namespace Repository.BackendRepository.Implement
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("Insert into import_niagara_item", stopwatchSection.ElapsedMilliseconds);
if (isDome.Contains("ibms_dome")) if (isDome.Contains("ibms_dome"))
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" update import_niagara_item a sb.Append($@" update import_niagara_item a
join variable b on a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.system_value COLLATE utf8mb4_0900_ai_ci and b.deleted = 0 and b.system_type = 'device_system_category_layer3' join variable b on a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.system_value COLLATE utf8mb4_0900_ai_ci and b.deleted = 0 and b.system_type = 'device_system_category_layer3'
join variable v1 on b.system_parent_id = v1.id join variable v1 on b.system_parent_id = v1.id
@ -306,17 +348,30 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
stopwatchSection.Stop();
await KeepTimeLog("Insert into import_niagara_item", stopwatchSection.ElapsedMilliseconds);
} }
sb.Clear(); sb.Clear();
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" update import_niagara_item set full_name = device_point_name where full_name = '' or full_name is null;"); sb.Append($@" update import_niagara_item set full_name = device_point_name where full_name = '' or full_name is null;");
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("update device point name where full name is empty or null", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
await RecoverSysTag("import_niagara_item");
stopwatchSection.Stop();
await KeepTimeLog("update import_niagara_item system tag", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@"SET SQL_SAFE_UPDATES = 0; sb.Append($@"SET SQL_SAFE_UPDATES = 0;
update import_niagara_item ini update import_niagara_item ini
join ( join (
@ -332,7 +387,11 @@ namespace Repository.BackendRepository.Implement
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("update device_item history", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@"SET SQL_SAFE_UPDATES = 0; sb.Append($@"SET SQL_SAFE_UPDATES = 0;
update import_niagara_item ini update import_niagara_item ini
join ( join (
@ -344,8 +403,11 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
stopwatchSection.Stop();
await KeepTimeLog("update device_item parent_path", stopwatchSection.ElapsedMilliseconds);
await RecoverSysTag("import_niagara_item"); stopwatch.Stop();
await KeepTimeLog("InsertItemFromNiagara", stopwatch.ElapsedMilliseconds);
#endregion #endregion
} }
catch (Exception exception) catch (Exception exception)
@ -359,12 +421,15 @@ namespace Repository.BackendRepository.Implement
} }
} }
/// <summary> /// <summary>
/// 比對 device /// 比對 device
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task DeviceComparison(string LightSwitchLevel) public async Task DeviceComparison(string LightSwitchLevel)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
@ -373,6 +438,8 @@ namespace Repository.BackendRepository.Implement
List<NiagaraTags> result; List<NiagaraTags> result;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder(); StringBuilder sb2 = new StringBuilder();
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@"SELECT m.* sb.Append($@"SELECT m.*
FROM import_niagara_tag m FROM import_niagara_tag m
WHERE NOT EXISTS ( WHERE NOT EXISTS (
@ -381,9 +448,13 @@ namespace Repository.BackendRepository.Implement
WHERE m.niagara_tags = d.device_number WHERE m.niagara_tags = d.device_number
);"); );");
result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>(); result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("filter device item from import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
#region device, device_kind #region device, device_kind
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
//新增至device, is_link = 1 //新增至device, is_link = 1
if (result.Count > 0) if (result.Count > 0)
{ {
@ -437,6 +508,20 @@ namespace Repository.BackendRepository.Implement
} }
} }
if (sb.Length > 0)
{
using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString());
}
sb.Clear();
}
stopwatchSection.Stop();
await KeepTimeLog("insert into device", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
count = 0; count = 0;
foreach (var data in result2) foreach (var data in result2)
{ {
@ -464,16 +549,6 @@ namespace Repository.BackendRepository.Implement
} }
} }
if (sb.Length > 0)
{
using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
{
await conn.ExecuteAsync(sb.ToString());
}
sb.Clear();
}
if (sb2.Length > 0) if (sb2.Length > 0)
{ {
using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
@ -483,10 +558,15 @@ namespace Repository.BackendRepository.Implement
sb2.Clear(); sb2.Clear();
} }
stopwatchSection.Stop();
await KeepTimeLog("insert into device_kind", stopwatchSection.ElapsedMilliseconds);
} }
#endregion #endregion
#region device_node #region device_node
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" SELECT m.* sb.Append($@" SELECT m.*
FROM import_niagara_tag m FROM import_niagara_tag m
WHERE NOT EXISTS ( WHERE NOT EXISTS (
@ -544,9 +624,13 @@ namespace Repository.BackendRepository.Implement
} }
sb.Clear(); sb.Clear();
} }
stopwatchSection.Stop();
await KeepTimeLog("insert into device_node", stopwatchSection.ElapsedMilliseconds);
} }
#endregion #endregion
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
//device有niagara有is_link 更新成 1 //device有niagara有is_link 更新成 1
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE device d UPDATE device d
@ -560,9 +644,12 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("update device is_link = 1, deleted = 0", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
//device有niagara沒有is_link 更新成 0 //device有niagara沒有is_link 更新成 0
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE device d UPDATE device d
@ -577,9 +664,12 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("update device is_link = 0", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
// device_node 有, niagara沒有, is_link 更新成 0 // device_node 有, niagara沒有, is_link 更新成 0
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE device_node dn UPDATE device_node dn
@ -595,6 +685,11 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
stopwatchSection.Stop();
await KeepTimeLog("update device_node is_link = 0", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
await KeepTimeLog("DeviceComparison", stopwatch.ElapsedMilliseconds);
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -615,11 +710,15 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task DeviceItemComparison() public async Task DeviceItemComparison()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
try try
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
List<NiagaraTagsForItem> result; List<NiagaraTagsForItem> result;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append($@" SELECT m.device_point_name, m.is_history, m.device_building_tag, m.device_system_tag, m.device_name_tag, m.parent_path, m.full_name sb.Append($@" SELECT m.device_point_name, m.is_history, m.device_building_tag, m.device_system_tag, m.device_name_tag, m.parent_path, m.full_name
@ -633,9 +732,13 @@ namespace Repository.BackendRepository.Implement
and m.device_building_tag = d.device_building_tag and m.device_building_tag = d.device_building_tag
where d.points is null;"); where d.points is null;");
result = (await conn.QueryAsync<NiagaraTagsForItem>(sb.ToString())).ToList<NiagaraTagsForItem>(); result = (await conn.QueryAsync<NiagaraTagsForItem>(sb.ToString())).ToList<NiagaraTagsForItem>();
sb.Clear(); sb.Clear();
# region device, is_link = 1 stopwatchSection.Stop();
await KeepTimeLog("filter device_item from import_niagara_item", stopwatchSection.ElapsedMilliseconds);
#region device, is_link = 1
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
if (result.Count > 0) if (result.Count > 0)
{ {
foreach (var data in result) foreach (var data in result)
@ -674,6 +777,8 @@ namespace Repository.BackendRepository.Implement
sb.Clear(); sb.Clear();
} }
} }
stopwatchSection.Stop();
await KeepTimeLog("insert into device_item", stopwatchSection.ElapsedMilliseconds);
#endregion #endregion
@ -686,6 +791,8 @@ namespace Repository.BackendRepository.Implement
// WHERE m.device_point_name IS NULL"); // WHERE m.device_point_name IS NULL");
//本次有匯入的 isLink 改為 1, 沒有的 isLink = 0 //本次有匯入的 isLink 改為 1, 沒有的 isLink = 0
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE device_item d UPDATE device_item d
LEFT JOIN ( LEFT JOIN (
@ -703,8 +810,12 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("update device_item is_link", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE device_item d UPDATE device_item d
LEFT JOIN ( LEFT JOIN (
@ -723,6 +834,11 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
stopwatchSection.Stop();
await KeepTimeLog("update device_item is_show_history", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
await KeepTimeLog("DeviceItemComparison", stopwatch.ElapsedMilliseconds);
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -741,6 +857,8 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task InsertBuildingMenu() public async Task InsertBuildingMenu()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
@ -749,6 +867,8 @@ namespace Repository.BackendRepository.Implement
List<NiagaraTags> result; List<NiagaraTags> result;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
#region comparison building_menu and import_niagara_tag #region comparison building_menu and import_niagara_tag
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" select a.* from ( sb.Append($@" select a.* from (
select device_building_tag, device_system_tag, device_name_tag select device_building_tag, device_system_tag, device_name_tag
from import_niagara_tag from import_niagara_tag
@ -762,7 +882,11 @@ namespace Repository.BackendRepository.Implement
result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>(); result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
#endregion #endregion
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("filter building_menu from import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
if (result.Count > 0) if (result.Count > 0)
{ {
foreach (var data in result) foreach (var data in result)
@ -792,6 +916,11 @@ namespace Repository.BackendRepository.Implement
sb.Clear(); sb.Clear();
} }
} }
stopwatchSection.Stop();
await KeepTimeLog("insert into building_menu", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
//building_menu有import_niagara_tag沒有is_link 更新成 0 //building_menu有import_niagara_tag沒有is_link 更新成 0
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE building_menu b LEFT JOIN ( UPDATE building_menu b LEFT JOIN (
@ -806,8 +935,12 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("update building_menu is_link = 0", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@"SET SQL_SAFE_UPDATES = 0; sb.Append($@"SET SQL_SAFE_UPDATES = 0;
UPDATE building_menu b LEFT JOIN ( UPDATE building_menu b LEFT JOIN (
select device_building_tag, device_system_tag, device_name_tag select device_building_tag, device_system_tag, device_name_tag
@ -822,6 +955,11 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
stopwatchSection.Stop();
await KeepTimeLog("update building_menu is_link = 1", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
await KeepTimeLog("InsertBuildingMenu", stopwatch.ElapsedMilliseconds);
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -840,6 +978,8 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task InsertSubSystemFloor() public async Task InsertSubSystemFloor()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
@ -848,6 +988,8 @@ namespace Repository.BackendRepository.Implement
List<NiagaraTags> result; List<NiagaraTags> result;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
#region comparison sub_system_floor and import_niagara_tag #region comparison sub_system_floor and import_niagara_tag
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" select a.* from ( sb.Append($@" select a.* from (
select device_building_tag, device_system_tag, device_name_tag, device_floor_tag select device_building_tag, device_system_tag, device_name_tag, device_floor_tag
from import_niagara_tag from import_niagara_tag
@ -860,9 +1002,13 @@ namespace Repository.BackendRepository.Implement
a.device_floor_tag COLLATE utf8mb4_0900_ai_ci = b.floor_tag COLLATE utf8mb4_0900_ai_ci a.device_floor_tag COLLATE utf8mb4_0900_ai_ci = b.floor_tag COLLATE utf8mb4_0900_ai_ci
WHERE b.building_tag IS NULL"); WHERE b.building_tag IS NULL");
result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>(); result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("filter subsystem floor from import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
#endregion #endregion
sb.Clear(); stopwatchSection = new Stopwatch();
stopwatchSection.Start();
if (result.Count > 0) if (result.Count > 0)
{ {
foreach (var data in result) foreach (var data in result)
@ -887,7 +1033,12 @@ namespace Repository.BackendRepository.Implement
sb.Clear(); sb.Clear();
} }
} }
stopwatchSection.Stop();
await KeepTimeLog("insert into subsystem floor", stopwatchSection.ElapsedMilliseconds);
//building_menu有import_niagara_tag沒有is_link 更新成 0 //building_menu有import_niagara_tag沒有is_link 更新成 0
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE sub_system_floor b LEFT JOIN ( UPDATE sub_system_floor b LEFT JOIN (
select device_building_tag, device_system_tag, device_name_tag, device_floor_tag select device_building_tag, device_system_tag, device_name_tag, device_floor_tag
@ -904,9 +1055,12 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("update subsystem floor is_link = 0", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" SET SQL_SAFE_UPDATES = 0; sb.Append($@" SET SQL_SAFE_UPDATES = 0;
UPDATE sub_system_floor b LEFT JOIN ( UPDATE sub_system_floor b LEFT JOIN (
select device_building_tag, device_system_tag, device_name_tag, device_floor_tag select device_building_tag, device_system_tag, device_name_tag, device_floor_tag
@ -923,6 +1077,11 @@ namespace Repository.BackendRepository.Implement
{ {
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
stopwatchSection.Stop();
await KeepTimeLog("update subsystem floor is_link = 1", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
await KeepTimeLog("InsertSubSystemFloor", stopwatch.ElapsedMilliseconds);
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -940,6 +1099,8 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task CheckFullNameEmptyReplaceByDeviceName() public async Task CheckFullNameEmptyReplaceByDeviceName()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
@ -961,6 +1122,8 @@ namespace Repository.BackendRepository.Implement
} }
} }
} }
stopwatch.Stop();
await KeepTimeLog("CheckFullNameEmptyReplaceByDeviceName", stopwatch.ElapsedMilliseconds);
} }
/// <summary> /// <summary>
@ -969,7 +1132,8 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task ItemCheckFullNameEmptyReplaceByDeviceName() public async Task ItemCheckFullNameEmptyReplaceByDeviceName()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
//檢查是否有空值 //檢查是否有空值
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
@ -991,8 +1155,9 @@ namespace Repository.BackendRepository.Implement
conn.Close(); conn.Close();
} }
} }
} }
stopwatch.Stop();
await KeepTimeLog("ItemCheckFullNameEmptyReplaceByDeviceName", stopwatch.ElapsedMilliseconds);
} }
/// <summary> /// <summary>
@ -1001,6 +1166,8 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task CheckDiffFullNameAndCover() public async Task CheckDiffFullNameAndCover()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
@ -1032,8 +1199,9 @@ namespace Repository.BackendRepository.Implement
conn.Close(); conn.Close();
} }
} }
} }
stopwatch.Stop();
await KeepTimeLog("CheckDiffFullNameAndCover", stopwatch.ElapsedMilliseconds);
} }
@ -1044,6 +1212,8 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task CheckItemDiffFullNameAndCover() public async Task CheckItemDiffFullNameAndCover()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
//檢查 //檢查
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
@ -1077,6 +1247,8 @@ namespace Repository.BackendRepository.Implement
} }
} }
} }
stopwatch.Stop();
await KeepTimeLog("CheckItemDiffFullNameAndCover", stopwatch.ElapsedMilliseconds);
} }
/// <summary> /// <summary>
@ -1086,6 +1258,8 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task CheckItemIsShowHistory() public async Task CheckItemIsShowHistory()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
//先將所有is_show_history關閉,niagara有的才開啟 //先將所有is_show_history關閉,niagara有的才開啟
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
@ -1126,12 +1300,15 @@ namespace Repository.BackendRepository.Implement
{ {
conn.Close(); conn.Close();
} }
} }
stopwatch.Stop();
await KeepTimeLog("getLightSwitchLevel", stopwatch.ElapsedMilliseconds);
} }
public async Task DeviceDisasterAysnc(List<Device_value_disaster> dv) public async Task DeviceDisasterAysnc(List<Device_value_disaster> dv)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
@ -1142,11 +1319,20 @@ namespace Repository.BackendRepository.Implement
{ {
try try
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append("select * from device_disaster;"); sb.Append("select * from device_disaster;");
deviceDisaster = (await conn.QueryAsync<DeviceDisasterOutput>(sb.ToString())).ToList(); deviceDisaster = (await conn.QueryAsync<DeviceDisasterOutput>(sb.ToString())).ToList();
sb.Clear(); sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("select all device_disaster", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append("select * from device where deleted = 0;"); sb.Append("select * from device where deleted = 0;");
device = (await conn.QueryAsync<DeviceDisasterOutput>(sb.ToString())).ToList(); device = (await conn.QueryAsync<DeviceDisasterOutput>(sb.ToString())).ToList();
stopwatchSection.Stop();
await KeepTimeLog("select all device", stopwatchSection.ElapsedMilliseconds);
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -1158,6 +1344,8 @@ namespace Repository.BackendRepository.Implement
} }
} }
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
dv = dv.Where(x => device.Any(d => d.device_number == x.value.Split('/')[6])).ToList(); dv = dv.Where(x => device.Any(d => d.device_number == x.value.Split('/')[6])).ToList();
sb.Clear(); sb.Clear();
var updateList = dv.Where(x => deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6] && dd.device_system_value != x.disasterValue)).ToList(); var updateList = dv.Where(x => deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6] && dd.device_system_value != x.disasterValue)).ToList();
@ -1201,6 +1389,11 @@ namespace Repository.BackendRepository.Implement
conn.Close(); conn.Close();
} }
} }
stopwatchSection.Stop();
await KeepTimeLog("update or insert into device_disaster", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
await KeepTimeLog("DeviceDisasterAysnc", stopwatch.ElapsedMilliseconds);
} }
} }
@ -1210,6 +1403,8 @@ namespace Repository.BackendRepository.Implement
/// <returns></returns> /// <returns></returns>
public async Task InsertFloor() public async Task InsertFloor()
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
@ -1220,6 +1415,8 @@ namespace Repository.BackendRepository.Implement
List<NiagaraTags> result; List<NiagaraTags> result;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
#region comparison floor and sub_system_floor #region comparison floor and sub_system_floor
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sb.Append($@" select tag.* sb.Append($@" select tag.*
from ( from (
select device_building_tag, device_floor_tag select device_building_tag, device_floor_tag
@ -1232,10 +1429,14 @@ namespace Repository.BackendRepository.Implement
and f.deleted = 0 and f.deleted = 0
WHERE f.building_tag IS NULL;"); WHERE f.building_tag IS NULL;");
result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>(); result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
sb.Clear();
stopwatchSection.Stop();
await KeepTimeLog("filter floor from import_niagara_tag", stopwatchSection.ElapsedMilliseconds);
#endregion #endregion
sb.Clear();
#region insert floor #region insert floor
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
if (result.Count > 0) if (result.Count > 0)
{ {
Dictionary<string, int> floorPriority = new Dictionary<string, int>(); Dictionary<string, int> floorPriority = new Dictionary<string, int>();
@ -1261,9 +1462,13 @@ namespace Repository.BackendRepository.Implement
sb.Clear(); sb.Clear();
} }
} }
stopwatchSection.Stop();
await KeepTimeLog("insert into floor", stopwatchSection.ElapsedMilliseconds);
#endregion #endregion
#region update unuse floor with niagara data #region update unuse floor with niagara data
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
var floor = (await conn.QueryAsync<BuildFloor>($@"select f1.* var floor = (await conn.QueryAsync<BuildFloor>($@"select f1.*
from floor as f1 from floor as f1
left join ( left join (
@ -1290,6 +1495,11 @@ namespace Repository.BackendRepository.Implement
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
} }
stopwatchSection.Stop();
await KeepTimeLog("update floor deleted = 1", stopwatchSection.ElapsedMilliseconds);
stopwatch.Stop();
await KeepTimeLog("InsertFloor", stopwatch.ElapsedMilliseconds);
#endregion #endregion
} }
catch (Exception exception) catch (Exception exception)
@ -1306,11 +1516,15 @@ namespace Repository.BackendRepository.Implement
public async Task InsertItemFromNiagara_History(List<ImpNiaItem> ds, List<string> building) public async Task InsertItemFromNiagara_History(List<ImpNiaItem> ds, List<string> building)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
try try
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
//改成每次都新增 //改成每次都新增
string sql = @"SET NAMES utf8mb4; string sql = @"SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0; SET FOREIGN_KEY_CHECKS = 0;
@ -1343,6 +1557,8 @@ SET FOREIGN_KEY_CHECKS = 1;";
{ {
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
stopwatchSection.Stop();
await KeepTimeLog("recreate import_niagara_item_history", stopwatchSection.ElapsedMilliseconds);
#region import_niagara_item資料表中選取的棟別 #region import_niagara_item資料表中選取的棟別
//foreach (var b in building) //foreach (var b in building)
@ -1356,6 +1572,8 @@ SET FOREIGN_KEY_CHECKS = 1;";
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int count = 0; int count = 0;
#region import_niagara_item資料表 #region import_niagara_item資料表
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
foreach (var row in ds) foreach (var row in ds)
{ {
string ss = $@" insert import_niagara_item_history(device_area_tag, device_building_tag, device_system_tag, device_name_tag, string ss = $@" insert import_niagara_item_history(device_area_tag, device_building_tag, device_system_tag, device_name_tag,
@ -1394,6 +1612,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
} }
sb.Clear(); sb.Clear();
} }
stopwatchSection.Stop();
await KeepTimeLog("insert into import_niagara_item_history", stopwatchSection.ElapsedMilliseconds);
#endregion #endregion
} }
catch (Exception exception) catch (Exception exception)
@ -1405,6 +1625,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
conn.Close(); conn.Close();
} }
} }
stopwatch.Stop();
await KeepTimeLog("InsertItemFromNiagara_History", stopwatch.ElapsedMilliseconds);
} }
/// <summary> /// <summary>
@ -1413,6 +1635,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
/// <returns></returns> /// <returns></returns>
public async Task RecoverSysTag(string tableName) public async Task RecoverSysTag(string tableName)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
//檢查 //檢查
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
@ -1440,6 +1664,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
} }
} }
} }
stopwatch.Stop();
await KeepTimeLog("RecoverSysTag", stopwatch.ElapsedMilliseconds);
} }
/// <summary> /// <summary>
@ -1449,10 +1675,14 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
/// <returns></returns> /// <returns></returns>
public async Task DeviceControlPoint (List<DeviceControlPoint> dcp) public async Task DeviceControlPoint (List<DeviceControlPoint> dcp)
{ {
stopwatch = new Stopwatch();
stopwatch.Start();
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
try try
{ {
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_device_control_point` ( string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_device_control_point` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(1000) DEFAULT NULL, `name` varchar(1000) DEFAULT NULL,
@ -1470,13 +1700,21 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
{ {
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
stopwatchSection.Stop();
await KeepTimeLog("check and create import_niagara_device_control_point", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
sql = @"truncate table import_niagara_device_control_point"; sql = @"truncate table import_niagara_device_control_point";
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{ {
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
stopwatchSection.Stop();
await KeepTimeLog("truncate import_niagara_device_control_point", stopwatchSection.ElapsedMilliseconds);
stopwatchSection = new Stopwatch();
stopwatchSection.Start();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int count = 0; int count = 0;
foreach (var data in dcp) foreach (var data in dcp)
@ -1499,6 +1737,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
await conn.ExecuteAsync(sb.ToString()); await conn.ExecuteAsync(sb.ToString());
sb.Clear(); sb.Clear();
} }
stopwatchSection.Stop();
await KeepTimeLog("insert into import_niagara_device_control_point", stopwatchSection.ElapsedMilliseconds);
} }
catch (Exception e) catch (Exception e)
{ {
@ -1509,6 +1749,25 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d
conn.Close(); conn.Close();
} }
} }
stopwatch.Stop();
await KeepTimeLog("DeviceControlPoint", stopwatch.ElapsedMilliseconds);
}
public async Task KeepTimeLog(string functionName, long millisecond, [CallerLineNumber] int line = 0, [CallerMemberName] string name = "")
{
string path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Logs");
string filepath = System.IO.Path.Combine(path, "SyncLog" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine($"Log current time {DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}, Log function name: {functionName}, Log function usage time: {TimeSpan.FromMilliseconds(millisecond)}, Log function record line: {line}, Log called function name: {name}");
sw.WriteLine(Environment.NewLine);
sw.Flush();
sw.Close();
fs.Close();
}
} }
} }
} }

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using Repository.Models; using Repository.Models;
@ -89,5 +91,7 @@ namespace Repository.BackendRepository.Interface
/// <param name="dcp"></param> /// <param name="dcp"></param>
/// <returns></returns> /// <returns></returns>
Task DeviceControlPoint(List<DeviceControlPoint> dcp); Task DeviceControlPoint(List<DeviceControlPoint> dcp);
Task KeepTimeLog(string functionName, long milliSecond, [CallerLineNumber] int line = 0, [CallerMemberName] string name = "");
} }
} }