diff --git a/Backend/Controllers/NiagaraDataSynchronizeController.cs b/Backend/Controllers/NiagaraDataSynchronizeController.cs index d2adcb4..cad41e1 100644 --- a/Backend/Controllers/NiagaraDataSynchronizeController.cs +++ b/Backend/Controllers/NiagaraDataSynchronizeController.cs @@ -114,8 +114,8 @@ namespace Backend.Controllers } string LightSwitchLevel = await niagaraDataSynchronizeRepository.getLightSwitchLevel(); //獲取照明開關 是否在 device or device_node - //await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building, tag_quantity); // 匯入 MySQL table: import_niagara_tag - //await niagaraDataSynchronizeRepository.DeviceComparison(LightSwitchLevel); + await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building, tag_quantity); // 匯入 MySQL table: import_niagara_tag + await niagaraDataSynchronizeRepository.DeviceComparison(LightSwitchLevel); await niagaraDataSynchronizeRepository.CheckDiffFullNameAndCover(); await niagaraDataSynchronizeRepository.CheckFullNameEmptyReplaceByDeviceName(); await niagaraDataSynchronizeRepository.InsertBuildingMenu(); diff --git a/Backend/Services/Implement/webRequestService.cs b/Backend/Services/Implement/webRequestService.cs index 7219dcd..50cd254 100644 --- a/Backend/Services/Implement/webRequestService.cs +++ b/Backend/Services/Implement/webRequestService.cs @@ -4,23 +4,9 @@ using System.Linq; using System.Xml; using Newtonsoft.Json; using Repository.Models; -using Newtonsoft.Json.Linq; using System.IO; using System.Net; -using System.Text; using System.Xml.Linq; -using Repository.BackendRepository.Implement; -using Repository.BackendRepository; -using Ubiety.Dns.Core; -using System.Diagnostics; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; -using Repository.Models.CodeBeautify; -using NPOI.SS.Formula.Functions; -using Microsoft.AspNetCore.Localization; -using MySqlX.XDevAPI.Relational; -using Backend.Models; -using NPOI.OpenXmlFormats.Dml; namespace Backend.Services.Implement { @@ -151,6 +137,7 @@ namespace Backend.Services.Implement #region get control point data List conPoint = new List(); String cp_API_Url = bqlUrlString; + String building_tag = slot.Split("/")[2].Replace("|", "_").Trim(); //Ex: D2_, M_ HttpWebRequest cp_Postrequest = (HttpWebRequest)WebRequest.Create(cp_API_Url); cp_Postrequest.Method = "POST"; @@ -220,8 +207,14 @@ namespace Backend.Services.Implement XmlDocument xmlDoc2 = new XmlDocument(); xmlDoc2.LoadXml(responseString2); + // Create a namespace manager to handle the XML namespace + XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc2.NameTable); + string xmlns = xmlDoc2.DocumentElement.GetAttribute("xmlns") ?? "http://obix.org/ns/schema/1.0"; + namespaceManager.AddNamespace("obix", xmlns); - foreach (XmlNode node2 in xmlDoc2.DocumentElement.ChildNodes) + // Select all "ref" nodes using an XPath expression + XmlNodeList refNodes = xmlDoc2.SelectNodes($"//obix:ref[starts-with(@name, '{building_tag}')]", namespaceManager); + foreach (XmlNode node2 in refNodes) { string tagName = node2.Attributes["name"].InnerText; if (tagName.Split('_').Length == 9) @@ -241,8 +234,8 @@ namespace Backend.Services.Implement row.isHistory = true; result.Add(row); - - } else if (tagName.Split('_').Length == 6) + } + else if (tagName.Split('_').Length == 6) { //巨蛋 tag 5 段版本 ImpNiaItem row = new ImpNiaItem(); if (tagName.Split('_')[1] == "S") //for security system diff --git a/Backend/Views/NiagaraDataSynchronize/Index.cshtml b/Backend/Views/NiagaraDataSynchronize/Index.cshtml index 8c9e2c7..25254ad 100644 --- a/Backend/Views/NiagaraDataSynchronize/Index.cshtml +++ b/Backend/Views/NiagaraDataSynchronize/Index.cshtml @@ -140,9 +140,9 @@ //#endregion function SynchronizeData() { - showSpinner(); - document.getElementById('loadDataText').innerText = "同步中..."; if (ds.length > 0) { + document.getElementById('loadDataText').innerText = "同步中..."; + showSpinner(); //比對資料,有差異的話,再同步到device等資料表 var url_synchronize_data = "/NiagaraDataSynchronize/CompareData/"; @@ -160,16 +160,20 @@ dataType: 'json', success: function (rel) { if (rel.code != "0000") { - toast_error(rel.msg); hideSpinner(); + toast_error(rel.msg); document.getElementById('loadDataText').innerText = "比對資料出錯了!"; return; } + SyncDevItem(); //var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); //var dateTime = date + ' ' + time; //document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime; }, + error: function () { + hideSpinner(); + } }); } else { @@ -194,11 +198,12 @@ dataType: 'json', success: function (rel) { if (rel.code != "0000") { + hideSpinner(); toast_error(rel.msg); document.getElementById('loadDataText').innerText = "比對資料出錯了!"; return; } - + $.ajax({ method: "POST", url: url_synchronize_data_device_item, @@ -221,6 +226,9 @@ var dateTime = date + ' ' + time; document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime; }, + error: function () { + hideSpinner(); + } }) var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); var dateTime = date + ' ' + time; diff --git a/FrontendWebApi/ApiControllers/BuildController.cs b/FrontendWebApi/ApiControllers/BuildController.cs index abe3f57..e9b72fb 100644 --- a/FrontendWebApi/ApiControllers/BuildController.cs +++ b/FrontendWebApi/ApiControllers/BuildController.cs @@ -180,7 +180,7 @@ namespace FrontendWebApi.ApiControllers v2.system_key subfull_name, v2.system_priority subpriority from building_menu me - left join building b on b.building_tag = me.building_tag + left join building b on b.building_tag = me.building_tag and b.deleted = 0 left join variable v1 on me.main_system_tag = v1.system_value and v1.deleted = 0 and v1.system_type = 'device_system_category_layer2' left join variable v2 on me.sub_system_tag = v2.system_value and v2.deleted = 0 and v2.system_type = 'device_system_category_layer3' inner join ( diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index 6f6b1c5..d5f2077 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -13,6 +13,7 @@ using System.Data.SqlTypes; using Microsoft.Extensions.Primitives; using System.Web; using Org.BouncyCastle.Utilities.Collections; +using System.Reflection; namespace Repository.BackendRepository.Implement { @@ -409,18 +410,18 @@ namespace Repository.BackendRepository.Implement //device有,niagara沒有,is_link 更新成 0 sb.Append($@" SET SQL_SAFE_UPDATES = 0; - UPDATE device d - SET d.is_link = 0 - WHERE d.is_link = 1 and not exists (select niagara_tags from import_niagara_tag where niagara_tags = d.device_number);"); + UPDATE device + SET is_link = 0 + WHERE is_link = 1 AND device_number NOT IN (SELECT niagara_tags FROM import_niagara_tag);"); await conn.ExecuteAsync(sb.ToString(), commandTimeout: 100); sb.Clear(); // device_node 有, niagara沒有, is_link 更新成 0 sb.Append($@" SET SQL_SAFE_UPDATES = 0; - UPDATE device_node d - SET d.is_link = 0 - WHERE d.is_link = 1 and not exists (select niagara_tags from import_niagara_tag where niagara_tags = d.device_number);"); + UPDATE device_node + SET is_link = 0 + WHERE is_link = 1 AND device_number NOT IN (SELECT niagara_tags FROM import_niagara_tag);"); await conn.ExecuteAsync(sb.ToString(), commandTimeout: 100); } catch (Exception exception) @@ -767,10 +768,16 @@ namespace Repository.BackendRepository.Implement try { StringBuilder sb = new StringBuilder(); - sb.Append("update device d inner JOIN import_niagara_tag m ON m.niagara_tags = d.device_number " + - "set d.full_name=m.device_full_name " + - "where m.device_full_name<>d.full_name;"); - await conn.ExecuteAsync(sb.ToString(), commandTimeout: 999); + sb.Append(@"UPDATE device d + INNER JOIN import_niagara_tag m ON m.niagara_tags = d.device_number + SET d.full_name = m.device_full_name + WHERE NOT EXISTS ( + SELECT 1 + FROM import_niagara_tag m2 + WHERE m2.niagara_tags = d.device_number + AND m2.device_full_name = d.full_name + );"); + await conn.ExecuteAsync(sb.ToString()); } catch (Exception exception) { @@ -863,42 +870,18 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled, new TimeSpan(0, 5, 0))) + StringBuilder sb = new StringBuilder(); + List deviceDisaster = new List(); + List device = new List(); + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) { try { - StringBuilder sb = new StringBuilder(); sb.Append("select * from device_disaster;"); - var deviceDisaster = (await conn.QueryAsync(sb.ToString())).ToList(); + deviceDisaster = (await conn.QueryAsync(sb.ToString())).ToList(); sb.Clear(); sb.Append("select * from device where deleted = 0;"); - var device = (await conn.QueryAsync(sb.ToString())).ToList(); - dv = dv.Where(x => device.Any(d => d.device_number == x.value.Split('/')[6])).ToList(); - - sb.Clear(); - var updateList = dv.Where(x => deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6] && dd.device_system_value != x.disasterValue)); - foreach (var d in updateList) - { - string device_number = d.value.Split('/')[6]; - sb.Append($"update device_disaster set device_system_value = '{d.disasterValue}' where device_number = '{device_number}'; "); - } - - var insertList = dv.Where(x => !deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6])); - foreach (var d in insertList) - { - string device_number = d.value.Split('/')[6]; - string device_guid = device.Where(x => x.device_number == device_number).Select(x => x.device_guid).FirstOrDefault(); - string device_system_tag = device.Where(x => x.device_number == device_number).Select(x => x.device_system_tag).FirstOrDefault(); - string device_name_tag = device.Where(x => x.device_number == device_number).Select(x => x.device_name_tag).FirstOrDefault(); - sb.Append($@" - insert into device_disaster - (device_guid, device_system_value, device_building_tag, device_system_tag, device_name_tag, device_floor_tag, device_serial_tag, device_number) - values ('{device_guid}', '{d.disasterValue}', '{device_number.Split('_')[0]}', '{device_system_tag}', '{device_name_tag}', '{device_number.Split('_')[2]}', - '{device_number.Split('_')[4]}', '{device_number}'); "); - } - - if (!string.IsNullOrEmpty(sb.ToString())) - await conn.ExecuteAsync(sb.ToString()); + device = (await conn.QueryAsync(sb.ToString())).ToList(); } catch (Exception exception) { @@ -909,6 +892,46 @@ namespace Repository.BackendRepository.Implement conn.Close(); } } + + dv = dv.Where(x => device.Any(d => d.device_number == x.value.Split('/')[6])).ToList(); + sb.Clear(); + var updateList = dv.Where(x => deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6] && dd.device_system_value != x.disasterValue)).ToList(); + foreach (var d in updateList) + { + string device_number = d.value.Split('/')[6]; + sb.Append($"update device_disaster set device_system_value = '{d.disasterValue}' where device_number = '{device_number}'; "); + } + + var insertList = dv.Where(x => !deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6])).ToList(); + foreach (var d in insertList) + { + string device_number = d.value.Split('/')[6]; + string device_guid = device.Where(x => x.device_number == device_number).Select(x => x.device_guid).FirstOrDefault(); + string device_system_tag = device.Where(x => x.device_number == device_number).Select(x => x.device_system_tag).FirstOrDefault(); + string device_name_tag = device.Where(x => x.device_number == device_number).Select(x => x.device_name_tag).FirstOrDefault(); + sb.Append($@" + insert into device_disaster + (device_guid, device_system_value, device_building_tag, device_system_tag, device_name_tag, device_floor_tag, device_serial_tag, device_number) + values ('{device_guid}', '{d.disasterValue}', '{device_number.Split('_')[0]}', '{device_system_tag}', '{device_name_tag}', '{device_number.Split('_')[2]}', + '{device_number.Split('_')[4]}', '{device_number}'); "); + } + + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + try + { + if (!string.IsNullOrEmpty(sb.ToString())) + await conn.ExecuteAsync(sb.ToString()); + } + catch (Exception ex) + { + throw ex; + } + finally + { + conn.Close(); + } + } } }