From 40ba482d701b013f28ac82d3a75d1b364477bdb2 Mon Sep 17 00:00:00 2001 From: dev02 Date: Wed, 7 Jun 2023 12:16:44 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=89=8D=E5=8F=B0]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=8D=B3=E6=99=82=E5=91=8A=E8=AD=A6table=20render=20=E5=95=8F?= =?UTF-8?q?=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/appsettings.Development.json | 4 +- .../Quartz/Jobs/ArchiveElectricMeterDayJob.cs | 221 +++++++++++------- Frontend/_alert.html | 2 +- 3 files changed, 138 insertions(+), 89 deletions(-) diff --git a/Backend/appsettings.Development.json b/Backend/appsettings.Development.json index 83cd09a..b5af96e 100644 --- a/Backend/appsettings.Development.json +++ b/Backend/appsettings.Development.json @@ -18,10 +18,10 @@ "Port": "js2LutKe+rdjzdxMPQUrvQ==", //"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30 //"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp - "Database": "7gWfmZ28HGIJZbxEbK+0yg==", //tpe_dome_dome + //"Database": "7gWfmZ28HGIJZbxEbK+0yg==", //tpe_dome_dome //"Database": "siTUcDaC/g2yGTMFWD72Kg==", //tpe_dome_hotel //"Database": "iuaY0h0+TWkir44/eZLDqw==", //tpe_dome_office - //"Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //mcut + "Database": "Rq7Gn4x6LwBvVtl7GY8LbA==", //mcut "Root": "SzdxEgaJJ7tcTCrUl2zKsA==", "Password": "FVAPxztxpY4gJJKQ/se4bQ==" }, diff --git a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs index 8a3b8cc..cdfeb09 100644 --- a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs +++ b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs @@ -55,13 +55,20 @@ namespace BackendWorkerService.Quartz.Jobs EDFunction ed = new EDFunction(); XmlDocument xmlDocument = new XmlDocument(); + ServicePointManager.DefaultConnectionLimit = 10; var sqlArchive = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'archiveConfig'"; var saveToMSDB = await backgroundServiceRepository.GetOneAsync("select system_value from variable where system_type = 'save_to_ms_db' and deleted = 0"); - var variableArchive = await backgroundServiceRepository.GetAllAsync(sqlArchive); var electricMeterGuid = variableArchive.Where(x => x.Name == "ElectricMeterGuid").Select(x => x.Value).FirstOrDefault(); var waterMeterGuid = variableArchive.Where(x => x.Name == "WaterMeterGuid").Select(x => x.Value).FirstOrDefault(); + #region http variable + HttpWebRequest archiveRequest = null; + HttpWebResponse archiveResponse = null; + string archiveResponseContent = null; + string archiveJson = null; + JObject archiveJsonResult = new JObject(); + #endregion #region 找出所有電錶設備 var sWhere = "deleted = 0 AND device_name_tag = @sub_system_guid"; var electricMeters = await backgroundServiceRepository.GetAllAsync("device", sWhere, new { sub_system_guid = electricMeterGuid }); @@ -147,26 +154,29 @@ namespace BackendWorkerService.Quartz.Jobs foreach (var deviceNumberPoint in electricDeviceNumberPoints) { - HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveDayRequest.Method = "POST"; - archiveDayRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveDayRequest.PreAuthenticate = true; + archiveRequest.Method = "POST"; + archiveRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveRequest.PreAuthenticate = true; + archiveRequest.Timeout = 3000; byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); - using (Stream reqStream = archiveDayRequest.GetRequestStream()) + using (Stream reqStream = archiveRequest.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } - HttpWebResponse archiveDayResponse = (HttpWebResponse)archiveDayRequest.GetResponse(); - var archiveDayResponseContent = new StreamReader(archiveDayResponse.GetResponseStream()).ReadToEnd(); + archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); - xmlDocument.LoadXml(archiveDayResponseContent); - string archiveDayJson = JsonConvert.SerializeXmlNode(xmlDocument); - JObject archiveDayJsonResult = (JObject)JsonConvert.DeserializeObject(archiveDayJson); + xmlDocument.LoadXml(archiveResponseContent); + archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - if (archiveDayJsonResult.ContainsKey("err")) //抓取錯誤 + if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 { //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】"); //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveDayJsonResult); @@ -178,7 +188,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveDayRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); archiveDayRawData.Add("@is_complete", 0); archiveDayRawData.Add("@repeat_times", 0); - archiveDayRawData.Add("@fail_reason", archiveDayJson); + archiveDayRawData.Add("@fail_reason", archiveJson); archiveDayRawData.Add("@count_rawdata", 0); archiveDayRawData.Add("@min_rawdata", 0); @@ -190,9 +200,9 @@ namespace BackendWorkerService.Quartz.Jobs electericArchiveDayRawDatas.Add(archiveDayRawData); } - if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult); + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { electericArchiveDayRawDatas.AddRange(ArrangeRawDatas); @@ -202,26 +212,29 @@ namespace BackendWorkerService.Quartz.Jobs foreach (var deviceNumberPoint in waterDeviceNumberPoints) { - HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveDayRequest.Method = "POST"; - archiveDayRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveDayRequest.PreAuthenticate = true; + archiveRequest.Method = "POST"; + archiveRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveRequest.PreAuthenticate = true; + archiveRequest.Timeout = 3000; byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); - using (Stream reqStream = archiveDayRequest.GetRequestStream()) + using (Stream reqStream = archiveRequest.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } - HttpWebResponse archiveDayResponse = (HttpWebResponse)archiveDayRequest.GetResponse(); - var archiveDayResponseContent = new StreamReader(archiveDayResponse.GetResponseStream()).ReadToEnd(); + archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); - xmlDocument.LoadXml(archiveDayResponseContent); - string archiveDayJson = JsonConvert.SerializeXmlNode(xmlDocument); - JObject archiveDayJsonResult = (JObject)JsonConvert.DeserializeObject(archiveDayJson); + xmlDocument.LoadXml(archiveResponseContent); + archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - if (archiveDayJsonResult.ContainsKey("err")) //抓取錯誤 + if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 { //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】"); //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveDayJsonResult); @@ -233,7 +246,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveDayRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); archiveDayRawData.Add("@is_complete", 0); archiveDayRawData.Add("@repeat_times", 0); - archiveDayRawData.Add("@fail_reason", archiveDayJson); + archiveDayRawData.Add("@fail_reason", archiveJson); archiveDayRawData.Add("@count_rawdata", 0); archiveDayRawData.Add("@min_rawdata", 0); @@ -245,9 +258,9 @@ namespace BackendWorkerService.Quartz.Jobs waterArchiveDayRawDatas.Add(archiveDayRawData); } - if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult); + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { waterArchiveDayRawDatas.AddRange(ArrangeRawDatas); @@ -573,6 +586,14 @@ namespace BackendWorkerService.Quartz.Jobs logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【任務失敗】"); logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【任務失敗】[Exception]:{0}", exception.ToString()); } + finally + { + if (archiveResponse != null) + { + archiveResponse.Dispose(); + archiveResponse.Close(); + } + } } #endregion 天歸檔 @@ -602,26 +623,29 @@ namespace BackendWorkerService.Quartz.Jobs List> waterArchiveWeekRawDatas = new List>(); foreach (var deviceNumberPoint in electricDeviceNumberPoints) { - HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveWeekRequest.Method = "POST"; - archiveWeekRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveWeekRequest.PreAuthenticate = true; + archiveRequest.Method = "POST"; + archiveRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveRequest.PreAuthenticate = true; + archiveRequest.Timeout = 3000; byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); - using (Stream reqStream = archiveWeekRequest.GetRequestStream()) + using (Stream reqStream = archiveRequest.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } - HttpWebResponse archiveWeekResponse = (HttpWebResponse)archiveWeekRequest.GetResponse(); - var archiveWeekResponseContent = new StreamReader(archiveWeekResponse.GetResponseStream()).ReadToEnd(); + archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); - xmlDocument.LoadXml(archiveWeekResponseContent); - string archiveWeekJson = JsonConvert.SerializeXmlNode(xmlDocument); - JObject archiveWeekJsonResult = (JObject)JsonConvert.DeserializeObject(archiveWeekJson); + xmlDocument.LoadXml(archiveResponseContent); + archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - if (archiveWeekJsonResult.ContainsKey("err")) //抓取錯誤 + if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 { //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】"); //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveWeekJsonResult); @@ -633,7 +657,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveWeekRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); archiveWeekRawData.Add("@is_complete", 0); archiveWeekRawData.Add("@repeat_times", 0); - archiveWeekRawData.Add("@fail_reason", archiveWeekJson); + archiveWeekRawData.Add("@fail_reason", archiveJson); archiveWeekRawData.Add("@count_rawdata", 0); archiveWeekRawData.Add("@min_rawdata", 0); @@ -645,9 +669,9 @@ namespace BackendWorkerService.Quartz.Jobs electricArchiveWeekRawDatas.Add(archiveWeekRawData); } - if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult); + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { electricArchiveWeekRawDatas.AddRange(ArrangeRawDatas); @@ -656,26 +680,29 @@ namespace BackendWorkerService.Quartz.Jobs } foreach (var deviceNumberPoint in waterDeviceNumberPoints) { - HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveWeekRequest.Method = "POST"; - archiveWeekRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveWeekRequest.PreAuthenticate = true; + archiveRequest.Method = "POST"; + archiveRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveRequest.PreAuthenticate = true; + archiveRequest.Timeout = 3000; byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); - using (Stream reqStream = archiveWeekRequest.GetRequestStream()) + using (Stream reqStream = archiveRequest.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } - HttpWebResponse archiveWeekResponse = (HttpWebResponse)archiveWeekRequest.GetResponse(); - var archiveWeekResponseContent = new StreamReader(archiveWeekResponse.GetResponseStream()).ReadToEnd(); + archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); - xmlDocument.LoadXml(archiveWeekResponseContent); - string archiveWeekJson = JsonConvert.SerializeXmlNode(xmlDocument); - JObject archiveWeekJsonResult = (JObject)JsonConvert.DeserializeObject(archiveWeekJson); + xmlDocument.LoadXml(archiveResponseContent); + archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - if (archiveWeekJsonResult.ContainsKey("err")) //抓取錯誤 + if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 { //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】"); //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveWeekJsonResult); @@ -687,7 +714,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveWeekRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); archiveWeekRawData.Add("@is_complete", 0); archiveWeekRawData.Add("@repeat_times", 0); - archiveWeekRawData.Add("@fail_reason", archiveWeekJson); + archiveWeekRawData.Add("@fail_reason", archiveJson); archiveWeekRawData.Add("@count_rawdata", 0); archiveWeekRawData.Add("@min_rawdata", 0); @@ -699,9 +726,9 @@ namespace BackendWorkerService.Quartz.Jobs waterArchiveWeekRawDatas.Add(archiveWeekRawData); } - if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult); + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { waterArchiveWeekRawDatas.AddRange(ArrangeRawDatas); @@ -923,6 +950,14 @@ namespace BackendWorkerService.Quartz.Jobs logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【任務失敗】"); logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【任務失敗】[Exception]:{0}", exception.ToString()); } + finally + { + if (archiveResponse != null) + { + archiveResponse.Dispose(); + archiveResponse.Close(); + } + } } #endregion 週歸檔 @@ -954,26 +989,29 @@ namespace BackendWorkerService.Quartz.Jobs List> waterArchiveMonthRawDatas = new List>(); foreach (var deviceNumberPoint in electricDeviceNumberPoints) { - HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveMonthRequest.Method = "POST"; - archiveMonthRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveMonthRequest.PreAuthenticate = true; + archiveRequest.Method = "POST"; + archiveRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveRequest.PreAuthenticate = true; + archiveRequest.Timeout = 3000; byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); - using (Stream reqStream = archiveMonthRequest.GetRequestStream()) + using (Stream reqStream = archiveRequest.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } - HttpWebResponse archiveMonthResponse = (HttpWebResponse)archiveMonthRequest.GetResponse(); - var archiveMonthResponseContent = new StreamReader(archiveMonthResponse.GetResponseStream()).ReadToEnd(); + archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); - xmlDocument.LoadXml(archiveMonthResponseContent); - string archiveMonthJson = JsonConvert.SerializeXmlNode(xmlDocument); - JObject archiveMonthJsonResult = (JObject)JsonConvert.DeserializeObject(archiveMonthJson); + xmlDocument.LoadXml(archiveResponseContent); + archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - if (archiveMonthJsonResult.ContainsKey("err")) //抓取錯誤 + if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 { //logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【取得資料失敗】"); //logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveMonthJsonResult); @@ -985,7 +1023,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveMonthRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); archiveMonthRawData.Add("@is_complete", 0); archiveMonthRawData.Add("@repeat_times", 0); - archiveMonthRawData.Add("@fail_reason", archiveMonthJson); + archiveMonthRawData.Add("@fail_reason", archiveJson); archiveMonthRawData.Add("@count_rawdata", 0); archiveMonthRawData.Add("@min_rawdata", 0); @@ -997,9 +1035,9 @@ namespace BackendWorkerService.Quartz.Jobs electricArchiveMonthRawDatas.Add(archiveMonthRawData); } - if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult); + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { electricArchiveMonthRawDatas.AddRange(ArrangeRawDatas); @@ -1008,26 +1046,29 @@ namespace BackendWorkerService.Quartz.Jobs } foreach (var deviceNumberPoint in waterDeviceNumberPoints) { - HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveMonthRequest.Method = "POST"; - archiveMonthRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveMonthRequest.PreAuthenticate = true; + archiveRequest.Method = "POST"; + archiveRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveRequest.PreAuthenticate = true; + archiveRequest.Timeout = 3000; byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); - using (Stream reqStream = archiveMonthRequest.GetRequestStream()) + using (Stream reqStream = archiveRequest.GetRequestStream()) { reqStream.Write(byteArray, 0, byteArray.Length); } - HttpWebResponse archiveMonthResponse = (HttpWebResponse)archiveMonthRequest.GetResponse(); - var archiveMonthResponseContent = new StreamReader(archiveMonthResponse.GetResponseStream()).ReadToEnd(); + archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); - xmlDocument.LoadXml(archiveMonthResponseContent); - string archiveMonthJson = JsonConvert.SerializeXmlNode(xmlDocument); - JObject archiveMonthJsonResult = (JObject)JsonConvert.DeserializeObject(archiveMonthJson); + xmlDocument.LoadXml(archiveResponseContent); + archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - if (archiveMonthJsonResult.ContainsKey("err")) //抓取錯誤 + if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 { //logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【取得資料失敗】"); //logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveMonthJsonResult); @@ -1039,7 +1080,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveMonthRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); archiveMonthRawData.Add("@is_complete", 0); archiveMonthRawData.Add("@repeat_times", 0); - archiveMonthRawData.Add("@fail_reason", archiveMonthJson); + archiveMonthRawData.Add("@fail_reason", archiveJson); archiveMonthRawData.Add("@count_rawdata", 0); archiveMonthRawData.Add("@min_rawdata", 0); @@ -1051,9 +1092,9 @@ namespace BackendWorkerService.Quartz.Jobs waterArchiveMonthRawDatas.Add(archiveMonthRawData); } - if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult); + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { waterArchiveMonthRawDatas.AddRange(ArrangeRawDatas); @@ -1268,6 +1309,14 @@ namespace BackendWorkerService.Quartz.Jobs logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【任務失敗】"); logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【任務失敗】[Exception]:{0}", exception.ToString()); } + finally + { + if (archiveResponse != null) + { + archiveResponse.Dispose(); + archiveResponse.Close(); + } + } } #endregion 月歸檔 @@ -1277,7 +1326,7 @@ namespace BackendWorkerService.Quartz.Jobs { try { - await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Compensate", "任務開始"); + await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Compensate", "補償機制任務開始"); ProcEletricMeterService procEletricMeterService = new ProcEletricMeterService(backgroundServiceRepository, backgroundServiceMsSqlRepository); await procEletricMeterService.ArchiveData(); await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Compensate", "任務完成"); diff --git a/Frontend/_alert.html b/Frontend/_alert.html index 496f2d5..e66ffa3 100644 --- a/Frontend/_alert.html +++ b/Frontend/_alert.html @@ -875,7 +875,7 @@ { "title": "設備名稱", "data": "sourceName_zh", - render: (data) => { + "render" : function (data) { return allDevices.find(d => d.device_number === data)?.full_name || "" } },