分离实时缓存和sql储存,独立时间周期
This commit is contained in:
@ -18,6 +18,19 @@ namespace HTCoreServiceApp.Common
|
|||||||
public string SystemName { get; set; }
|
public string SystemName { get; set; }
|
||||||
public string GroupName { get; set; }
|
public string GroupName { get; set; }
|
||||||
public int? UpdateRate { get; set; }
|
public int? UpdateRate { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 数据存储周期(毫秒),用于控制数据存储到数据库的频率
|
||||||
|
/// 默认值: 1000
|
||||||
|
/// </summary>
|
||||||
|
public int? StorageRate { get; set; } = 1000;
|
||||||
|
/// <summary>
|
||||||
|
/// 存储周期计时器,记录距离上次存储的累计时间(毫秒)
|
||||||
|
/// </summary>
|
||||||
|
public long StorageTimer { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 上次存储的时间戳
|
||||||
|
/// </summary>
|
||||||
|
public long LastStorageTime { get; set; }
|
||||||
public string DriverName { get; set;}
|
public string DriverName { get; set;}
|
||||||
public AddressArg AddressArg { get; set; }
|
public AddressArg AddressArg { get; set; }
|
||||||
public bool IncludeBChaned { get; set; }
|
public bool IncludeBChaned { get; set; }
|
||||||
|
@ -99,8 +99,19 @@ namespace HTCoreServiceApp.Communicate.ModbusTcp
|
|||||||
ScanTagsPara scanTagsPara = new ScanTagsPara();
|
ScanTagsPara scanTagsPara = new ScanTagsPara();
|
||||||
scanTagsPara.GroupName = MetaGroups.Where(x=>x.GroupId == metaGroup.Key.GroupId).ToList().First().GroupName;
|
scanTagsPara.GroupName = MetaGroups.Where(x=>x.GroupId == metaGroup.Key.GroupId).ToList().First().GroupName;
|
||||||
scanTagsPara.UpdateRate = MetaGroups.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList().First().UpdateRate;
|
scanTagsPara.UpdateRate = MetaGroups.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList().First().UpdateRate;
|
||||||
|
scanTagsPara.StorageRate = MetaGroups.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList().First().StorageRate;
|
||||||
scanTagsPara.IsConsecutive = (bool)MetaGroups.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList().First().IsActive;
|
scanTagsPara.IsConsecutive = (bool)MetaGroups.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList().First().IsActive;
|
||||||
scanTagsPara.IsActive = MetaGroups.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList().First().IsActive;
|
scanTagsPara.IsActive = MetaGroups.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList().First().IsActive;
|
||||||
|
|
||||||
|
// 初始化存储周期相关字段
|
||||||
|
scanTagsPara.StorageTimer = 0;
|
||||||
|
scanTagsPara.LastStorageTime = 0;
|
||||||
|
// 如果没有设置存储周期,默认等于更新周期(保持原有行为)
|
||||||
|
if (scanTagsPara.StorageRate == null || scanTagsPara.StorageRate <= 0)
|
||||||
|
{
|
||||||
|
scanTagsPara.StorageRate = scanTagsPara.UpdateRate;
|
||||||
|
}
|
||||||
|
|
||||||
scanTagsPara.ScanTags = MetaTags.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList();
|
scanTagsPara.ScanTags = MetaTags.Where(x => x.GroupId == metaGroup.Key.GroupId).ToList();
|
||||||
if (scanTagsPara.ScanTags.Count < 1)
|
if (scanTagsPara.ScanTags.Count < 1)
|
||||||
{
|
{
|
||||||
@ -239,7 +250,7 @@ namespace HTCoreServiceApp.Communicate.ModbusTcp
|
|||||||
var t1 = DateTime.Now.Millisecond;
|
var t1 = DateTime.Now.Millisecond;
|
||||||
Console.WriteLine(string.Join(",", buffer) + "ModbusTcp读取完成:" + " 字数:" + amount.ToString());
|
Console.WriteLine(string.Join(",", buffer) + "ModbusTcp读取完成:" + " 字数:" + amount.ToString());
|
||||||
//Task<int> saveResult = DataExtract.DataExtractCAsync(scanTagsPara.ScanTags, scanTagsPara.GroupName, scanTagsPara.DriverName, buffer, addressArg);
|
//Task<int> saveResult = DataExtract.DataExtractCAsync(scanTagsPara.ScanTags, scanTagsPara.GroupName, scanTagsPara.DriverName, buffer, addressArg);
|
||||||
DataExtractMB.DataExtractCAsync(scanTagsPara.ScanTags, scanTagsPara.SystemName, scanTagsPara.GroupName, scanTagsPara.DriverName, buffer, addressArg);
|
DataExtractMB.DataExtractCAsync(scanTagsPara.ScanTags, scanTagsPara.SystemName, scanTagsPara.GroupName, scanTagsPara.DriverName, buffer, addressArg, scanTagsPara);
|
||||||
var t2 = DateTime.Now.Millisecond;
|
var t2 = DateTime.Now.Millisecond;
|
||||||
var timediff = t2 - t1;
|
var timediff = t2 - t1;
|
||||||
Console.WriteLine($"ModbusTcp缓存和储存耗时:{timediff}");
|
Console.WriteLine($"ModbusTcp缓存和储存耗时:{timediff}");
|
||||||
|
@ -20,7 +20,18 @@ namespace HTCoreServiceApp.DataHandle
|
|||||||
{
|
{
|
||||||
string res = await DataExtractConsecutive(metaTags, systenName, groupName, driverName, bytes, addressArg);
|
string res = await DataExtractConsecutive(metaTags, systenName, groupName, driverName, bytes, addressArg);
|
||||||
}
|
}
|
||||||
public static Task <string> DataExtractConsecutive(List<MetaTag> metaTags, string systenName, string groupName, string driverName, int[] bytes, AddressArg addressArg)
|
|
||||||
|
public static async void DataExtractCAsync(List<MetaTag> metaTags, string systenName, string groupName, string driverName, int[] bytes, AddressArg addressArg, ScanTagsPara scanTagsPara)
|
||||||
|
{
|
||||||
|
string res = await DataExtractConsecutive(metaTags, systenName, groupName, driverName, bytes, addressArg, scanTagsPara);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<string> DataExtractConsecutive(List<MetaTag> metaTags, string systenName, string groupName, string driverName, int[] bytes, AddressArg addressArg)
|
||||||
|
{
|
||||||
|
return DataExtractConsecutive(metaTags, systenName, groupName, driverName, bytes, addressArg, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<string> DataExtractConsecutive(List<MetaTag> metaTags, string systenName, string groupName, string driverName, int[] bytes, AddressArg addressArg, ScanTagsPara scanTagsPara)
|
||||||
{
|
{
|
||||||
return Task.Run(() =>
|
return Task.Run(() =>
|
||||||
{
|
{
|
||||||
@ -55,15 +66,29 @@ namespace HTCoreServiceApp.DataHandle
|
|||||||
//Console.WriteLine($"转换耗时:{timess}");
|
//Console.WriteLine($"转换耗时:{timess}");
|
||||||
//Console.WriteLine(string.Join(",", (Array)listMetaTagValue.ToArray()) + "字节数据转换完成" + ",数据量:" + listMetaTagValue.Count.ToString());
|
//Console.WriteLine(string.Join(",", (Array)listMetaTagValue.ToArray()) + "字节数据转换完成" + ",数据量:" + listMetaTagValue.Count.ToString());
|
||||||
|
|
||||||
//异步缓存
|
//异步缓存 - 始终执行,用于实时数据更新
|
||||||
Task.Run(()=>
|
Task.Run(()=>
|
||||||
DataLiving.DataLive2CaChe(listMetaTagValue, driverName, groupName)
|
DataLiving.DataLive2CaChe(listMetaTagValue, driverName, groupName)
|
||||||
);
|
);
|
||||||
|
|
||||||
//异步储存
|
//异步储存 - 根据存储周期控制是否执行
|
||||||
Task.Run(() =>
|
bool shouldStore = true;
|
||||||
DataStorage.DataSave2DB(listMetaTagValue, systenName, driverName, groupName, hdatetime)
|
if (scanTagsPara != null)
|
||||||
);
|
{
|
||||||
|
shouldStore = CheckStorageTiming(scanTagsPara, hdatetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldStore)
|
||||||
|
{
|
||||||
|
Task.Run(() =>
|
||||||
|
DataStorage.DataSave2DB(listMetaTagValue, systenName, driverName, groupName, hdatetime)
|
||||||
|
);
|
||||||
|
Console.WriteLine($"{driverName}_{groupName}执行数据存储");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{driverName}_{groupName}跳过数据存储(未到存储周期)");
|
||||||
|
}
|
||||||
|
|
||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
@ -74,6 +99,38 @@ namespace HTCoreServiceApp.DataHandle
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否到达存储周期
|
||||||
|
/// 每一秒(unix时间戳的每一秒内)必须触发一次存储
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scanTagsPara">扫描参数</param>
|
||||||
|
/// <param name="currentTime">当前时间戳</param>
|
||||||
|
/// <returns>是否应该执行存储</returns>
|
||||||
|
private static bool CheckStorageTiming(ScanTagsPara scanTagsPara, long currentTime)
|
||||||
|
{
|
||||||
|
// 获取当前时间戳的秒数(去掉毫秒部分)
|
||||||
|
long currentSecond = currentTime / 1000;
|
||||||
|
|
||||||
|
// 如果是第一次执行,直接存储
|
||||||
|
if (scanTagsPara.LastStorageTime == 0)
|
||||||
|
{
|
||||||
|
scanTagsPara.LastStorageTime = currentTime;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取上次存储时间的秒数(去掉毫秒部分)
|
||||||
|
long lastStorageSecond = scanTagsPara.LastStorageTime / 1000;
|
||||||
|
|
||||||
|
// 如果当前秒数大于上次存储的秒数,说明已经进入新的一秒,需要执行存储
|
||||||
|
if (currentSecond > lastStorageSecond)
|
||||||
|
{
|
||||||
|
scanTagsPara.LastStorageTime = currentTime;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 父类的value赋值给子类
|
/// 父类的value赋值给子类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -36,6 +36,12 @@ namespace HTCoreServiceApp.Models
|
|||||||
[SugarColumn(ColumnName="UpdateRate" )]
|
[SugarColumn(ColumnName="UpdateRate" )]
|
||||||
public int? UpdateRate { get; set; }
|
public int? UpdateRate { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// 数据存储周期(毫秒),用于控制数据存储到数据库的频率,应小于等于UpdateRate
|
||||||
|
/// 默认值: ((1000))
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName="StorageRate" )]
|
||||||
|
public int? StorageRate { get; set; } = 1000;
|
||||||
|
/// <summary>
|
||||||
///
|
///
|
||||||
///</summary>
|
///</summary>
|
||||||
[SugarColumn(ColumnName="DeadBand" )]
|
[SugarColumn(ColumnName="DeadBand" )]
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"Type": "SqlServer",
|
"Type": "SqlServer",
|
||||||
"SqlServer": "Zk48ARnbLq1Lk+lzxvCij1zzENjv04mu23Y8D8hdM3VxApXnvB2iA7xm5JfYWNkbLw7YB0A01VrIJSpoUrEMyqiV8fRiux6LH2hkV1/VLUdoCCG+zOCCkqgML8iuOSXlbE2i3UagxHrXZhgxt4C3ifaWykWgbiGq9h4U4WfKnimMXuCqhSgUm9Gk3c/2KDxSWgLrKUW29N2X4oWhBbwjWg=="
|
"SqlServer": "Zk48ARnbLq1Lk+lzxvCij1zzENjv04mu23Y8D8hdM3VxApXnvB2iA7xm5JfYWNkbR4m0u8SvmSkPpW1HqgyIlX9lbHiIuIQpS6lLThUT43JYrzX58HTXFtr/7DZ/QgrdUaOZqOBpVB9riZ4eSsJ+swn6WOptm+aEuvxLC/6Te15/OW/WsUX/Mhwb55VidHdGGLY2nbbpX8GYelD8PXMznA=="
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
Reference in New Issue
Block a user