修改歷史資料匯出流程
This commit is contained in:
parent
e14be83897
commit
b17952ed2c
@ -76,6 +76,21 @@ namespace FrontendWebApi.ApiControllers
|
||||
var device = backendRepository.GetAllAsync<Device>($"select * from device where deleted = 0 and is_link = 1 and device_number = '{lhe.device_number}'").Result;
|
||||
var devicePoint = backendRepository.GetAllAsync<Device_item>($"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<DeviceNumberPoint> listDevicePoint = new List<DeviceNumberPoint>();
|
||||
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
|
||||
</obj>";
|
||||
|
||||
List<HistoryExport> he = new List<HistoryExport>();
|
||||
List<JsonDevice> jd = new List<JsonDevice>();
|
||||
|
||||
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;
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user