From b17952ed2c61fd12ab146b1e1c8072becd05680c Mon Sep 17 00:00:00 2001 From: dev02 Date: Thu, 3 Aug 2023 14:14:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=AD=B7=E5=8F=B2=E8=B3=87?= =?UTF-8?q?=E6=96=99=E5=8C=AF=E5=87=BA=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/HistoryController.cs | 206 ++++++++++++------ FrontendWebApi/Models/HistoryClass.cs | 10 +- .../operation/維修_2023-01-01_2023-08-03.xlsx | Bin 0 -> 5553 bytes 3 files changed, 149 insertions(+), 67 deletions(-) create mode 100644 FrontendWebApi/wwwroot/excel/operation/維修_2023-01-01_2023-08-03.xlsx diff --git a/FrontendWebApi/ApiControllers/HistoryController.cs b/FrontendWebApi/ApiControllers/HistoryController.cs index e65d2cf..9ebc514 100644 --- a/FrontendWebApi/ApiControllers/HistoryController.cs +++ b/FrontendWebApi/ApiControllers/HistoryController.cs @@ -76,6 +76,21 @@ namespace FrontendWebApi.ApiControllers var device = backendRepository.GetAllAsync($"select * from device where deleted = 0 and is_link = 1 and device_number = '{lhe.device_number}'").Result; var devicePoint = backendRepository.GetAllAsync($"select * from device_item where deleted = 0 and is_link = 1 and is_show_history = 1 and device_building_tag = '{lhe.building_tag}'").Result; #endregion + + #region combine device and point + List listDevicePoint = new List(); + var dp = devicePoint.Where(x => x.device_building_tag == lhe.device_number.Split("_")[1]).ToList(); + foreach (var point in dp) + { + DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); + deviceNumberPoint.DeviceNumber = lhe.device_number; + deviceNumberPoint.Point = point.points; + deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", lhe.device_number, point.points); + + listDevicePoint.Add(deviceNumberPoint); + } + #endregion + #region get data from niagara var startTimestamp = string.Format("{0}T00:00:00.000+08:00", lhe.starttime.ToString("yyyy-MM-dd")); var endTimestamp = string.Format("{0}T23:59:59.000+08:00", lhe.endtime?.ToString("yyyy-MM-dd")); @@ -86,47 +101,67 @@ namespace FrontendWebApi.ApiControllers "; List he = new List(); - - var archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{lhe.deviceComName}/{lhe.device_number.Replace("$3", "")}_{lhe.device_item}/~historyRollup/"); - archiveRequest.Method = "POST"; - archiveRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveRequest.PreAuthenticate = true; + List jd = new List(); - byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); - using (Stream reqStream = archiveRequest.GetRequestStream()) + if (listDevicePoint.Count > 0) { - reqStream.Write(byteArray, 0, byteArray.Length); + foreach (var d in listDevicePoint) + { + var archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{lhe.deviceComName}/{d.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); + archiveRequest.Method = "POST"; + archiveRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveRequest.PreAuthenticate = true; + + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveRequest.GetRequestStream()) + { + reqStream.Write(byteArray, 0, byteArray.Length); + } + + var archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + var archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); + + XmlDocument xmlDocument = new XmlDocument(); + xmlDocument.LoadXml(archiveResponseContent); + var archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + var archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); + + if (!archiveJsonResult.ContainsKey("err")) + { + var jsonDevice = new JsonDevice(); + jsonDevice.deviceNumberPoint = d; + jsonDevice.json = archiveJsonResult; + jsonDevice.building_tag = lhe.building_tag; + jd.Add(jsonDevice); + } + } } - var archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); - var archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); - archiveResponse.Dispose(); - archiveResponse.Close(); - - XmlDocument xmlDocument = new XmlDocument(); - xmlDocument.LoadXml(archiveResponseContent); - var archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); - var archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - - if (!archiveJsonResult.ContainsKey("err")) + if (jd != null && jd.Count > 0) { - var ArrangeRawDatas = ArrangeRawData(new DeviceNumberPoint() { DeviceNumber = lhe.device_number, Point = lhe.device_item, FullDeviceNumberPoint = lhe.device_number + "_" + lhe.device_item}, archiveJsonResult); - if (ArrangeRawDatas != null && ArrangeRawDatas.Count > 0) + foreach (var d in jd) { - foreach (var ard in ArrangeRawDatas) + var ArrangeRawDatas = ArrangeRawData(d.deviceNumberPoint, d.json); + if (ArrangeRawDatas != null && ArrangeRawDatas.Count > 0) { - HistoryExport hed = new HistoryExport(); - hed.type = devicePoint.Where(x => x.device_building_tag == lhe.building_tag && x.points == ard["@point"].ToString()).Select(x => x.full_name).FirstOrDefault(); - hed.deviceName = device.Where(x => x.device_number == ard["@device_number"].ToString()).Select(x => x.full_name).FirstOrDefault(); - hed.value = ard["@avg_rawdata"].ToString() == "-1" ? "NaN" : Math.Round(Decimal.Parse(ard["@avg_rawdata"].ToString(), System.Globalization.NumberStyles.Float), 2).ToString(); - hed.timestamp = Convert.ToDateTime(ard["@start_timestamp"].ToString()); - hed.building_tag = lhe.building_tag; - he.Add(hed); + foreach (var ard in ArrangeRawDatas) + { + HistoryExport hed = new HistoryExport(); + hed.type = devicePoint.Where(x => x.device_building_tag == d.building_tag && x.points == ard["@point"].ToString()).Select(x => x.full_name).FirstOrDefault(); + hed.deviceName = device.Where(x => x.device_number == ard["@device_number"].ToString()).Select(x => x.full_name).FirstOrDefault(); + hed.value = ard["@avg_rawdata"].ToString() == "-1" ? "NaN" : Math.Round((decimal.Parse(ard["@avg_rawdata"].ToString(), System.Globalization.NumberStyles.Float)), 2).ToString(); + hed.timestamp = Convert.ToDateTime(ard["@start_timestamp"].ToString()); + hed.building_tag = d.building_tag; + he.Add(hed); + } } } } #endregion + #region export file progress #region excel設定 IFont font12 = workbook.CreateFont(); font12.FontName = "新細明體"; @@ -168,55 +203,94 @@ namespace FrontendWebApi.ApiControllers stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; stylein12.WrapText = true; #endregion - - ISheet sheet = workbook.CreateSheet("歷史資料"); - int RowPosition = 0; - #region set cell - IRow row = sheet.CreateRow(RowPosition); - sheet.SetColumnWidth(0, 4 * 160 * 12); - sheet.SetColumnWidth(1, 4 * 160 * 12); - sheet.SetColumnWidth(2, 4 * 160 * 12); - sheet.SetColumnWidth(3, 4 * 160 * 12); - ICell cell = row.CreateCell(0); - cell.SetCellValue("類型"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(1); - cell.SetCellValue("設備名稱"); - cell.CellStyle = styleLine12; - cell = row.CreateCell(2); - cell.SetCellValue("數值"); - cell = row.CreateCell(3); - cell.SetCellValue("記錄時間"); - cell.CellStyle = styleLine12; - #endregion - - foreach (var d in he) + var data = he.ToList(); + if (data.Count > 0) { - RowPosition += 1; + string lastDeviceItem = string.Empty; + int RowPosition = 0; + IRow row; + ISheet sheet; + #region set cell + sheet = workbook.CreateSheet($"{data[0].type}"); row = sheet.CreateRow(RowPosition); - for (var i = 0; i < 4; i++) + sheet.SetColumnWidth(0, 4 * 160 * 12); + sheet.SetColumnWidth(1, 4 * 160 * 12); + sheet.SetColumnWidth(2, 4 * 160 * 12); + sheet.SetColumnWidth(3, 4 * 160 * 12); + ICell cell = row.CreateCell(0); + cell.SetCellValue("類型"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(1); + cell.SetCellValue("設備名稱"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(2); + cell.SetCellValue("數值"); + cell = row.CreateCell(3); + cell.SetCellValue("記錄時間"); + cell.CellStyle = styleLine12; + #endregion + + foreach (var d in data) { - cell = row.CreateCell(i); - if (i == 0) + if (RowPosition == 0 && lastDeviceItem == "") { - cell.SetCellValue(d.type); + lastDeviceItem = d.type; //第一次不用建立 sheet; } - if (i == 1) + if (d.type != lastDeviceItem) { - cell.SetCellValue(d.deviceName); + lastDeviceItem = d.type; + sheet = workbook.CreateSheet($"{d.type}"); + #region set cell + row = sheet.CreateRow(RowPosition); + sheet.SetColumnWidth(0, 4 * 160 * 12); + sheet.SetColumnWidth(1, 4 * 160 * 12); + sheet.SetColumnWidth(2, 4 * 160 * 12); + sheet.SetColumnWidth(3, 4 * 160 * 12); + cell = row.CreateCell(0); + cell.SetCellValue("類型"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(1); + cell.SetCellValue("設備名稱"); + cell.CellStyle = styleLine12; + cell = row.CreateCell(2); + cell.SetCellValue("數值"); + cell = row.CreateCell(3); + cell.SetCellValue("記錄時間"); + cell.CellStyle = styleLine12; + #endregion + RowPosition = 0; } - if (i == 2) + else { - cell.SetCellValue(d.value); - } - if (i == 3) - { - cell.SetCellValue(d.timestamp.ToString("yyyy-MM-dd HH:mm") + ":00");// + RowPosition += 1; } - cell.CellStyle = style12; + row = sheet.CreateRow(RowPosition); + for (var i = 0; i < 4; i++) + { + cell = row.CreateCell(i); + if (i == 0) + { + cell.SetCellValue(d.type); + } + if (i == 1) + { + cell.SetCellValue(d.deviceName); + } + if (i == 2) + { + cell.SetCellValue(d.value); + } + if (i == 3) + { + cell.SetCellValue(d.timestamp.ToString("yyyy-MM-dd HH:mm") + ":00");// + } + + cell.CellStyle = style12; + } } } + #endregion } catch (Exception exception) { diff --git a/FrontendWebApi/Models/HistoryClass.cs b/FrontendWebApi/Models/HistoryClass.cs index bb04e69..b062150 100644 --- a/FrontendWebApi/Models/HistoryClass.cs +++ b/FrontendWebApi/Models/HistoryClass.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -326,4 +327,11 @@ namespace FrontendWebApi.Models public byte is_link { get; set; } public int is_show_history { get; set; } } + + public class JsonDevice + { + public DeviceNumberPoint deviceNumberPoint { get; set; } + public JObject json { get; set; } + public string building_tag { get; set; } + } } diff --git a/FrontendWebApi/wwwroot/excel/operation/維修_2023-01-01_2023-08-03.xlsx b/FrontendWebApi/wwwroot/excel/operation/維修_2023-01-01_2023-08-03.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..a828359f8097cb021934fe2bc20a8c0a7d6d6fe2 GIT binary patch literal 5553 zcmZ`-by$;o*dE=TBGQdCqDV={kWOhv%Eo|!gtQhwprQ_wM)ZkNdga>wfZnbbwgc)BpegAMoBC%UDD`o#G4n-y|jgK!iT6JnX?R zA;Dk26%+DWUBcuzW924`cavzt$jguFCQxpJ#JLN0_*ARCRC^D(6lx*Ygi%qZUzMWi(0jv2<1aoAG0~R`5Yv7SW^o^t;7{PNf6T zZXa=fQEe)4Pqb415BXl<{nv0|^Q~f8oP4Wn0F?yw3J9EQ2u>j+GaU5~mQ*Wx%4Iv@ITt zqzW14+SH;-^ChZre%0wK-a?Ai3AAVrjoDP<&zusmp>SMm6_zH)jrzt*f<>6!Shhal z6enh6m>(cdV4R1Nv-r-tTL*_d^y$B#8GU8sSU2yrOssI&ej-#W*P2;>A3Sd9aTOzb zm73_SLUm{yAkZeqG0zteh5(AU>}{7kCTQf$!K`Z3Y5#X5W^i;x;-Tr-Lf;hsqyz-@ zu>T_t6NWXqg{i}>3hkB_>w4UIAdY$Xp>kNHJO>JXjzzN<@$)rnZ<~)2{Bb59@xy`M z{jE!9_gD|Bp&L6T@3wDbL1U^+Ub@ldr4~7Vt+K_N>jgHH0i7AEvTC+Vl$pb$zlGP) zAgK_T9fLA_D@hNw-^?=DG{F%LO&#OCa8nGuu~pdZE8_wP=*4Kc#S= zI_x1eA`?0P`na~Qg0gLqn-DWFUB zk~-7i__d7MAy8B#WH0ByHDCGSz?GpsG?a~LY3L{um7XD#trUkV+@LSorK@(#CMS~t z&G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