Compare commits

...

6 Commits

Author SHA1 Message Date
Diego
8e938f18be 去除不必要的控制台日志输出 2025-05-28 16:52:51 +08:00
Diego
ab1b364c54 fix: 同步插件反写空错误 2025-05-28 12:30:16 +08:00
Diego
5ec65b2fb0 10.6.29 2025-05-28 10:47:31 +08:00
Diego
926eced724 10.6.28 2025-05-27 17:20:05 +08:00
Diego
f7f8802272 2025-05-27 13:19:39 +08:00
Diego
c6910dff02 update src/Gateway/ThingsGateway.Gateway.Application/ThingsGateway.Gateway.Application.csproj.
Signed-off-by: Diego <2248356998@qq.com>
2025-05-27 01:37:29 +00:00
19 changed files with 95 additions and 50 deletions

View File

@@ -28,7 +28,7 @@ public class UserIdProvider : IUserIdProvider
if (UserId > 0)
{
return $"{UserId}{SysHub.Separate}{YitIdHelper.NextId()}";//返回用户ID
return $"{UserId}{SysHub.Separate}{CommonUtils.GetSingleId()}";//返回用户ID
}
return connection.ConnectionId;

View File

@@ -101,10 +101,13 @@ public class Startup : AppStartup
try
{
using var db = DbContext.GetDB<SysOperateLog>();
if (!db.DbMaintenance.IsAnyIndex("idx_operatelog_optime_date"))
if (db.CurrentConnectionConfig.DbType == SqlSugar.DbType.Sqlite)
{
var indexsql = "CREATE INDEX idx_operatelog_optime_date ON sys_operatelog(strftime('%Y-%m-%d', OpTime));";
db.Ado.ExecuteCommand(indexsql);
if (!db.DbMaintenance.IsAnyIndex("idx_operatelog_optime_date"))
{
var indexsql = "CREATE INDEX idx_operatelog_optime_date ON sys_operatelog(strftime('%Y-%m-%d', OpTime));";
db.Ado.ExecuteCommand(indexsql);
}
}
}
catch { }

View File

@@ -29,7 +29,6 @@ using ThingsGateway.Admin.Application;
using ThingsGateway.Admin.Razor;
using ThingsGateway.Extension;
using ThingsGateway.NewLife.Caching;
using ThingsGateway.NewLife.Extension;
namespace ThingsGateway.AdminServer;
@@ -161,7 +160,8 @@ public class Startup : AppStartup
{
options.WriteFilter = (logMsg) =>
{
if (logMsg.Message.IsNullOrEmpty()) return false;
if (App.HostApplicationLifetime.ApplicationStopping.IsCancellationRequested && logMsg.LogLevel >= LogLevel.Warning) return false;
if (string.IsNullOrEmpty(logMsg.Message)) return false;
else return true;
};

View File

@@ -71,13 +71,25 @@ public static class App
/// </summary>
public static IServiceProvider RootServices => InternalApp.RootServices;
private static IHostApplicationLifetime hostApplicationLifetime;
public static IHostApplicationLifetime HostApplicationLifetime
{
get
{
if ((hostApplicationLifetime == null))
{
hostApplicationLifetime = RootServices?.GetService<IHostApplicationLifetime>();
}
return hostApplicationLifetime;
}
}
private static IStringLocalizerFactory? stringLocalizerFactory;
/// <summary>
/// 本地化服务工厂
/// </summary>
public static IStringLocalizerFactory? StringLocalizerFactory
{
get
{

View File

@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<PluginVersion>10.6.26</PluginVersion>
<ProPluginVersion>10.6.26</ProPluginVersion>
<PluginVersion>10.6.31</PluginVersion>
<ProPluginVersion>10.6.31</ProPluginVersion>
<AuthenticationVersion>2.1.8</AuthenticationVersion>
</PropertyGroup>

View File

@@ -61,14 +61,14 @@ internal sealed class HeartbeatAndReceivePlugin : PluginBase, ITcpConnectedPlugi
{
return;//此处可判断,如果为服务器,则不用使用心跳。
}
if (HeartbeatTime > 0)
SendHeartbeat = true;
HeartbeatTime = Math.Max(HeartbeatTime, 1000);
if (DtuId.IsNullOrWhiteSpace()) return;
if (client is ITcpClient tcpClient)
{
SendHeartbeat = true;
await tcpClient.SendAsync(DtuIdByte).ConfigureAwait(false);
if (Task == null)

View File

@@ -518,7 +518,7 @@ internal sealed class PluginService : IPluginService
{
var fileInfo = new FileInfo(path);
if (fileInfo.Exists)
fileInfo.MoveTo($"{path}{YitIdHelper.NextId()}{DelEx}", true);
fileInfo.MoveTo($"{path}{CommonUtils.GetSingleId()}{DelEx}", true);
else
return false;
return true;

View File

@@ -133,10 +133,14 @@ public class Startup : AppStartup
try
{
using var db = DbContext.GetDB<BackendLog>();
if (!db.DbMaintenance.IsAnyIndex("idx_backendlog_logtime_date"))
if (db.CurrentConnectionConfig.DbType == SqlSugar.DbType.Sqlite)
{
var indexsql = "CREATE INDEX idx_backendlog_logtime_date ON backend_log(strftime('%Y-%m-%d', LogTime));";
db.Ado.ExecuteCommand(indexsql);
if (!db.DbMaintenance.IsAnyIndex("idx_backendlog_logtime_date"))
{
var indexsql = "CREATE INDEX idx_backendlog_logtime_date ON backend_log(strftime('%Y-%m-%d', LogTime));";
db.Ado.ExecuteCommand(indexsql);
}
}
}
catch { }
@@ -144,10 +148,14 @@ public class Startup : AppStartup
try
{
using var db = DbContext.GetDB<RpcLog>();
if (!db.DbMaintenance.IsAnyIndex("idx_rpclog_logtime_date"))
if (db.CurrentConnectionConfig.DbType == SqlSugar.DbType.Sqlite)
{
var indexsql = "CREATE INDEX idx_rpclog_logtime_date ON rpc_log(strftime('%Y-%m-%d', LogTime));";
db.Ado.ExecuteCommand(indexsql);
if (!db.DbMaintenance.IsAnyIndex("idx_rpclog_logtime_date"))
{
var indexsql = "CREATE INDEX idx_rpclog_logtime_date ON rpc_log(strftime('%Y-%m-%d', LogTime));";
db.Ado.ExecuteCommand(indexsql);
}
}
}
catch { }

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(SolutionDir)Version.props" />
<Import Project="$(SolutionDir)PackNuget.props" />
@@ -10,8 +10,7 @@
<PackageReference Include="Rougamo.Fody" Version="5.0.0" />
<PackageReference Include="TouchSocket.Dmtp" Version="3.1.5" />
<PackageReference Include="TouchSocket.WebApi.Swagger" Version="3.1.5" />
<!--<PackageReference Include="ThingsGateway.Authentication" Version="$(AuthenticationVersion)" />-->
<ProjectReference Include="..\..\PluginPro\ThingsGateway.Authentication\ThingsGateway.Authentication.csproj" />
<PackageReference Include="ThingsGateway.Authentication" Version="$(AuthenticationVersion)" />
</ItemGroup>

View File

@@ -10,6 +10,7 @@
using Mapster;
using ThingsGateway.Admin.Application;
using ThingsGateway.Gateway.Application;
using Yitter.IdGenerator;
@@ -54,14 +55,14 @@ public partial class ChannelCopyComponent
for (int i = 0; i < CopyCount; i++)
{
Channel channel = Model.Adapt<Channel>();
channel.Id = YitIdHelper.NextId();
channel.Id = CommonUtils.GetSingleId();
channel.Name = $"{CopyChannelNamePrefix}{CopyChannelNameSuffixNumber + i}";
int index = 0;
foreach (var item in Devices)
{
Device device = item.Key.Adapt<Device>();
device.Id = YitIdHelper.NextId();
device.Id = CommonUtils.GetSingleId();
device.Name = $"{channel.Name}_{CopyDeviceNamePrefix}{CopyDeviceNameSuffixNumber + (index++)}";
device.ChannelId = channel.Id;
List<Variable> variables = new();
@@ -69,7 +70,7 @@ public partial class ChannelCopyComponent
foreach (var variable in item.Value)
{
Variable v = variable.Adapt<Variable>();
v.Id = YitIdHelper.NextId();
v.Id = CommonUtils.GetSingleId();
v.DeviceId = device.Id;
variables.Add(v);
}

View File

@@ -10,6 +10,7 @@
using Mapster;
using ThingsGateway.Admin.Application;
using ThingsGateway.Gateway.Application;
using Yitter.IdGenerator;
@@ -50,14 +51,14 @@ public partial class DeviceCopyComponent
for (int i = 0; i < CopyCount; i++)
{
Device device = Model.Adapt<Device>();
device.Id = YitIdHelper.NextId();
device.Id = CommonUtils.GetSingleId();
device.Name = $"{CopyDeviceNamePrefix}{CopyDeviceNameSuffixNumber + i}";
List<Variable> variables = new();
foreach (var item in Variables)
{
Variable v = item.Adapt<Variable>();
v.Id = YitIdHelper.NextId();
v.Id = CommonUtils.GetSingleId();
v.DeviceId = device.Id;
variables.Add(v);
}

View File

@@ -10,6 +10,7 @@
using Mapster;
using ThingsGateway.Admin.Application;
using ThingsGateway.Gateway.Application;
using Yitter.IdGenerator;
@@ -47,7 +48,7 @@ public partial class VariableCopyComponent
var variable = Model.Adapt<List<Variable>>();
foreach (var item in variable)
{
item.Id = YitIdHelper.NextId();
item.Id = CommonUtils.GetSingleId();
item.Name = $"{CopyVariableNamePrefix}{CopyVariableNameSuffixNumber + i}";
variables.Add(item);
}

View File

@@ -51,7 +51,7 @@ public class PackHelper
// 获取变量的位偏移量
//if (item.DataType == DataTypeEnum.Boolean)
item.Index = device.GetBitOffsetDefault(address);
if (item.DataType == DataTypeEnum.Byte)
if (item.DataType == DataTypeEnum.Byte&& !(item.ArrayLength>1))
item.Index += (item.Index % 2 == 0) ? 1 : -1;
}

View File

@@ -12,6 +12,7 @@ using Mapster;
using Newtonsoft.Json.Linq;
using ThingsGateway.Admin.Application;
using ThingsGateway.Extension.Generic;
using ThingsGateway.Foundation;
@@ -21,6 +22,8 @@ using TouchSocket.Dmtp.Rpc;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using Yitter.IdGenerator;
namespace ThingsGateway.Plugin.Synchronization;
@@ -60,11 +63,8 @@ public partial class Synchronization : BusinessBase, IRpcDriver
protected override async ValueTask ProtectedExecuteAsync(CancellationToken cancellationToken)
{
try
{
if (_driverPropertys.IsServer)
{
if (_tcpDmtpService.ServerState != ServerState.Running)
@@ -129,6 +129,7 @@ public partial class Synchronization : BusinessBase, IRpcDriver
// 如果 online 为 true表示设备在线
if (online)
{
var deviceRunTimes = CollectDevices.Where(a => a.Value.IsCollect == true).Select(a => a.Value).Adapt<List<DeviceDataWithValue>>();
@@ -258,10 +259,7 @@ public partial class Synchronization : BusinessBase, IRpcDriver
var data = await _tcpDmtpClient.GetDmtpRpcActor().InvokeTAsync<List<DataWithDatabase>>(
nameof(ReverseCallbackServer.GetData), waitInvoke).ConfigureAwait(false);
data.ForEach(a => a.Channel.Enable = false);
await GlobalData.ChannelRuntimeService.CopyAsync(data.Select(a => a.Channel).ToList(), data.SelectMany(a => a.DeviceVariables).ToDictionary(a => a.Device, a => a.Variables), true, cancellationToken).ConfigureAwait(false);
LogMessage?.LogTrace($"ForcedSync data success");
await Add(data, cancellationToken).ConfigureAwait(false);
}
}
@@ -277,10 +275,10 @@ public partial class Synchronization : BusinessBase, IRpcDriver
foreach (var item in _tcpDmtpService.Clients)
{
var data = await item.GetDmtpRpcActor().InvokeTAsync<List<DataWithDatabase>>(nameof(ReverseCallbackServer.GetData), waitInvoke).ConfigureAwait(false);
data.ForEach(a => a.Channel.Enable = false);
await GlobalData.ChannelRuntimeService.CopyAsync(data.Select(a => a.Channel).ToList(), data.SelectMany(a => a.DeviceVariables).ToDictionary(a => a.Device, a => a.Variables), true, cancellationToken).ConfigureAwait(false);
LogMessage?.LogTrace($"{item.GetIPPort()}: ForcedSync data success");
await Add(data, cancellationToken).ConfigureAwait(false);
}
}
@@ -300,6 +298,27 @@ public partial class Synchronization : BusinessBase, IRpcDriver
}
}
private async Task Add(List<DataWithDatabase> data, CancellationToken cancellationToken)
{
data.ForEach(a =>
{
a.Channel.Enable = false;
a.Channel.Id = CommonUtils.GetSingleId();
a.DeviceVariables.ForEach(b =>
{
b.Device.ChannelId = a.Channel.Id;
b.Device.Id = CommonUtils.GetSingleId();
b.Variables.ForEach(c =>
{
c.DeviceId = b.Device.Id;
c.Id = 0;
});
});
});
await GlobalData.ChannelRuntimeService.CopyAsync(data.Select(a => a.Channel).ToList(), data.SelectMany(a => a.DeviceVariables).ToDictionary(a => a.Device, a => a.Variables), true, cancellationToken).ConfigureAwait(false);
LogMessage?.LogTrace($"ForcedSync data success");
}
/// <summary>
/// 异步写入方法
@@ -321,12 +340,12 @@ public partial class Synchronization : BusinessBase, IRpcDriver
{
if (deviceDatas.TryGetValue(item.Key.DeviceName ?? string.Empty, out var variableDatas))
{
variableDatas.Add(item.Key.Name, item.Value?.ToString() ?? string.Empty);
variableDatas.TryAdd(item.Key.Name, item.Value?.ToString() ?? string.Empty);
}
else
{
deviceDatas.Add(item.Key.DeviceName ?? string.Empty, new());
deviceDatas[item.Key.DeviceName ?? string.Empty].Add(item.Key.Name, item.Value?.ToString() ?? string.Empty);
deviceDatas.TryAdd(item.Key.DeviceName ?? string.Empty, new());
deviceDatas[item.Key.DeviceName ?? string.Empty].TryAdd(item.Key.Name, item.Value?.ToString() ?? string.Empty);
}
}
@@ -383,7 +402,7 @@ public partial class Synchronization : BusinessBase, IRpcDriver
try
{
var data = await _tcpDmtpClient.GetDmtpRpcActor().InvokeTAsync<Dictionary<string, Dictionary<string, OperResult<object>>>>(
var data = await client.GetDmtpRpcActor().InvokeTAsync<Dictionary<string, Dictionary<string, OperResult<object>>>>(
nameof(ReverseCallbackServer.Rpc), waitInvoke, new Dictionary<string, Dictionary<string, string>>() { { item.Key, item.Value } }).ConfigureAwait(false);
dataResult.AddRange(data);
@@ -396,7 +415,7 @@ public partial class Synchronization : BusinessBase, IRpcDriver
foreach (var vItem in item.Value)
{
dataResult[item.Key].Add(vItem.Key, new OperResult<object>(ex));
dataResult[item.Key].TryAdd(vItem.Key, new OperResult<object>(ex));
}
}
}
@@ -407,7 +426,7 @@ public partial class Synchronization : BusinessBase, IRpcDriver
foreach (var vItem in item.Value)
{
dataResult[item.Key].Add(vItem.Key, new OperResult<object>("No online"));
dataResult[item.Key].TryAdd(vItem.Key, new OperResult<object>("No online"));
}
}

View File

@@ -15,7 +15,7 @@ namespace ThingsGateway.Plugin.Synchronization;
/// <summary>
/// <inheritdoc/>
/// </summary>
public class SynchronizationProperty : BusinessPropertyBase
public class SynchronizationProperty : BusinessPropertyBase, IBusinessPropertyAllVariableBase
{
[DynamicProperty]
public bool IsServer { get; set; } = true;

View File

@@ -37,7 +37,6 @@ using ThingsGateway.Debug;
using ThingsGateway.Extension;
using ThingsGateway.Gateway.Application;
using ThingsGateway.NewLife.Caching;
using ThingsGateway.NewLife.Extension;
namespace ThingsGateway.Server;
@@ -138,7 +137,8 @@ public class Startup : AppStartup
{
options.WriteFilter = (logMsg) =>
{
if (logMsg.Message.IsNullOrEmpty()) return false;
if (App.HostApplicationLifetime.ApplicationStopping.IsCancellationRequested && logMsg.LogLevel >= LogLevel.Warning) return false;
if (string.IsNullOrEmpty(logMsg.Message)) return false;
else return true;
};

View File

@@ -29,7 +29,6 @@ using ThingsGateway.Admin.Application;
using ThingsGateway.Admin.Razor;
using ThingsGateway.Extension;
using ThingsGateway.NewLife.Caching;
using ThingsGateway.NewLife.Extension;
namespace ThingsGateway.Server;
@@ -162,7 +161,8 @@ public class Startup : AppStartup
{
options.WriteFilter = (logMsg) =>
{
if (logMsg.Message.IsNullOrEmpty()) return false;
if (App.HostApplicationLifetime.ApplicationStopping.IsCancellationRequested && logMsg.LogLevel >= LogLevel.Warning) return false;
if (string.IsNullOrEmpty(logMsg.Message)) return false;
else return true;
};

View File

@@ -157,6 +157,7 @@ public class Startup : AppStartup
{
options.WriteFilter = (logMsg) =>
{
if (App.HostApplicationLifetime.ApplicationStopping.IsCancellationRequested && logMsg.LogLevel >= LogLevel.Warning) return false;
if (string.IsNullOrEmpty(logMsg.Message)) return false;
else return true;
};

View File

@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>10.6.26</Version>
<Version>10.6.31</Version>
</PropertyGroup>
<ItemGroup>