diff --git a/Backend/Controllers/NiagaraDataSynchronizeController.cs b/Backend/Controllers/NiagaraDataSynchronizeController.cs index cad41e1..4153bc4 100644 --- a/Backend/Controllers/NiagaraDataSynchronizeController.cs +++ b/Backend/Controllers/NiagaraDataSynchronizeController.cs @@ -162,12 +162,12 @@ namespace Backend.Controllers //obixApiConfig.UrlSlot = variableObix.Where(x => x.Name == "url_slot").Select(x => x.Value).FirstOrDefault(); var urlSlots = backendRepository.GetAllAsync("select obixSlot from building where deleted = 0").Result; - foreach(var us in urlSlots) - { - var data = svc.obixHisQuery(obixApiConfig.ApiBase + "obix/config/Program/ObixQuery/query/", obixApiConfig.ApiBase + "obix/histories", us, + //foreach(var us in urlSlots) + //{ + var data = svc.obixHisQuery(obixApiConfig.ApiBase + "obix/config/Program/ObixQuery/query/", obixApiConfig.ApiBase + "obix/histories", "", obixApiConfig.UserName, obixApiConfig.Password); ds.AddRange(data); - } + // } var buildings = backendRepository.GetAllAsync("select building_tag from building where deleted = 0").Result; apiResult.Code = "0000"; @@ -202,11 +202,30 @@ namespace Backend.Controllers //var building = ss.Split("_")[0]; var building = ds.GroupBy(x => x.device_building_tag).Select(x => x.Key).ToList(); - await niagaraDataSynchronizeRepository.InsertItemFromNiagara(ds, building); // insert 暫存table import_niagara_item + await niagaraDataSynchronizeRepository.InsertItemFromNiagara(ds, building); // insert 暫存table import_niagara_item await niagaraDataSynchronizeRepository.DeviceItemComparison(); //insert device_item await niagaraDataSynchronizeRepository.CheckItemDiffFullNameAndCover(); // update device_item.fullname await niagaraDataSynchronizeRepository.ItemCheckFullNameEmptyReplaceByDeviceName(); // 檢查device_item內FullName為空的值,以points取代 - await niagaraDataSynchronizeRepository.CheckItemIsShowHistory(); + await niagaraDataSynchronizeRepository.CheckItemIsShowHistory(); + + #region 歷史資料處理 + ds.Clear(); + var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; + var variableObix = backendRepository.GetAllAsync(sqlObix).Result; + var obixApiConfig = new Backend.Models.ObixApiConfig(); + EDFunction ed = new EDFunction(); + obixApiConfig.ApiBase = variableObix.Where(x => x.Name == "ApiBase").Select(x => x.Value).FirstOrDefault(); + obixApiConfig.UserName = variableObix.Where(x => x.Name == "UserName").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(); + webRequestService svc = new webRequestService(); + var data = svc.obixHisQuery_v2(obixApiConfig.ApiBase + "obix/config/Program/ObixQuery/query/", obixApiConfig.ApiBase + "obix/histories", "", + obixApiConfig.UserName, obixApiConfig.Password); + ds.AddRange(data); + // save to history + await niagaraDataSynchronizeRepository.InsertItemFromNiagara_History(ds, building); // insert 暫存table import_niagara_item_history + #endregion + result = true; apiResult.Code = "0000"; diff --git a/Backend/Services/Implement/webRequestService.cs b/Backend/Services/Implement/webRequestService.cs index 7219dcd..e1e08a8 100644 --- a/Backend/Services/Implement/webRequestService.cs +++ b/Backend/Services/Implement/webRequestService.cs @@ -131,6 +131,149 @@ namespace Backend.Services.Implement return result; } + /// + /// 獲取 niagara history 中的點位 + /// + /// + /// + /// + /// + public List obixHisQuery_v2(string bqlUrlString, string urlString, string slot, string acc, string pass) + { + //String username = "obixUser"; + //String password = "Admin123456"; + //String username = "stanGG"; + //String password = "St12345678"; + String username = acc; + String password = pass; + String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); + String oneStationName = null; + #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].Remove(s1[0].Length - 1, 1).Remove(0, 1); + // row.displayName = displayName; + // row.isHistory = false; + + // 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); + + // 1.循環所有的 station + //ref https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c + foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) + { + var stationName = node.Attributes["name"].InnerText; // WSP_Supervisor + oneStationName = oneStationName ?? stationName; + String API_Url2 = urlString + "/" + stationName; // http://192.168.0.136:8081/obix/histories/WSP_Supervisor + + 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); + // 2.獲取 station 中全部的設備+點位 + foreach (XmlNode node2 in xmlDoc2.DocumentElement.ChildNodes) + { + string tagName = node2.Attributes["name"].InnerText; + if (tagName.Split('_').Length == 9) + { + if (tagName.Split('_')[1].IndexOf("8F5") != -1) + { + string ss = tagName.Split('_')[1]; + } + ImpNiaItem row = new ImpNiaItem(); + row.device_area_tag = tagName.Split('_')[0]; + row.device_building_tag = tagName.Split('_')[1]; + row.device_system_tag = tagName.Split('_')[2]; + row.device_name_tag = tagName.Split('_')[3]; + row.device_floor_tag= tagName.Split('_')[4]; + row.device_master_tag= tagName.Split('_')[5]; + row.device_last_name_tag = tagName.Split('_')[6]; + row.device_serial_tag = tagName.Split('_')[7]; + row.device_point_name = tagName.Split('_')[8]; + row.parent_path = stationName; + + result.Add(row); + } + else if (tagName.Split('_').Length == 6) + { //巨蛋 tag 5 段版本 + ImpNiaItem row = new ImpNiaItem(); + if (tagName.Split('_')[1] == "S") //for security system + { + row.device_building_tag = tagName.Split('_')[0]; + row.device_system_tag = tagName.Split('_')[1]; + row.device_floor_tag = tagName.Split('_')[2]; + row.device_name_tag = tagName.Split('_')[3]; + row.device_point_name = tagName.Split('_')[5]; + } + else //for normal + { + row.device_building_tag = tagName.Split('_')[0]; + //row.device_system_tag = tagName.Split('_')[1]; + row.device_floor_tag = tagName.Split('_')[2]; + row.device_name_tag = tagName.Split('_')[1]; + row.device_point_name = tagName.Split('_')[5]; + } + row.parent_path = stationName; + result.Add(row); + } + } + } + #endregion + return result.ToList(); + //return result.Where(x => x.full_name != null).ToList(); + } + /// /// 獲取 niagara history 中的點位 /// @@ -204,7 +347,7 @@ namespace Backend.Services.Implement xmlDoc.LoadXml(responseString); //ref https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c - foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) + foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) //循環所有的 station { var stationName = node.Attributes["name"].InnerText; // WSP_Supervisor oneStationName = oneStationName ?? stationName; @@ -328,6 +471,8 @@ namespace Backend.Services.Implement return result.Where(x => x.full_name != null).ToList(); } + + public List obixDevDisaster(string urlString, string bql, string tag_quantity, string acc, string pass, string dt) { List result = new List(); diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index 4dc5fa1..18cdf7b 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -183,27 +183,39 @@ namespace Repository.BackendRepository.Implement { try { + string sql = @" +DROP TABLE IF EXISTS `import_niagara_item`; +CREATE TABLE `import_niagara_item` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `device_area_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_building_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_system_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_name_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_floor_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_master_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_last_name_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_serial_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_point_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `parent_path` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `is_history` bit(1) NULL DEFAULT b'0', + `full_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `check_status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 271 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; + "; + await conn.ExecuteAsync(sql); + #region 刪除 import_niagara_item資料表中選取的棟別 - foreach(var b in building) - { - string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_item` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `device_area_tag` varchar(50) DEFAULT NULL, - `device_building_tag` varchar(50) DEFAULT NULL, - `device_system_tag` varchar(50) DEFAULT NULL, - `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); - sql = "delete from import_niagara_item where device_building_tag = '" + b + "'"; - await conn.ExecuteAsync(sql); - } + //foreach(var b in building) + //{ + + // sql = "delete from import_niagara_item where device_building_tag = '" + b + "'"; + // await conn.ExecuteAsync(sql); + //} #endregion - ds = ds.GroupBy(x => new { + ds = ds.GroupBy(x => new + { device_area_tag = x.device_area_tag, device_building_tag = x.device_building_tag, device_system_tag = x.device_system_tag, @@ -229,17 +241,22 @@ namespace Repository.BackendRepository.Implement #region 放入import_niagara_item資料表 foreach (var row in ds) { - 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, is_history) - values('"+ + string ss = $@" insert import_niagara_item(device_area_tag, device_building_tag, device_system_tag, device_name_tag, + device_point_name, parent_path, full_name, is_history, created_at) + values('" + row.device_area_tag + "', '" + row.device_building_tag + "', '" + row.device_system_tag + "', '" + row.device_name_tag + "', '" + + //row.device_floor_tag + "', '" + + //row.device_master_tag + "', '" + + //row.device_last_name_tag + "', '" + row.device_point_name + "', '" + row.parent_path + "', '" + row.full_name + "'," + - row.isHistory + - ");"); + row.isHistory + ", now() " + + ");"; + sb.Append(ss); } var isDome = await GetOneAsync("select system_value from variable where deleted = 0 and system_type = 'project_name';"); @@ -270,6 +287,96 @@ namespace Repository.BackendRepository.Implement } } } + + public async Task InsertItemFromNiagara_History(List ds, List building) + { + using (IDbConnection conn = GetDbConnection()) + { + conn.Open(); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + try + { + //改成每次都新增 + string sql = @"SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for import_niagara_item_history +-- ---------------------------- +DROP TABLE IF EXISTS `import_niagara_item_history`; +CREATE TABLE `import_niagara_item_history` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `device_area_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_building_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_system_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_name_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_floor_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_master_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_last_name_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_serial_tag` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `device_point_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `parent_path` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `is_history` bit(1) NULL DEFAULT b'0', + `full_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `check_status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 271 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; + +SET FOREIGN_KEY_CHECKS = 1;"; + await conn.ExecuteAsync(sql); + + #region 刪除 import_niagara_item資料表中選取的棟別 + //foreach (var b in building) + //{ + + // sql = "delete from import_niagara_item_history where device_building_tag = '" + b + "'"; + // await conn.ExecuteAsync(sql); + //} + #endregion + + + StringBuilder sb = new StringBuilder(); + #region 放入import_niagara_item資料表 + foreach (var row in ds) + { + string ss = $@" insert import_niagara_item_history(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, device_point_name, parent_path, full_name, is_history, created_at) + values('" + + row.device_area_tag + "', '" + + row.device_building_tag + "', '" + + row.device_system_tag + "', '" + + row.device_name_tag + "', '" + + row.device_floor_tag + "', '" + + row.device_master_tag + "', '" + + row.device_last_name_tag + "', '" + + row.device_serial_tag + "', '" + + row.device_point_name + "', '" + + row.parent_path + "', '" + + row.full_name + "'," + + row.isHistory + ", now() " + + ");"; + sb.Append(ss); + } + if (sb.Length > 0) + { + await conn.ExecuteAsync(sb.ToString()); + sb.Clear(); + } + #endregion + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + } + } + } /// /// 比對 device /// diff --git a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs index cc583ef..09525fc 100644 --- a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs @@ -28,6 +28,14 @@ namespace Repository.BackendRepository.Interface /// /// Task InsertItemFromNiagara(List ds, List building); + + /// + /// 同步N4至資料表 import_niagara_item_History + /// + /// + /// + /// + Task InsertItemFromNiagara_History(List ds, List building); /// /// 比對 device /// diff --git a/Repository/Models/BackgroundServiceTask.cs b/Repository/Models/BackgroundServiceTask.cs index d806dad..7def7f1 100644 --- a/Repository/Models/BackgroundServiceTask.cs +++ b/Repository/Models/BackgroundServiceTask.cs @@ -117,7 +117,12 @@ 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 device_floor_tag { get; set; } + public string device_floor_tag { get; set; } //add by jiahao 2023-07-23 + public string device_master_tag { get; set; } //add by jiahao 2023-07-23 + + public string device_last_name_tag { get; set; }//add by jiahao 2023-07-23 + + public string device_serial_tag { get; set; }//add by jiahao 2023-07-23 public string parent_path { get; set; } public string check_status { get; set; } diff --git a/z01_WinAPP/Program.cs b/z01_WinAPP/Program.cs index 5de7e2b..ef35d3a 100644 --- a/z01_WinAPP/Program.cs +++ b/z01_WinAPP/Program.cs @@ -18,8 +18,8 @@ namespace tpDomeWinAPP Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - // Application.Run(new fmCheckTagName()); - Application.Run(new fmDecrypt()); + Application.Run(new fmCheckTagName()); + //Application.Run(new fmDecrypt()); } } }