解决各种转换造成的时间损耗

This commit is contained in:
dell
2024-07-04 16:10:59 +08:00
parent 823c0ab346
commit 46ba36bfa6
4 changed files with 120 additions and 108 deletions

View File

@ -19,6 +19,7 @@ using Pipelines.Sockets.Unofficial.Arenas;
using System.Numerics; using System.Numerics;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Newtonsoft.Json; using Newtonsoft.Json;
using Dm;
namespace HTCoreServiceApp.Communicate.DriverSiemensS7 namespace HTCoreServiceApp.Communicate.DriverSiemensS7
{ {
@ -79,7 +80,7 @@ namespace HTCoreServiceApp.Communicate.DriverSiemensS7
Client.SetConnectionType(0x3); Client.SetConnectionType(0x3);
Client.ConnTimeout= Timeout; Client.ConnTimeout= Timeout;
Client.RecvTimeout = 30; Client.RecvTimeout = 50;
@ -204,7 +205,7 @@ namespace HTCoreServiceApp.Communicate.DriverSiemensS7
//object _lock = new(); //object _lock = new();
Task task = Task.Factory.StartNew(() => Task task = Task.Run(() =>
{ {
lock (_lock) lock (_lock)
{ {
@ -230,40 +231,48 @@ namespace HTCoreServiceApp.Communicate.DriverSiemensS7
byte[] buffer = new byte[amount]; byte[] buffer = new byte[amount];
int pdu = PLCHelper.GetSiemensPDU(PLCType) * 2 - 18; int pdu = PLCHelper.GetSiemensPDU(PLCType) * 2 - 18;
int Result = 0; int Result = 0;
if (amount <= pdu)
{
Result = ReadArea(Area, DBNumber, startAdress, amount, WordLen, ref buffer); //byte[] bufferr = new byte[amount];
} Result = ReadArea(Area, DBNumber, startAdress, amount, WordLen, ref buffer);
else
{
//int additional = amount- pdu; //if (amount <= pdu)
int forCount = (int)Math.Ceiling(float.Parse(((float)amount / (float)pdu).ToString())); //{
List<byte[]> bytes= new List<byte[]>(); // Result = ReadArea(Area, DBNumber, startAdress, amount, WordLen, ref buffer);
for (int i = 0; i < forCount; i++) //}
{ //else
if ((amount - (i * pdu)) > pdu) //{
{ // //int additional = amount- pdu;
int start = startAdress + i * pdu; // int forCount = (int)Math.Ceiling(float.Parse(((float)amount / (float)pdu).ToString()));
byte[] bytes1 = new byte[pdu]; // List<byte[]> bytes= new List<byte[]>();
Result = ReadArea(Area, DBNumber, start, pdu, WordLen, ref bytes1); // for (int i = 0; i < forCount; i++)
bytes.Add(bytes1); // {
} // if ((amount - (i * pdu)) > pdu)
else // {
{ // int start = startAdress + i * pdu;
int start = startAdress + i * pdu; // byte[] bytes1 = new byte[pdu];
byte[] bytes1 = new byte[amount - (i * pdu)]; // Result = ReadArea(Area, DBNumber, start, pdu, WordLen, ref bytes1);
Result = ReadArea(Area, DBNumber, start, amount - (i * pdu), WordLen, ref bytes1); // bytes.Add(bytes1);
bytes.Add(bytes1); // }
} // else
Thread.Sleep(3); // {
} // int start = startAdress + i * pdu;
var byetstemp = Array.Empty<byte>(); // byte[] bytes1 = new byte[amount - (i * pdu)];
foreach (var buff in bytes) // Result = ReadArea(Area, DBNumber, start, amount - (i * pdu), WordLen, ref bytes1);
{ // bytes.Add(bytes1);
byetstemp = byetstemp.Concat(buff).ToArray(); // }
} // Thread.Sleep(3);
buffer = byetstemp; // }
} // var byetstemp = Array.Empty<byte>();
// foreach (var buff in bytes)
// {
// byetstemp = byetstemp.Concat(buff).ToArray();
// }
// buffer = byetstemp;
//}
var tcollend = DateTime.Now; var tcollend = DateTime.Now;
var timecolldiff = tcollend - tcollstart; var timecolldiff = tcollend - tcollstart;
Console.WriteLine($"S7读取耗时{timecolldiff},设备:{DriverName}变量组:{scanTagsPara.GroupName}"); Console.WriteLine($"S7读取耗时{timecolldiff},设备:{DriverName}变量组:{scanTagsPara.GroupName}");
@ -275,8 +284,11 @@ namespace HTCoreServiceApp.Communicate.DriverSiemensS7
var t1 = DateTime.Now; var t1 = DateTime.Now;
Console.WriteLine(string.Join(",", buffer) + "S7读取完成DB" + DBNumber.ToString() + ",字节数:" + amount.ToString()); Console.WriteLine(string.Join(",", buffer) + "S7读取完成DB" + DBNumber.ToString() + ",字节数:" + 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);
var ss = new DataExtract();
long hdatetime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
Task.Run(()=> Task.Run(()=>
DataExtract.DataExtractCAsync(scanTagsPara.ScanTags, scanTagsPara.SystemName, scanTagsPara.GroupName, scanTagsPara.DriverName, buffer, addressArg)
ss.DataExtractCAsync(scanTagsPara.ScanTags, scanTagsPara.SystemName, scanTagsPara.GroupName, scanTagsPara.DriverName, buffer, addressArg, hdatetime)
); );
var t2 = DateTime.Now; var t2 = DateTime.Now;
var timediff = t2- t1; var timediff = t2- t1;
@ -424,7 +436,9 @@ namespace HTCoreServiceApp.Communicate.DriverSiemensS7
var t1 = DateTime.Now; var t1 = DateTime.Now;
Console.WriteLine(string.Join(",", buffer) + "S7<工艺设定参数>读取完成DB" + DBNumber.ToString() + ",字节数:" + amount.ToString()); Console.WriteLine(string.Join(",", buffer) + "S7<工艺设定参数>读取完成DB" + DBNumber.ToString() + ",字节数:" + 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);
DataExtract.DataExtractCAsync(scanTagsPara.ScanTags, scanTagsPara.SystemName, scanTagsPara.GroupName, scanTagsPara.DriverName, buffer, addressArg); var ss = new DataExtract();
long hdatetime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
ss.DataExtractCAsync(scanTagsPara.ScanTags, scanTagsPara.SystemName, scanTagsPara.GroupName, scanTagsPara.DriverName, buffer, addressArg, hdatetime);
var t2 = DateTime.Now; var t2 = DateTime.Now;
var timediff = t2 - t1; var timediff = t2 - t1;
Console.WriteLine($"S7<工艺设定参数>缓存和储存耗时:{timediff}"); Console.WriteLine($"S7<工艺设定参数>缓存和储存耗时:{timediff}");
@ -474,7 +488,7 @@ namespace HTCoreServiceApp.Communicate.DriverSiemensS7
//Connect(); //Connect();
return -1; return -1;
} }
Thread.Sleep(50); Thread.Sleep(10);
} }
return recode; return recode;

View File

@ -16,15 +16,16 @@ namespace HTCoreServiceApp.DataHandle
{ {
public class DataExtract public class DataExtract
{ {
public static async void DataExtractCAsync(List<MetaTag> metaTags, string systenName, string groupName, string driverName, byte[] bytes, AddressArg addressArg) public async void DataExtractCAsync(List<MetaTag> metaTags, string systenName, string groupName, string driverName, byte[] bytes, AddressArg addressArg, long hdatetime)
{ {
string res = await DataExtractConsecutive(metaTags, systenName, groupName, driverName, bytes, addressArg); string res = await DataExtractConsecutive(metaTags, systenName, groupName, driverName, bytes, addressArg, hdatetime);
} }
public static Task <string> DataExtractConsecutive(List<MetaTag> metaTags, string systenName, string groupName, string driverName, byte[] bytes, AddressArg addressArg) public Task <string> DataExtractConsecutive(List<MetaTag> metaTags, string systenName, string groupName, string driverName, byte[] bytes, AddressArg addressArg, long hdatetime)
{ {
return Task.Run(() => return Task.Run(() =>
{ {
long hdatetime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); Console.WriteLine($"{systenName}_{driverName}_{groupName}开始执行储存和缓存 时间 {hdatetime}XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
//long hdatetime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
List<MetaTagValue> listMetaTagValue = new List<MetaTagValue>(); List<MetaTagValue> listMetaTagValue = new List<MetaTagValue>();
try try
{ {
@ -66,17 +67,19 @@ namespace HTCoreServiceApp.DataHandle
//异步缓存 //异步缓存
Task.Run(() => var task1 = Task.Run(() =>
DataLiving.DataLive2CaChe(listMetaTagValue, driverName, groupName) DataLiving.DataLive2CaChe(listMetaTagValue, driverName, groupName)
); );
//异步储存 //异步储存
Task.Run(()=> var task2 = Task.Run(()=>
DataStorage.DataSave2DB(listMetaTagValue, systenName, driverName, groupName, hdatetime) DataStorage.DataSave2DB(listMetaTagValue, systenName, driverName, groupName, hdatetime)
); );
//Task.WaitAll(new Task[] {task1,task2});
return "0"; return "0";
} }
catch (Exception ex) catch (Exception ex)
@ -171,7 +174,7 @@ namespace HTCoreServiceApp.DataHandle
switch (dataType) switch (dataType)
{ {
case (short)DataType.FLOAT: case (short)DataType.FLOAT:
return S7.GetRealAt(bytes, address_int).ToString(); return S7.GetRealAt(bytes, address_int).ToString("0.########");
case (short)DataType.BOOL: case (short)DataType.BOOL:
return S7.GetBitAt(bytes, address_int,address_dem).ToString(); return S7.GetBitAt(bytes, address_int,address_dem).ToString();
case (short)DataType.INT: case (short)DataType.INT:

View File

@ -27,10 +27,11 @@ namespace HTCoreServiceApp.DataHandle
//string tableName = $"{GroupName}_etldata"; //string tableName = $"{GroupName}_etldata";
string tableName = $"ED_{systenName}_{driverName}_{GroupName}"; string tableName = $"ED_{systenName}_{driverName}_{GroupName}";
sql += $"INSERT INTO dbo.{tableName} "; sql += $"INSERT INTO dbo.{tableName} ";
string field = "HDATETIME"; string field = "HDATETIME,HTIME";
string value = ""; string value = "";
var htime = hdatetime / 1000;
//long hdatetime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); //long hdatetime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
value += hdatetime; value += $"{hdatetime},{htime}";
foreach (MetaTagValue metaTagValue in metaTagvalues) foreach (MetaTagValue metaTagValue in metaTagvalues)
{ {
field += $",{metaTagValue.TagName}"; field += $",{metaTagValue.TagName}";
@ -63,11 +64,12 @@ namespace HTCoreServiceApp.DataHandle
} }
} }
sql += $"({field}) VALUES ({value});"; sql += $"({field}) VALUES ({value});";
var t9 = DateTime.Now;
//string ConnectionStrings = AESKeyEncrypt.DecryptDES(ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer")); //string ConnectionStrings = AESKeyEncrypt.DecryptDES(ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer"));
var db =new SqlSugarClient(StaticLibrary.DBConfig); var db =new SqlSugarClient(StaticLibrary.DBConfig);
try try
{ {
int sqlExecuteResult = db.Ado.ExecuteCommand(sql); //db.Database.ExecuteSqlRaw(sql); int sqlExecuteResult = db.Ado.ExecuteCommand(sql); //db.Database.ExecuteSqlRaw(sql);
if (sqlExecuteResult == 1) if (sqlExecuteResult == 1)
@ -81,7 +83,7 @@ namespace HTCoreServiceApp.DataHandle
db.Dispose(); db.Dispose();
var t6 = DateTime.Now; var t6 = DateTime.Now;
var timediff3 = t6 - t5; var timediff3 = t6 - t9;
Console.WriteLine($"{driverName}_{GroupName}数据储存耗时:{timediff3}"); Console.WriteLine($"{driverName}_{GroupName}数据储存耗时:{timediff3}");
return 0; return 0;
@ -99,15 +101,17 @@ namespace HTCoreServiceApp.DataHandle
var sqlcol = ""; var sqlcol = "";
foreach (var sysType in sysTypeList) foreach (var sysType in sysTypeList)
{ {
sqlcol = "";
var metaTagofSys = metaTagvalues.Where(x => x.SysType == sysType).ToList(); var metaTagofSys = metaTagvalues.Where(x => x.SysType == sysType).ToList();
string sql = ""; string sql = "";
//string tableName = $"{GroupName}_etldata"; //string tableName = $"{GroupName}_etldata";
string tableName = $"ED_{systenName}_{driverName}_{GroupName}_{sysType}"; string tableName = $"ED_{systenName}_{driverName}_{GroupName}_{sysType}";
sql += $"INSERT INTO dbo.{tableName} "; sql += $"INSERT INTO dbo.{tableName} ";
string field = "HDATETIME"; string field = "HDATETIME,HTIME";
string value = ""; string value = "";
var htime = hdatetime / 1000;
//long hdatetime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); //long hdatetime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
value += hdatetime; value += $"{hdatetime},{htime}";
foreach (MetaTagValue metaTagValue in metaTagofSys) foreach (MetaTagValue metaTagValue in metaTagofSys)
{ {
field += $",{metaTagValue.TagName}"; field += $",{metaTagValue.TagName}";
@ -127,10 +131,19 @@ namespace HTCoreServiceApp.DataHandle
{ {
try try
{ {
d_value = decimal.Parse(metaTagValue.TagValue); //var t11 = DateTime.Now;
//d_value = decimal.Parse(metaTagValue.TagValue);
//var t12 = DateTime.Now;
//var timediff12 = t12 - t11;
//Console.WriteLine($"{driverName}_{GroupName}string转decimal耗时{timediff12}");
string s_d_value = string.Format("{0:#,0.########}", d_value).Replace(",", ""); //string s_d_value = string.Format("{0:#,0.########}", d_value).Replace(",", "");
value += $",{s_d_value}"; //var t13 = DateTime.Now;
//var timediff13 = t13 - t11;
//Console.WriteLine($"{driverName}_{GroupName}处理科学计数法耗时:{timediff13}");
value += $",{metaTagValue.TagValue}";
} }
catch catch
{ {
@ -141,60 +154,41 @@ namespace HTCoreServiceApp.DataHandle
} }
sql += $"({field}) VALUES ({value});"; sql += $"({field}) VALUES ({value});";
sqlcol += sql; sqlcol += sql;
sqlcol += ";";
//string ConnectionStrings = ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer");
//var db = new ScadaContext(ConnectionStrings); var t7 = DateTime.Now;
var timediff7 = t7 - t5;
Console.WriteLine($"{driverName}_{GroupName}SQL数据整理耗时{timediff7}");
//string ConnectionStrings = AESKeyEncrypt.DecryptDES(ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer"));
var db = new SqlSugarClient(StaticLibrary.DBConfig);
var t9 = DateTime.Now;
try
{
int sqlExecuteResult = db.Ado.ExecuteCommand(sqlcol); //db.Database.ExecuteSqlRaw(sqlcol);
if (sqlExecuteResult == 1)
{
Console.WriteLine($"{driverName}_{GroupName}写入成功:{sqlExecuteResult}行");
}
else
{
Console.WriteLine($"{driverName}_{GroupName}写入失败:{sqlExecuteResult}");
}
db.Dispose();
//return 0;
}
catch (Exception ex)
{
Console.WriteLine($"{driverName}_{GroupName}写入出错:{ex}");
db.Dispose();
//return -1;
}
var t6 = DateTime.Now;
var timediff3 = t6 - t9;
Console.WriteLine($"{driverName}_{GroupName}数据储存耗时:{timediff3}");
//try
//{
// int sqlExecuteResult = db.Database.ExecuteSqlRaw(sql);
// if (sqlExecuteResult == 1)
// {
// Console.WriteLine($"{tableName}写入成功:{sqlExecuteResult}行");
// }
// else
// {
// Console.WriteLine($"{tableName}写入失败:{sqlExecuteResult}");
// }
// db.Dispose();
// //return 0;
//}
//catch (Exception ex)
//{
// Console.WriteLine($"{tableName}写入出错:{ex}");
// db.Dispose();
// continue;
// //return -1;
//}
} }
//string ConnectionStrings = AESKeyEncrypt.DecryptDES(ConfigHelper.ReadAppSettings("ConnectionStrings", "SqlServer"));
var db = new SqlSugarClient(StaticLibrary.DBConfig);
try
{
int sqlExecuteResult = db.Ado.ExecuteCommand(sqlcol); //db.Database.ExecuteSqlRaw(sqlcol);
if (sqlExecuteResult == sysTypeList.Count)
{
Console.WriteLine($"{driverName}_{GroupName}写入成功:{sqlExecuteResult}行");
}
else
{
Console.WriteLine($"{driverName}_{GroupName}写入失败:{sqlExecuteResult}");
}
db.Dispose();
//return 0;
}
catch (Exception ex)
{
Console.WriteLine($"{driverName}_{GroupName}写入出错:{ex}");
db.Dispose();
//return -1;
}
var t6 = DateTime.Now;
var timediff3 = t6 - t5;
Console.WriteLine($"{driverName}_{GroupName}数据储存耗时:{timediff3}");
return 0; return 0;

View File

@ -1,4 +1,5 @@
using HTCoreServiceApp.Common; using HTCoreServiceApp.Common;
using HTCoreServiceApp.DataHandle;
using HTCoreServiceApp.WebApi; using HTCoreServiceApp.WebApi;
using Microsoft.AspNetCore.Http.Headers; using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Http.HttpResults;
@ -41,7 +42,7 @@ namespace HTCoreServiceApp
//builder.Services.AddEndpointsApiExplorer(); //builder.Services.AddEndpointsApiExplorer();
builder.Services.AddScoped<DataLiveCache>();//<2F><><EFBFBD><EFBFBD>api<70><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD> builder.Services.AddScoped<DataLiveCache>();//<2F><><EFBFBD><EFBFBD>api<70><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>
builder.Services.AddTransient<DataExtract>();
builder.Services.AddSingleton<DeviceControl>(); builder.Services.AddSingleton<DeviceControl>();
var app = builder.Build(); var app = builder.Build();