From 05383488660d34ebf4530a7fa4a2f72a7b43ad44 Mon Sep 17 00:00:00 2001 From: dev02 Date: Wed, 10 May 2023 18:06:02 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=8E=92=E6=88=90=20=E5=A2=9E=E5=8A=A0=E7=8D=B2=E5=8F=96?= =?UTF-8?q?=E6=B0=B4=E8=A1=A8=E8=B3=87=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Quartz/Jobs/ArchiveElectricMeterDayJob.cs | 530 +++++++++++++++++- .../Implement/ProcEletricMeterService.cs | 303 +++++++++- BackendWorkerService/root/PowerfulRain.xml | 14 +- 3 files changed, 779 insertions(+), 68 deletions(-) diff --git a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs index 52f2bf5..3e45e0b 100644 --- a/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs +++ b/BackendWorkerService/Quartz/Jobs/ArchiveElectricMeterDayJob.cs @@ -59,29 +59,47 @@ 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(); #region 找出所有電錶設備 var sWhere = "deleted = 0 AND device_name_tag = @sub_system_guid"; var electricMeters = await backgroundServiceRepository.GetAllAsync("device", sWhere, new { sub_system_guid = electricMeterGuid }); + var waterMeters = await backgroundServiceRepository.GetAllAsync("device", sWhere, new { sub_system_guid = waterMeterGuid }); #endregion 找出所有電錶設備 #region 找出所有電錶系統的點位 var sPointWhere = "deleted = 0 AND device_name_tag = @sub_system_guid"; - var points = await backgroundServiceRepository.GetAllAsync("device_item", sPointWhere, new { sub_system_guid = electricMeterGuid }); + 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 找出所有電錶系統的點位 #region 組合出所有電錶設備點位 - List deviceNumberPoints = new List(); + List electricDeviceNumberPoints = new List(); foreach (var electricMeter in electricMeters) { - foreach (var point in points) + foreach (var point in electricPoints) { DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); deviceNumberPoint.DeviceNumber = electricMeter.Device_number; deviceNumberPoint.Point = point.points; deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", electricMeter.Device_number, point.points); - deviceNumberPoints.Add(deviceNumberPoint); + electricDeviceNumberPoints.Add(deviceNumberPoint); + } + } + #endregion 組合出所有電錶設備點位 + #region 組合出所有水錶設備點位 + List waterDeviceNumberPoints = new List(); + foreach (var waterMeter in waterMeters) + { + foreach (var point in waterPoints) + { + DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); + deviceNumberPoint.DeviceNumber = waterMeter.Device_number; + deviceNumberPoint.Point = point.points; + deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", waterMeter.Device_number, point.points); + + waterDeviceNumberPoints.Add(deviceNumberPoint); } } #endregion 組合出所有電錶設備點位 @@ -122,8 +140,9 @@ namespace BackendWorkerService.Quartz.Jobs //stopWatch.Start(); //抓取每個設備的資料 - List> archiveDayRawDatas = new List>(); - foreach (var deviceNumberPoint in deviceNumberPoints) + List> electericArchiveDayRawDatas = new List>(); + List> waterArchiveDayRawDatas = new List>(); + foreach (var deviceNumberPoint in electricDeviceNumberPoints) { HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); @@ -166,7 +185,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveDayRawData.Add("@sum_rawdata", 0); archiveDayRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - archiveDayRawDatas.Add(archiveDayRawData); + electericArchiveDayRawDatas.Add(archiveDayRawData); } if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容 @@ -174,7 +193,62 @@ namespace BackendWorkerService.Quartz.Jobs var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { - archiveDayRawDatas.AddRange(ArrangeRawDatas); + electericArchiveDayRawDatas.AddRange(ArrangeRawDatas); + } + } + } + foreach (var deviceNumberPoint in waterDeviceNumberPoints) + { + + HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); + archiveDayRequest.Method = "POST"; + archiveDayRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveDayRequest.PreAuthenticate = true; + + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveDayRequest.GetRequestStream()) + { + 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")) //抓取錯誤 + { + //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", 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); } } } @@ -182,7 +256,7 @@ namespace BackendWorkerService.Quartz.Jobs //stopWatch.Stop(); //logger.LogInformation("【ArchiveElectricMeterDayJob】【天歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); - if (archiveDayRawDatas.Count() > 0) + if (electericArchiveDayRawDatas.Count() > 0) { var sql = $@" UPDATE archive_electric_meter_day SET @@ -275,8 +349,104 @@ namespace BackendWorkerService.Quartz.Jobs END COMMIT TRANSACTION;"; - await backgroundServiceRepository.ExecuteSql(sql, archiveDayRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, archiveDayRawDatas); + await backgroundServiceRepository.ExecuteSql(sql, electericArchiveDayRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electericArchiveDayRawDatas); + } + if (waterArchiveDayRawDatas.Count() > 0) + { + var sql = $@" + UPDATE archive_water_meter_day SET + count_rawdata = @count_rawdata, + min_rawdata = @min_rawdata, + max_rawdata = @max_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_day ( + device_number, + point, + start_timestamp, + end_timestamp, + count_rawdata, + min_rawdata, + max_rawdata, + avg_rawdata, + sum_rawdata, + is_complete, + repeat_times, + fail_reason) + SELECT + @device_number, + @point, + @start_timestamp, + @end_timestamp, + @count_rawdata, + @min_rawdata, + @max_rawdata, + @avg_rawdata, + @sum_rawdata, + @is_complete, + @repeat_times, + @fail_reason + WHERE ROW_COUNT() = 0;"; + + var mySql = $@"BEGIN TRANSACTION; + + UPDATE archive_water_meter_day SET + count_rawdata = @count_rawdata, + min_rawdata = @min_rawdata, + max_rawdata = @max_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_day ( + device_number, + point, + start_timestamp, + end_timestamp, + count_rawdata, + min_rawdata, + max_rawdata, + avg_rawdata, + sum_rawdata, + is_complete, + repeat_times, + fail_reason) + VALUES ( + @device_number, + @point, + @start_timestamp, + @end_timestamp, + @count_rawdata, + @min_rawdata, + @max_rawdata, + @avg_rawdata, + @sum_rawdata, + @is_complete, + @repeat_times, + @fail_reason) + END + + COMMIT TRANSACTION;"; + await backgroundServiceRepository.ExecuteSql(sql, waterArchiveDayRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveDayRawDatas); } await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Day", "任務完成"); } @@ -311,8 +481,9 @@ namespace BackendWorkerService.Quartz.Jobs //stopWatch.Start(); //抓取每個設備的資料 - List> archiveWeekRawDatas = new List>(); - foreach (var deviceNumberPoint in deviceNumberPoints) + List> electricArchiveWeekRawDatas = new List>(); + List> waterArchiveWeekRawDatas = new List>(); + foreach (var deviceNumberPoint in electricDeviceNumberPoints) { HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); @@ -354,7 +525,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveWeekRawData.Add("@sum_rawdata", 0); archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - archiveWeekRawDatas.Add(archiveWeekRawData); + electricArchiveWeekRawDatas.Add(archiveWeekRawData); } if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容 @@ -362,7 +533,61 @@ namespace BackendWorkerService.Quartz.Jobs var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { - archiveWeekRawDatas.AddRange(ArrangeRawDatas); + electricArchiveWeekRawDatas.AddRange(ArrangeRawDatas); + } + } + } + foreach (var deviceNumberPoint in waterDeviceNumberPoints) + { + HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); + archiveWeekRequest.Method = "POST"; + archiveWeekRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveWeekRequest.PreAuthenticate = true; + + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveWeekRequest.GetRequestStream()) + { + reqStream.Write(byteArray, 0, byteArray.Length); + } + + HttpWebResponse archiveWeekResponse = (HttpWebResponse)archiveWeekRequest.GetResponse(); + var archiveWeekResponseContent = new StreamReader(archiveWeekResponse.GetResponseStream()).ReadToEnd(); + + xmlDocument.LoadXml(archiveWeekResponseContent); + string archiveWeekJson = JsonConvert.SerializeXmlNode(xmlDocument); + JObject archiveWeekJsonResult = (JObject)JsonConvert.DeserializeObject(archiveWeekJson); + + if (archiveWeekJsonResult.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", archiveWeekJson); + + 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); + } + + if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + { + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult); + if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + { + waterArchiveWeekRawDatas.AddRange(ArrangeRawDatas); } } } @@ -370,7 +595,7 @@ namespace BackendWorkerService.Quartz.Jobs //stopWatch.Stop(); //logger.LogInformation("【ArchiveElectricMeterDayJob】【週歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); - if (archiveWeekRawDatas.Count() > 0) + if (electricArchiveWeekRawDatas.Count() > 0) { var sql = $@" @@ -465,8 +690,107 @@ namespace BackendWorkerService.Quartz.Jobs END COMMIT TRANSACTION;"; - await backgroundServiceRepository.ExecuteSql(sql, archiveWeekRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, archiveWeekRawDatas); + await backgroundServiceRepository.ExecuteSql(sql, electricArchiveWeekRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveWeekRawDatas); + + } + if (waterArchiveWeekRawDatas.Count() > 0) + { + var sql = $@" + + UPDATE archive_water_meter_week SET + count_rawdata = @count_rawdata, + min_rawdata = @min_rawdata, + max_rawdata = @max_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, + avg_rawdata, + sum_rawdata, + is_complete, + repeat_times, + fail_reason) + SELECT + @device_number, + @point, + @start_timestamp, + @end_timestamp, + @count_rawdata, + @min_rawdata, + @max_rawdata, + @avg_rawdata, + @sum_rawdata, + @is_complete, + @repeat_times, + @fail_reason + WHERE ROW_COUNT() = 0; + "; + + var mySql = $@"BEGIN TRANSACTION; + + UPDATE archive_water_meter_week SET + count_rawdata = @count_rawdata, + min_rawdata = @min_rawdata, + max_rawdata = @max_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, + avg_rawdata, + sum_rawdata, + is_complete, + repeat_times, + fail_reason) + VALUES ( + @device_number, + @point, + @start_timestamp, + @end_timestamp, + @count_rawdata, + @min_rawdata, + @max_rawdata, + @avg_rawdata, + @sum_rawdata, + @is_complete, + @repeat_times, + @fail_reason) + END + + COMMIT TRANSACTION;"; + await backgroundServiceRepository.ExecuteSql(sql, waterArchiveWeekRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveWeekRawDatas); } await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Week", "任務完成"); @@ -504,8 +828,9 @@ namespace BackendWorkerService.Quartz.Jobs //stopWatch.Start(); //抓取每個設備的資料 - List> archiveMonthRawDatas = new List>(); - foreach (var deviceNumberPoint in deviceNumberPoints) + List> electricArchiveMonthRawDatas = new List>(); + List> waterArchiveMonthRawDatas = new List>(); + foreach (var deviceNumberPoint in electricDeviceNumberPoints) { HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); @@ -547,7 +872,7 @@ namespace BackendWorkerService.Quartz.Jobs archiveMonthRawData.Add("@sum_rawdata", 0); archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - archiveMonthRawDatas.Add(archiveMonthRawData); + electricArchiveMonthRawDatas.Add(archiveMonthRawData); } if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容 @@ -555,7 +880,61 @@ namespace BackendWorkerService.Quartz.Jobs var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { - archiveMonthRawDatas.AddRange(ArrangeRawDatas); + electricArchiveMonthRawDatas.AddRange(ArrangeRawDatas); + } + } + } + foreach (var deviceNumberPoint in waterDeviceNumberPoints) + { + HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); + archiveMonthRequest.Method = "POST"; + archiveMonthRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveMonthRequest.PreAuthenticate = true; + + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveMonthRequest.GetRequestStream()) + { + reqStream.Write(byteArray, 0, byteArray.Length); + } + + HttpWebResponse archiveMonthResponse = (HttpWebResponse)archiveMonthRequest.GetResponse(); + var archiveMonthResponseContent = new StreamReader(archiveMonthResponse.GetResponseStream()).ReadToEnd(); + + xmlDocument.LoadXml(archiveMonthResponseContent); + string archiveMonthJson = JsonConvert.SerializeXmlNode(xmlDocument); + JObject archiveMonthJsonResult = (JObject)JsonConvert.DeserializeObject(archiveMonthJson); + + if (archiveMonthJsonResult.ContainsKey("err")) //抓取錯誤 + { + //logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【取得資料失敗】"); + //logger.LogError("【ArchiveElectricMeterDayJob】【月歸檔】【取得資料失敗】[錯誤內容]:{0}", archiveMonthJsonResult); + + Dictionary archiveMonthRawData = new Dictionary(); + archiveMonthRawData.Add("@device_number", deviceNumberPoint.DeviceNumber); + archiveMonthRawData.Add("@point", deviceNumberPoint.Point); + archiveMonthRawData.Add("@start_timestamp", startTimestamp.Replace("T", " ").Substring(0, 19)); + archiveMonthRawData.Add("@end_timestamp", endTimestamp.Replace("T", " ").Substring(0, 19)); + archiveMonthRawData.Add("@is_complete", 0); + archiveMonthRawData.Add("@repeat_times", 0); + archiveMonthRawData.Add("@fail_reason", archiveMonthJson); + + archiveMonthRawData.Add("@count_rawdata", 0); + archiveMonthRawData.Add("@min_rawdata", 0); + archiveMonthRawData.Add("@max_rawdata", 0); + archiveMonthRawData.Add("@avg_rawdata", 0); + archiveMonthRawData.Add("@sum_rawdata", 0); + archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + + waterArchiveMonthRawDatas.Add(archiveMonthRawData); + } + + if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + { + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult); + if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + { + waterArchiveMonthRawDatas.AddRange(ArrangeRawDatas); } } } @@ -563,7 +942,7 @@ namespace BackendWorkerService.Quartz.Jobs //stopWatch.Stop(); //logger.LogInformation("【ArchiveElectricMeterDayJob】【月歸檔效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); - if (archiveMonthRawDatas.Count() > 0) + if (electricArchiveMonthRawDatas.Count() > 0) { var sql = $@" UPDATE archive_electric_meter_month SET @@ -655,8 +1034,103 @@ namespace BackendWorkerService.Quartz.Jobs END COMMIT TRANSACTION;"; - await backgroundServiceRepository.ExecuteSql(sql, archiveMonthRawDatas); - await backgroundServiceMsSqlRepository.ExecuteSql(mySql, archiveMonthRawDatas); + await backgroundServiceRepository.ExecuteSql(sql, electricArchiveMonthRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, electricArchiveMonthRawDatas); + } + if (waterArchiveMonthRawDatas.Count() > 0) + { + var sql = $@" + UPDATE archive_water_meter_month SET + count_rawdata = @count_rawdata, + min_rawdata = @min_rawdata, + max_rawdata = @max_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_month ( + device_number, + point, + start_timestamp, + end_timestamp, + count_rawdata, + min_rawdata, + max_rawdata, + avg_rawdata, + sum_rawdata, + is_complete, + repeat_times, + fail_reason) + SELECT + @device_number, + @point, + @start_timestamp, + @end_timestamp, + @count_rawdata, + @min_rawdata, + @max_rawdata, + @avg_rawdata, + @sum_rawdata, + @is_complete, + @repeat_times, + @fail_reason + WHERE ROW_COUNT() = 0;"; + + var mySql = $@"BEGIN TRANSACTION; + + UPDATE archive_water_meter_month SET + count_rawdata = @count_rawdata, + min_rawdata = @min_rawdata, + max_rawdata = @max_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_month ( + device_number, + point, + start_timestamp, + end_timestamp, + count_rawdata, + min_rawdata, + max_rawdata, + avg_rawdata, + sum_rawdata, + is_complete, + repeat_times, + fail_reason) + VALUES ( + @device_number, + @point, + @start_timestamp, + @end_timestamp, + @count_rawdata, + @min_rawdata, + @max_rawdata, + @avg_rawdata, + @sum_rawdata, + @is_complete, + @repeat_times, + @fail_reason) + END + + COMMIT TRANSACTION;"; + await backgroundServiceRepository.ExecuteSql(sql, waterArchiveMonthRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(mySql, waterArchiveMonthRawDatas); } await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Month", "任務完成"); } @@ -686,13 +1160,7 @@ namespace BackendWorkerService.Quartz.Jobs logger.LogError("【ArchiveElectricMeterDayJob】【補償機制】【任務失敗】"); logger.LogError("【ArchiveElectricMeterDayJob】【補償機制】【任務失敗】[Exception]:{0}", ex.ToString()); } - - - } - - - #endregion 補償機制 await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "All","任務完成"); diff --git a/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs b/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs index 21c01fd..4b54c1d 100644 --- a/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs +++ b/BackendWorkerService/Services/Implement/ProcEletricMeterService.cs @@ -250,12 +250,11 @@ namespace BackendWorkerService.Services.Implement //取得所有須補償的設備資訊 targetTable = "archive_electric_meter_day"; var sql_error_day = string.Format(sql_error_format, targetTable); - var error_days = await backgroundServiceRepository.GetAllAsync(sql_error_day, new { RepeatTimes = repeatTimes }); - - List> archiveDayRawDatas = new List>(); - if (error_days.Count() > 0) + var electric_error_days = await backgroundServiceRepository.GetAllAsync(sql_error_day, new { RepeatTimes = repeatTimes }); + List> electricArchiveDayRawDatas = new List>(); + if (electric_error_days.Count() > 0) { - foreach (var error_day in error_days) + foreach (var error_day in electric_error_days) { DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); deviceNumberPoint.DeviceNumber = error_day.Device_number; @@ -308,7 +307,7 @@ namespace BackendWorkerService.Services.Implement archiveDayRawData.Add("@sum_rawdata", 0); archiveDayRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - archiveDayRawDatas.Add(archiveDayRawData); + electricArchiveDayRawDatas.Add(archiveDayRawData); } if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容 @@ -316,17 +315,98 @@ namespace BackendWorkerService.Services.Implement var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { - archiveDayRawDatas.AddRange(ArrangeRawDatas); + electricArchiveDayRawDatas.AddRange(ArrangeRawDatas); } } } - if (archiveDayRawDatas.Count() > 0) + if (electricArchiveDayRawDatas.Count() > 0) { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, archiveDayRawDatas); - await backgroundServiceRepository.ExecuteSql(Mysql_error_update, archiveDayRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveDayRawDatas); + await backgroundServiceRepository.ExecuteSql(Mysql_error_update, electricArchiveDayRawDatas); + } + } + + targetTable = "archive_water_meter_day"; + sql_error_day = string.Format(sql_error_format, targetTable); + 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/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + //HttpWebRequest archiveDayRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); + archiveDayRequest.Method = "POST"; + archiveDayRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveDayRequest.PreAuthenticate = true; + + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveDayRequest.GetRequestStream()) + { + 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); + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveDayRawDatas); + await backgroundServiceRepository.ExecuteSql(Mysql_error_update, waterArchiveDayRawDatas); } } #endregion 天歸檔補償 @@ -335,12 +415,11 @@ namespace BackendWorkerService.Services.Implement //取得所有須補償的設備資訊 targetTable = "archive_electric_meter_week"; var sql_error_week = string.Format(sql_error_format, targetTable); - var error_weeks = await backgroundServiceRepository.GetAllAsync(sql_error_week, new { RepeatTimes = repeatTimes }); - - List> archiveWeekRawDatas = new List>(); - if (error_weeks.Count() > 0) + var eletric_error_weeks = await backgroundServiceRepository.GetAllAsync(sql_error_week, new { RepeatTimes = repeatTimes }); + List> electricArchiveWeekRawDatas = new List>(); + if (eletric_error_weeks.Count() > 0) { - foreach (var error_week in error_weeks) + foreach (var error_week in eletric_error_weeks) { DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); deviceNumberPoint.DeviceNumber = error_week.Device_number; @@ -393,7 +472,7 @@ namespace BackendWorkerService.Services.Implement archiveWeekRawData.Add("@sum_rawdata", 0); archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - archiveWeekRawDatas.Add(archiveWeekRawData); + electricArchiveWeekRawDatas.Add(archiveWeekRawData); } if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容 @@ -401,17 +480,98 @@ namespace BackendWorkerService.Services.Implement var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { - archiveWeekRawDatas.AddRange(ArrangeRawDatas); + electricArchiveWeekRawDatas.AddRange(ArrangeRawDatas); } } } - if (archiveWeekRawDatas.Count() > 0) + if (electricArchiveWeekRawDatas.Count() > 0) { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, archiveWeekRawDatas); - await backgroundServiceRepository.ExecuteSql(Mysql_error_update, archiveWeekRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveWeekRawDatas); + await backgroundServiceRepository.ExecuteSql(Mysql_error_update, electricArchiveWeekRawDatas); + } + } + + targetTable = "archive_water_meter_week"; + sql_error_week = string.Format(sql_error_format, targetTable); + var water_error_weeks = await backgroundServiceRepository.GetAllAsync(sql_error_week, new { RepeatTimes = repeatTimes }); + List> waterArchiveWeekRawDatas = new List>(); + if (water_error_weeks.Count() > 0) + { + foreach (var error_week in water_error_weeks) + { + DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); + deviceNumberPoint.DeviceNumber = error_week.Device_number; + deviceNumberPoint.Point = error_week.Point; + deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", error_week.Device_number, error_week.Point); + + var startTimestamp = string.Format("{0}+08:00", error_week.Start_timestamp.Replace(" ", "T")); + var endTimestamp = string.Format("{0}+08:00", error_week.End_timestamp.Replace(" ", "T")); + + var historyQueryFilter = $@" + + + + "; + + HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + //HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); + archiveWeekRequest.Method = "POST"; + archiveWeekRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveWeekRequest.PreAuthenticate = true; + + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveWeekRequest.GetRequestStream()) + { + reqStream.Write(byteArray, 0, byteArray.Length); + } + + HttpWebResponse archiveWeekResponse = (HttpWebResponse)archiveWeekRequest.GetResponse(); + var archiveWeekResponseContent = new StreamReader(archiveWeekResponse.GetResponseStream()).ReadToEnd(); + + xmlDocument.LoadXml(archiveWeekResponseContent); + string archiveWeekJson = JsonConvert.SerializeXmlNode(xmlDocument); + JObject archiveWeekJsonResult = (JObject)JsonConvert.DeserializeObject(archiveWeekJson); + + if (archiveWeekJsonResult.ContainsKey("err")) //抓取錯誤 + { + Dictionary archiveWeekRawData = new Dictionary(); + archiveWeekRawData.Add("@device_number", error_week.Device_number); + archiveWeekRawData.Add("@point", error_week.Point); + archiveWeekRawData.Add("@start_timestamp", DateTime.Parse(error_week.Start_timestamp, System.Globalization.CultureInfo.CurrentCulture)); + archiveWeekRawData.Add("@end_timestamp", DateTime.Parse(error_week.End_timestamp, System.Globalization.CultureInfo.CurrentCulture)); + archiveWeekRawData.Add("@is_complete", 0); + archiveWeekRawData.Add("@repeat_times", ++error_week.Repeat_times); + archiveWeekRawData.Add("@fail_reason", archiveWeekJson); + + 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); + } + + if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + { + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult); + if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + { + waterArchiveWeekRawDatas.AddRange(ArrangeRawDatas); + } + } + } + + if (waterArchiveWeekRawDatas.Count() > 0) + { + var Mysql_error_update = string.Format(MYsql_update_format, targetTable); + var sql_error_update = string.Format(sql_update_format, targetTable); + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveWeekRawDatas); + await backgroundServiceRepository.ExecuteSql(Mysql_error_update, waterArchiveWeekRawDatas); } } #endregion 週歸檔補償 @@ -420,12 +580,11 @@ namespace BackendWorkerService.Services.Implement //取得所有須補償的設備資訊 targetTable = "archive_electric_meter_month"; var sql_error_month = string.Format(sql_error_format, targetTable); - var error_months = await backgroundServiceRepository.GetAllAsync(sql_error_month, new { RepeatTimes = repeatTimes }); - - List> archiveMonthRawDatas = new List>(); - if (error_months.Count() > 0) + var electric_error_months = await backgroundServiceRepository.GetAllAsync(sql_error_month, new { RepeatTimes = repeatTimes }); + List> electricArchiveMonthRawDatas = new List>(); + if (electric_error_months.Count() > 0) { - foreach (var error_month in error_months) + foreach (var error_month in electric_error_months) { DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); deviceNumberPoint.DeviceNumber = error_month.Device_number; @@ -481,7 +640,7 @@ namespace BackendWorkerService.Services.Implement archiveMonthRawData.Add("@sum_rawdata", 0); archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); - archiveMonthRawDatas.Add(archiveMonthRawData); + electricArchiveMonthRawDatas.Add(archiveMonthRawData); } if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容 @@ -489,17 +648,101 @@ namespace BackendWorkerService.Services.Implement var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult); if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) { - archiveMonthRawDatas.AddRange(ArrangeRawDatas); + electricArchiveMonthRawDatas.AddRange(ArrangeRawDatas); } } } - if (archiveMonthRawDatas.Count() > 0) + if (electricArchiveMonthRawDatas.Count() > 0) { var Mysql_error_update = string.Format(MYsql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable); - await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, archiveMonthRawDatas); - await backgroundServiceRepository.ExecuteSql(MYsql_update_format, archiveMonthRawDatas); + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveMonthRawDatas); + await backgroundServiceRepository.ExecuteSql(MYsql_update_format, electricArchiveMonthRawDatas); + } + } + + targetTable = "archive_electric_meter_month"; + sql_error_month = string.Format(sql_error_format, targetTable); + var water_error_months = await backgroundServiceRepository.GetAllAsync(sql_error_month, new { RepeatTimes = repeatTimes }); + List> waterArchiveMonthRawDatas = new List>(); + if (water_error_months.Count() > 0) + { + foreach (var error_month in water_error_months) + { + DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); + deviceNumberPoint.DeviceNumber = error_month.Device_number; + deviceNumberPoint.Point = error_month.Point; + deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", error_month.Device_number, error_month.Point); + + var startTimestamp = string.Format("{0}+08:00", error_month.Start_timestamp.Replace(" ", "T")); + var endTimestamp = string.Format("{0}+08:00", error_month.End_timestamp.Replace(" ", "T")); + + var startDateTime = Convert.ToDateTime(error_month.Start_timestamp); + var dayInMonth = DateTime.DaysInMonth(startDateTime.Year, startDateTime.Month); + + var historyQueryFilter = $@" + + + + "; + + HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/"); + //HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~historyRollup/"); + archiveMonthRequest.Method = "POST"; + archiveMonthRequest.Headers.Add("Authorization", "Basic " + encoded); + archiveMonthRequest.PreAuthenticate = true; + + byte[] byteArray = Encoding.UTF8.GetBytes(historyQueryFilter); + using (Stream reqStream = archiveMonthRequest.GetRequestStream()) + { + reqStream.Write(byteArray, 0, byteArray.Length); + } + + HttpWebResponse archiveMonthResponse = (HttpWebResponse)archiveMonthRequest.GetResponse(); + var archiveMonthResponseContent = new StreamReader(archiveMonthResponse.GetResponseStream()).ReadToEnd(); + + xmlDocument.LoadXml(archiveMonthResponseContent); + string archiveMonthJson = JsonConvert.SerializeXmlNode(xmlDocument); + JObject archiveMonthJsonResult = (JObject)JsonConvert.DeserializeObject(archiveMonthJson); + + if (archiveMonthJsonResult.ContainsKey("err")) //抓取錯誤 + { + Dictionary archiveMonthRawData = new Dictionary(); + archiveMonthRawData.Add("@device_number", error_month.Device_number); + archiveMonthRawData.Add("@point", error_month.Point); + archiveMonthRawData.Add("@start_timestamp", DateTime.Parse(error_month.Start_timestamp, System.Globalization.CultureInfo.CurrentCulture)); + archiveMonthRawData.Add("@end_timestamp", DateTime.Parse(error_month.End_timestamp, System.Globalization.CultureInfo.CurrentCulture)); + archiveMonthRawData.Add("@is_complete", 0); + archiveMonthRawData.Add("@repeat_times", ++error_month.Repeat_times); + archiveMonthRawData.Add("@fail_reason", archiveMonthJson); + + archiveMonthRawData.Add("@count_rawdata", 0); + archiveMonthRawData.Add("@min_rawdata", 0); + archiveMonthRawData.Add("@max_rawdata", 0); + archiveMonthRawData.Add("@avg_rawdata", 0); + archiveMonthRawData.Add("@sum_rawdata", 0); + archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); + + waterArchiveMonthRawDatas.Add(archiveMonthRawData); + } + + if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容 + { + var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult); + if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) + { + waterArchiveMonthRawDatas.AddRange(ArrangeRawDatas); + } + } + } + + if (waterArchiveMonthRawDatas.Count() > 0) + { + var Mysql_error_update = string.Format(MYsql_update_format, targetTable); + var sql_error_update = string.Format(sql_update_format, targetTable); + await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, waterArchiveMonthRawDatas); + await backgroundServiceRepository.ExecuteSql(MYsql_update_format, waterArchiveMonthRawDatas); } } #endregion 月歸檔補償 diff --git a/BackendWorkerService/root/PowerfulRain.xml b/BackendWorkerService/root/PowerfulRain.xml index 6f49241..fdfa044 100644 --- a/BackendWorkerService/root/PowerfulRain.xml +++ b/BackendWorkerService/root/PowerfulRain.xml @@ -1,13 +1,13 @@ - CWB-Weather_extremely-rain_202304300430001 + CWB-Weather_extremely-rain_202305081515001 weather@cwb.gov.tw - 2023-04-30T04:34:49+08:00 + 2023-05-08T15:21:31+08:00 Actual Cancel Public - weather@cwb.gov.tw,CWB-Weather_extremely-rain_202304292135001,2023-04-29T21:44:03+08:00 + weather@cwb.gov.tw,CWB-Weather_extremely-rain_202305081030001,2023-05-08T10:37:48+08:00 weather@cwb.gov.tw,CWB-Weather_extremely-rain_202305080840001,2023-05-08T08:51:11+08:00 zh-TW Met @@ -19,13 +19,13 @@ profile:CAP-TWP:Event:1.0 rainfall - 2023-04-30T04:30:00+08:00 - 2023-04-30T04:30:00+08:00 - 2023-04-30T04:44:49+08:00 + 2023-05-08T15:15:00+08:00 + 2023-05-08T15:17:00+08:00 + 2023-05-08T15:31:31+08:00 中央氣象局 解除大雨特報 -由於降雨趨於緩和,發生大雨的機率降低,故解除大雨特報;今日綠島及蘭嶼仍有局部較大雨勢發生的機率,請注意。 +由於降雨趨於緩和,發生大雨的機率降低,故解除大雨特報;今(8)日屏東及臺東山區仍有局部較大雨勢發生,請注意。 https://www.cwb.gov.tw/V8/C/P/Warning/FIFOWS.html