mirror of
https://gitee.com/ThingsGateway/ThingsGateway.git
synced 2025-10-24 04:17:08 +08:00
92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
#region copyright
|
||
//------------------------------------------------------------------------------
|
||
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
|
||
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
|
||
// 源代码使用协议遵循本仓库的开源协议及附加协议
|
||
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
|
||
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
|
||
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
|
||
// QQ群:605534569
|
||
//------------------------------------------------------------------------------
|
||
#endregion
|
||
|
||
using ThingsGateway.Foundation.Serial;
|
||
|
||
using TouchSocket.Core;
|
||
|
||
namespace ThingsGateway.Blazor;
|
||
|
||
/// <inheritdoc/>
|
||
public partial class SerialSessionPage
|
||
{
|
||
/// <summary>
|
||
/// 日志输出
|
||
/// </summary>
|
||
public Action<LogLevel, object, string, Exception> LogAction;
|
||
|
||
private TouchSocketConfig config;
|
||
|
||
private readonly SerialProperty serialProperty = new();
|
||
|
||
private SerialsSession SerialsSession { get; set; } = new();
|
||
|
||
/// <inheritdoc/>
|
||
public void Dispose()
|
||
{
|
||
SerialsSession.SafeDispose();
|
||
}
|
||
/// <summary>
|
||
/// 获取对象
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public SerialsSession GetSerialSession()
|
||
{
|
||
config?.Dispose();
|
||
config = new TouchSocketConfig();
|
||
var LogMessage = new TouchSocket.Core.LoggerGroup() { LogLevel = TouchSocket.Core.LogLevel.Trace };
|
||
LogMessage.AddLogger(new EasyLogger(LogOut) { LogLevel = TouchSocket.Core.LogLevel.Trace });
|
||
config.ConfigureContainer(a => a.RegisterSingleton<ILog>(LogMessage));
|
||
config.SetSerialProperty(serialProperty);
|
||
//载入配置
|
||
SerialsSession.Setup(config);
|
||
return SerialsSession;
|
||
}
|
||
private async Task ConnectAsync()
|
||
{
|
||
try
|
||
{
|
||
SerialsSession.Close();
|
||
await GetSerialSession().ConnectAsync();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogAction?.Invoke(LogLevel.Error, null, null, ex);
|
||
}
|
||
|
||
}
|
||
private void DisConnect()
|
||
{
|
||
try
|
||
{
|
||
SerialsSession.Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogAction?.Invoke(LogLevel.Error, null, null, ex);
|
||
}
|
||
}
|
||
/// <inheritdoc/>
|
||
protected override void OnInitialized()
|
||
{
|
||
config ??= new TouchSocketConfig();
|
||
var LogMessage = new TouchSocket.Core.LoggerGroup() { LogLevel = TouchSocket.Core.LogLevel.Trace };
|
||
LogMessage.AddLogger(new EasyLogger(LogOut) { LogLevel = TouchSocket.Core.LogLevel.Trace });
|
||
config.ConfigureContainer(a => a.RegisterSingleton<ILog>(LogMessage));
|
||
SerialsSession = new SerialsSession();
|
||
SerialsSession.Setup(config);
|
||
base.OnInitialized();
|
||
}
|
||
|
||
private void LogOut(LogLevel logLevel, object source, string message, Exception exception) => LogAction?.Invoke(logLevel, source, message, exception);
|
||
|
||
} |