You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
7.3 KiB
167 lines
7.3 KiB
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using X1.DynamicClientCore.Interfaces;
|
|
using X1.DynamicClientCore.Models;
|
|
|
|
namespace X1.DynamicClientCore.Features.Service
|
|
{
|
|
/// <summary>
|
|
/// 设备协议客户端实现类
|
|
/// 提供与设备设备通信的协议服务,包括设备信息获取、网络控制等功能
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 实现 IInstrumentProtocolClient 和 IBaseInstrumentClient 接口
|
|
/// 支持动态服务端点管理和HTTP通信
|
|
/// </remarks>
|
|
public class InstrumentProtocolClient : IInstrumentHttpClient
|
|
{
|
|
private readonly ILogger<InstrumentProtocolClient> _logger;
|
|
private readonly IDynamicHttpClient _dynamicHttpClient;
|
|
private readonly IServiceEndpointManager _serviceEndpointManager;
|
|
|
|
/// <summary>
|
|
/// 初始化设备协议客户端
|
|
/// </summary>
|
|
/// <param name="dynamicHttpClient">动态HTTP客户端</param>
|
|
/// <param name="serviceEndpointManager">服务端点管理器</param>
|
|
/// <param name="logger">日志记录器</param>
|
|
/// <exception cref="ArgumentNullException">当任何必需参数为null时抛出</exception>
|
|
public InstrumentProtocolClient(
|
|
IDynamicHttpClient dynamicHttpClient,
|
|
IServiceEndpointManager serviceEndpointManager,
|
|
ILogger<InstrumentProtocolClient> logger)
|
|
{
|
|
_dynamicHttpClient = dynamicHttpClient ?? throw new ArgumentNullException(nameof(dynamicHttpClient));
|
|
_serviceEndpointManager = serviceEndpointManager ?? throw new ArgumentNullException(nameof(serviceEndpointManager));
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取设备序列号
|
|
/// </summary>
|
|
/// <param name="endpoint">服务端点配置</param>
|
|
/// <param name="options">请求选项</param>
|
|
/// <param name="cancellationToken">取消令牌</param>
|
|
/// <returns>设备序列号,如果获取失败则返回空字符串</returns>
|
|
/// <exception cref="ArgumentNullException">当endpoint为null时抛出</exception>
|
|
public async Task<string> GetDeviceSerialNumberAsync(
|
|
string instrumentNumber,
|
|
RequestOptions? options = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(instrumentNumber))
|
|
throw new ArgumentException("设备编号不能为空", nameof(instrumentNumber));
|
|
|
|
try
|
|
{
|
|
_logger.LogDebug("开始获取设备序列号,端点:{EndpointName}", instrumentNumber);
|
|
|
|
|
|
// 发送HTTP请求获取设备信息
|
|
var response = await _dynamicHttpClient.GetAsync<ApiActionResult<DeviceInfoResponse>>(
|
|
instrumentNumber,
|
|
"System/serial-number",
|
|
options,
|
|
cancellationToken);
|
|
|
|
if (response == null)
|
|
{
|
|
_logger.LogWarning("获取设备序列号失败:响应为空,端点:{EndpointName}", instrumentNumber);
|
|
return string.Empty;
|
|
}
|
|
|
|
if (!response.IsSuccess)
|
|
{
|
|
_logger.LogWarning("获取设备序列号失败:请求未成功,端点:{EndpointName},错误:{ErrorMessage}",
|
|
instrumentNumber, response.Message ?? "未知错误");
|
|
return string.Empty;
|
|
}
|
|
|
|
var serialNumber = response.Data?.SerialNumber ?? string.Empty;
|
|
_logger.LogInformation("成功获取设备序列号:{SerialNumber},端点:{EndpointName}",
|
|
serialNumber, instrumentNumber);
|
|
|
|
return serialNumber;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取设备序列号时发生异常,端点:{EndpointName}", instrumentNumber);
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动网络连接
|
|
/// </summary>
|
|
/// <param name="instrumentNumber">设备编号</param>
|
|
/// <param name="options">请求选项</param>
|
|
/// <param name="cancellationToken">取消令牌</param>
|
|
/// <returns>异步任务</returns>
|
|
/// <exception cref="ArgumentException">当instrumentNumber为空或null时抛出</exception>
|
|
public async Task StartNetworkAsync(
|
|
CellularNetworkConfiguration cellular,
|
|
RequestOptions? options = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
string instrumentNumber = cellular.DeviceCode;
|
|
if (string.IsNullOrWhiteSpace(instrumentNumber))
|
|
throw new ArgumentException("设备编号不能为空", nameof(instrumentNumber));
|
|
|
|
try
|
|
{
|
|
_logger.LogInformation("开始启动网络连接,设备编号:{InstrumentNumber}", instrumentNumber);
|
|
var response = await _dynamicHttpClient.PostAsync<ApiActionResult<DeviceInfoResponse>>(
|
|
instrumentNumber,
|
|
"CellularNetwork/generalStart",
|
|
cellular,
|
|
options,
|
|
cancellationToken);
|
|
await Task.CompletedTask.ConfigureAwait(false);
|
|
|
|
_logger.LogInformation("网络连接启动完成,设备编号:{InstrumentNumber}", instrumentNumber);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "启动网络连接时发生异常,设备编号:{InstrumentNumber}", instrumentNumber);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止网络连接
|
|
/// </summary>
|
|
/// <param name="instrumentNumber">设备编号</param>
|
|
/// <param name="options">请求选项</param>
|
|
/// <param name="cancellationToken">取消令牌</param>
|
|
/// <returns>异步任务</returns>
|
|
/// <exception cref="ArgumentException">当instrumentNumber为空或null时抛出</exception>
|
|
public async Task StopNetworkAsync(
|
|
string instrumentNumber,
|
|
RequestOptions? options = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(instrumentNumber))
|
|
throw new ArgumentException("设备编号不能为空", nameof(instrumentNumber));
|
|
|
|
try
|
|
{
|
|
_logger.LogInformation("开始停止网络连接,设备编号:{InstrumentNumber}", instrumentNumber);
|
|
|
|
// TODO: 实现网络停止逻辑
|
|
// 这里需要根据具体的协议实现来调用相应的API
|
|
await Task.CompletedTask.ConfigureAwait(false);
|
|
|
|
_logger.LogInformation("网络连接停止完成,设备编号:{InstrumentNumber}", instrumentNumber);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "停止网络连接时发生异常,设备编号:{InstrumentNumber}", instrumentNumber);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|