From 904bdfc3e72bfb33f9a8e9d4784297a3a9739692 Mon Sep 17 00:00:00 2001 From: "jay.chang" Date: Wed, 31 Jan 2024 14:09:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B0=B4=E9=9B=BB=E8=A1=A8?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E7=A8=8B=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Backend/Models/Device.cs | 11 + .../Quartz/Jobs/ArchiveElectricMeterDayJob.cs | 1232 +++++++++-------- 2 files changed, 655 insertions(+), 588 deletions(-) diff --git a/Backend/Models/Device.cs b/Backend/Models/Device.cs index 23cf472..72c53d6 100644 --- a/Backend/Models/Device.cs +++ b/Backend/Models/Device.cs @@ -81,6 +81,16 @@ namespace Backend.Models public string device_model_tag { get; set; } public List Device_disasters { get; set; } //防災類型 public List Device_nodes { get; set; } //設備子節點 + + /// + /// 前次成功日期;下次重新歸檔日期 + /// + public System.DateTime archive_lastDate { get; set; } + + /// + /// 前次執行日期 + /// + public System.DateTime archive_lastActionDate { get; set; } } public class DeviceDisaster @@ -266,6 +276,7 @@ namespace Backend.Models public string DeviceNumber { get; set; } public string Point { get; set; } public string FullDeviceNumberPoint { get; set; } + public System.DateTime archive_lastDate { get; set; } } public class ImportDevForCoo diff --git a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs index 350131e..aeb8626 100644 --- a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs +++ b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs @@ -50,7 +50,7 @@ namespace BackendWorkerService.Quartz.Jobs string device_number = string.Empty; try { - if(await task_Detail.GetNeedWorkTask("ArchiveElectricMeterDayJob", "All")) + if (await task_Detail.GetNeedWorkTask("ArchiveElectricMeterDayJob", "All")) { await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "All", "任務開始"); EDFunction ed = new EDFunction(); @@ -79,7 +79,7 @@ namespace BackendWorkerService.Quartz.Jobs #endregion 找出所有電錶設備 #region 找出所有電錶系統的點位 - var sPointWhere = "deleted = 0 AND device_name_tag = @sub_system_guid"; + var sPointWhere = "is_needArchive = 1 and deleted = 0 AND device_name_tag = @sub_system_guid"; var electricPoints = await backgroundServiceRepository.GetAllAsync("device_item", sPointWhere, new { sub_system_guid = electricMeterGuid }); var waterPoints = await backgroundServiceRepository.GetAllAsync("device_item", sPointWhere, new { sub_system_guid = waterMeterGuid }); #endregion 找出所有電錶系統的點位 @@ -96,7 +96,7 @@ namespace BackendWorkerService.Quartz.Jobs deviceNumberPoint.DeviceNumber = electricMeter.Device_number; deviceNumberPoint.Point = point.points; deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", electricMeter.Device_number, point.points); - + deviceNumberPoint.archive_lastDate = electricMeter.archive_lastDate; electricDeviceNumberPoints.Add(deviceNumberPoint); } } @@ -114,6 +114,7 @@ namespace BackendWorkerService.Quartz.Jobs deviceNumberPoint.DeviceNumber = waterMeter.Device_number; deviceNumberPoint.Point = point.points; deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", waterMeter.Device_number, point.points); + deviceNumberPoint.archive_lastDate = waterMeter.archive_lastDate; waterDeviceNumberPoints.Add(deviceNumberPoint); } @@ -140,17 +141,25 @@ namespace BackendWorkerService.Quartz.Jobs try { await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Day", "水電表天任務開始"); + //var preDay = now.AddDays(-1); //取得前一天 + //var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0'); + + //var startTimestamp = string.Format("{0}T00:00:00.000+08:00", preDay.ToString("yyyy-MM-dd")); + //var endTimestamp = string.Format("{0}T23:59:59.000+08:00", preDay.ToString("yyyy-MM-dd")); + + //var historyQueryFilter = $@" + // + // + // + // "; + + #region 從前次 成功日期到昨天為止 + var preDay = now.AddDays(-1); //取得前一天 + var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0'); - var startTimestamp = string.Format("{0}T00:00:00.000+08:00", preDay.ToString("yyyy-MM-dd")); - var endTimestamp = string.Format("{0}T23:59:59.000+08:00", preDay.ToString("yyyy-MM-dd")); - - var historyQueryFilter = $@" - - - - "; + #endregion //Stopwatch stopWatch = new Stopwatch(); //stopWatch.Start(); @@ -160,148 +169,178 @@ namespace BackendWorkerService.Quartz.Jobs List> waterArchiveDayRawDatas = new List>(); foreach (var deviceNumberPoint in electricDeviceNumberPoints) { - device_number = deviceNumberPoint.FullDeviceNumberPoint; - if (tagQuantity.Equals("5")) - station = await backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history + var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd"); + var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); + foreach (DateTime day in EachDay(startDay, endDay)) + { + var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); + var eDay = string.Format("{0}T00:15:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T")); + + + string historyQueryFilter = $@" + + + + "; + device_number = deviceNumberPoint.FullDeviceNumberPoint; + if (tagQuantity.Equals("5")) + station = await backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history where device_building_tag = '{device_number.Split("_")[0]}' and device_floor_tag = '{device_number.Split("_")[2]}' and device_name_tag = '{device_number.Split("_")[1]}' and device_point_name = '{device_number.Split("_")[5]}'"); - else - station = backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history + else + station = backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history where device_building_tag = '{device_number.Split("_")[1].Replace("$3", "")}' and device_system_tag = '{device_number.Split("_")[2]}' and device_name_tag = '{device_number.Split("_")[3]}' and device_floor_tag = '{device_number.Split("_")[4]}' and device_master_tag = '{device_number.Split("_")[5]}' and device_last_name_tag = '{device_number.Split("_")[6]}' and device_serial_tag = '{device_number.Split("_")[7]}' and device_point_name = '{device_number.Split("_")[8]}'").Result; - archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); - //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveRequest.Method = "POST"; - archiveRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveRequest.PreAuthenticate = true; + archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); + //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~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); - } - - archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); - archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); - archiveResponse.Dispose(); - archiveResponse.Close(); - - xmlDocument.LoadXml(archiveResponseContent); - archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); - archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - - if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 - { - //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】"); - //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveDayJsonResult); - - Dictionary archiveDayRawData = new Dictionary(); - archiveDayRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); - archiveDayRawData.Add("@point", deviceNumberPoint.Point); - archiveDayRawData.Add("@start_timestamp", startTimestamp.Replace("T", " ").Substring(0, 19)); - archiveDayRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); - archiveDayRawData.Add("@is_complete", 0); - archiveDayRawData.Add("@repeat_times", 0); - archiveDayRawData.Add("@fail_reason", archiveJson); - - 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")); - - electericArchiveDayRawDatas.Add(archiveDayRawData); - } - - if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 - { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); - if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveRequest.GetRequestStream()) { - electericArchiveDayRawDatas.AddRange(ArrangeRawDatas); + reqStream.Write(byteArray, 0, byteArray.Length); + } + + archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); + + xmlDocument.LoadXml(archiveResponseContent); + archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); + + if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 + { + //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】"); + //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveDayJsonResult); + + Dictionary archiveDayRawData = new Dictionary(); + archiveDayRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); + archiveDayRawData.Add("@point", deviceNumberPoint.Point); + archiveDayRawData.Add("@start_timestamp", sDay.Replace("T", " ").Substring(0, 19)); + archiveDayRawData.Add("@end_timestamp", eDay.Replace("T", " ").Substring(0, 19)); + archiveDayRawData.Add("@is_complete", 0); + archiveDayRawData.Add("@repeat_times", 0); + archiveDayRawData.Add("@fail_reason", archiveJson); + + 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")); + + electericArchiveDayRawDatas.Add(archiveDayRawData); + } + + if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + { + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); + if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + { + electericArchiveDayRawDatas.AddRange(ArrangeRawDatas); + } } } } foreach (var deviceNumberPoint in waterDeviceNumberPoints) { - device_number = deviceNumberPoint.FullDeviceNumberPoint; - if (tagQuantity.Equals("5")) - station = await backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history + var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd"); + var endDay = System.DateTime.Now.ToString("yyyy-MM-dd"); + foreach (DateTime day in EachDay(startDay, endDay)) + { + var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); + var eDay = string.Format("{0}T00:15:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T")); + + + string historyQueryFilter = $@" + + + + "; + + device_number = deviceNumberPoint.FullDeviceNumberPoint; + if (tagQuantity.Equals("5")) + station = await backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history where device_building_tag = '{device_number.Split("_")[0]}' and device_floor_tag = '{device_number.Split("_")[2]}' and device_name_tag = '{device_number.Split("_")[1]}' and device_point_name = '{device_number.Split("_")[5]}'"); - else - station = backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history + else + station = backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history where device_building_tag = '{device_number.Split("_")[1].Replace("$3", "")}' and device_system_tag = '{device_number.Split("_")[2]}' and device_name_tag = '{device_number.Split("_")[3]}' and device_floor_tag = '{device_number.Split("_")[4]}' and device_master_tag = '{device_number.Split("_")[5]}' and device_last_name_tag = '{device_number.Split("_")[6]}' and device_serial_tag = '{device_number.Split("_")[7]}' and device_point_name = '{device_number.Split("_")[8]}'").Result; - archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); - //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveRequest.Method = "POST"; - archiveRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveRequest.PreAuthenticate = true; + archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); + //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~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); - } - - archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); - archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); - archiveResponse.Dispose(); - archiveResponse.Close(); - - xmlDocument.LoadXml(archiveResponseContent); - archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); - archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - - if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 - { - //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】"); - //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveDayJsonResult); - - Dictionary archiveDayRawData = new Dictionary(); - archiveDayRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); - archiveDayRawData.Add("@point", deviceNumberPoint.Point); - archiveDayRawData.Add("@start_timestamp", startTimestamp.Replace("T", " ").Substring(0, 19)); - archiveDayRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); - archiveDayRawData.Add("@is_complete", 0); - archiveDayRawData.Add("@repeat_times", 0); - archiveDayRawData.Add("@fail_reason", archiveJson); - - 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 (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 - { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); - if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveRequest.GetRequestStream()) { - waterArchiveDayRawDatas.AddRange(ArrangeRawDatas); + reqStream.Write(byteArray, 0, byteArray.Length); + } + + archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + archiveResponse.Dispose(); + archiveResponse.Close(); + + xmlDocument.LoadXml(archiveResponseContent); + archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); + + if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 + { + //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】"); + //logger.LogError("【ArchiveElectricMeterDayJob】【天歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveDayJsonResult); + + Dictionary archiveDayRawData = new Dictionary(); + archiveDayRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); + archiveDayRawData.Add("@point", deviceNumberPoint.Point); + archiveDayRawData.Add("@start_timestamp", sDay.Replace("T", " ").Substring(0, 19)); + archiveDayRawData.Add("@end_timestamp", eDay.Replace("T", " ").Substring(0, 19)); + archiveDayRawData.Add("@is_complete", 0); + archiveDayRawData.Add("@repeat_times", 0); + archiveDayRawData.Add("@fail_reason", archiveJson); + + 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 (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + { + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); + if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + { + waterArchiveDayRawDatas.AddRange(ArrangeRawDatas); + } } } } + //stopWatch.Stop(); //logger.LogInformation("【ArchiveElectricMeterDayJob】【天歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); if (electericArchiveDayRawDatas.Count() > 0) { - var sql = $@"CREATE TABLE IF NOT EXISTS `archive_electric_meter_day_{dbDateName}` ( + var sql = $@"CREATE TABLE IF NOT EXISTS `archive_electric_water_meter_day_{dbDateName}` ( `device_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `point` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `start_timestamp` datetime(6) NOT NULL, @@ -309,7 +348,7 @@ namespace BackendWorkerService.Quartz.Jobs `count_rawdata` int(11) NULL DEFAULT NULL, `min_rawdata` decimal(15, 3) NULL DEFAULT NULL, `max_rawdata` decimal(15, 3) NULL DEFAULT NULL, - `kwh_result` decimal(15, 3) NULL DEFAULT NULL, + `sub_result` decimal(15, 3) NULL DEFAULT NULL, `avg_rawdata` decimal(15, 3) NULL DEFAULT NULL, `sum_rawdata` decimal(15, 3) NULL DEFAULT NULL, `is_complete` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '是否完成,0:未完成 1:完成', @@ -321,11 +360,11 @@ namespace BackendWorkerService.Quartz.Jobs ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC; SET FOREIGN_KEY_CHECKS = 1; - UPDATE archive_electric_meter_day_{dbDateName} SET + UPDATE archive_electric_water_meter_day_{dbDateName} SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, + sub_result = @max_rawdata - @min_rawdata, avg_rawdata = @avg_rawdata, sum_rawdata = @sum_rawdata, is_complete = @is_complete, @@ -336,7 +375,7 @@ namespace BackendWorkerService.Quartz.Jobs AND point = @point AND start_timestamp = @start_timestamp; - INSERT INTO archive_electric_meter_day_{dbDateName} ( + INSERT INTO archive_electric_water_meter_day_{dbDateName} ( device_number, point, start_timestamp, @@ -344,7 +383,7 @@ namespace BackendWorkerService.Quartz.Jobs count_rawdata, min_rawdata, max_rawdata, - kwh_result, + sub_result, avg_rawdata, sum_rawdata, is_complete, @@ -364,53 +403,55 @@ namespace BackendWorkerService.Quartz.Jobs @is_complete, @repeat_times, @fail_reason - WHERE ROW_COUNT() = 0;"; + WHERE ROW_COUNT() = 0; + update device set archive_lastDate = @start_timestamp, archive_lastActionDate = @updated_at + where device_number = @device_number;"; //archive_lastDate 前次完成時間 ,archive_lastActionDate 本次作業時間 var mySql = $@"BEGIN TRANSACTION; - IF OBJECT_ID(N'dbo.archive_electric_meter_day_{dbDateName}', N'U') is null + IF OBJECT_ID(N'dbo.archive_electric_water_meter_day_{dbDateName}', N'U') is null BEGIN - CREATE TABLE [dbo].[archive_electric_meter_day_{dbDateName}]( - [device_number] [varchar](50) NOT NULL, - [point] [varchar](20) NOT NULL, - [start_timestamp] [datetime] NOT NULL, - [end_timestamp] [datetime] NULL, - [count_rawdata] [int] NULL, - [min_rawdata] [decimal](15, 3) NULL, - [max_rawdata] [decimal](15, 3) NULL, - [kwh_result] [decimal](15, 3) NULL, - [avg_rawdata] [decimal](15, 3) NULL, - [sum_rawdata] [decimal](15, 3) NULL, - [is_complete] [tinyint] NULL, - [repeat_times] [int] NULL, - [fail_reason] [nvarchar](max) NULL, - [created_at] [datetime] NULL, - [updated_at] [datetime] NULL, - CONSTRAINT [PK_archive_electric_meter_day_{dbDateName}] PRIMARY KEY CLUSTERED + CREATE TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}]( + [device_number] [varchar](50) NOT NULL, + [point] [varchar](20) NOT NULL, + [start_timestamp] [datetime] NOT NULL, + [end_timestamp] [datetime] NULL, + [count_rawdata] [int] NULL, + [min_rawdata] [decimal](15, 3) NULL, + [max_rawdata] [decimal](15, 3) NULL, + [sub_result] [decimal](15, 3) NULL, + [avg_rawdata] [decimal](15, 3) NULL, + [sum_rawdata] [decimal](15, 3) NULL, + [is_complete] [tinyint] NULL, + [repeat_times] [int] NULL, + [fail_reason] [nvarchar](max) NULL, + [created_at] [datetime] NULL, + [updated_at] [datetime] NULL, + CONSTRAINT [PK_archive_electric_water_meter_day_{dbDateName}] PRIMARY KEY CLUSTERED ( - [device_number] ASC, - [point] ASC, - [start_timestamp] ASC + [device_number] ASC, + [point] ASC, + [start_timestamp] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] - ALTER TABLE [dbo].[archive_electric_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_meter_day_{dbDateName}_repeat_times] DEFAULT ((0)) FOR [repeat_times] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_repeat_times] DEFAULT ((0)) FOR [repeat_times] - ALTER TABLE [dbo].[archive_electric_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_meter_day_{dbDateName}_created_at] DEFAULT (getdate()) FOR [created_at] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_created_at] DEFAULT (getdate()) FOR [created_at] - ALTER TABLE [dbo].[archive_electric_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_meter_day_{dbDateName}_updated_at] DEFAULT (NULL) FOR [updated_at] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_updated_at] DEFAULT (NULL) FOR [updated_at] - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否完成,0:未完成 1:完成' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'is_complete' + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否完成,0:未完成 1:完成' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'is_complete' - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'重複次數' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'repeat_times' + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'重複次數' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'repeat_times' - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'失敗原因' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'失敗原因' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' END - UPDATE archive_electric_meter_day_{dbDateName} SET + UPDATE archive_electric_water_meter_day_{dbDateName} SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, + sub_result = @max_rawdata - @min_rawdata, avg_rawdata = @avg_rawdata, sum_rawdata = @sum_rawdata, is_complete = @is_complete, @@ -423,7 +464,7 @@ namespace BackendWorkerService.Quartz.Jobs IF @@ROWCOUNT = 0 BEGIN - INSERT INTO archive_electric_meter_day_{dbDateName} ( + INSERT INTO archive_electric_water_meter_day_{dbDateName} ( device_number, point, start_timestamp, @@ -431,7 +472,7 @@ namespace BackendWorkerService.Quartz.Jobs count_rawdata, min_rawdata, max_rawdata, - kwh_result, + sub_result, avg_rawdata, sum_rawdata, is_complete, @@ -463,7 +504,7 @@ namespace BackendWorkerService.Quartz.Jobs if (waterArchiveDayRawDatas.Count() > 0) { var sql = $@" - CREATE TABLE IF NOT EXISTS `archive_water_meter_day_{dbDateName}` ( + CREATE TABLE IF NOT EXISTS `archive_electric_water_meter_day_{dbDateName}` ( `device_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `point` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `start_timestamp` datetime(6) NOT NULL, @@ -471,7 +512,7 @@ namespace BackendWorkerService.Quartz.Jobs `count_rawdata` int(11) NULL DEFAULT NULL, `min_rawdata` decimal(15, 3) NULL DEFAULT NULL, `max_rawdata` decimal(15, 3) NULL DEFAULT NULL, - `kwh_result` decimal(15, 3) NULL DEFAULT NULL, + `sub_result` decimal(15, 3) NULL DEFAULT NULL, `avg_rawdata` decimal(15, 3) NULL DEFAULT NULL, `sum_rawdata` decimal(15, 3) NULL DEFAULT NULL, `is_complete` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '是否完成,0:未完成 1:完成', @@ -482,11 +523,11 @@ namespace BackendWorkerService.Quartz.Jobs PRIMARY KEY (`device_number`, `point`, `start_timestamp`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC; - UPDATE archive_water_meter_day_{dbDateName} SET + UPDATE archive_electric_water_meter_day_{dbDateName} SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, + sub_result = @max_rawdata - @min_rawdata, avg_rawdata = @avg_rawdata, sum_rawdata = @sum_rawdata, is_complete = @is_complete, @@ -497,8 +538,8 @@ namespace BackendWorkerService.Quartz.Jobs AND point = @point AND start_timestamp = @start_timestamp; - - INSERT INTO archive_water_meter_day_{dbDateName} ( + + INSERT INTO archive_electric_water_meter_day_{dbDateName} ( device_number, point, start_timestamp, @@ -506,7 +547,7 @@ namespace BackendWorkerService.Quartz.Jobs count_rawdata, min_rawdata, max_rawdata, - kwh_result, + sub_result, avg_rawdata, sum_rawdata, is_complete, @@ -526,53 +567,55 @@ namespace BackendWorkerService.Quartz.Jobs @is_complete, @repeat_times, @fail_reason - WHERE ROW_COUNT() = 0;"; + WHERE ROW_COUNT() = 0; + update device set archive_lastDate = @start_timestamp, archive_lastActionDate = @updated_at + where device_number = @device_number;"; //archive_lastDate 前次完成時間 ,archive_lastActionDate 本次作業時間 var mySql = $@"BEGIN TRANSACTION; - IF OBJECT_ID(N'dbo.archive_water_meter_day_{dbDateName}', N'U') is null + IF OBJECT_ID(N'dbo.archive_electric_water_meter_day_{dbDateName}', N'U') is null BEGIN - CREATE TABLE [dbo].[archive_water_meter_day_{dbDateName}]( - [device_number] [varchar](50) NOT NULL, - [point] [varchar](20) NOT NULL, - [start_timestamp] [datetime] NOT NULL, - [end_timestamp] [datetime] NULL, - [count_rawdata] [int] NULL, - [min_rawdata] [decimal](15, 3) NULL, - [max_rawdata] [decimal](15, 3) NULL, - [kwh_result] [decimal](15, 3) NULL, - [avg_rawdata] [decimal](15, 3) NULL, - [sum_rawdata] [decimal](15, 3) NULL, - [is_complete] [tinyint] NULL, - [repeat_times] [int] NULL, - [fail_reason] [nvarchar](max) NULL, - [created_at] [datetime] NULL, - [updated_at] [datetime] NULL, - CONSTRAINT [PK_archive_water_meter_day_{dbDateName}] PRIMARY KEY CLUSTERED + CREATE TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}]( + [device_number] [varchar](50) NOT NULL, + [point] [varchar](20) NOT NULL, + [start_timestamp] [datetime] NOT NULL, + [end_timestamp] [datetime] NULL, + [count_rawdata] [int] NULL, + [min_rawdata] [decimal](15, 3) NULL, + [max_rawdata] [decimal](15, 3) NULL, + [sub_result] [decimal](15, 3) NULL, + [avg_rawdata] [decimal](15, 3) NULL, + [sum_rawdata] [decimal](15, 3) NULL, + [is_complete] [tinyint] NULL, + [repeat_times] [int] NULL, + [fail_reason] [nvarchar](max) NULL, + [created_at] [datetime] NULL, + [updated_at] [datetime] NULL, + CONSTRAINT [PK_archive_electric_water_meter_day_{dbDateName}] PRIMARY KEY CLUSTERED ( - [device_number] ASC, - [point] ASC, - [start_timestamp] ASC + [device_number] ASC, + [point] ASC, + [start_timestamp] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] - ALTER TABLE [dbo].[archive_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_water_meter_day_{dbDateName}_repeat_times] DEFAULT ((0)) FOR [repeat_times] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_repeat_times] DEFAULT ((0)) FOR [repeat_times] - ALTER TABLE [dbo].[archive_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_water_meter_day_{dbDateName}_created_at] DEFAULT (getdate()) FOR [created_at] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_created_at] DEFAULT (getdate()) FOR [created_at] - ALTER TABLE [dbo].[archive_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_water_meter_day_{dbDateName}_updated_at] DEFAULT (NULL) FOR [updated_at] + ALTER TABLE [dbo].[archive_electric_water_meter_day_{dbDateName}] ADD CONSTRAINT [DF_archive_electric_water_meter_day_{dbDateName}_updated_at] DEFAULT (NULL) FOR [updated_at] - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否完成,0:未完成 1:完成' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'is_complete' + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否完成,0:未完成 1:完成' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'is_complete' - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'重複次數' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'repeat_times' + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'重複次數' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'repeat_times' - EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'失敗原因' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' + EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'失敗原因' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_day_{dbDateName}', @level2type=N'COLUMN',@level2name=N'fail_reason' END - UPDATE archive_water_meter_day_{dbDateName} SET + UPDATE archive_electric_water_meter_day_{dbDateName} SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, + sub_result = @max_rawdata - @min_rawdata, avg_rawdata = @avg_rawdata, sum_rawdata = @sum_rawdata, is_complete = @is_complete, @@ -585,7 +628,7 @@ namespace BackendWorkerService.Quartz.Jobs IF @@ROWCOUNT = 0 BEGIN - INSERT INTO archive_water_meter_day_{dbDateName} ( + INSERT INTO archive_electric_water_meter_day_{dbDateName} ( device_number, point, start_timestamp, @@ -593,7 +636,7 @@ namespace BackendWorkerService.Quartz.Jobs count_rawdata, min_rawdata, max_rawdata, - kwh_result, + sub_result, avg_rawdata, sum_rawdata, is_complete, @@ -643,402 +686,402 @@ namespace BackendWorkerService.Quartz.Jobs #endregion 天歸檔 #region 週歸檔 - if (await task_Detail.GetNeedWorkTask("ArchiveElectricMeterDayJob", "Week")) - { - try - { - await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Week", "水電表周任務開始"); - int week = Convert.ToInt32(now.DayOfWeek); - week = week == 0 ? 7 : week; + //if (await task_Detail.GetNeedWorkTask("ArchiveElectricMeterDayJob", "Week")) + //{ + // try + // { + // await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Week", "水電表周任務開始"); + // int week = Convert.ToInt32(now.DayOfWeek); + // week = week == 0 ? 7 : week; - var startTimestamp = string.Format("{0}T00:00:00.000+08:00", now.AddDays(1 - week).ToString("yyyy-MM-dd")); - var endTimestamp = string.Format("{0}T23:59:59.000+08:00", now.AddDays(7 - week).ToString("yyyy-MM-dd")); + // var startTimestamp = string.Format("{0}T00:00:00.000+08:00", now.AddDays(1 - week).ToString("yyyy-MM-dd")); + // var endTimestamp = string.Format("{0}T23:59:59.000+08:00", now.AddDays(7 - week).ToString("yyyy-MM-dd")); - var historyQueryFilter = $@" - - - - "; + // var historyQueryFilter = $@" + // + // + // + // "; - //Stopwatch stopWatch = new Stopwatch(); - //stopWatch.Start(); + // //Stopwatch stopWatch = new Stopwatch(); + // //stopWatch.Start(); - //抓取每個設備的資料 - List> electricArchiveWeekRawDatas = new List>(); - List> waterArchiveWeekRawDatas = new List>(); - foreach (var deviceNumberPoint in electricDeviceNumberPoints) - { - if (tagQuantity.Equals("5")) - station = await backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history - where device_building_tag = '{device_number.Split("_")[0]}' and device_floor_tag = '{device_number.Split("_")[2]}' - and device_name_tag = '{device_number.Split("_")[1]}' and device_point_name = '{device_number.Split("_")[5]}'"); - else - station = backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history - where device_building_tag = '{device_number.Split("_")[1].Replace("$3", "")}' and - device_system_tag = '{device_number.Split("_")[2]}' and device_name_tag = '{device_number.Split("_")[3]}' - and device_floor_tag = '{device_number.Split("_")[4]}' and device_master_tag = '{device_number.Split("_")[5]}' - and device_last_name_tag = '{device_number.Split("_")[6]}' and device_serial_tag = '{device_number.Split("_")[7]}' - and device_point_name = '{device_number.Split("_")[8]}'").Result; - device_number = deviceNumberPoint.FullDeviceNumberPoint; - archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); - //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveRequest.Method = "POST"; - archiveRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveRequest.PreAuthenticate = true; + // //抓取每個設備的資料 + // List> electricArchiveWeekRawDatas = new List>(); + // List> waterArchiveWeekRawDatas = new List>(); + // foreach (var deviceNumberPoint in electricDeviceNumberPoints) + // { + // if (tagQuantity.Equals("5")) + // station = await backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history + // where device_building_tag = '{device_number.Split("_")[0]}' and device_floor_tag = '{device_number.Split("_")[2]}' + // and device_name_tag = '{device_number.Split("_")[1]}' and device_point_name = '{device_number.Split("_")[5]}'"); + // else + // station = backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history + // where device_building_tag = '{device_number.Split("_")[1].Replace("$3", "")}' and + // device_system_tag = '{device_number.Split("_")[2]}' and device_name_tag = '{device_number.Split("_")[3]}' + // and device_floor_tag = '{device_number.Split("_")[4]}' and device_master_tag = '{device_number.Split("_")[5]}' + // and device_last_name_tag = '{device_number.Split("_")[6]}' and device_serial_tag = '{device_number.Split("_")[7]}' + // and device_point_name = '{device_number.Split("_")[8]}'").Result; + // device_number = deviceNumberPoint.FullDeviceNumberPoint; + // archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); + // //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~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); - } + // byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + // using (Stream reqStream = archiveRequest.GetRequestStream()) + // { + // reqStream.Write(byteArray, 0, byteArray.Length); + // } - archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); - archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); - archiveResponse.Dispose(); - archiveResponse.Close(); + // archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + // archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + // archiveResponse.Dispose(); + // archiveResponse.Close(); - xmlDocument.LoadXml(archiveResponseContent); - archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); - archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); + // xmlDocument.LoadXml(archiveResponseContent); + // archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + // archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 - { - //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】"); - //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveWeekJsonResult); + // if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 + // { + // //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】"); + // //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveWeekJsonResult); - Dictionary archiveWeekRawData = new Dictionary(); - archiveWeekRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); - archiveWeekRawData.Add("@point", deviceNumberPoint.Point); - archiveWeekRawData.Add("@start_timestamp", startTimestamp.Replace("T", " ").Substring(0, 19)); - archiveWeekRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); - archiveWeekRawData.Add("@is_complete", 0); - archiveWeekRawData.Add("@repeat_times", 0); - archiveWeekRawData.Add("@fail_reason", archiveJson); + // Dictionary archiveWeekRawData = new Dictionary(); + // archiveWeekRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); + // archiveWeekRawData.Add("@point", deviceNumberPoint.Point); + // archiveWeekRawData.Add("@start_timestamp", startTimestamp.Replace("T", " ").Substring(0, 19)); + // archiveWeekRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); + // archiveWeekRawData.Add("@is_complete", 0); + // archiveWeekRawData.Add("@repeat_times", 0); + // archiveWeekRawData.Add("@fail_reason", archiveJson); - archiveWeekRawData.Add("@count_rawdata", 0); - archiveWeekRawData.Add("@min_rawdata", 0); - archiveWeekRawData.Add("@max_rawdata", 0); - archiveWeekRawData.Add("@avg_rawdata", 0); - archiveWeekRawData.Add("@sum_rawdata", 0); - archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + // archiveWeekRawData.Add("@count_rawdata", 0); + // archiveWeekRawData.Add("@min_rawdata", 0); + // archiveWeekRawData.Add("@max_rawdata", 0); + // archiveWeekRawData.Add("@avg_rawdata", 0); + // archiveWeekRawData.Add("@sum_rawdata", 0); + // archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - electricArchiveWeekRawDatas.Add(archiveWeekRawData); - } + // electricArchiveWeekRawDatas.Add(archiveWeekRawData); + // } - if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 - { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); - if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) - { - electricArchiveWeekRawDatas.AddRange(ArrangeRawDatas); - } - } - } - foreach (var deviceNumberPoint in waterDeviceNumberPoints) - { - device_number = deviceNumberPoint.FullDeviceNumberPoint; - if (tagQuantity.Equals("5")) - station = await backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history - where device_building_tag = '{device_number.Split("_")[0]}' and device_floor_tag = '{device_number.Split("_")[2]}' - and device_name_tag = '{device_number.Split("_")[1]}' and device_point_name = '{device_number.Split("_")[5]}'"); - else - station = backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history - where device_building_tag = '{device_number.Split("_")[1].Replace("$3", "")}' and - device_system_tag = '{device_number.Split("_")[2]}' and device_name_tag = '{device_number.Split("_")[3]}' - and device_floor_tag = '{device_number.Split("_")[4]}' and device_master_tag = '{device_number.Split("_")[5]}' - and device_last_name_tag = '{device_number.Split("_")[6]}' and device_serial_tag = '{device_number.Split("_")[7]}' - and device_point_name = '{device_number.Split("_")[8]}'").Result; - archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); - //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); - archiveRequest.Method = "POST"; - archiveRequest.Headers.Add("Authorization", "Basic " + encoded); - archiveRequest.PreAuthenticate = true; + // if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + // { + // var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); + // if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + // { + // electricArchiveWeekRawDatas.AddRange(ArrangeRawDatas); + // } + // } + // } + // foreach (var deviceNumberPoint in waterDeviceNumberPoints) + // { + // device_number = deviceNumberPoint.FullDeviceNumberPoint; + // if (tagQuantity.Equals("5")) + // station = await backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history + // where device_building_tag = '{device_number.Split("_")[0]}' and device_floor_tag = '{device_number.Split("_")[2]}' + // and device_name_tag = '{device_number.Split("_")[1]}' and device_point_name = '{device_number.Split("_")[5]}'"); + // else + // station = backgroundServiceRepository.GetOneAsync($@"select parent_path from import_niagara_item_history + // where device_building_tag = '{device_number.Split("_")[1].Replace("$3", "")}' and + // device_system_tag = '{device_number.Split("_")[2]}' and device_name_tag = '{device_number.Split("_")[3]}' + // and device_floor_tag = '{device_number.Split("_")[4]}' and device_master_tag = '{device_number.Split("_")[5]}' + // and device_last_name_tag = '{device_number.Split("_")[6]}' and device_serial_tag = '{device_number.Split("_")[7]}' + // and device_point_name = '{device_number.Split("_")[8]}'").Result; + // archiveRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/{deviceNumberPoint.FullDeviceNumberPoint.Replace("$3", "")}/~historyRollup/"); + // //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/{station}/H_E1_B1F_MVCB_MVCBH_V1/~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); - } + // byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + // using (Stream reqStream = archiveRequest.GetRequestStream()) + // { + // reqStream.Write(byteArray, 0, byteArray.Length); + // } - archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); - archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); - archiveResponse.Dispose(); - archiveResponse.Close(); + // archiveResponse = (HttpWebResponse)archiveRequest.GetResponse(); + // archiveResponseContent = new StreamReader(archiveResponse.GetResponseStream()).ReadToEnd(); + // archiveResponse.Dispose(); + // archiveResponse.Close(); - xmlDocument.LoadXml(archiveResponseContent); - archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); - archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); + // xmlDocument.LoadXml(archiveResponseContent); + // archiveJson = JsonConvert.SerializeXmlNode(xmlDocument); + // archiveJsonResult = (JObject)JsonConvert.DeserializeObject(archiveJson); - if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 - { - //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】"); - //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveWeekJsonResult); + // if (archiveJsonResult.ContainsKey("err")) //抓取錯誤 + // { + // //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】"); + // //logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveWeekJsonResult); - Dictionary archiveWeekRawData = new Dictionary(); - archiveWeekRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); - archiveWeekRawData.Add("@point", deviceNumberPoint.Point); - archiveWeekRawData.Add("@start_timestamp", startTimestamp.Replace("T", " ").Substring(0, 19)); - archiveWeekRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); - archiveWeekRawData.Add("@is_complete", 0); - archiveWeekRawData.Add("@repeat_times", 0); - archiveWeekRawData.Add("@fail_reason", archiveJson); + // Dictionary archiveWeekRawData = new Dictionary(); + // archiveWeekRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); + // archiveWeekRawData.Add("@point", deviceNumberPoint.Point); + // archiveWeekRawData.Add("@start_timestamp", startTimestamp.Replace("T", " ").Substring(0, 19)); + // archiveWeekRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); + // archiveWeekRawData.Add("@is_complete", 0); + // archiveWeekRawData.Add("@repeat_times", 0); + // archiveWeekRawData.Add("@fail_reason", archiveJson); - archiveWeekRawData.Add("@count_rawdata", 0); - archiveWeekRawData.Add("@min_rawdata", 0); - archiveWeekRawData.Add("@max_rawdata", 0); - archiveWeekRawData.Add("@avg_rawdata", 0); - archiveWeekRawData.Add("@sum_rawdata", 0); - archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + // archiveWeekRawData.Add("@count_rawdata", 0); + // archiveWeekRawData.Add("@min_rawdata", 0); + // archiveWeekRawData.Add("@max_rawdata", 0); + // archiveWeekRawData.Add("@avg_rawdata", 0); + // archiveWeekRawData.Add("@sum_rawdata", 0); + // archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - waterArchiveWeekRawDatas.Add(archiveWeekRawData); - } + // waterArchiveWeekRawDatas.Add(archiveWeekRawData); + // } - if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 - { - var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); - if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) - { - waterArchiveWeekRawDatas.AddRange(ArrangeRawDatas); - } - } - } + // if (archiveJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + // { + // var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveJsonResult); + // if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + // { + // waterArchiveWeekRawDatas.AddRange(ArrangeRawDatas); + // } + // } + // } - //stopWatch.Stop(); - //logger.LogInformation("【ArchiveElectricMeterDayJob】【週歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); + // //stopWatch.Stop(); + // //logger.LogInformation("【ArchiveElectricMeterDayJob】【週歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); - if (electricArchiveWeekRawDatas.Count() > 0) - { - var sql = $@" + // if (electricArchiveWeekRawDatas.Count() > 0) + // { + // var sql = $@" - UPDATE archive_electric_meter_week SET - count_rawdata = @count_rawdata, - min_rawdata = @min_rawdata, - max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, - avg_rawdata = @avg_rawdata, - sum_rawdata = @sum_rawdata, - is_complete = @is_complete, - repeat_times = @repeat_times, - fail_reason = @fail_reason, - updated_at = @updated_at - WHERE device_number = @device_number - AND point = @point - AND start_timestamp = @start_timestamp; + // UPDATE archive_electric_water_meter_week SET + // count_rawdata = @count_rawdata, + // min_rawdata = @min_rawdata, + // max_rawdata = @max_rawdata, + // sub_result = @max_rawdata - @min_rawdata, + // avg_rawdata = @avg_rawdata, + // sum_rawdata = @sum_rawdata, + // is_complete = @is_complete, + // repeat_times = @repeat_times, + // fail_reason = @fail_reason, + // updated_at = @updated_at + // WHERE device_number = @device_number + // AND point = @point + // AND start_timestamp = @start_timestamp; - INSERT INTO archive_electric_meter_week ( - device_number, - point, - start_timestamp, - end_timestamp, - count_rawdata, - min_rawdata, - max_rawdata, - kwh_result, - avg_rawdata, - sum_rawdata, - is_complete, - repeat_times, - fail_reason) - SELECT - @device_number, - @point, - @start_timestamp, - @end_timestamp, - @count_rawdata, - @min_rawdata, - @max_rawdata, - @max_rawdata - @min_rawdata, - @avg_rawdata, - @sum_rawdata, - @is_complete, - @repeat_times, - @fail_reason - WHERE ROW_COUNT() = 0; - "; + // INSERT INTO archive_electric_water_meter_week ( + // device_number, + // point, + // start_timestamp, + // end_timestamp, + // count_rawdata, + // min_rawdata, + // max_rawdata, + // sub_result, + // avg_rawdata, + // sum_rawdata, + // is_complete, + // repeat_times, + // fail_reason) + // SELECT + // @device_number, + // @point, + // @start_timestamp, + // @end_timestamp, + // @count_rawdata, + // @min_rawdata, + // @max_rawdata, + // @max_rawdata - @min_rawdata, + // @avg_rawdata, + // @sum_rawdata, + // @is_complete, + // @repeat_times, + // @fail_reason + // WHERE ROW_COUNT() = 0; + // "; - var mySql = $@"BEGIN TRANSACTION; + // var mySql = $@"BEGIN TRANSACTION; - UPDATE archive_electric_meter_week SET - count_rawdata = @count_rawdata, - min_rawdata = @min_rawdata, - max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, - avg_rawdata = @avg_rawdata, - sum_rawdata = @sum_rawdata, - is_complete = @is_complete, - repeat_times = @repeat_times, - fail_reason = @fail_reason, - updated_at = @updated_at - WHERE device_number = @device_number - AND point = @point - AND start_timestamp = @start_timestamp; + // UPDATE archive_electric_water_meter_week SET + // count_rawdata = @count_rawdata, + // min_rawdata = @min_rawdata, + // max_rawdata = @max_rawdata, + // sub_result = @max_rawdata - @min_rawdata, + // avg_rawdata = @avg_rawdata, + // sum_rawdata = @sum_rawdata, + // is_complete = @is_complete, + // repeat_times = @repeat_times, + // fail_reason = @fail_reason, + // updated_at = @updated_at + // WHERE device_number = @device_number + // AND point = @point + // AND start_timestamp = @start_timestamp; - IF @@ROWCOUNT = 0 - BEGIN - INSERT INTO archive_electric_meter_week ( - device_number, - point, - start_timestamp, - end_timestamp, - count_rawdata, - min_rawdata, - max_rawdata, - kwh_result, - avg_rawdata, - sum_rawdata, - is_complete, - repeat_times, - fail_reason) - VALUES ( - @device_number, - @point, - @start_timestamp, - @end_timestamp, - @count_rawdata, - @min_rawdata, - @max_rawdata, - @max_rawdata - @min_rawdata, - @avg_rawdata, - @sum_rawdata, - @is_complete, - @repeat_times, - @fail_reason) - END + // IF @@ROWCOUNT = 0 + // BEGIN + // INSERT INTO archive_electric_water_meter_week ( + // device_number, + // point, + // start_timestamp, + // end_timestamp, + // count_rawdata, + // min_rawdata, + // max_rawdata, + // sub_result, + // avg_rawdata, + // sum_rawdata, + // is_complete, + // repeat_times, + // fail_reason) + // VALUES ( + // @device_number, + // @point, + // @start_timestamp, + // @end_timestamp, + // @count_rawdata, + // @min_rawdata, + // @max_rawdata, + // @max_rawdata - @min_rawdata, + // @avg_rawdata, + // @sum_rawdata, + // @is_complete, + // @repeat_times, + // @fail_reason) + // END - COMMIT TRANSACTION;"; - await backgroundServiceRepository.ExecuteSql(sql, electricArchiveWeekRawDatas); - if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") - { - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveWeekRawDatas); - } + // COMMIT TRANSACTION;"; + // await backgroundServiceRepository.ExecuteSql(sql, electricArchiveWeekRawDatas); + // if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + // { + // await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveWeekRawDatas); + // } - } - if (waterArchiveWeekRawDatas.Count() > 0) - { - var sql = $@" + // } + // if (waterArchiveWeekRawDatas.Count() > 0) + // { + // var sql = $@" - UPDATE archive_water_meter_week SET - count_rawdata = @count_rawdata, - min_rawdata = @min_rawdata, - max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, - avg_rawdata = @avg_rawdata, - sum_rawdata = @sum_rawdata, - is_complete = @is_complete, - repeat_times = @repeat_times, - fail_reason = @fail_reason, - updated_at = @updated_at - WHERE device_number = @device_number - AND point = @point - AND start_timestamp = @start_timestamp; + // UPDATE archive_electric_water_meter_week SET + // count_rawdata = @count_rawdata, + // min_rawdata = @min_rawdata, + // max_rawdata = @max_rawdata, + // sub_result = @max_rawdata - @min_rawdata, + // avg_rawdata = @avg_rawdata, + // sum_rawdata = @sum_rawdata, + // is_complete = @is_complete, + // repeat_times = @repeat_times, + // fail_reason = @fail_reason, + // updated_at = @updated_at + // WHERE device_number = @device_number + // AND point = @point + // AND start_timestamp = @start_timestamp; - INSERT INTO archive_water_meter_week ( - device_number, - point, - start_timestamp, - end_timestamp, - count_rawdata, - min_rawdata, - max_rawdata, - kwh_result, - avg_rawdata, - sum_rawdata, - is_complete, - repeat_times, - fail_reason) - SELECT - @device_number, - @point, - @start_timestamp, - @end_timestamp, - @count_rawdata, - @min_rawdata, - @max_rawdata, - @max_rawdata - @min_rawdata, - @avg_rawdata, - @sum_rawdata, - @is_complete, - @repeat_times, - @fail_reason - WHERE ROW_COUNT() = 0; - "; + // INSERT INTO archive_electric_water_meter_week ( + // device_number, + // point, + // start_timestamp, + // end_timestamp, + // count_rawdata, + // min_rawdata, + // max_rawdata, + // sub_result, + // avg_rawdata, + // sum_rawdata, + // is_complete, + // repeat_times, + // fail_reason) + // SELECT + // @device_number, + // @point, + // @start_timestamp, + // @end_timestamp, + // @count_rawdata, + // @min_rawdata, + // @max_rawdata, + // @max_rawdata - @min_rawdata, + // @avg_rawdata, + // @sum_rawdata, + // @is_complete, + // @repeat_times, + // @fail_reason + // WHERE ROW_COUNT() = 0; + // "; - var mySql = $@"BEGIN TRANSACTION; + // var mySql = $@"BEGIN TRANSACTION; - UPDATE archive_water_meter_week SET - count_rawdata = @count_rawdata, - min_rawdata = @min_rawdata, - max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, - avg_rawdata = @avg_rawdata, - sum_rawdata = @sum_rawdata, - is_complete = @is_complete, - repeat_times = @repeat_times, - fail_reason = @fail_reason, - updated_at = @updated_at - WHERE device_number = @device_number - AND point = @point - AND start_timestamp = @start_timestamp; + // UPDATE archive_electric_water_meter_week SET + // count_rawdata = @count_rawdata, + // min_rawdata = @min_rawdata, + // max_rawdata = @max_rawdata, + // sub_result = @max_rawdata - @min_rawdata, + // avg_rawdata = @avg_rawdata, + // sum_rawdata = @sum_rawdata, + // is_complete = @is_complete, + // repeat_times = @repeat_times, + // fail_reason = @fail_reason, + // updated_at = @updated_at + // WHERE device_number = @device_number + // AND point = @point + // AND start_timestamp = @start_timestamp; - IF @@ROWCOUNT = 0 - BEGIN - INSERT INTO archive_water_meter_week ( - device_number, - point, - start_timestamp, - end_timestamp, - count_rawdata, - min_rawdata, - max_rawdata, - kwh_result, - avg_rawdata, - sum_rawdata, - is_complete, - repeat_times, - fail_reason) - VALUES ( - @device_number, - @point, - @start_timestamp, - @end_timestamp, - @count_rawdata, - @min_rawdata, - @max_rawdata, - @max_rawdata - @min_rawdata, - @avg_rawdata, - @sum_rawdata, - @is_complete, - @repeat_times, - @fail_reason) - END + // IF @@ROWCOUNT = 0 + // BEGIN + // INSERT INTO archive_electric_water_meter_week ( + // device_number, + // point, + // start_timestamp, + // end_timestamp, + // count_rawdata, + // min_rawdata, + // max_rawdata, + // sub_result, + // avg_rawdata, + // sum_rawdata, + // is_complete, + // repeat_times, + // fail_reason) + // VALUES ( + // @device_number, + // @point, + // @start_timestamp, + // @end_timestamp, + // @count_rawdata, + // @min_rawdata, + // @max_rawdata, + // @max_rawdata - @min_rawdata, + // @avg_rawdata, + // @sum_rawdata, + // @is_complete, + // @repeat_times, + // @fail_reason) + // END - COMMIT TRANSACTION;"; - await backgroundServiceRepository.ExecuteSql(sql, waterArchiveWeekRawDatas); - if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") - { - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveWeekRawDatas); - } - } - await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Week", "任務完成"); - } - catch (Exception exception) - { - await task_Detail.WorkFail("ArchiveElectricMeterDayJob", "Week", exception.ToString()); - logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【任務失敗】"); - logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【任務失敗】[Exception]:{0}", exception.ToString()); - } - finally - { - if (archiveResponse != null) - { - archiveResponse.Dispose(); - archiveResponse.Close(); - } - } - } + // COMMIT TRANSACTION;"; + // await backgroundServiceRepository.ExecuteSql(sql, waterArchiveWeekRawDatas); + // if (!string.IsNullOrEmpty(saveToMSDB) && saveToMSDB == "1") + // { + // await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveWeekRawDatas); + // } + // } + // await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Week", "任務完成"); + // } + // catch (Exception exception) + // { + // await task_Detail.WorkFail("ArchiveElectricMeterDayJob", "Week", exception.ToString()); + // logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【任務失敗】"); + // logger.LogError("【ArchiveElectricMeterDayJob】【週歸檔】【任務失敗】[Exception]:{0}", exception.ToString()); + // } + // finally + // { + // if (archiveResponse != null) + // { + // archiveResponse.Dispose(); + // archiveResponse.Close(); + // } + // } + //} #endregion 週歸檔 #region 月歸檔 @@ -1048,12 +1091,16 @@ namespace BackendWorkerService.Quartz.Jobs { await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Month", "水電表月任務開始"); var FirstDay = now.AddDays(-now.Day + 1); - var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day); + //var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day); + var LastDay = FirstDay.AddMonths(1); + var dayInMonth = DateTime.DaysInMonth(now.Year, now.Month); var startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.ToString("yyyy-MM-dd")); - var endTimestamp = string.Format("{0}T23:59:59.000+08:00", LastDay.ToString("yyyy-MM-dd")); + //var endTimestamp = string.Format("{0}T23:59:59.000+08:00", LastDay.ToString("yyyy-MM-dd")); + var endTimestamp = string.Format("{0}T00:15:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome + var historyQueryFilter = $@" @@ -1211,11 +1258,11 @@ namespace BackendWorkerService.Quartz.Jobs if (electricArchiveMonthRawDatas.Count() > 0) { var sql = $@" - UPDATE archive_electric_meter_month SET + UPDATE archive_electric_water_meter_month SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, + sub_result = @max_rawdata - @min_rawdata, avg_rawdata = @avg_rawdata, sum_rawdata = @sum_rawdata, is_complete = @is_complete, @@ -1226,7 +1273,7 @@ namespace BackendWorkerService.Quartz.Jobs AND point = @point AND start_timestamp = @start_timestamp; - INSERT INTO archive_electric_meter_month ( + INSERT INTO archive_electric_water_meter_month ( device_number, point, start_timestamp, @@ -1234,7 +1281,7 @@ namespace BackendWorkerService.Quartz.Jobs count_rawdata, min_rawdata, max_rawdata, - kwh_result, + sub_result, avg_rawdata, sum_rawdata, is_complete, @@ -1258,11 +1305,11 @@ namespace BackendWorkerService.Quartz.Jobs var mySql = $@"BEGIN TRANSACTION; - UPDATE archive_electric_meter_month SET + UPDATE archive_electric_water_meter_month SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, + sub_result = @max_rawdata - @min_rawdata, avg_rawdata = @avg_rawdata, sum_rawdata = @sum_rawdata, is_complete = @is_complete, @@ -1275,7 +1322,7 @@ namespace BackendWorkerService.Quartz.Jobs IF @@ROWCOUNT = 0 BEGIN - INSERT INTO archive_electric_meter_month ( + INSERT INTO archive_electric_water_meter_month ( device_number, point, start_timestamp, @@ -1283,7 +1330,7 @@ namespace BackendWorkerService.Quartz.Jobs count_rawdata, min_rawdata, max_rawdata, - kwh_result, + sub_result, avg_rawdata, sum_rawdata, is_complete, @@ -1315,11 +1362,11 @@ namespace BackendWorkerService.Quartz.Jobs if (waterArchiveMonthRawDatas.Count() > 0) { var sql = $@" - UPDATE archive_water_meter_month SET + UPDATE archive_electric_water_meter_month SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, + sub_result = @max_rawdata - @min_rawdata, avg_rawdata = @avg_rawdata, sum_rawdata = @sum_rawdata, is_complete = @is_complete, @@ -1330,7 +1377,7 @@ namespace BackendWorkerService.Quartz.Jobs AND point = @point AND start_timestamp = @start_timestamp; - INSERT INTO archive_water_meter_month ( + INSERT INTO archive_electric_water_meter_month ( device_number, point, start_timestamp, @@ -1338,7 +1385,7 @@ namespace BackendWorkerService.Quartz.Jobs count_rawdata, min_rawdata, max_rawdata, - kwh_result, + sub_result, avg_rawdata, sum_rawdata, is_complete, @@ -1362,11 +1409,11 @@ namespace BackendWorkerService.Quartz.Jobs var mySql = $@"BEGIN TRANSACTION; - UPDATE archive_water_meter_month SET + UPDATE archive_electric_water_meter_month SET count_rawdata = @count_rawdata, min_rawdata = @min_rawdata, max_rawdata = @max_rawdata, - kwh_result = @max_rawdata - @min_rawdata, + sub_result = @max_rawdata - @min_rawdata, avg_rawdata = @avg_rawdata, sum_rawdata = @sum_rawdata, is_complete = @is_complete, @@ -1379,7 +1426,7 @@ namespace BackendWorkerService.Quartz.Jobs IF @@ROWCOUNT = 0 BEGIN - INSERT INTO archive_water_meter_month ( + INSERT INTO archive_electric_water_meter_month ( device_number, point, start_timestamp, @@ -1387,7 +1434,7 @@ namespace BackendWorkerService.Quartz.Jobs count_rawdata, min_rawdata, max_rawdata, - kwh_result, + sub_result, avg_rawdata, sum_rawdata, is_complete, @@ -1436,25 +1483,26 @@ namespace BackendWorkerService.Quartz.Jobs #endregion 月歸檔 #region 補償機制 - //取得連線字串 - if (await task_Detail.GetNeedWorkTask("ArchiveElectricMeterDayJob", "Compensate")) - { - try - { - await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Compensate", "補償機制任務開始"); - ProcEletricMeterService procEletricMeterService = new ProcEletricMeterService(backgroundServiceRepository, backgroundServiceMsSqlRepository); - await procEletricMeterService.ArchiveData(); - await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Compensate", "任務完成"); - } - catch(Exception ex) - { - await task_Detail.WorkFail("ArchiveElectricMeterDayJob", "Compensate", ex.ToString()); - } - } + ////取得連線字串 + //if (await task_Detail.GetNeedWorkTask("ArchiveElectricMeterDayJob", "Compensate")) + //{ + // try + // { + // await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Compensate", "補償機制任務開始"); + // ProcEletricMeterService procEletricMeterService = new ProcEletricMeterService(backgroundServiceRepository, backgroundServiceMsSqlRepository); + // await procEletricMeterService.ArchiveData(); + // await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Compensate", "任務完成"); + // } + // catch (Exception ex) + // { + // await task_Detail.WorkFail("ArchiveElectricMeterDayJob", "Compensate", ex.ToString()); + // } + //} #endregion 補償機制 - await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "All","任務完成"); + await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "All", "任務完成"); } + } catch (Exception exception) { @@ -1464,6 +1512,14 @@ namespace BackendWorkerService.Quartz.Jobs } } + + public IEnumerable EachDay(string from, string thru) + { + var strtday = DateTime.Parse(from).AddDays(-1); //每次重做 2天 + var endday = DateTime.Parse(thru); + for (var day = strtday.Date; day.Date <= endday.Date; day = day.AddDays(1)) + yield return day; + } private List> ArrangeRawData(DeviceNumberPoint deviceNumberPoint, JObject jsonResult) { List> arrangeRawDatas = new List>();