按照软件设计文档开始重构代码01
This commit is contained in:
@@ -6,15 +6,15 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Services\" />
|
||||
<Folder Include="DTOs\" />
|
||||
<Folder Include="Interfaces\" />
|
||||
<Folder Include="Exceptions\" />
|
||||
<ProjectReference Include="..\DMS.Core\DMS.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DMS.Infrastructure\DMS.Infrastructure.csproj" />
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
14
DMS.Application/DTOs/CreateDeviceDto.cs
Normal file
14
DMS.Application/DTOs/CreateDeviceDto.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于创建新设备时传输数据的DTO。
|
||||
/// </summary>
|
||||
public class CreateDeviceDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public ProtocolType Protocol { get; set; }
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
}
|
||||
11
DMS.Application/DTOs/CreateDeviceWithDetailsDto.cs
Normal file
11
DMS.Application/DTOs/CreateDeviceWithDetailsDto.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于创建新设备及其关联的变量表和菜单的DTO。
|
||||
/// </summary>
|
||||
public class CreateDeviceWithDetailsDto
|
||||
{
|
||||
public CreateDeviceDto Device { get; set; }
|
||||
public VariableTableDto VariableTable { get; set; }
|
||||
// public MenuBeanDto Menu { get; set; } // 如果需要包含菜单信息
|
||||
}
|
||||
18
DMS.Application/DTOs/DeviceDto.cs
Normal file
18
DMS.Application/DTOs/DeviceDto.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在UI上显示设备基本信息的DTO。
|
||||
/// </summary>
|
||||
public class DeviceDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Protocol { get; set; }
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
public int Rack { get; set; }
|
||||
public int Slot { get; set; }
|
||||
public string OpcUaServerUrl { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public string Status { get; set; } // "在线", "离线", "连接中..."
|
||||
}
|
||||
15
DMS.Application/DTOs/MenuBeanDto.cs
Normal file
15
DMS.Application/DTOs/MenuBeanDto.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在UI上显示菜单项的DTO。
|
||||
/// </summary>
|
||||
public class MenuBeanDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int? ParentId { get; set; }
|
||||
public string Header { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public string TargetViewKey { get; set; }
|
||||
public string NavigationParameter { get; set; }
|
||||
public int DisplayOrder { get; set; }
|
||||
}
|
||||
24
DMS.Application/DTOs/MqttServerDto.cs
Normal file
24
DMS.Application/DTOs/MqttServerDto.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在UI上显示MQTT服务器配置信息的DTO。
|
||||
/// </summary>
|
||||
public class MqttServerDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string ServerName { get; set; }
|
||||
public string BrokerAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public string SubscribeTopic { get; set; }
|
||||
public string PublishTopic { get; set; }
|
||||
public string ClientId { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime? ConnectedAt { get; set; }
|
||||
public long ConnectionDuration { get; set; }
|
||||
public string MessageFormat { get; set; }
|
||||
}
|
||||
19
DMS.Application/DTOs/UpdateDeviceDto.cs
Normal file
19
DMS.Application/DTOs/UpdateDeviceDto.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于更新设备时传输数据的DTO。
|
||||
/// </summary>
|
||||
public class UpdateDeviceDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public ProtocolType Protocol { get; set; }
|
||||
public string IpAddress { get; set; }
|
||||
public int Port { get; set; }
|
||||
public int Rack { get; set; }
|
||||
public int Slot { get; set; }
|
||||
public string OpcUaServerUrl { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
12
DMS.Application/DTOs/UserDto.cs
Normal file
12
DMS.Application/DTOs/UserDto.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在UI上显示用户信息的DTO。
|
||||
/// </summary>
|
||||
public class UserDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Role { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
32
DMS.Application/DTOs/VariableDto.cs
Normal file
32
DMS.Application/DTOs/VariableDto.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using DMS.Core.Enums;
|
||||
using System;
|
||||
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在UI上显示变量基本信息的DTO。
|
||||
/// </summary>
|
||||
public class VariableDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Address { get; set; }
|
||||
public SignalType DataType { get; set; }
|
||||
public PollLevelType PollLevel { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public int VariableTableId { get; set; }
|
||||
public string OpcUaNodeId { get; set; }
|
||||
public bool IsHistoryEnabled { get; set; }
|
||||
public double HistoryDeadband { get; set; }
|
||||
public bool IsAlarmEnabled { get; set; }
|
||||
public double AlarmMinValue { get; set; }
|
||||
public double AlarmMaxValue { get; set; }
|
||||
public double AlarmDeadband { get; set; }
|
||||
public ProtocolType Protocol { get; set; }
|
||||
public CSharpDataType CSharpDataType { get; set; }
|
||||
public string ConversionFormula { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
public string UpdatedBy { get; set; }
|
||||
public bool IsModified { get; set; }
|
||||
}
|
||||
14
DMS.Application/DTOs/VariableHistoryDto.cs
Normal file
14
DMS.Application/DTOs/VariableHistoryDto.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在UI上显示变量历史记录的DTO。
|
||||
/// </summary>
|
||||
public class VariableHistoryDto
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public int VariableId { get; set; }
|
||||
public string Value { get; set; }
|
||||
public DateTime Timestamp { get; set; }
|
||||
}
|
||||
13
DMS.Application/DTOs/VariableMqttAliasDto.cs
Normal file
13
DMS.Application/DTOs/VariableMqttAliasDto.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在UI上显示和管理变量与MQTT服务器关联别名的DTO。
|
||||
/// </summary>
|
||||
public class VariableMqttAliasDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int VariableId { get; set; }
|
||||
public int MqttServerId { get; set; }
|
||||
public string MqttServerName { get; set; } // 用于UI显示关联的服务器名称
|
||||
public string Alias { get; set; }
|
||||
}
|
||||
16
DMS.Application/DTOs/VariableTableDto.cs
Normal file
16
DMS.Application/DTOs/VariableTableDto.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在UI上显示变量表基本信息的DTO。
|
||||
/// </summary>
|
||||
public class VariableTableDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public int DeviceId { get; set; }
|
||||
public ProtocolType Protocol { get; set; }
|
||||
}
|
||||
47
DMS.Application/Interfaces/IDeviceAppService.cs
Normal file
47
DMS.Application/Interfaces/IDeviceAppService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 定义设备管理相关的应用服务操作。
|
||||
/// </summary>
|
||||
public interface IDeviceAppService
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步根据ID获取设备DTO。
|
||||
/// </summary>
|
||||
Task<DeviceDto> GetDeviceByIdAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有设备DTO列表。
|
||||
/// </summary>
|
||||
Task<List<DeviceDto>> GetAllDevicesAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="dto">包含设备、变量表和菜单信息的DTO。</param>
|
||||
/// <returns>新创建设备的ID。</returns>
|
||||
Task<int> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的设备。
|
||||
/// </summary>
|
||||
Task UpdateDeviceAsync(UpdateDeviceDto deviceDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
Task DeleteDeviceAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步切换设备的激活状态。
|
||||
/// </summary>
|
||||
Task ToggleDeviceActiveStateAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取指定协议类型的设备列表。
|
||||
/// </summary>
|
||||
Task<List<DeviceDto>> GetDevicesByProtocolAsync(ProtocolType protocol);
|
||||
}
|
||||
34
DMS.Application/Interfaces/IMenuService.cs
Normal file
34
DMS.Application/Interfaces/IMenuService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using DMS.Application.DTOs;
|
||||
|
||||
namespace DMS.Application.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 定义菜单管理相关的应用服务操作。
|
||||
/// </summary>
|
||||
public interface IMenuService
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步根据ID获取菜单DTO。
|
||||
/// </summary>
|
||||
Task<MenuBeanDto> GetMenuByIdAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有菜单DTO列表。
|
||||
/// </summary>
|
||||
Task<List<MenuBeanDto>> GetAllMenusAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新菜单。
|
||||
/// </summary>
|
||||
Task<int> CreateMenuAsync(MenuBeanDto menuDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的菜单。
|
||||
/// </summary>
|
||||
Task UpdateMenuAsync(MenuBeanDto menuDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个菜单。
|
||||
/// </summary>
|
||||
Task DeleteMenuAsync(int id);
|
||||
}
|
||||
34
DMS.Application/Interfaces/IMqttAliasAppService.cs
Normal file
34
DMS.Application/Interfaces/IMqttAliasAppService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using DMS.Application.DTOs;
|
||||
|
||||
namespace DMS.Application.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 定义MQTT别名管理相关的应用服务操作。
|
||||
/// </summary>
|
||||
public interface IMqttAliasAppService
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步根据ID获取MQTT别名DTO。
|
||||
/// </summary>
|
||||
Task<VariableMqttAliasDto> GetMqttAliasByIdAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有MQTT别名DTO列表。
|
||||
/// </summary>
|
||||
Task<List<VariableMqttAliasDto>> GetAllMqttAliasesAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新的MQTT别名。
|
||||
/// </summary>
|
||||
Task<int> CreateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的MQTT别名。
|
||||
/// </summary>
|
||||
Task UpdateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个MQTT别名。
|
||||
/// </summary>
|
||||
Task DeleteMqttAliasAsync(int id);
|
||||
}
|
||||
34
DMS.Application/Interfaces/IMqttAppService.cs
Normal file
34
DMS.Application/Interfaces/IMqttAppService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using DMS.Application.DTOs;
|
||||
|
||||
namespace DMS.Application.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 定义MQTT服务器管理相关的应用服务操作。
|
||||
/// </summary>
|
||||
public interface IMqttAppService
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步根据ID获取MQTT服务器DTO。
|
||||
/// </summary>
|
||||
Task<MqttServerDto> GetMqttServerByIdAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有MQTT服务器DTO列表。
|
||||
/// </summary>
|
||||
Task<List<MqttServerDto>> GetAllMqttServersAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新的MQTT服务器。
|
||||
/// </summary>
|
||||
Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的MQTT服务器。
|
||||
/// </summary>
|
||||
Task UpdateMqttServerAsync(MqttServerDto mqttServerDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个MQTT服务器。
|
||||
/// </summary>
|
||||
Task DeleteMqttServerAsync(int id);
|
||||
}
|
||||
34
DMS.Application/Interfaces/IVariableAppService.cs
Normal file
34
DMS.Application/Interfaces/IVariableAppService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using DMS.Application.DTOs;
|
||||
|
||||
namespace DMS.Application.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// 定义变量管理相关的应用服务操作。
|
||||
/// </summary>
|
||||
public interface IVariableAppService
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步根据ID获取变量DTO。
|
||||
/// </summary>
|
||||
Task<VariableDto> GetVariableByIdAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有变量DTO列表。
|
||||
/// </summary>
|
||||
Task<List<VariableDto>> GetAllVariablesAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新变量。
|
||||
/// </summary>
|
||||
Task<int> CreateVariableAsync(VariableDto variableDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的变量。
|
||||
/// </summary>
|
||||
Task UpdateVariableAsync(VariableDto variableDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个变量。
|
||||
/// </summary>
|
||||
Task DeleteVariableAsync(int id);
|
||||
}
|
||||
47
DMS.Application/Profiles/MappingProfile.cs
Normal file
47
DMS.Application/Profiles/MappingProfile.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.Profiles;
|
||||
|
||||
/// <summary>
|
||||
/// 配置AutoMapper的映射规则。
|
||||
/// </summary>
|
||||
public class MappingProfile : Profile
|
||||
{
|
||||
public MappingProfile()
|
||||
{
|
||||
// Device 映射
|
||||
CreateMap<CreateDeviceDto, Device>();
|
||||
CreateMap<UpdateDeviceDto, Device>();
|
||||
CreateMap<Device, DeviceDto>()
|
||||
.ForMember(dest => dest.Protocol, opt => opt.MapFrom(src => src.Protocol.ToString()));
|
||||
|
||||
// VariableTable 映射
|
||||
CreateMap<VariableTable, VariableTableDto>().ReverseMap();
|
||||
|
||||
// Variable 映射
|
||||
CreateMap<Variable, VariableDto>()
|
||||
.ForMember(dest => dest.DataType, opt => opt.MapFrom(src => src.DataType.ToString()))
|
||||
.ForMember(dest => dest.CSharpDataType, opt => opt.MapFrom(src => src.CSharpDataType));
|
||||
|
||||
// MqttServer 映射
|
||||
CreateMap<MqttServer, MqttServerDto>().ReverseMap();
|
||||
|
||||
// VariableMqttAlias 映射
|
||||
CreateMap<VariableMqttAlias, VariableMqttAliasDto>().ReverseMap();
|
||||
|
||||
// VariableTable 映射
|
||||
CreateMap<VariableTable, VariableTableDto>().ReverseMap();
|
||||
|
||||
// VariableHistory 映射
|
||||
CreateMap<VariableHistory, VariableHistoryDto>().ReverseMap();
|
||||
|
||||
// MenuBean 映射
|
||||
CreateMap<MenuBean, MenuBeanDto>().ReverseMap();
|
||||
|
||||
// User 映射
|
||||
CreateMap<User, UserDto>().ReverseMap();
|
||||
}
|
||||
}
|
||||
133
DMS.Application/Services/DeviceAppService.cs
Normal file
133
DMS.Application/Services/DeviceAppService.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现设备管理的应用服务。
|
||||
/// </summary>
|
||||
public class DeviceAppService : IDeviceAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||
/// </summary>
|
||||
public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取设备DTO。
|
||||
/// </summary>
|
||||
public async Task<DeviceDto> GetDeviceByIdAsync(int id)
|
||||
{
|
||||
var device = await _repoManager.Devices.GetByIdAsync(id);
|
||||
return _mapper.Map<DeviceDto>(device);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有设备DTO列表。
|
||||
/// </summary>
|
||||
public async Task<List<DeviceDto>> GetAllDevicesAsync()
|
||||
{
|
||||
var devices = await _repoManager.Devices.GetAllAsync();
|
||||
return _mapper.Map<List<DeviceDto>>(devices);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
|
||||
/// </summary>
|
||||
public async Task<int> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTransaction();
|
||||
|
||||
var device = _mapper.Map<Device>(dto.Device);
|
||||
device.IsActive = true; // 默认激活
|
||||
await _repoManager.Devices.AddAsync(device);
|
||||
|
||||
// 假设 CreateDeviceWithDetailsDto 包含了变量表和菜单信息
|
||||
if (dto.VariableTable != null)
|
||||
{
|
||||
var variableTable = _mapper.Map<VariableTable>(dto.VariableTable);
|
||||
variableTable.DeviceId = device.Id; // 关联新设备ID
|
||||
await _repoManager.VariableTables.AddAsync(variableTable);
|
||||
}
|
||||
|
||||
// 假设有菜单服务或仓储
|
||||
// if (dto.Menu != null)
|
||||
// {
|
||||
// var menu = _mapper.Map<Menu>(dto.Menu);
|
||||
// menu.TargetId = device.Id;
|
||||
// await _repoManager.Menus.AddAsync(menu);
|
||||
// }
|
||||
|
||||
await _repoManager.CommitAsync();
|
||||
|
||||
return device.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
// 可以在此记录日志
|
||||
throw new ApplicationException("创建设备时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的设备。
|
||||
/// </summary>
|
||||
public async Task UpdateDeviceAsync(UpdateDeviceDto deviceDto)
|
||||
{
|
||||
var device = await _repoManager.Devices.GetByIdAsync(deviceDto.Id);
|
||||
if (device == null)
|
||||
{
|
||||
throw new ApplicationException($"Device with ID {deviceDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(deviceDto, device);
|
||||
await _repoManager.Devices.UpdateAsync(device);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
public async Task DeleteDeviceAsync(int id)
|
||||
{
|
||||
await _repoManager.Devices.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步切换设备的激活状态。
|
||||
/// </summary>
|
||||
public async Task ToggleDeviceActiveStateAsync(int id)
|
||||
{
|
||||
var device = await _repoManager.Devices.GetByIdAsync(id);
|
||||
if (device == null)
|
||||
{
|
||||
throw new ApplicationException($"Device with ID {id} not found.");
|
||||
}
|
||||
device.IsActive = !device.IsActive;
|
||||
await _repoManager.Devices.UpdateAsync(device);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取指定协议类型的设备列表。
|
||||
/// </summary>
|
||||
public async Task<List<DeviceDto>> GetDevicesByProtocolAsync(ProtocolType protocol)
|
||||
{
|
||||
var devices = await _repoManager.Devices.GetActiveDevicesWithDetailsAsync(protocol);
|
||||
return _mapper.Map<List<DeviceDto>>(devices);
|
||||
}
|
||||
}
|
||||
60
DMS.Application/Services/MenuService.cs
Normal file
60
DMS.Application/Services/MenuService.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现菜单管理的应用服务。
|
||||
/// </summary>
|
||||
public class MenuService : IMenuService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public MenuService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<MenuBeanDto> GetMenuByIdAsync(int id)
|
||||
{
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(id);
|
||||
return _mapper.Map<MenuBeanDto>(menu);
|
||||
}
|
||||
|
||||
public async Task<List<MenuBeanDto>> GetAllMenusAsync()
|
||||
{
|
||||
var menus = await _repoManager.Menus.GetAllAsync();
|
||||
return _mapper.Map<List<MenuBeanDto>>(menus);
|
||||
}
|
||||
|
||||
public async Task<int> CreateMenuAsync(MenuBeanDto menuDto)
|
||||
{
|
||||
var menu = _mapper.Map<MenuBean>(menuDto);
|
||||
await _repoManager.Menus.AddAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
return menu.Id;
|
||||
}
|
||||
|
||||
public async Task UpdateMenuAsync(MenuBeanDto menuDto)
|
||||
{
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(menuDto.Id);
|
||||
if (menu == null)
|
||||
{
|
||||
throw new ApplicationException($"Menu with ID {menuDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(menuDto, menu);
|
||||
await _repoManager.Menus.UpdateAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteMenuAsync(int id)
|
||||
{
|
||||
await _repoManager.Menus.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
}
|
||||
60
DMS.Application/Services/MqttAliasAppService.cs
Normal file
60
DMS.Application/Services/MqttAliasAppService.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现MQTT别名管理的应用服务。
|
||||
/// </summary>
|
||||
public class MqttAliasAppService : IMqttAliasAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public MqttAliasAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<VariableMqttAliasDto> GetMqttAliasByIdAsync(int id)
|
||||
{
|
||||
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(id);
|
||||
return _mapper.Map<VariableMqttAliasDto>(mqttAlias);
|
||||
}
|
||||
|
||||
public async Task<List<VariableMqttAliasDto>> GetAllMqttAliasesAsync()
|
||||
{
|
||||
var mqttAliases = await _repoManager.VariableMqttAliases.GetAllAsync();
|
||||
return _mapper.Map<List<VariableMqttAliasDto>>(mqttAliases);
|
||||
}
|
||||
|
||||
public async Task<int> CreateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
|
||||
{
|
||||
var mqttAlias = _mapper.Map<VariableMqttAlias>(mqttAliasDto);
|
||||
await _repoManager.VariableMqttAliases.AddAsync(mqttAlias);
|
||||
await _repoManager.CommitAsync();
|
||||
return mqttAlias.Id;
|
||||
}
|
||||
|
||||
public async Task UpdateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
|
||||
{
|
||||
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(mqttAliasDto.Id);
|
||||
if (mqttAlias == null)
|
||||
{
|
||||
throw new ApplicationException($"MQTT Alias with ID {mqttAliasDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(mqttAliasDto, mqttAlias);
|
||||
await _repoManager.VariableMqttAliases.UpdateAsync(mqttAlias);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteMqttAliasAsync(int id)
|
||||
{
|
||||
await _repoManager.VariableMqttAliases.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
}
|
||||
60
DMS.Application/Services/MqttAppService.cs
Normal file
60
DMS.Application/Services/MqttAppService.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现MQTT服务器管理的应用服务。
|
||||
/// </summary>
|
||||
public class MqttAppService : IMqttAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public MqttAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<MqttServerDto> GetMqttServerByIdAsync(int id)
|
||||
{
|
||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(id);
|
||||
return _mapper.Map<MqttServerDto>(mqttServer);
|
||||
}
|
||||
|
||||
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
|
||||
{
|
||||
var mqttServers = await _repoManager.MqttServers.GetAllAsync();
|
||||
return _mapper.Map<List<MqttServerDto>>(mqttServers);
|
||||
}
|
||||
|
||||
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
|
||||
{
|
||||
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
|
||||
await _repoManager.MqttServers.AddAsync(mqttServer);
|
||||
await _repoManager.CommitAsync();
|
||||
return mqttServer.Id;
|
||||
}
|
||||
|
||||
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
|
||||
{
|
||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
|
||||
if (mqttServer == null)
|
||||
{
|
||||
throw new ApplicationException($"MQTT Server with ID {mqttServerDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(mqttServerDto, mqttServer);
|
||||
await _repoManager.MqttServers.UpdateAsync(mqttServer);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteMqttServerAsync(int id)
|
||||
{
|
||||
await _repoManager.MqttServers.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
}
|
||||
60
DMS.Application/Services/VariableAppService.cs
Normal file
60
DMS.Application/Services/VariableAppService.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现变量管理的应用服务。
|
||||
/// </summary>
|
||||
public class VariableAppService : IVariableAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public VariableAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<VariableDto> GetVariableByIdAsync(int id)
|
||||
{
|
||||
var variable = await _repoManager.Variables.GetByIdAsync(id);
|
||||
return _mapper.Map<VariableDto>(variable);
|
||||
}
|
||||
|
||||
public async Task<List<VariableDto>> GetAllVariablesAsync()
|
||||
{
|
||||
var variables = await _repoManager.Variables.GetAllAsync();
|
||||
return _mapper.Map<List<VariableDto>>(variables);
|
||||
}
|
||||
|
||||
public async Task<int> CreateVariableAsync(VariableDto variableDto)
|
||||
{
|
||||
var variable = _mapper.Map<Variable>(variableDto);
|
||||
await _repoManager.Variables.AddAsync(variable);
|
||||
await _repoManager.CommitAsync();
|
||||
return variable.Id;
|
||||
}
|
||||
|
||||
public async Task UpdateVariableAsync(VariableDto variableDto)
|
||||
{
|
||||
var variable = await _repoManager.Variables.GetByIdAsync(variableDto.Id);
|
||||
if (variable == null)
|
||||
{
|
||||
throw new ApplicationException($"Variable with ID {variableDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(variableDto, variable);
|
||||
await _repoManager.Variables.UpdateAsync(variable);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteVariableAsync(int id)
|
||||
{
|
||||
await _repoManager.Variables.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user