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