using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Options;
|
using SqlSugar;
|
using WIDESEAWCS_S7Simulator.Application.DTOs;
|
|
namespace WIDESEAWCS_S7Simulator.Application;
|
|
/// <summary>
|
/// 读取 WCS 数据库设备数据
|
/// </summary>
|
public class DatabaseDeviceService : IDisposable
|
{
|
private readonly WcsDbOptions _options;
|
private readonly ILogger<DatabaseDeviceService> _logger;
|
private readonly SqlSugarScope _db;
|
|
public DatabaseDeviceService(IOptions<WcsDbOptions> options, ILogger<DatabaseDeviceService> logger)
|
{
|
_options = options.Value;
|
_logger = logger;
|
|
_db = new SqlSugarScope(new ConnectionConfig
|
{
|
ConnectionString = _options.ConnectionString,
|
DbType = DbType.SqlServer,
|
IsAutoCloseConnection = true,
|
InitKeyType = InitKeyType.Attribute
|
});
|
}
|
|
/// <summary>
|
/// 获取所有 SiemensS7 类型的设备
|
/// </summary>
|
public async Task<List<WcsDeviceDto>> GetSiemensS7DevicesAsync()
|
{
|
try
|
{
|
var devices = await _db.Queryable<WcsDeviceDto>()
|
.Where(d => d.DevicePlcType == "SiemensS7")
|
.ToListAsync();
|
_logger.LogInformation("从数据库获取到 {Count} 个 SiemensS7 设备", devices.Count);
|
return devices;
|
}
|
catch (Exception ex)
|
{
|
_logger.LogError(ex, "从数据库获取设备失败");
|
return new List<WcsDeviceDto>();
|
}
|
}
|
|
/// <summary>
|
/// 获取指定设备的协议列表
|
/// </summary>
|
public async Task<List<WcsDeviceProtocolDto>> GetDeviceProtocolsAsync(int deviceId)
|
{
|
try
|
{
|
var protocols = await _db.Queryable<WcsDeviceProtocolDto>()
|
.Where(p => p.DeviceId == deviceId)
|
.ToListAsync();
|
return protocols;
|
}
|
catch (Exception ex)
|
{
|
_logger.LogError(ex, "获取设备协议失败 DeviceId={DeviceId}", deviceId);
|
return new List<WcsDeviceProtocolDto>();
|
}
|
}
|
|
public void Dispose()
|
{
|
_db.Dispose();
|
}
|
}
|