From 803d74d6454cbab65c222df61de71228b70dfe44 Mon Sep 17 00:00:00 2001 From: dev02 Date: Thu, 29 Jun 2023 11:21:36 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=AD=B7=E5=8F=B2=E8=B3=87=E6=96=99=E5=8C=AF=E5=87=BA,=20?= =?UTF-8?q?=E5=BE=8C=E5=8F=B0=E5=90=8C=E6=AD=A5station=E6=9B=B4=E6=8F=9B?= =?UTF-8?q?=20[=E5=89=8D=E7=AB=AF]=20=E5=A2=9E=E5=8A=A0loading=E7=95=8C?= =?UTF-8?q?=E9=9D=A2,=20=E4=BF=AE=E6=94=B9=E9=80=81=E5=87=BAniagara=20buil?= =?UTF-8?q?ding=5Ftag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/Models/Mybase.cs | 6 + .../Quartz/Jobs/ArchiveElectricMeterDayJob.cs | 10 +- .../Implement/ProcEletricMeterService.cs | 138 ++++--- Frontend/_historyData.html | 56 +-- .../ApiControllers/HistoryController.cs | 381 +++++++++++++++--- FrontendWebApi/Models/HistoryClass.cs | 43 +- .../excel/history/歷史資料_2023-06-29.xlsx | Bin 0 -> 2601389 bytes 7 files changed, 481 insertions(+), 153 deletions(-) create mode 100644 FrontendWebApi/wwwroot/excel/history/歷史資料_2023-06-29.xlsx diff --git a/Backend/Models/Mybase.cs b/Backend/Models/Mybase.cs index 254ec07..6732228 100644 --- a/Backend/Models/Mybase.cs +++ b/Backend/Models/Mybase.cs @@ -88,4 +88,10 @@ namespace Backend.Models public int Value { get; set; } //通常放id public string Name { get; set; } } + + public class BuildStation + { + public string system_value { get; set; } + public string system_key { get; set; } + } } diff --git a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs index 79bc6db..3de882a 100644 --- a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs +++ b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs @@ -61,6 +61,7 @@ namespace BackendWorkerService.Quartz.Jobs 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(); + var buildingStation = await backgroundServiceRepository.GetAllAsync("select SUBSTRING_INDEX(system_value, '/', 1) system_value, system_key from variable where system_type = 'dashboard_total_elec' and deleted = 0 and (SUBSTRING_INDEX(system_value, '/', 1) != '' and SUBSTRING_INDEX(system_value, '/', 1) is not null)"); #region http variable HttpWebRequest archiveRequest = null; @@ -127,7 +128,6 @@ namespace BackendWorkerService.Quartz.Jobs obixApiConfig.ApiBase = variableObix.Where(x => x.Name == "ApiBase").Select(x => x.Value).FirstOrDefault(); obixApiConfig.UserName = ed.AESDecrypt(variableObix.Where(x => x.Name == "UserName").Select(x => x.Value).FirstOrDefault()); obixApiConfig.Password = ed.AESDecrypt(variableObix.Where(x => x.Name == "Password").Select(x => x.Value).FirstOrDefault()); - var station = await backgroundServiceRepository.GetOneAsync($@"select system_value from variable where system_type = 'obixStatus' and deleted = 0"); String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(obixApiConfig.UserName + ":" + obixApiConfig.Password)); #endregion 取得obix 設定 @@ -159,7 +159,7 @@ namespace BackendWorkerService.Quartz.Jobs List> waterArchiveDayRawDatas = new List>(); foreach (var deviceNumberPoint in electricDeviceNumberPoints) { - + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveRequest.Method = "POST"; @@ -216,7 +216,7 @@ namespace BackendWorkerService.Quartz.Jobs } foreach (var deviceNumberPoint in waterDeviceNumberPoints) { - + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveRequest.Method = "POST"; @@ -628,6 +628,7 @@ namespace BackendWorkerService.Quartz.Jobs List> waterArchiveWeekRawDatas = new List>(); foreach (var deviceNumberPoint in electricDeviceNumberPoints) { + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveRequest.Method = "POST"; @@ -685,6 +686,7 @@ namespace BackendWorkerService.Quartz.Jobs } foreach (var deviceNumberPoint in waterDeviceNumberPoints) { + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveRequest.Method = "POST"; @@ -993,6 +995,7 @@ namespace BackendWorkerService.Quartz.Jobs List> waterArchiveMonthRawDatas = new List>(); foreach (var deviceNumberPoint in electricDeviceNumberPoints) { + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveRequest.Method = "POST"; @@ -1049,6 +1052,7 @@ namespace BackendWorkerService.Quartz.Jobs } foreach (var deviceNumberPoint in waterDeviceNumberPoints) { + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveRequest.Method = "POST"; diff --git a/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs b/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs index a574ce1..a4dea3c 100644 --- a/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs +++ b/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs @@ -55,6 +55,7 @@ namespace BackendWorkerService.Services.Implement var variableArchive = await backgroundServiceRepository.GetAllAsync(sqlArchive); repeatTimes = Convert.ToInt32(variableArchive.Where(x => x.Name == "RepeatTimes").Select(x => x.Value).FirstOrDefault()); var saveToMSDB = await backgroundServiceRepository.GetOneAsync("select system_value from variable where system_type = 'save_to_ms_db' and deleted = 0"); + var buildingStation = await backgroundServiceRepository.GetAllAsync("select SUBSTRING_INDEX(system_value, '/', 1) system_value, system_key from variable where system_type = 'dashboard_total_elec' and deleted = 0 and (SUBSTRING_INDEX(system_value, '/', 1) != '' and SUBSTRING_INDEX(system_value, '/', 1) is not null)"); #region 取得obix 設定 var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; @@ -63,7 +64,6 @@ namespace BackendWorkerService.Services.Implement 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(); - var station = await backgroundServiceRepository.GetOneAsync($@"select system_value from variable where system_type = 'obixStatus' and deleted = 0"); encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(obixApiConfig.UserName + ":" + obixApiConfig.Password)); #endregion 取得obix 設定 @@ -280,6 +280,7 @@ namespace BackendWorkerService.Services.Implement "; + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveDayRequest.Method = "POST"; @@ -354,84 +355,85 @@ namespace BackendWorkerService.Services.Implement var water_error_days = await backgroundServiceRepository.GetAllAsync(sql_error_day, new { RepeatTimes = repeatTimes }); List> waterArchiveDayRawDatas = new List>(); if (water_error_days.Count() > 0) - { - foreach (var error_day in water_error_days) { - DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); - deviceNumberPoint.DeviceNumber = error_day.Device_number; - deviceNumberPoint.Point = error_day.Point; - deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", error_day.Device_number, error_day.Point); - - var startTimestamp = string.Format("{0}+08:00", error_day.Start_timestamp.Replace(" ", "T")); - var endTimestamp = string.Format("{0}+08:00", error_day.End_timestamp.Replace(" ", "T")); - var historyQueryFilter = $@" - - - - "; - - HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); - //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveDayRequest.Method = "POST"; - archiveDayRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveDayRequest.PreAuthenticate = true; - - byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); - using (Stream reqStream = archiveDayRequest.GetRequestStream()) + foreach (var error_day in water_error_days) { - reqStream.Write(byteArray, 0, byteArray.Length); - } + DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); + deviceNumberPoint.DeviceNumber = error_day.Device_number; + deviceNumberPoint.Point = error_day.Point; + deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", error_day.Device_number, error_day.Point); - HttpWebResponse archiveDayResponse = (HttpWebResponse)archiveDayRequest.GetResponse(); - var archiveDayResponseContent = new StreamReader(archiveDayResponse.GetResponseStream()).ReadToEnd(); + var startTimestamp = string.Format("{0}+08:00", error_day.Start_timestamp.Replace(" ", "T")); + var endTimestamp = string.Format("{0}+08:00", error_day.End_timestamp.Replace(" ", "T")); + var historyQueryFilter = $@" + + + + "; - xmlDocument.LoadXml(archiveDayResponseContent); - string archiveDayJson = JsonConvert.SerializeXmlNode(xmlDocument); - JObject archiveDayJsonResult = (JObject)JsonConvert.DeserializeObject(archiveDayJson); + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); + HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); + archiveDayRequest.Method = "POST"; + archiveDayRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveDayRequest.PreAuthenticate = true; - if (archiveDayJsonResult.ContainsKey("err")) //抓取錯誤 - { - Dictionary archiveDayRawData = new Dictionary(); - archiveDayRawData.Add("@device_number", error_day.Device_number); - archiveDayRawData.Add("@point", error_day.Point); - archiveDayRawData.Add("@start_timestamp", DateTime.Parse(error_day.Start_timestamp, System.Globalization.CultureInfo.CurrentCulture)); - archiveDayRawData.Add("@end_timestamp", DateTime.Parse(error_day.End_timestamp, System.Globalization.CultureInfo.CurrentCulture)); - archiveDayRawData.Add("@is_complete", 0); - archiveDayRawData.Add("@repeat_times", ++error_day.Repeat_times); - archiveDayRawData.Add("@fail_reason", archiveDayJson); - - archiveDayRawData.Add("@count_rawdata", 0); - archiveDayRawData.Add("@min_rawdata", 0); - archiveDayRawData.Add("@max_rawdata", 0); - archiveDayRawData.Add("@avg_rawdata", 0); - archiveDayRawData.Add("@sum_rawdata", 0); - archiveDayRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - - waterArchiveDayRawDatas.Add(archiveDayRawData); - } - - if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容 - { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult); - if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveDayRequest.GetRequestStream()) { - waterArchiveDayRawDatas.AddRange(ArrangeRawDatas); + reqStream.Write(byteArray, 0, byteArray.Length); + } + + HttpWebResponse archiveDayResponse = (HttpWebResponse)archiveDayRequest.GetResponse(); + var archiveDayResponseContent = new StreamReader(archiveDayResponse.GetResponseStream()).ReadToEnd(); + + xmlDocument.LoadXml(archiveDayResponseContent); + string archiveDayJson = JsonConvert.SerializeXmlNode(xmlDocument); + JObject archiveDayJsonResult = (JObject)JsonConvert.DeserializeObject(archiveDayJson); + + if (archiveDayJsonResult.ContainsKey("err")) //抓取錯誤 + { + Dictionary archiveDayRawData = new Dictionary(); + archiveDayRawData.Add("@device_number", error_day.Device_number); + archiveDayRawData.Add("@point", error_day.Point); + archiveDayRawData.Add("@start_timestamp", DateTime.Parse(error_day.Start_timestamp, System.Globalization.CultureInfo.CurrentCulture)); + archiveDayRawData.Add("@end_timestamp", DateTime.Parse(error_day.End_timestamp, System.Globalization.CultureInfo.CurrentCulture)); + archiveDayRawData.Add("@is_complete", 0); + archiveDayRawData.Add("@repeat_times", ++error_day.Repeat_times); + archiveDayRawData.Add("@fail_reason", archiveDayJson); + + archiveDayRawData.Add("@count_rawdata", 0); + archiveDayRawData.Add("@min_rawdata", 0); + archiveDayRawData.Add("@max_rawdata", 0); + archiveDayRawData.Add("@avg_rawdata", 0); + archiveDayRawData.Add("@sum_rawdata", 0); + archiveDayRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + + waterArchiveDayRawDatas.Add(archiveDayRawData); + } + + if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + { + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult); + if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + { + waterArchiveDayRawDatas.AddRange(ArrangeRawDatas); + } } } - } - if (waterArchiveDayRawDatas.Count() > 0) - { - var Mysql_error_update = string.Format(MYsql_update_format, targetTable); - var sql_error_update = string.Format(sql_update_format, targetTable); - if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + if (waterArchiveDayRawDatas.Count() > 0) { - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveDayRawDatas); + var Mysql_error_update = string.Format(MYsql_update_format, targetTable); + var sql_error_update = string.Format(sql_update_format, targetTable); + if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + { + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveDayRawDatas); + } + await backgroundServiceRepository.ExecuteSql(Mysql_error_update, waterArchiveDayRawDatas); } - await backgroundServiceRepository.ExecuteSql(Mysql_error_update, waterArchiveDayRawDatas); } } - } #endregion 天歸檔補償 #region 週歸檔補償 @@ -458,6 +460,7 @@ namespace BackendWorkerService.Services.Implement "; + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveWeekRequest.Method = "POST"; @@ -542,6 +545,7 @@ namespace BackendWorkerService.Services.Implement "; + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveWeekRequest.Method = "POST"; @@ -632,6 +636,7 @@ namespace BackendWorkerService.Services.Implement "; + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveMonthRequest.Method = "POST"; @@ -719,6 +724,7 @@ namespace BackendWorkerService.Services.Implement "; + var station = buildingStation.Where(x => x.system_key == deviceNumberPoint.DeviceNumber.Split("_")[1]).Select(x => x.system_value).FirstOrDefault(); HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); archiveMonthRequest.Method = "POST"; diff --git a/Frontend/_historyData.html b/Frontend/_historyData.html index ca1da80..2c959c7 100644 --- a/Frontend/_historyData.html +++ b/Frontend/_historyData.html @@ -19,10 +19,8 @@
-
+
@@ -81,11 +79,14 @@