diff --git a/Backend/Controllers/NiagaraDataSynchronizeController.cs b/Backend/Controllers/NiagaraDataSynchronizeController.cs index c9610c7..2f2c473 100644 --- a/Backend/Controllers/NiagaraDataSynchronizeController.cs +++ b/Backend/Controllers/NiagaraDataSynchronizeController.cs @@ -120,6 +120,7 @@ namespace Backend.Controllers await niagaraDataSynchronizeRepository.CheckFullNameEmptyReplaceByDeviceName(); await niagaraDataSynchronizeRepository.InsertBuildingMenu(); await niagaraDataSynchronizeRepository.InsertSubSystemFloor(); + await this.DeviceDisasterAsync(); result = true; apiResult.Code = "0000"; @@ -221,5 +222,32 @@ namespace Backend.Controllers return apiResult; } + + private async Task DeviceDisasterAsync() + { + 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(sqlObix); + var obixApiConfig = new Models.ObixApiConfig(); + 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.TagQuantity = variableObix.Where(x => x.Name == "tag_quantity").Select(x => x.Value).FirstOrDefault(); + + if (obixApiConfig.TagQuantity == "5") + { + List dv = new List(); + List urlSlots = await backendRepository.GetAllAsync("select obixSlot from building where deleted = 0"); + List disasterTag = await backendRepository.GetAllAsync("select system_value from variable where system_type = 'disaster' and deleted = 0"); + webRequestService svc = new webRequestService(); + foreach(var dt in disasterTag) + { + string bql = "neql:hs:geoAddr=\"ER" + dt + "\""; + dv.AddRange(svc.obixDevDisaster(obixApiConfig.ApiBase + "obix/config/Arena/Program/ObixQuery/query/", bql, obixApiConfig.TagQuantity, obixApiConfig.UserName, + obixApiConfig.Password, dt)); + } + + await niagaraDataSynchronizeRepository.DeviceDisasterAysnc(dv); + } + } } } diff --git a/Backend/Models/DeviceDisaster.cs b/Backend/Models/DeviceDisaster.cs new file mode 100644 index 0000000..d01abc4 --- /dev/null +++ b/Backend/Models/DeviceDisaster.cs @@ -0,0 +1,13 @@ +namespace Backend.Models +{ + public class DeviceDisasterOutput + { + public string device_guid { 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_floor_tag { get; set; } + public string device_serial_tag { get; set; } + public string device_number { get; set; } + } +} diff --git a/Backend/Services/Implement/webRequestService.cs b/Backend/Services/Implement/webRequestService.cs index 96e0630..e8ccca6 100644 --- a/Backend/Services/Implement/webRequestService.cs +++ b/Backend/Services/Implement/webRequestService.cs @@ -327,6 +327,69 @@ 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(); + //String username = "obixUser"; + //String password = "Admin123456"; + String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(acc + ":" + pass)); + String API_Url = urlString; + + HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url); + Postrequest.Method = "POST"; + Postrequest.Headers.Add("Authorization", "Basic " + encoded); + Postrequest.PreAuthenticate = true; + + using (var streamWriter = new StreamWriter(Postrequest.GetRequestStream())) + { + string json = ""; + + streamWriter.Write(json); + } + + 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); + + if (data.Obj.Str != null) + { + foreach (var item in data.Obj.Str) + { + Device_value_disaster row = new Device_value_disaster(); + row.value = item.Val; + string[] s1 = item.Val.Split(','); + string[] s2 = s1[0].Split('/'); + row.tag_name = ""; + row.disasterValue = dt; + + for (int i = 0; i < s2.Length; i++) + { + if (i == s2.Length - 2) + { + 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].Replace("$3", ""); + } + } + result.Add(row); + } + } + + return result; + } + //private static void getRefData(string urlString, string encoded, List conPoint, List result, Welcome data ) //obixHistory data //{ // foreach (var item in data.Obj.Ref) diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index 53e91eb..8ffba7b 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -858,5 +858,57 @@ namespace Repository.BackendRepository.Implement } } } + + public async Task DeviceDisasterAysnc(List dv) + { + using (IDbConnection conn = GetDbConnection()) + { + conn.Open(); + using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + try + { + StringBuilder sb = new StringBuilder(); + sb.Append("select * from device_disaster"); + var 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(); + foreach (var d in dv.Where(x => deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6]))) + { + string device_number = d.value.Split('/')[6]; + sb.Append($"update device_disaster set device_system_value = '{d.disasterValue}' where device_number = '{device_number}'; "); + } + + foreach (var d in dv.Where(x => !deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6]))) + { + 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_number.Split('_')[1]}, {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()); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + } + } + } } } diff --git a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs index ef2688a..a535158 100644 --- a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs @@ -70,5 +70,6 @@ namespace Repository.BackendRepository.Interface /// /// Task InsertSubSystemFloor(); + Task DeviceDisasterAysnc(List dv); } } diff --git a/Repository/Models/BackgroundServiceTask.cs b/Repository/Models/BackgroundServiceTask.cs index 7ccafa3..c049778 100644 --- a/Repository/Models/BackgroundServiceTask.cs +++ b/Repository/Models/BackgroundServiceTask.cs @@ -131,4 +131,25 @@ namespace Repository.Models public string displayName { get; set; } public bool isHistory { get; set; } } + + public class DeviceDisasterOutput + { + public string device_guid { 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_floor_tag { get; set; } + public string device_serial_tag { get; set; } + public string device_number { get; set; } + } + + public class Device_value_disaster + { + 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 disasterValue { get; set; } + } }