Compare commits

...

2 Commits

Author SHA1 Message Date
Diego
4a3b5163f1 更新依赖 2025-06-09 09:29:41 +08:00
2248356998 qq.com
3a0946d357 优化单例db对象获取 2025-06-08 21:09:07 +08:00
21 changed files with 234 additions and 113 deletions

View File

@@ -40,6 +40,61 @@ public static class ConcurrentDictionaryExtensions
// 返回成功移除的项目数量
return count;
}
/// <summary>
/// 批量出队
/// </summary>
public static List<T> ToListWithDequeue<TKEY, T>(this ConcurrentDictionary<TKEY, T> values, int maxCount = 0)
{
if (maxCount <= 0)
{
maxCount = values.Count;
}
else
{
maxCount = Math.Min(maxCount, values.Count);
}
var list = new List<T>(maxCount);
if (maxCount == 0) return list;
var keys = values.Keys;
foreach (var key in keys)
{
if (maxCount-- <= 0) break;
if (values.TryRemove(key, out var result))
{
list.Add(result);
}
}
return list;
}
/// <summary>
/// 批量出队
/// </summary>
public static Dictionary<TKEY, T> ToDictWithDequeue<TKEY, T>(this ConcurrentDictionary<TKEY, T> values, int maxCount = 0)
{
if (maxCount <= 0)
{
maxCount = values.Count;
}
else
{
maxCount = Math.Min(maxCount, values.Count);
}
var dict = new Dictionary<TKEY, T>(maxCount);
if (maxCount == 0) return dict;
var keys = values.Keys;
foreach (var key in keys)
{
if (maxCount-- <= 0) break;
if (values.TryRemove(key, out var result))
{
dict.Add(key, result);
}
}
return dict;
}
}

View File

@@ -7,7 +7,6 @@ namespace SqlSugar
/// </summary>
public partial class SqlSugarScope : ISqlSugarClient, ITenant
{
private List<ConnectionConfig> _configs;
private Action<SqlSugarClient> _configAction;
@@ -15,25 +14,25 @@ namespace SqlSugar
{
SqlSugarClient result = null;
var key = _configs.GetHashCode().ToString();
StackTrace st = new StackTrace(true);
StackTrace st = new StackTrace(false);
var methods = st.GetFrames();
var isAsync = UtilMethods.IsAnyAsyncMethod(methods);
if (methods?.Length >= 0)
{
foreach (var method in methods.Take(35))
{
var refType = method.GetMethod()?.ReflectedType;
if (refType != null)
{
var getInterfaces = refType.Name.StartsWith('<') ? refType?.ReflectedType?.GetInterfaces() : refType?.GetInterfaces();
if (getInterfaces?.Any(it => it.Name.IsIn("IJob")) == true)
{
key = $"{key}IJob";
break;
}
}
}
}
//if (methods?.Length >= 0)
//{
// foreach (var method in methods.Take(35))
// {
// var refType = method.GetMethod()?.ReflectedType;
// if (refType != null)
// {
// var getInterfaces = refType.Name.StartsWith('<') ? refType?.ReflectedType?.GetInterfaces() : refType?.GetInterfaces();
// if (getInterfaces?.Any(it => it.Name.IsIn("IJob")) == true)
// {
// key = $"{key}IJob";
// break;
// }
// }
// }
//}
if (isAsync)
{
result = GetAsyncContext(key);

View File

@@ -8,7 +8,7 @@ namespace SqlSugar
public class SqlSugarScopeProvider : ISqlSugarClient
{
internal SqlSugarProvider conn;
internal string initThreadMainId;
internal int initThreadMainId;
internal string initkey = null;
StackFrame[] frames;
@@ -21,9 +21,9 @@ namespace SqlSugar
this.GetContext(true);
}
private static string GetCurrentThreadId()
private static int GetCurrentThreadId()
{
return Environment.CurrentManagedThreadId + "";
return Environment.CurrentManagedThreadId;
}
public SqlSugarProvider ScopedContext { get { return GetContext(); } }
@@ -79,7 +79,7 @@ namespace SqlSugar
{
SqlSugarProvider result = null;
var key = GetKey(); ;
StackTrace st = new StackTrace(true);
StackTrace st = new StackTrace(false);
var methods = st.GetFrames();
var isAsync = UtilMethods.IsAnyAsyncMethod(methods);
if (isAsync)
@@ -101,24 +101,24 @@ namespace SqlSugar
var key = "SqlSugarProviderScope_" + conn.CurrentConnectionConfig.ConfigId;
if (frames == null)
{
frames = new StackTrace(true).GetFrames();
}
if (true)
{
foreach (var method in frames.Take(35))
{
var refType = method.GetMethod()?.ReflectedType;
if (refType != null)
{
var getInterfaces = refType.Name.StartsWith('<') ? refType?.ReflectedType?.GetInterfaces() : refType?.GetInterfaces();
if (getInterfaces?.Any(it => it.Name.IsIn("IJob")) == true)
{
key = $"{key}IJob";
break;
}
}
}
frames = new StackTrace(false).GetFrames();
}
//if (true)
//{
// foreach (var method in frames.Take(35))
// {
// var refType = method.GetMethod()?.ReflectedType;
// if (refType != null)
// {
// var getInterfaces = refType.Name.StartsWith('<') ? refType?.ReflectedType?.GetInterfaces() : refType?.GetInterfaces();
// if (getInterfaces?.Any(it => it.Name.IsIn("IJob")) == true)
// {
// key = $"{key}IJob";
// break;
// }
// }
// }
//}
return key;
}

View File

@@ -1,22 +0,0 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>SqlSugarCore.Dm</id>
<version>1.0</version>
<authors>sunkaixuan</authors>
<owners>Landa</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>
<projectUrl>https://github.com/sunkaixuan/SqlSugar</projectUrl>
<iconUrl>https://secure.gravatar.com/avatar/a82c03402497b2e58fd65038a3699b30</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>dm sqlsugar .net core</description>
<copyright>Copyright 2016</copyright>
<tags>Asp.net core orm</tags>
<dependencies>
</dependencies>
</metadata>
<files>
<file src="E:\Code\SqlSugar\Src\Asp.NetCore2\SqlSeverTest\SqlSugar\dll\DmProvider.dll" target="lib\netstandard2.0"></file>
</files>
</package>

View File

@@ -1,22 +0,0 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>SqlSugarCore.Kdbndp</id>
<version>1.0</version>
<authors>sunkaixuan</authors>
<owners>Landa</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>
<projectUrl>https://github.com/sunkaixuan/SqlSugar</projectUrl>
<iconUrl>https://secure.gravatar.com/avatar/a82c03402497b2e58fd65038a3699b30</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>dm sqlsugar .net core</description>
<copyright>Copyright 2016</copyright>
<tags>Asp.net core orm</tags>
<dependencies>
</dependencies>
</metadata>
<files>
<file src="E:\Code\SqlSugar\Src\Asp.NetCore2\SqlSeverTest\SqlSugar\dll\Kdbndp.dll" target="lib\netstandard2.0"></file>
</files>
</package>

View File

@@ -1 +0,0 @@
%~dp0nuget.exe pack %~dp0Dm.nuspec -OutputDirectory %~dp0

View File

@@ -1 +0,0 @@
%~dp0nuget.exe pack %~dp0Kdbndp.nuspec -OutputDirectory %~dp0

View File

@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<PluginVersion>10.7.35</PluginVersion>
<ProPluginVersion>10.7.35</ProPluginVersion>
<PluginVersion>10.7.36</PluginVersion>
<ProPluginVersion>10.7.36</ProPluginVersion>
<AuthenticationVersion>2.5.0</AuthenticationVersion>
</PropertyGroup>

View File

@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CS-Script" Version="4.9.6" />
<PackageReference Include="CS-Script" Version="4.9.7" />
</ItemGroup>
<ItemGroup>

View File

@@ -10,8 +10,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="9.0.5" />
<PackageReference Include="TouchSocket" Version="3.1.6" />
<PackageReference Include="TouchSocket.SerialPorts" Version="3.1.6" />
<PackageReference Include="TouchSocket" Version="3.1.7" />
<PackageReference Include="TouchSocket.SerialPorts" Version="3.1.7" />
</ItemGroup>
<ItemGroup>

View File

@@ -244,6 +244,31 @@ public abstract class BusinessBaseWithCacheIntervalAlarmModel<VarModel, DevModel
AlarmChange(alarmVariable);
}
}
public override void PauseThread(bool pause)
{
lock (this)
{
var oldV = CurrentDevice.Pause;
base.PauseThread(pause);
if (!pause && oldV != pause)
{
GlobalData.ReadOnlyRealAlarmIdVariables?.ForEach(a =>
{
AlarmChange(a.Value);
});
CollectDevices?.ForEach(a =>
{
if (a.Value.DeviceStatus == DeviceStatusEnum.OnLine && _businessPropertyWithCacheInterval.BusinessUpdateEnum != BusinessUpdateEnum.Interval)
DeviceStatusChange(a.Value, a.Value.Adapt<DeviceBasicData>());
});
IdVariableRuntimes.ForEach(a =>
{
if (a.Value.IsOnline && _businessPropertyWithCacheInterval.BusinessUpdateEnum != BusinessUpdateEnum.Interval)
VariableValueChange(a.Value, a.Value.Adapt<VariableBasicData>());
});
}
}
}
/// <summary>
/// 当设备状态发生变化时触发此事件处理方法。该方法内部会检查是否需要进行设备上传,如果需要,则调用 <see cref="DeviceChange(DeviceRuntime, DeviceBasicData)"/> 方法。

View File

@@ -257,4 +257,27 @@ public abstract class BusinessBaseWithCacheIntervalDeviceModel<VarModel, DevMode
VariableChange(variableRuntime, variable);
}
}
public override void PauseThread(bool pause)
{
lock (this)
{
var oldV = CurrentDevice.Pause;
base.PauseThread(pause);
if (!pause && oldV != pause)
{
CollectDevices?.ForEach(a =>
{
if (a.Value.DeviceStatus == DeviceStatusEnum.OnLine && _businessPropertyWithCacheInterval.BusinessUpdateEnum != BusinessUpdateEnum.Interval)
DeviceStatusChange(a.Value, a.Value.Adapt<DeviceBasicData>());
});
IdVariableRuntimes.ForEach(a =>
{
if (a.Value.IsOnline && _businessPropertyWithCacheInterval.BusinessUpdateEnum != BusinessUpdateEnum.Interval)
VariableValueChange(a.Value, a.Value.Adapt<VariableBasicData>());
});
}
}
}
}

View File

@@ -175,4 +175,21 @@ public abstract class BusinessBaseWithCacheIntervalVariableModel<VarModel> : Bus
VariableChange(variableRuntime, variable);
}
}
public override void PauseThread(bool pause)
{
lock (this)
{
var oldV = CurrentDevice.Pause;
base.PauseThread(pause);
if (!pause && oldV != pause)
{
IdVariableRuntimes.ForEach(a =>
{
if (a.Value.IsOnline && _businessPropertyWithCacheInterval.BusinessUpdateEnum != BusinessUpdateEnum.Interval)
VariableValueChange(a.Value, a.Value.Adapt<VariableBasicData>());
});
}
}
}
}

View File

@@ -117,7 +117,7 @@ public abstract class DriverBase : DisposableObject, IDriver
/// 暂停
/// </summary>
/// <param name="pause">暂停</param>
public void PauseThread(bool pause)
public virtual void PauseThread(bool pause)
{
lock (this)
{

View File

@@ -8,8 +8,8 @@
<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="Rougamo.Fody" Version="5.0.0" />
<PackageReference Include="TouchSocket.Dmtp" Version="3.1.6" />
<PackageReference Include="TouchSocket.WebApi.Swagger" Version="3.1.6" />
<PackageReference Include="TouchSocket.Dmtp" Version="3.1.7" />
<PackageReference Include="TouchSocket.WebApi.Swagger" Version="3.1.7" />
<PackageReference Include="ThingsGateway.Authentication" Version="$(AuthenticationVersion)" />
<!--<ProjectReference Include="..\..\PluginPro\ThingsGateway.Authentication\ThingsGateway.Authentication.csproj" />-->

View File

@@ -40,6 +40,21 @@ public partial class SqlHistoryAlarm : BusinessBaseWithCacheVariableModel<Histor
return;
AddQueueVarModel(new CacheDBItem<HistoryAlarm>(alarmVariable.Adapt<HistoryAlarm>(_config)));
}
public override void PauseThread(bool pause)
{
lock (this)
{
var oldV = CurrentDevice.Pause;
base.PauseThread(pause);
if (!pause && oldV != pause)
{
GlobalData.ReadOnlyRealAlarmIdVariables?.ForEach(a =>
{
AlarmWorker_OnAlarmChanged(a.Value);
});
}
}
}
private async ValueTask<OperResult> InserableAsync(List<HistoryAlarm> dbInserts, CancellationToken cancellationToken)
{

View File

@@ -16,7 +16,6 @@ using SqlSugar;
using System.Collections.Concurrent;
using ThingsGateway.Extension;
using ThingsGateway.Foundation.Modbus;
using ThingsGateway.Gateway.Application;
using ThingsGateway.NewLife;
@@ -33,7 +32,7 @@ public class ModbusSlave : BusinessBase
{
private readonly ModbusSlaveProperty _driverPropertys = new();
private readonly ConcurrentQueue<(string, VariableRuntime)> _modbusVariableQueue = new();
private readonly ConcurrentDictionary<string, VariableRuntime> ModbusVariableQueue = new();
private readonly ModbusSlaveVariableProperty _variablePropertys = new();
@@ -124,7 +123,7 @@ public class ModbusSlave : BusinessBase
public override async Task AfterVariablesChangedAsync(CancellationToken cancellationToken)
{
await base.AfterVariablesChangedAsync(cancellationToken).ConfigureAwait(false);
_modbusVariableQueue?.Clear();
ModbusVariableQueue?.Clear();
IdVariableRuntimes.ForEach(a =>
{
VariableValueChange(a.Value, default);
@@ -145,7 +144,7 @@ public class ModbusSlave : BusinessBase
protected override void Dispose(bool disposing)
{
ModbusVariables?.Clear();
_modbusVariableQueue?.Clear();
ModbusVariableQueue?.Clear();
GlobalData.VariableValueChangeEvent -= VariableValueChange;
_plc?.SafeDispose();
base.Dispose(disposing);
@@ -180,19 +179,19 @@ public class ModbusSlave : BusinessBase
await Task.Delay(10000, cancellationToken).ConfigureAwait(false);
}
}
var list = _modbusVariableQueue.ToListWithDequeue();
var list = ModbusVariableQueue.ToDictWithDequeue();
foreach (var item in list)
{
if (cancellationToken.IsCancellationRequested)
break;
var type = item.Item2.GetPropertyValue(CurrentDevice.Id, nameof(ModbusSlaveVariableProperty.DataType));
var type = item.Value.GetPropertyValue(CurrentDevice.Id, nameof(ModbusSlaveVariableProperty.DataType));
if (Enum.TryParse(type, out DataTypeEnum result))
{
await _plc.WriteAsync(item.Item1, JToken.FromObject(item.Item2.Value), result, cancellationToken).ConfigureAwait(false);
await _plc.WriteAsync(item.Key, JToken.FromObject(item.Value.Value), result, cancellationToken).ConfigureAwait(false);
}
else
{
await _plc.WriteAsync(item.Item1, JToken.FromObject(item.Item2.Value), item.Item2.DataType, cancellationToken).ConfigureAwait(false);
await _plc.WriteAsync(item.Key, JToken.FromObject(item.Value.Value), item.Value.DataType, cancellationToken).ConfigureAwait(false);
}
}
@@ -259,7 +258,25 @@ public class ModbusSlave : BusinessBase
var address = variableRuntime.GetPropertyValue(DeviceId, nameof(_variablePropertys.ServiceAddress));
if (address != null && variableRuntime.Value != null)
{
_modbusVariableQueue?.Enqueue((address, variableRuntime));
ModbusVariableQueue?.AddOrUpdate(address, address => variableRuntime, (address, addvalue) => variableRuntime);
}
}
public override void PauseThread(bool pause)
{
lock (this)
{
var oldV = CurrentDevice.Pause;
base.PauseThread(pause);
if (!pause && oldV != pause)
{
IdVariableRuntimes.ForEach(a =>
{
VariableValueChange(a.Value, null);
});
}
}
}
}

View File

@@ -21,6 +21,7 @@ using System.Collections.Concurrent;
using ThingsGateway.Extension;
using ThingsGateway.Extension.Generic;
using ThingsGateway.Gateway.Application;
using ThingsGateway.NewLife.Extension;
using ThingsGateway.NewLife.Threading;
using TouchSocket.Core;
@@ -47,7 +48,7 @@ public partial class OpcUaServer : BusinessBase
protected override BusinessPropertyBase _businessPropertyBase => _driverPropertys;
protected IStringLocalizer Localizer { get; private set; }
private ConcurrentQueue<VariableBasicData> CollectVariableRuntimes { get; set; } = new();
private ConcurrentDictionary<long, VariableBasicData> CollectVariableRuntimes { get; set; } = new();
private static readonly string[] separator = new string[] { ";" };
@@ -183,13 +184,9 @@ public partial class OpcUaServer : BusinessBase
await Task.Delay(10000, cancellationToken).ConfigureAwait(false);
}
}
var data = CollectVariableRuntimes.ToListWithDequeue();
if (data.Count > 0)
var varList = CollectVariableRuntimes.ToListWithDequeue();
if (varList.Count > 0)
{
data.Reverse();
////变化推送
var varList = data.DistinctBy(a => a.Id).ToList();
if (varList?.Count > 0)
{
foreach (var item in varList)
@@ -411,17 +408,36 @@ public partial class OpcUaServer : BusinessBase
return config;
}
private void VariableValueChange(VariableRuntime variableRuntime, VariableBasicData variableData)
{
if (CurrentDevice.Pause)
return;
if (DisposedValue) return;
if (IdVariableRuntimes.ContainsKey(variableData.Id))
CollectVariableRuntimes.Enqueue(variableData);
CollectVariableRuntimes.AddOrUpdate(variableData.Id, id => variableData, (id, addValue) => variableData);
else
{
}
}
/// <summary>
/// 暂停
/// </summary>
/// <param name="pause">暂停</param>
public override void PauseThread(bool pause)
{
lock (this)
{
var oldV = CurrentDevice.Pause;
base.PauseThread(pause);
if (!pause && oldV != pause)
{
IdVariableRuntimes.ForEach(a =>
{
VariableValueChange(a.Value, a.Value.Adapt<VariableBasicData>());
});
}
}
}
}

View File

@@ -16,7 +16,7 @@
<ItemGroup>
<PackageReference Include="TouchSocket.Dmtp" Version="3.1.6" />
<PackageReference Include="TouchSocket.Dmtp" Version="3.1.7" />
</ItemGroup>

View File

@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>10.7.35</Version>
<Version>10.7.36</Version>
</PropertyGroup>
<ItemGroup>