[後端] 修改排成 增加獲取水表資料

This commit is contained in:
dev02 2023-05-10 18:06:02 +08:00
parent df02d1593b
commit 0538348866
3 changed files with 779 additions and 68 deletions

View File

@ -59,29 +59,47 @@ namespace BackendWorkerService.Quartz.Jobs
var variableArchive = await backgroundServiceRepository.GetAllAsync<KeyValue>(sqlArchive); var variableArchive = await backgroundServiceRepository.GetAllAsync<KeyValue>(sqlArchive);
var electricMeterGuid = variableArchive.Where(x => x.Name == "ElectricMeterGuid").Select(x => x.Value).FirstOrDefault(); 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 #region
var sWhere = "deleted = 0 AND device_name_tag = @sub_system_guid"; var sWhere = "deleted = 0 AND device_name_tag = @sub_system_guid";
var electricMeters = await backgroundServiceRepository.GetAllAsync<Device>("device", sWhere, new { sub_system_guid = electricMeterGuid }); var electricMeters = await backgroundServiceRepository.GetAllAsync<Device>("device", sWhere, new { sub_system_guid = electricMeterGuid });
var waterMeters = await backgroundServiceRepository.GetAllAsync<Device>("device", sWhere, new { sub_system_guid = waterMeterGuid });
#endregion #endregion
#region #region
var sPointWhere = "deleted = 0 AND device_name_tag = @sub_system_guid"; var sPointWhere = "deleted = 0 AND device_name_tag = @sub_system_guid";
var points = await backgroundServiceRepository.GetAllAsync<Device_item>("device_item", sPointWhere, new { sub_system_guid = electricMeterGuid }); var electricPoints = await backgroundServiceRepository.GetAllAsync<Device_item>("device_item", sPointWhere, new { sub_system_guid = electricMeterGuid });
var waterPoints = await backgroundServiceRepository.GetAllAsync<Device_item>("device_item", sPointWhere, new { sub_system_guid = waterMeterGuid });
#endregion #endregion
#region #region
List<DeviceNumberPoint> deviceNumberPoints = new List<DeviceNumberPoint>(); List<DeviceNumberPoint> electricDeviceNumberPoints = new List<DeviceNumberPoint>();
foreach (var electricMeter in electricMeters) foreach (var electricMeter in electricMeters)
{ {
foreach (var point in points) foreach (var point in electricPoints)
{ {
DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint();
deviceNumberPoint.DeviceNumber = electricMeter.Device_number; deviceNumberPoint.DeviceNumber = electricMeter.Device_number;
deviceNumberPoint.Point = point.points; deviceNumberPoint.Point = point.points;
deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", electricMeter.Device_number, point.points); deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", electricMeter.Device_number, point.points);
deviceNumberPoints.Add(deviceNumberPoint); electricDeviceNumberPoints.Add(deviceNumberPoint);
}
}
#endregion
#region
List<DeviceNumberPoint> waterDeviceNumberPoints = new List<DeviceNumberPoint>();
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 #endregion
@ -122,8 +140,9 @@ namespace BackendWorkerService.Quartz.Jobs
//stopWatch.Start(); //stopWatch.Start();
//抓取每個設備的資料 //抓取每個設備的資料
List<Dictionary<string, object>> archiveDayRawDatas = new List<Dictionary<string, object>>(); List<Dictionary<string, object>> electericArchiveDayRawDatas = new List<Dictionary<string, object>>();
foreach (var deviceNumberPoint in deviceNumberPoints) List<Dictionary<string, object>> waterArchiveDayRawDatas = new List<Dictionary<string, object>>();
foreach (var deviceNumberPoint in electricDeviceNumberPoints)
{ {
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/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/");
@ -166,7 +185,7 @@ namespace BackendWorkerService.Quartz.Jobs
archiveDayRawData.Add("@sum_rawdata", 0); archiveDayRawData.Add("@sum_rawdata", 0);
archiveDayRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); archiveDayRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
archiveDayRawDatas.Add(archiveDayRawData); electericArchiveDayRawDatas.Add(archiveDayRawData);
} }
if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容 if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容
@ -174,7 +193,62 @@ namespace BackendWorkerService.Quartz.Jobs
var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult); var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult);
if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) 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<string, object> archiveDayRawData = new Dictionary<string, object>();
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(); //stopWatch.Stop();
//logger.LogInformation("【ArchiveElectricMeterDayJob】【天歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); //logger.LogInformation("【ArchiveElectricMeterDayJob】【天歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);
if (archiveDayRawDatas.Count() > 0) if (electericArchiveDayRawDatas.Count() > 0)
{ {
var sql = $@" var sql = $@"
UPDATE archive_electric_meter_day SET UPDATE archive_electric_meter_day SET
@ -275,8 +349,104 @@ namespace BackendWorkerService.Quartz.Jobs
END END
COMMIT TRANSACTION;"; COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, archiveDayRawDatas); await backgroundServiceRepository.ExecuteSql(sql, electericArchiveDayRawDatas);
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, archiveDayRawDatas); 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", "任務完成"); await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Day", "任務完成");
} }
@ -311,8 +481,9 @@ namespace BackendWorkerService.Quartz.Jobs
//stopWatch.Start(); //stopWatch.Start();
//抓取每個設備的資料 //抓取每個設備的資料
List<Dictionary<string, object>> archiveWeekRawDatas = new List<Dictionary<string, object>>(); List<Dictionary<string, object>> electricArchiveWeekRawDatas = new List<Dictionary<string, object>>();
foreach (var deviceNumberPoint in deviceNumberPoints) List<Dictionary<string, object>> waterArchiveWeekRawDatas = new List<Dictionary<string, object>>();
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/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/");
//HttpWebRequest archiveWeekRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~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("@sum_rawdata", 0);
archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
archiveWeekRawDatas.Add(archiveWeekRawData); electricArchiveWeekRawDatas.Add(archiveWeekRawData);
} }
if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容 if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容
@ -362,7 +533,61 @@ namespace BackendWorkerService.Quartz.Jobs
var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult); var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult);
if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) 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<string, object> archiveWeekRawData = new Dictionary<string, object>();
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(); //stopWatch.Stop();
//logger.LogInformation("【ArchiveElectricMeterDayJob】【週歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); //logger.LogInformation("【ArchiveElectricMeterDayJob】【週歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);
if (archiveWeekRawDatas.Count() > 0) if (electricArchiveWeekRawDatas.Count() > 0)
{ {
var sql = $@" var sql = $@"
@ -465,8 +690,107 @@ namespace BackendWorkerService.Quartz.Jobs
END END
COMMIT TRANSACTION;"; COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, archiveWeekRawDatas); await backgroundServiceRepository.ExecuteSql(sql, electricArchiveWeekRawDatas);
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, archiveWeekRawDatas); 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", "任務完成"); await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Week", "任務完成");
@ -504,8 +828,9 @@ namespace BackendWorkerService.Quartz.Jobs
//stopWatch.Start(); //stopWatch.Start();
//抓取每個設備的資料 //抓取每個設備的資料
List<Dictionary<string, object>> archiveMonthRawDatas = new List<Dictionary<string, object>>(); List<Dictionary<string, object>> electricArchiveMonthRawDatas = new List<Dictionary<string, object>>();
foreach (var deviceNumberPoint in deviceNumberPoints) List<Dictionary<string, object>> waterArchiveMonthRawDatas = new List<Dictionary<string, object>>();
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/{deviceNumberPoint.FullDeviceNumberPoint}/~historyRollup/");
//HttpWebRequest archiveMonthRequest = (HttpWebRequest)WebRequest.Create($"{obixApiConfig.ApiBase}obix/histories/FIC_Center/H_E1_B1F_MVCB_MVCBH_V1/~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("@sum_rawdata", 0);
archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
archiveMonthRawDatas.Add(archiveMonthRawData); electricArchiveMonthRawDatas.Add(archiveMonthRawData);
} }
if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容 if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容
@ -555,7 +880,61 @@ namespace BackendWorkerService.Quartz.Jobs
var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult); var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult);
if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) 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<string, object> archiveMonthRawData = new Dictionary<string, object>();
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(); //stopWatch.Stop();
//logger.LogInformation("【ArchiveElectricMeterDayJob】【月歸檔效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); //logger.LogInformation("【ArchiveElectricMeterDayJob】【月歸檔效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);
if (archiveMonthRawDatas.Count() > 0) if (electricArchiveMonthRawDatas.Count() > 0)
{ {
var sql = $@" var sql = $@"
UPDATE archive_electric_meter_month SET UPDATE archive_electric_meter_month SET
@ -655,8 +1034,103 @@ namespace BackendWorkerService.Quartz.Jobs
END END
COMMIT TRANSACTION;"; COMMIT TRANSACTION;";
await backgroundServiceRepository.ExecuteSql(sql, archiveMonthRawDatas); await backgroundServiceRepository.ExecuteSql(sql, electricArchiveMonthRawDatas);
await backgroundServiceMsSqlRepository.ExecuteSql(mySql, archiveMonthRawDatas); 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", "任務完成"); await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "Month", "任務完成");
} }
@ -686,13 +1160,7 @@ namespace BackendWorkerService.Quartz.Jobs
logger.LogError("【ArchiveElectricMeterDayJob】【補償機制】【任務失敗】"); logger.LogError("【ArchiveElectricMeterDayJob】【補償機制】【任務失敗】");
logger.LogError("【ArchiveElectricMeterDayJob】【補償機制】【任務失敗】[Exception]{0}", ex.ToString()); logger.LogError("【ArchiveElectricMeterDayJob】【補償機制】【任務失敗】[Exception]{0}", ex.ToString());
} }
} }
#endregion #endregion
await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "All","任務完成"); await task_Detail.InsertWorkTime_End("ArchiveElectricMeterDayJob", "All","任務完成");

View File

@ -250,12 +250,11 @@ namespace BackendWorkerService.Services.Implement
//取得所有須補償的設備資訊 //取得所有須補償的設備資訊
targetTable = "archive_electric_meter_day"; targetTable = "archive_electric_meter_day";
var sql_error_day = string.Format(sql_error_format, targetTable); var sql_error_day = string.Format(sql_error_format, targetTable);
var error_days = await backgroundServiceRepository.GetAllAsync<ArchiveElectricMeter>(sql_error_day, new { RepeatTimes = repeatTimes }); var electric_error_days = await backgroundServiceRepository.GetAllAsync<ArchiveElectricMeter>(sql_error_day, new { RepeatTimes = repeatTimes });
List<Dictionary<string, object>> electricArchiveDayRawDatas = new List<Dictionary<string, object>>();
List<Dictionary<string, object>> archiveDayRawDatas = new List<Dictionary<string, object>>(); if (electric_error_days.Count() > 0)
if (error_days.Count() > 0)
{ {
foreach (var error_day in error_days) foreach (var error_day in electric_error_days)
{ {
DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint();
deviceNumberPoint.DeviceNumber = error_day.Device_number; deviceNumberPoint.DeviceNumber = error_day.Device_number;
@ -308,7 +307,7 @@ namespace BackendWorkerService.Services.Implement
archiveDayRawData.Add("@sum_rawdata", 0); archiveDayRawData.Add("@sum_rawdata", 0);
archiveDayRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); archiveDayRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
archiveDayRawDatas.Add(archiveDayRawData); electricArchiveDayRawDatas.Add(archiveDayRawData);
} }
if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容 if (archiveDayJsonResult.ContainsKey("obj")) //表示可以讀取到內容
@ -316,17 +315,98 @@ namespace BackendWorkerService.Services.Implement
var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult); var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveDayJsonResult);
if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) 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 Mysql_error_update = string.Format(MYsql_update_format, targetTable);
var sql_error_update = string.Format(sql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable);
await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, archiveDayRawDatas); await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveDayRawDatas);
await backgroundServiceRepository.ExecuteSql(Mysql_error_update, archiveDayRawDatas); 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<ArchiveElectricMeter>(sql_error_day, new { RepeatTimes = repeatTimes });
List<Dictionary<string, object>> waterArchiveDayRawDatas = new List<Dictionary<string, object>>();
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 = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT1D' />
</obj>";
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<string, object> archiveDayRawData = new Dictionary<string, object>();
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 #endregion
@ -335,12 +415,11 @@ namespace BackendWorkerService.Services.Implement
//取得所有須補償的設備資訊 //取得所有須補償的設備資訊
targetTable = "archive_electric_meter_week"; targetTable = "archive_electric_meter_week";
var sql_error_week = string.Format(sql_error_format, targetTable); var sql_error_week = string.Format(sql_error_format, targetTable);
var error_weeks = await backgroundServiceRepository.GetAllAsync<ArchiveElectricMeter>(sql_error_week, new { RepeatTimes = repeatTimes }); var eletric_error_weeks = await backgroundServiceRepository.GetAllAsync<ArchiveElectricMeter>(sql_error_week, new { RepeatTimes = repeatTimes });
List<Dictionary<string, object>> electricArchiveWeekRawDatas = new List<Dictionary<string, object>>();
List<Dictionary<string, object>> archiveWeekRawDatas = new List<Dictionary<string, object>>(); if (eletric_error_weeks.Count() > 0)
if (error_weeks.Count() > 0)
{ {
foreach (var error_week in error_weeks) foreach (var error_week in eletric_error_weeks)
{ {
DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint();
deviceNumberPoint.DeviceNumber = error_week.Device_number; deviceNumberPoint.DeviceNumber = error_week.Device_number;
@ -393,7 +472,7 @@ namespace BackendWorkerService.Services.Implement
archiveWeekRawData.Add("@sum_rawdata", 0); archiveWeekRawData.Add("@sum_rawdata", 0);
archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); archiveWeekRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
archiveWeekRawDatas.Add(archiveWeekRawData); electricArchiveWeekRawDatas.Add(archiveWeekRawData);
} }
if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容 if (archiveWeekJsonResult.ContainsKey("obj")) //表示可以讀取到內容
@ -401,17 +480,98 @@ namespace BackendWorkerService.Services.Implement
var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult); var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveWeekJsonResult);
if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) 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 Mysql_error_update = string.Format(MYsql_update_format, targetTable);
var sql_error_update = string.Format(sql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable);
await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, archiveWeekRawDatas); await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveWeekRawDatas);
await backgroundServiceRepository.ExecuteSql(Mysql_error_update, archiveWeekRawDatas); 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<ArchiveElectricMeter>(sql_error_week, new { RepeatTimes = repeatTimes });
List<Dictionary<string, object>> waterArchiveWeekRawDatas = new List<Dictionary<string, object>>();
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 = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT7D' />
</obj>";
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<string, object> archiveWeekRawData = new Dictionary<string, object>();
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 #endregion
@ -420,12 +580,11 @@ namespace BackendWorkerService.Services.Implement
//取得所有須補償的設備資訊 //取得所有須補償的設備資訊
targetTable = "archive_electric_meter_month"; targetTable = "archive_electric_meter_month";
var sql_error_month = string.Format(sql_error_format, targetTable); var sql_error_month = string.Format(sql_error_format, targetTable);
var error_months = await backgroundServiceRepository.GetAllAsync<ArchiveElectricMeter>(sql_error_month, new { RepeatTimes = repeatTimes }); var electric_error_months = await backgroundServiceRepository.GetAllAsync<ArchiveElectricMeter>(sql_error_month, new { RepeatTimes = repeatTimes });
List<Dictionary<string, object>> electricArchiveMonthRawDatas = new List<Dictionary<string, object>>();
List<Dictionary<string, object>> archiveMonthRawDatas = new List<Dictionary<string, object>>(); if (electric_error_months.Count() > 0)
if (error_months.Count() > 0)
{ {
foreach (var error_month in error_months) foreach (var error_month in electric_error_months)
{ {
DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint(); DeviceNumberPoint deviceNumberPoint = new DeviceNumberPoint();
deviceNumberPoint.DeviceNumber = error_month.Device_number; deviceNumberPoint.DeviceNumber = error_month.Device_number;
@ -481,7 +640,7 @@ namespace BackendWorkerService.Services.Implement
archiveMonthRawData.Add("@sum_rawdata", 0); archiveMonthRawData.Add("@sum_rawdata", 0);
archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); archiveMonthRawData.Add("@updated_at", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
archiveMonthRawDatas.Add(archiveMonthRawData); electricArchiveMonthRawDatas.Add(archiveMonthRawData);
} }
if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容 if (archiveMonthJsonResult.ContainsKey("obj")) //表示可以讀取到內容
@ -489,17 +648,101 @@ namespace BackendWorkerService.Services.Implement
var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult); var ArrangeRawDatas = ArrangeRawData(deviceNumberPoint, archiveMonthJsonResult);
if (ArrangeRawDatas != null && ArrangeRawDatas.Count() > 0) 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 Mysql_error_update = string.Format(MYsql_update_format, targetTable);
var sql_error_update = string.Format(sql_update_format, targetTable); var sql_error_update = string.Format(sql_update_format, targetTable);
await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, archiveMonthRawDatas); await backgroundServiceMsSqlRepository.ExecuteSql(sql_error_update, electricArchiveMonthRawDatas);
await backgroundServiceRepository.ExecuteSql(MYsql_update_format, archiveMonthRawDatas); 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<ArchiveElectricMeter>(sql_error_month, new { RepeatTimes = repeatTimes });
List<Dictionary<string, object>> waterArchiveMonthRawDatas = new List<Dictionary<string, object>>();
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 = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT{dayInMonth}D' />
</obj>";
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<string, object> archiveMonthRawData = new Dictionary<string, object>();
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 #endregion

View File

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<alert xmlns="urn:oasis:names:tc:emergency:cap:1.2"> <alert xmlns="urn:oasis:names:tc:emergency:cap:1.2">
<identifier>CWB-Weather_extremely-rain_202304300430001</identifier> <identifier>CWB-Weather_extremely-rain_202305081515001</identifier>
<sender>weather@cwb.gov.tw</sender> <sender>weather@cwb.gov.tw</sender>
<sent>2023-04-30T04:34:49+08:00</sent> <sent>2023-05-08T15:21:31+08:00</sent>
<status>Actual</status> <status>Actual</status>
<msgType>Cancel</msgType> <msgType>Cancel</msgType>
<scope>Public</scope> <scope>Public</scope>
<references>weather@cwb.gov.tw,CWB-Weather_extremely-rain_202304292135001,2023-04-29T21:44:03+08:00</references> <references>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</references>
<info> <info>
<language>zh-TW</language> <language>zh-TW</language>
<category>Met</category> <category>Met</category>
@ -19,13 +19,13 @@
<valueName>profile:CAP-TWP:Event:1.0</valueName> <valueName>profile:CAP-TWP:Event:1.0</valueName>
<value>rainfall</value> <value>rainfall</value>
</eventCode> </eventCode>
<effective>2023-04-30T04:30:00+08:00</effective> <effective>2023-05-08T15:15:00+08:00</effective>
<onset>2023-04-30T04:30:00+08:00</onset> <onset>2023-05-08T15:17:00+08:00</onset>
<expires>2023-04-30T04:44:49+08:00</expires> <expires>2023-05-08T15:31:31+08:00</expires>
<senderName>中央氣象局</senderName> <senderName>中央氣象局</senderName>
<headline>解除大雨特報</headline> <headline>解除大雨特報</headline>
<description> <description>
由於降雨趨於緩和,發生大雨的機率降低,故解除大雨特報;今日綠島及蘭嶼仍有局部較大雨勢發生的機率,請注意。 由於降雨趨於緩和,發生大雨的機率降低,故解除大雨特報;今(8)日屏東及臺東山區仍有局部較大雨勢發生,請注意。
</description> </description>
<instruction></instruction> <instruction></instruction>
<web>https://www.cwb.gov.tw/V8/C/P/Warning/FIFOWS.html</web> <web>https://www.cwb.gov.tw/V8/C/P/Warning/FIFOWS.html</web>