diff --git a/Backend/Controllers/NiagaraDataSynchronizeController.cs b/Backend/Controllers/NiagaraDataSynchronizeController.cs index 692f525..5610f94 100644 --- a/Backend/Controllers/NiagaraDataSynchronizeController.cs +++ b/Backend/Controllers/NiagaraDataSynchronizeController.cs @@ -9,6 +9,8 @@ using Backend.Services; using Backend.Services.Implement; using Repository.Models; using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using NPOI.SS.Formula.Functions; namespace Backend.Controllers { @@ -46,7 +48,7 @@ namespace Backend.Controllers string top100 = "";//" top 100 "; webRequestService svc = new webRequestService(); - string bql = url_slot + "bql:select " + top100 + " * from control:ControlPoint "; + string bql = url_slot + "bql:select " + top100 + " * from baja:Folder "; //ds = svc.obixQuery("http://192.168.0.136:8080/obix/config/Arena/Program/ObixQuery/query/", bql); //ds = svc.obixQuery("http://localhost:8080/obix/config/Program/ObixQuery/query/", bql); ds = svc.obixQuery("http://192.168.0.136:8080/obix/config/Program/ObixQuery/query/", bql);//三菱 @@ -79,9 +81,7 @@ namespace Backend.Controllers { var building = ds.FirstOrDefault().building; await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building); - await niagaraDataSynchronizeRepository.InsertItemFromNiagara(ds, building); await niagaraDataSynchronizeRepository.DeviceComparison(); - await niagaraDataSynchronizeRepository.DeviceItemComparison(); await niagaraDataSynchronizeRepository.InsertBuildingMenu(); await niagaraDataSynchronizeRepository.InsertSubSystemFloor(); result = true; @@ -98,5 +98,65 @@ namespace Backend.Controllers return apiResult; } + /// + /// N4匯入列表(device_item) + /// + /// + [HttpPost] + public Task>> RawDataDevItemList() + { + ApiResult> apiResult = new ApiResult>(); + + try + { + List ds; + string url_slot = "slot:/TPE/B1|";//三菱 + //string url_slot = "slot:/NTPC/B1|";//wsp + + webRequestService svc = new webRequestService(); + ds = svc.obixHisQuery("http://192.168.0.136:8080/obix/config/Program/ObixQuery/query/", "http://192.168.0.136:8080/obix/histories", url_slot);//三菱 + //ds = svc.obixHisQuery("http://192.168.0.136:8081/obix/config/Program/ObixQuery/query/", "http://192.168.0.136:8081/obix/histories", url_slot);//wsp + + apiResult.Code = "0000"; + apiResult.Data = ds; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + //return apiResult; + return Task.FromResult(apiResult); + } + + /// + /// 比對N4與資料表,有差異在同步至資料表中(device_item) + /// + /// + [HttpPost] + public async Task> DevIteComData([FromBody] List ds) + { + bool result = false; + + ApiResult apiResult = new ApiResult(); + try + { + var building = ds.FirstOrDefault().device_building_tag; + await niagaraDataSynchronizeRepository.InsertItemFromNiagara(ds, building); + await niagaraDataSynchronizeRepository.DeviceItemComparison(); + result = true; + + apiResult.Code = "0000"; + apiResult.Data = result; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } } } diff --git a/Backend/Services/Implement/webRequestService.cs b/Backend/Services/Implement/webRequestService.cs index 51bdae0..d447fc9 100644 --- a/Backend/Services/Implement/webRequestService.cs +++ b/Backend/Services/Implement/webRequestService.cs @@ -12,6 +12,9 @@ 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; namespace Backend.Services.Implement { @@ -55,40 +58,150 @@ namespace Backend.Services.Implement row.value = item.Val; string[] s1 = item.Val.Split(','); - string[] s2 = s1[0].Split('/'); + string[] s2 = s1[0].Remove(s1[0].Length - 1, 1).Remove(0, 1).Split('/'); + + string displayName = s1[1].Split('=')[1]; row.tag_name = ""; - for (int i = 0; i < s2.Length; i++) + if (s2.Length == 8) { - if (i == s2.Length - 2) + for (int i = 0; i < s2.Length; i++) { - row.point_name = s2[i]; - } - else if (i == 1) - { - row.tag_name += s2[i]; - } - else if (i > 1 && i < s2.Length - 2) - { - row.tag_name += "_" + s2[i]; - } - } - /*foreach (var ss in s2) - { - if (ss.Contains('_')) - { - string[] s3 = ss.Split('_'); - if (s3.Count() > 3) + if (i == s2.Length - 2) { - row.tag_name = ss; - row.point_name = s2[s2.Length - 2]; - break; + row.point_name = s2[i]; + } + else if (i == 1) + { + row.tag_name += s2[i]; + } + else if (i > 1 && i < s2.Length - 2) + { + row.tag_name += "_" + s2[i]; } } - }*/ - result.Add(row); + row.displayName = displayName; + /*foreach (var ss in s2) + { + if (ss.Contains('_')) + { + string[] s3 = ss.Split('_'); + if (s3.Count() > 3) + { + row.tag_name = ss; + row.point_name = s2[s2.Length - 2]; + break; + } + } + }*/ + result.Add(row); + } } + + return result; + } + + public List obixHisQuery(string bqlUrlString, string urlString, string slot) + { + String username = "obixUser"; + String password = "Admin123456"; + String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); + + #region get control point data + List conPoint = new List(); + String cp_API_Url = bqlUrlString; + + HttpWebRequest cp_Postrequest = (HttpWebRequest)WebRequest.Create(cp_API_Url); + cp_Postrequest.Method = "POST"; + cp_Postrequest.Headers.Add("Authorization", "Basic " + encoded); + cp_Postrequest.PreAuthenticate = true; + + using (var streamWriter = new StreamWriter(cp_Postrequest.GetRequestStream())) + { + string json = ""; + + streamWriter.Write(json); + } + HttpWebResponse cp_response = (HttpWebResponse)cp_Postrequest.GetResponse(); + var cp_responseString = new StreamReader(cp_response.GetResponseStream()).ReadToEnd(); + + XmlDocument cp_xmlDoc = new XmlDocument(); + cp_xmlDoc.LoadXml(cp_responseString); + + string cp_jsonText = JsonConvert.SerializeXmlNode(cp_xmlDoc); + var cp_data = Welcome.FromJson(cp_jsonText); + + foreach (var item in cp_data.Obj.Str) + { + control_point row = new control_point(); + string[] s1 = item.Val.Split(','); + + string[] s2 = s1[0].Split('/'); + var displayName = s1[1].Split('=')[1]; + + row.name = s1[0].Replace('/', '_').Remove(s1[0].Length - 1, 1).Remove(0, 1); + row.displayName = displayName; + + conPoint.Add(row); + } + #endregion + + #region main program + List result = new List(); + String API_Url = urlString; + + HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url); + Postrequest.Method = "GET"; + Postrequest.Headers.Add("Authorization", "Basic " + encoded); + Postrequest.PreAuthenticate = true; + + HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse(); + var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); + + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(responseString); + + string jsonText = JsonConvert.SerializeXmlNode(xmlDoc); + var data = Welcome.FromJson(jsonText); + + foreach (var item in data.Obj.Ref) + { + String API_Url2 = urlString + "/" + item.Name; + + HttpWebRequest Postrequest2 = (HttpWebRequest)WebRequest.Create(API_Url2); + Postrequest2.Method = "GET"; + Postrequest2.Headers.Add("Authorization", "Basic " + encoded); + Postrequest2.PreAuthenticate = true; + + HttpWebResponse response2 = (HttpWebResponse)Postrequest2.GetResponse(); + var responseString2 = new StreamReader(response2.GetResponseStream()).ReadToEnd(); + + XmlDocument xmlDoc2 = new XmlDocument(); + xmlDoc2.LoadXml(responseString2); + + string jsonText2 = JsonConvert.SerializeXmlNode(xmlDoc2); + var data2 = Welcome.FromJson(jsonText2); + + foreach (var item2 in data2.Obj.Ref) + { + if (item2.Name.Split('_').Length == 9) + { + ImpNiaItem row = new ImpNiaItem(); + row.device_area_tag = item2.Name.Split('_')[0]; + row.device_building_tag = item2.Name.Split('_')[1]; + row.device_system_tag = item2.Name.Split('_')[2]; + row.device_name_tag = item2.Name.Split('_')[3]; + row.device_point_name = item2.Name.Split('_')[8]; + row.parent_path = item.Name; + row.full_name = conPoint.Where(x => x.name == item2.Name).Select(x => x.displayName).FirstOrDefault(); + + result.Add(row); + } + } + } + #endregion + return result; } diff --git a/Backend/Views/NiagaraDataSynchronize/Index.cshtml b/Backend/Views/NiagaraDataSynchronize/Index.cshtml index 3262ed3..cdc5dd0 100644 --- a/Backend/Views/NiagaraDataSynchronize/Index.cshtml +++ b/Backend/Views/NiagaraDataSynchronize/Index.cshtml @@ -163,9 +163,10 @@ document.getElementById('loadDataText').innerText = "比對資料出錯了!"; return; } - var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); - var dateTime = date + ' ' + time; - document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime; + SyncDevItem(); + //var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); + //var dateTime = date + ' ' + time; + //document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime; }, }); } @@ -174,5 +175,48 @@ } } + function SyncDevItem() + { + //比對資料,有差異的話,再同步到device等資料表 + var url_synchronize_data = "/NiagaraDataSynchronize/RawDataDevItemList/"; + var url_synchronize_data_device_item = "/NiagaraDataSynchronize/DevIteComData/"; + + $.ajax({ + method: "POST", + url: url_synchronize_data, + success: function (rel) { + if (rel.code != "0000") { + toast_error(rel.msg); + document.getElementById('loadDataText').innerText = "比對資料出錯了!"; + return; + } + + $.ajax({ + method: "POST", + url: url_synchronize_data_device_item, + data: JSON.stringify(rel.data), + cache: false, + async: false, + contentType: "application/json; charset=UTF-8", + dataType: 'json', + success: function (rel) { + if (rel.code != "0000") { + toast_error(rel.msg); + document.getElementById('loadDataText').innerText = "比對資料出錯了!"; + return; + } + + console.log(rel); + var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); + var dateTime = date + ' ' + time; + document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime; + }, + }) + var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); + var dateTime = date + ' ' + time; + document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime; + }, + }); + } } \ No newline at end of file diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index fe3a51c..0ef65e9 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -73,7 +73,7 @@ namespace Repository.BackendRepository.Implement if (arrTag.Length == 8) { sb.Append($@" insert import_niagara_tag(niagara_tags, device_area_tag, device_building_tag, device_system_tag, - device_name_tag, device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, atDateTime) values('" + + device_name_tag, device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, device_full_name, atDateTime) values('" + row.tag_name + "', '" + arrTag[0] + "', '" + arrTag[1] + "', '" + @@ -83,6 +83,7 @@ namespace Repository.BackendRepository.Implement arrTag[5] + "', '" + arrTag[6] + "', '" + arrTag[7] + "', " + + row.displayName + "', '" + "now());"); } } @@ -110,7 +111,7 @@ namespace Repository.BackendRepository.Implement /// /// /// - public async Task InsertItemFromNiagara(List ds, string building) + public async Task InsertItemFromNiagara(List ds, string building) { using (IDbConnection conn = GetDbConnection()) { @@ -128,6 +129,8 @@ namespace Repository.BackendRepository.Implement `device_name_tag` varchar(50) DEFAULT NULL, `device_point_name` varchar(50) DEFAULT NULL, `check_status` varchar(50) DEFAULT NULL, + `parent_path` varchar(50) DEFAULT NULL, + `full_name` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; await conn.ExecuteAsync(sql); @@ -135,62 +138,22 @@ namespace Repository.BackendRepository.Implement await conn.ExecuteAsync(sql); #endregion - List dt_item = new List(); - + StringBuilder sb = new StringBuilder(); + #region 放入import_niagara_item資料表 foreach (var row in ds) { - if (string.IsNullOrEmpty(row.tag_name)) continue; - string[] arrTag = row.tag_name.Split('_'); - - #region for item - if (arrTag.Length == 8) - { - Device_item8 row_item = new Device_item8(); - row_item.tag_name = row.tag_name; - row_item.device_area_tag = arrTag[0]; - row_item.device_building_tag = arrTag[1]; - row_item.device_system_tag = arrTag[2]; - row_item.device_name_tag = arrTag[3]; - row_item.device_floor_tag = arrTag[4]; - row_item.device_master_tag = arrTag[5]; - row_item.device_last_name_tag = arrTag[6]; - row_item.device_serial_tag = arrTag[7]; - row_item.point_name = row.point_name; - dt_item.Add(row_item); - } - #endregion - } - - #region N4資料groupBy後放入import_niagara_item資料表 - var ds2_item = dt_item.GroupBy(x => new - { - device_area_tag2 = x.device_area_tag, - device_building_tag2 = x.device_building_tag, - device_system_tag2 = x.device_system_tag, - device_name_tag2 = x.device_name_tag, - point_name2 = x.point_name - }).Select(x => new Device_item8 - { - device_area_tag = x.Key.device_area_tag2, - device_building_tag = x.Key.device_building_tag2, - device_system_tag = x.Key.device_system_tag2, - device_name_tag = x.Key.device_name_tag2, - point_name = x.Key.point_name2 - }); - - StringBuilder sb = new StringBuilder(); - - foreach (var row2 in ds2_item) - { - sb.Append($@" insert import_niagara_item(device_area_tag, device_building_tag, device_system_tag, device_name_tag, device_point_name) - values('" + - row2.device_area_tag + "', '" + - row2.device_building_tag + "', '" + - row2.device_system_tag + "', '" + - row2.device_name_tag + "', '" + - row2.point_name + "'" + - ");"); + sb.Append($@" insert import_niagara_item(device_area_tag, device_building_tag, device_system_tag, device_name_tag, device_point_name, parent_path, full_name) + values('"+ + row.device_area_tag + "', '" + + row.device_building_tag + "', '" + + row.device_system_tag + "', '" + + row.device_name_tag + "', '" + + row.device_point_name + "', '" + + row.parent_path + "', '" + + row.full_name + "'" + + ");"); } + if (sb.Length > 0) { await conn.ExecuteAsync(sb.ToString()); @@ -251,14 +214,14 @@ namespace Repository.BackendRepository.Implement foreach (var data in result) { sb.Append($@" insert device(device_guid, deleted, status, priority, is_link, device_area_tag, - device_building_tag, device_system_tag, device_name_tag, device_floor_tag, device_master, + device_building_tag, device_system_tag, device_name_tag, full_name, device_floor_tag, device_master, device_last_name, device_serial_tag, device_number, device_system_category_layer3, created_at, updated_at) values(uuid(), 0, 1, 0, 1, '" + data.device_area_tag + "', '" + data.device_building_tag + "', '" + data.device_system_tag + "', '" + data.device_name_tag + "', '" + - + data.device_full_name + "', '" + data.device_floor_tag + "', '" + data.device_master_tag + "', '" + data.device_last_name_tag + "', '" + @@ -351,13 +314,15 @@ namespace Repository.BackendRepository.Implement isBool = 1; } sb.Append($@"insert device_item(deleted, points, is_show, is_show_riserDiagram, is_controll, is_bool, is_link, is_show_history, - device_system_tag, device_name_tag, created_at, updated_at) + device_system_tag, device_name_tag, full_name, parent_path, created_at, updated_at) VALUES (0, '" + data.device_point_name + "', 1, 0, " + isControll + "," + isBool + ", 1, 1, '" + data.device_system_tag + "', '" + data.device_name_tag + "', " + + data.full_name + "', " + + data.parent_path + "', " + "now(), now());"); } if (sb.Length > 0) diff --git a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs index fdcead3..be4ba4e 100644 --- a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs @@ -19,7 +19,7 @@ namespace Repository.BackendRepository.Interface /// /// /// - Task InsertItemFromNiagara(List ds, string building); + Task InsertItemFromNiagara(List ds, string building); /// /// 比對 device /// diff --git a/Repository/Models/BackgroundServiceTask.cs b/Repository/Models/BackgroundServiceTask.cs index 3bc756e..e1b378e 100644 --- a/Repository/Models/BackgroundServiceTask.cs +++ b/Repository/Models/BackgroundServiceTask.cs @@ -43,13 +43,11 @@ namespace Repository.Models /// public class Device_value { - public string name { get; set; } public string value { get; set; } public string tag_name { get; set; } public string point_name { get; set; } public string building { get; set; } - public string DisplayName { get; set; } - public string FullName { get; set; } + public string displayName { get; set; } } public class Device_item8 @@ -83,6 +81,7 @@ namespace Repository.Models public string device_master_tag { get; set; } public string device_last_name_tag { get; set; } public string device_serial_tag { get; set; } + public string device_full_name { get; set; } public string atDateTime { get; set; } public int is_used { get; set; } } @@ -94,6 +93,8 @@ namespace Repository.Models public string device_system_tag { get; set; } public string device_name_tag { get; set; } public string device_point_name { get; set; } + public string full_name { get; set; } + public string parent_path { get; set; } } public class building_menu @@ -107,4 +108,22 @@ namespace Repository.Models { public string device_kind_guid { get; set; } } + + public class ImpNiaItem + { + public string device_area_tag { get; set; } + public string device_building_tag { get; set; } + public string device_system_tag { get; set; } + public string device_name_tag { get; set; } + public string device_point_name { get; set; } + public string parent_path { get; set; } + public string check_status { get; set; } + public string full_name { get; set; } + } + + public class control_point + { + public string name { get; set; } + public string displayName { get; set; } + } } diff --git a/Repository/Models/quickType.cs b/Repository/Models/quickType.cs index 226c03d..9e573e8 100644 --- a/Repository/Models/quickType.cs +++ b/Repository/Models/quickType.cs @@ -33,6 +33,9 @@ namespace Repository.Models [JsonProperty("str")] public Str[] Str { get; set; } + + [JsonProperty("ref")] + public Str[] Ref { get; set; } } public partial class Str @@ -47,6 +50,15 @@ namespace Repository.Models public string Href { get; set; } } + public partial class Ref + { + [JsonProperty("@name")] + public string Name { get; set; } + + [JsonProperty("@href")] + public string Href { get; set; } + } + public partial class Xml { [JsonProperty("@version")]