91 lines
4.1 KiB
C#
91 lines
4.1 KiB
C#
using Prism.Ioc;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using XP.Hardware.Plc.Abstractions;
|
|
using XP.Hardware.Plc.Core;
|
|
using XP.Hardware.Plc.Services;
|
|
using XP.Hardware.PLC.Configs;
|
|
|
|
namespace XP.Hardware.PLC.Extensions
|
|
{
|
|
/// <summary>
|
|
/// PLC 服务扩展方法类 | PLC service extension methods class
|
|
/// </summary>
|
|
public static class PlcServiceExtensions
|
|
{
|
|
/// <summary>
|
|
/// 注册 PLC 服务到容器 | Register PLC services to container
|
|
/// </summary>
|
|
/// <param name="containerRegistry">容器注册器 | Container registry</param>
|
|
/// <returns>容器注册器,支持链式调用 | Container registry, supports method chaining</returns>
|
|
/// <exception cref="ArgumentNullException">当 containerRegistry 为 null 时抛出 | Thrown when containerRegistry is null</exception>
|
|
public static IContainerRegistry RegisterPlcServices(this IContainerRegistry containerRegistry)
|
|
{
|
|
if (containerRegistry == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(containerRegistry),
|
|
"容器注册器不能为空 | Container registry cannot be null");
|
|
}
|
|
|
|
// 注册 PLC 客户端接口和实现类(瞬态)| Register PLC client interface and implementation (transient)
|
|
containerRegistry.Register<IPlcClient, S7PlcClient>();
|
|
|
|
// 注册 PLC 服务为单例 | Register PLC service as singleton
|
|
containerRegistry.RegisterSingleton<PlcService>();
|
|
|
|
// 注册配置加载器(瞬态)| Register configuration loader (transient)
|
|
containerRegistry.Register<ConfigLoader>();
|
|
|
|
return containerRegistry;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化 PLC 服务 | Initialize PLC service
|
|
/// </summary>
|
|
/// <param name="containerProvider">容器提供者 | Container provider</param>
|
|
/// <param name="config">PLC 配置 | PLC configuration</param>
|
|
/// <returns>初始化是否成功 | Whether initialization succeeded</returns>
|
|
/// <exception cref="ArgumentNullException">当 containerProvider 或 config 为 null 时抛出 | Thrown when containerProvider or config is null</exception>
|
|
public static async Task<bool> InitializePlcServiceAsync(
|
|
this IContainerProvider containerProvider,
|
|
PlcConfig config)
|
|
{
|
|
if (containerProvider == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(containerProvider),
|
|
"容器提供者不能为空 | Container provider cannot be null");
|
|
}
|
|
|
|
if (config == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(config),
|
|
"PLC 配置不能为空 | PLC configuration cannot be null");
|
|
}
|
|
|
|
// 从容器解析 PlcService 实例 | Resolve PlcService instance from container
|
|
var plcService = containerProvider.Resolve<PlcService>();
|
|
|
|
// 调用 InitializeAsync 方法并返回结果 | Call InitializeAsync method and return result
|
|
return await plcService.InitializeAsync(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 PLC 服务实例 | Get PLC service instance
|
|
/// </summary>
|
|
/// <param name="containerProvider">容器提供者 | Container provider</param>
|
|
/// <returns>PLC 服务单例实例 | PLC service singleton instance</returns>
|
|
/// <exception cref="ArgumentNullException">当 containerProvider 为 null 时抛出 | Thrown when containerProvider is null</exception>
|
|
public static PlcService GetPlcService(this IContainerProvider containerProvider)
|
|
{
|
|
if (containerProvider == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(containerProvider),
|
|
"容器提供者不能为空 | Container provider cannot be null");
|
|
}
|
|
|
|
// 从容器解析并返回 PlcService 单例实例 | Resolve and return PlcService singleton instance from container
|
|
return containerProvider.Resolve<PlcService>();
|
|
}
|
|
}
|
|
}
|