From cfa8e78d33813495ffa27434ee595623d63db024 Mon Sep 17 00:00:00 2001 From: dengboling Date: Fri, 28 Aug 2026 17:28:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=BE=E5=A4=87=E7=B1=BB?= =?UTF-8?q?=E3=80=81=E8=AE=BE=E5=A4=87=E6=97=A5=E5=BF=97=E7=B1=BB=E3=80=81?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=9C=8D=E5=8A=A1=E7=B1=BB=E3=80=81=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Config/IotDeviceController.cs | 84 ++++++++- Model/Dto/Config/DeviceLogDto.cs | 32 ++++ Model/Dto/Config/IotDeviceDto.cs | 55 ++++++ Model/Entity/Config/DeviceLogEntity.cs | 47 +++++ Model/Entity/Config/IotDeviceEntity.cs | 97 +++++++++++ Model/Entity/Config/IotDeviceEnums.cs | 59 +++++++ Model/Mapper/EntityMapper.cs | 89 ++++++++++ Service/Implement/Config/DeviceService.cs | 161 ++++++++++++++++++ Service/Implement/Config/IotDeviceService.cs | 12 -- Service/Interface/Config/IDeviceService.cs | 44 +++++ Service/Interface/Config/IIotDeviceService.cs | 10 -- 11 files changed, 667 insertions(+), 23 deletions(-) create mode 100644 Model/Dto/Config/DeviceLogDto.cs create mode 100644 Model/Dto/Config/IotDeviceDto.cs create mode 100644 Model/Entity/Config/DeviceLogEntity.cs create mode 100644 Model/Entity/Config/IotDeviceEntity.cs create mode 100644 Model/Entity/Config/IotDeviceEnums.cs create mode 100644 Service/Implement/Config/DeviceService.cs delete mode 100644 Service/Implement/Config/IotDeviceService.cs create mode 100644 Service/Interface/Config/IDeviceService.cs delete mode 100644 Service/Interface/Config/IIotDeviceService.cs diff --git a/IOT_API/Controllers/Config/IotDeviceController.cs b/IOT_API/Controllers/Config/IotDeviceController.cs index 59fb655..d9027f2 100644 --- a/IOT_API/Controllers/Config/IotDeviceController.cs +++ b/IOT_API/Controllers/Config/IotDeviceController.cs @@ -1,4 +1,9 @@ using Microsoft.AspNetCore.Mvc; +using Model; +using Model.Dto.Config; +using Service.Interface.Config; +using SqlSugar; +using System.Threading.Tasks; namespace WebAPI.Controllers { @@ -9,6 +14,83 @@ namespace WebAPI.Controllers [Route("api/config/device")] public class IotDeviceController : ControllerBase { - // TODO: 实现 IOT设备管理 相关接口 + private readonly IDeviceService _deviceService; + + public IotDeviceController(IDeviceService deviceService) + { + _deviceService = deviceService; + } + + /// + /// 设备列表(分页查询,支持关键字搜索) + /// + /// 页码(从1开始,默认1) + /// 每页数量(默认10) + /// 关键字(模糊匹配设备编号/名称/类型) + [HttpGet("list")] + public async Task GetList(int pageIndex = 1, int pageSize = 10, string? keyword = null) + { + RefAsync total = 0; + var result = await _deviceService.GetPagedAsync(pageIndex, pageSize, total, keyword); + Response.Headers["X-Total-Count"] = total.Value.ToString(); + return result.IsSuccess + ? Ok(Result>.Success(result.Data)) + : Ok(Result>.Error(result.Msg)); + } + + /// + /// 设备详情 + /// + /// 设备主键 Id + [HttpGet("{id}")] + public async Task GetById(long id) + { + return Ok(await _deviceService.GetByIdAsync(id)); + } + + /// + /// 新增设备 + /// + [HttpPost] + public async Task Add([FromBody] IotDeviceDto dto) + { + return Ok(await _deviceService.AddAsync(dto)); + } + + /// + /// 修改设备 + /// + [HttpPut] + public async Task Update([FromBody] IotDeviceDto dto) + { + return Ok(await _deviceService.UpdateAsync(dto)); + } + + /// + /// 删除设备(软删除) + /// + /// 设备主键 Id + [HttpDelete("{id}")] + public async Task Delete(long id) + { + return Ok(await _deviceService.DeleteAsync(id)); + } + + /// + /// 设备日志(设备独享日志,分页查询) + /// + /// 设备主键 Id + /// 页码(默认1) + /// 每页数量(默认20) + [HttpGet("{id}/logs")] + public async Task GetLogs(long id, int pageIndex = 1, int pageSize = 20) + { + RefAsync total = 0; + var result = await _deviceService.GetDeviceLogsAsync(id, pageIndex, pageSize, total); + Response.Headers["X-Total-Count"] = total.Value.ToString(); + return result.IsSuccess + ? Ok(Result>.Success(result.Data)) + : Ok(Result>.Error(result.Msg)); + } } } diff --git a/Model/Dto/Config/DeviceLogDto.cs b/Model/Dto/Config/DeviceLogDto.cs new file mode 100644 index 0000000..c980d76 --- /dev/null +++ b/Model/Dto/Config/DeviceLogDto.cs @@ -0,0 +1,32 @@ +namespace Model.Dto.Config +{ + /// + /// 设备日志 DTO(Id 由 long 改为 string,避免前端精度丢失) + /// + public class DeviceLogDto + { + /// 主键 Id(long → string) + public string Id { get; set; } + + /// 设备Id + public string DeviceId { get; set; } + + /// 设备编号 + public string DeviceCode { get; set; } + + /// 日志级别(Info/Warn/Error) + public string Level { get; set; } + + /// 日志类型(连接/通讯/采集/指令/系统) + public string LogType { get; set; } + + /// 日志内容 + public string Message { get; set; } + + /// 记录时间 + public DateTime LogTime { get; set; } + + /// 创建时间 + public DateTime? CreateTime { get; set; } + } +} diff --git a/Model/Dto/Config/IotDeviceDto.cs b/Model/Dto/Config/IotDeviceDto.cs new file mode 100644 index 0000000..6ce12a1 --- /dev/null +++ b/Model/Dto/Config/IotDeviceDto.cs @@ -0,0 +1,55 @@ +using Model.Entity.Config; + +namespace Model.Dto.Config +{ + /// + /// IOT 设备 DTO(Id 由 long 改为 string,避免前端精度丢失) + /// + public class IotDeviceDto + { + /// 主键 Id(long → string) + public string Id { get; set; } + + /// 设备编号 + public string Code { get; set; } + + /// 设备名称 + public string Name { get; set; } + + /// 设备类型(型号,对应 DeviceCommand 驱动类) + public string DeviceType { get; set; } + + /// 所属网关Code + public string GatewayCode { get; set; } + + /// 从站地址 + public byte SlaveId { get; set; } + + /// 制造商 + public string? Manufacturer { get; set; } + + /// 设备描述 + public string? Description { get; set; } + + /// 通讯协议类型 + public IotDeviceProtocolEnum ProtocolType { get; set; } + + /// 是否启用采集 + public bool IsEnabled { get; set; } = true; + + /// 在线状态 + public IotDeviceOnlineStatusEnum OnlineStatus { get; set; } + + /// 最后采集时间 + public DateTime? LastCollectTime { get; set; } + + /// 最近错误信息 + public string? LastError { get; set; } + + /// 备注 + public string? Remark { get; set; } + + /// 创建时间 + public DateTime? CreateTime { get; set; } + } +} diff --git a/Model/Entity/Config/DeviceLogEntity.cs b/Model/Entity/Config/DeviceLogEntity.cs new file mode 100644 index 0000000..dae70c5 --- /dev/null +++ b/Model/Entity/Config/DeviceLogEntity.cs @@ -0,0 +1,47 @@ +using SqlSugar; + +namespace Model.Entity.Config +{ + /// + /// 设备日志(每台设备独享的日志记录) + /// 记录设备的通讯、连接、数据采集等运行日志,按设备查询 + /// + public class DeviceLogEntity : BaseEntity + { + /// + /// 设备Id(关联 IotDeviceEntity.Id) + /// + [SugarColumn(ColumnDescription = "设备Id")] + public long DeviceId { get; set; } + + /// + /// 设备编号(冗余便于查询展示) + /// + [SugarColumn(ColumnDescription = "设备编号", Length = 64)] + public string? DeviceCode { get; set; } + + /// + /// 日志级别(Info/Warn/Error) + /// + [SugarColumn(ColumnDescription = "日志级别", Length = 16)] + public string? Level { get; set; } = "Info"; + + /// + /// 日志类型(连接/通讯/采集/指令/系统) + /// + [SugarColumn(ColumnDescription = "日志类型", Length = 32)] + public string? LogType { get; set; } + + /// + /// 日志内容 + /// + [SugarColumn(ColumnDescription = "日志内容", ColumnDataType = "text")] + public string? Message { get; set; } + + /// + /// 记录时间(日志产生时间,用于时间线展示) + /// + [SugarColumn(ColumnDescription = "记录时间")] + public DateTime LogTime { get; set; } + } +} diff --git a/Model/Entity/Config/IotDeviceEntity.cs b/Model/Entity/Config/IotDeviceEntity.cs new file mode 100644 index 0000000..db7b14e --- /dev/null +++ b/Model/Entity/Config/IotDeviceEntity.cs @@ -0,0 +1,97 @@ +using SqlSugar; + +namespace Model.Entity.Config +{ + /// + /// IOT 设备(IOT设备注册与管理 - 设备管理) + /// 一台设备 = 一个从站实例,通过所属网关(IP:端口 或 串口)通讯 + /// + public class IotDeviceEntity : BaseEntity + { + #region 基础信息 + /// + /// 设备编号(唯一) + /// + [SugarColumn(ColumnDescription = "设备编号", Length = 64)] + public string Code { get; set; } + + /// + /// 设备名称 + /// + [SugarColumn(ColumnDescription = "设备名称", Length = 100)] + public string Name { get; set; } + + /// + /// 设备类型(型号,如 温度箱/充放电柜,对应 DeviceCommand 驱动类) + /// + [SugarColumn(ColumnDescription = "设备类型", Length = 64)] + public string DeviceType { get; set; } + + /// + /// 所属网关Code(对应网关实体/通道的标识,连接时据此刻查找 IP/端口/串口) + /// + [SugarColumn(ColumnDescription = "所属网关Code", Length = 64)] + public string GatewayCode { get; set; } + + /// + /// 从站地址(协议从站号,网关下区分设备) + /// + [SugarColumn(ColumnDescription = "从站地址")] + public byte SlaveId { get; set; } + + /// + /// 制造商 + /// + [SugarColumn(ColumnDescription = "制造商", Length = 100, IsNullable = true)] + public string? Manufacturer { get; set; } + + /// + /// 设备描述 + /// + [SugarColumn(ColumnDescription = "设备描述", Length = 500, IsNullable = true)] + public string? Description { get; set; } + #endregion + + #region 连接与协议配置 + /// + /// 通讯协议类型(ModbusTcp/ModbusRtu/S7/Tcp/Serial) + /// + [SugarColumn(ColumnDescription = "通讯协议类型")] + public IotDeviceProtocolEnum ProtocolType { get; set; } = IotDeviceProtocolEnum.ModbusTcp; + + /// + /// 是否启用采集(停用则采集调度跳过该设备) + /// + [SugarColumn(ColumnDescription = "是否启用")] + public bool IsEnabled { get; set; } = true; + #endregion + + #region 运行状态(采集调度器更新) + /// + /// 在线状态 + /// + [SugarColumn(ColumnDescription = "在线状态")] + public IotDeviceOnlineStatusEnum OnlineStatus { get; set; } = IotDeviceOnlineStatusEnum.Offline; + + /// + /// 最后采集时间 + /// + [SugarColumn(ColumnDescription = "最后采集时间", IsNullable = true)] + public DateTime? LastCollectTime { get; set; } + + /// + /// 最近错误信息(通讯异常时记录) + /// + [SugarColumn(ColumnDescription = "最近错误", Length = 500, IsNullable = true)] + public string? LastError { get; set; } + #endregion + + #region 审计信息 + /// + /// 备注 + /// + [SugarColumn(ColumnDescription = "备注", Length = 500, IsNullable = true)] + public string? Remark { get; set; } + #endregion + } +} diff --git a/Model/Entity/Config/IotDeviceEnums.cs b/Model/Entity/Config/IotDeviceEnums.cs new file mode 100644 index 0000000..743bf75 --- /dev/null +++ b/Model/Entity/Config/IotDeviceEnums.cs @@ -0,0 +1,59 @@ +namespace Model.Entity.Config +{ + /// + /// 设备通讯协议类型(决定使用 DeviceCommand 中哪类驱动) + /// + public enum IotDeviceProtocolEnum + { + /// + /// Modbus TCP(网口,IP + 端口 + 从站地址) + /// + ModbusTcp = 1, + + /// + /// Modbus RTU(串口,COM 口 + 波特率 + 从站地址) + /// + ModbusRtu = 2, + + /// + /// 西门子 S7 协议 + /// + S7 = 3, + + /// + /// 自定义 TCP 协议 + /// + Tcp = 4, + + /// + /// 串口自定义协议 + /// + Serial = 5 + } + + /// + /// 设备在线状态 + /// + public enum IotDeviceOnlineStatusEnum + { + /// + /// 离线 + /// + Offline = 0, + + /// + /// 在线 + /// + Online = 1, + + /// + /// 连接中(网关已连,设备待确认) + /// + Connecting = 2, + + /// + /// 通讯异常 + /// + Fault = 3 + } +} diff --git a/Model/Mapper/EntityMapper.cs b/Model/Mapper/EntityMapper.cs index 5acb5c1..9f4724c 100644 --- a/Model/Mapper/EntityMapper.cs +++ b/Model/Mapper/EntityMapper.cs @@ -1,6 +1,8 @@ using Model.Dto.Asset; +using Model.Dto.Config; using Model.Dto.Inspection; using Model.Entity.Asset; +using Model.Entity.Config; using Model.Entity.Inspection; namespace Model.Mapper @@ -387,5 +389,92 @@ namespace Model.Mapper }; } #endregion + + #region IOT设备 + /// + /// IotDeviceEntity → IotDeviceDto + /// + public static IotDeviceDto ToDto(this IotDeviceEntity entity) + { + if (entity == null) return null; + return new IotDeviceDto + { + Id = entity.Id.ToString(), + Code = entity.Code, + Name = entity.Name, + DeviceType = entity.DeviceType, + GatewayCode = entity.GatewayCode, + SlaveId = entity.SlaveId, + Manufacturer = entity.Manufacturer, + Description = entity.Description, + ProtocolType = entity.ProtocolType, + IsEnabled = entity.IsEnabled, + OnlineStatus = entity.OnlineStatus, + LastCollectTime = entity.LastCollectTime, + LastError = entity.LastError, + Remark = entity.Remark, + CreateTime = entity.CreateTime + }; + } + + /// + /// List<IotDeviceEntity> → List<IotDeviceDto> + /// + public static List ToDtoList(this List entities) + { + return entities?.Select(e => e.ToDto()).ToList() ?? new List(); + } + + /// + /// IotDeviceDto → IotDeviceEntity(入参映射,IsDel/CreateTime/OnlineStatus 等服务端管控字段不映射) + /// + public static IotDeviceEntity ToEntity(this IotDeviceDto dto) + { + if (dto == null) return null; + return new IotDeviceEntity + { + Id = ParseId(dto.Id), + Code = dto.Code, + Name = dto.Name, + DeviceType = dto.DeviceType, + GatewayCode = dto.GatewayCode, + SlaveId = dto.SlaveId, + Manufacturer = dto.Manufacturer, + Description = dto.Description, + ProtocolType = dto.ProtocolType, + IsEnabled = dto.IsEnabled, + Remark = dto.Remark + }; + } + #endregion + + #region 设备日志 + /// + /// DeviceLogEntity → DeviceLogDto + /// + public static DeviceLogDto ToDto(this DeviceLogEntity entity) + { + if (entity == null) return null; + return new DeviceLogDto + { + Id = entity.Id.ToString(), + DeviceId = entity.DeviceId.ToString(), + DeviceCode = entity.DeviceCode, + Level = entity.Level, + LogType = entity.LogType, + Message = entity.Message, + LogTime = entity.LogTime, + CreateTime = entity.CreateTime + }; + } + + /// + /// List<DeviceLogEntity> → List<DeviceLogDto> + /// + public static List ToDtoList(this List entities) + { + return entities?.Select(e => e.ToDto()).ToList() ?? new List(); + } + #endregion } } diff --git a/Service/Implement/Config/DeviceService.cs b/Service/Implement/Config/DeviceService.cs new file mode 100644 index 0000000..ffdd447 --- /dev/null +++ b/Service/Implement/Config/DeviceService.cs @@ -0,0 +1,161 @@ +using Model; +using Model.Dto.Config; +using Model.Entity.Config; +using Model.Mapper; +using ORM; +using Service.Interface.Config; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Service.Implement.Config +{ + /// + /// IOT设备管理 服务实现 + /// + public class DeviceService : IDeviceService + { + /// + /// 分页查询设备列表(支持关键字搜索编号/名称/类型) + /// + public async Task>> GetPagedAsync(int pageIndex, int pageSize, RefAsync total, string? keyword) + { + try + { + var list = await SqlSugarContext.DbContext.Queryable() + .Where(x => x.IsDel == 0) + .WhereIF(!string.IsNullOrWhiteSpace(keyword), + x => x.Code.Contains(keyword!) || x.Name.Contains(keyword!) || x.DeviceType.Contains(keyword!)) + .OrderBy(x => x.CreateTime, OrderByType.Desc) + .ToPageListAsync(pageIndex, pageSize, total); + + return Result>.Success(list.ToDtoList()); + } + catch (Exception ex) + { + return Result>.Error("查询设备列表失败", ex); + } + } + + /// + /// 根据 Id 获取设备详情 + /// + public async Task> GetByIdAsync(long id) + { + try + { + var entity = await SqlSugarContext.DbContext.Queryable() + .Where(x => x.Id == id && x.IsDel == 0) + .FirstAsync(); + if (entity == null) + return Result.Error("设备不存在或已被删除"); + return Result.Success(entity.ToDto()); + } + catch (Exception ex) + { + return Result.Error("查询设备详情失败", ex); + } + } + + /// + /// 新增设备(校验编号唯一性) + /// + public async Task AddAsync(IotDeviceDto dto) + { + if (dto == null || string.IsNullOrWhiteSpace(dto.Code)) + return Result.Error("设备编号不能为空"); + if (string.IsNullOrWhiteSpace(dto.Name)) + return Result.Error("设备名称不能为空"); + + try + { + bool exists = await SqlSugarContext.DbContext.Queryable() + .AnyAsync(x => x.Code == dto.Code && x.IsDel == 0); + if (exists) + return Result.Error($"设备编号【{dto.Code}】已存在"); + + var entity = dto.ToEntity(); + entity.CreateTime = DateTime.Now; + // 初始离线 + entity.OnlineStatus = IotDeviceOnlineStatusEnum.Offline; + await SqlSugarContext.DbContext.Insertable(entity).ExecuteCommandAsync(); + return Result.Success(); + } + catch (Exception ex) + { + return Result.Error("新增设备失败", ex); + } + } + + /// + /// 修改设备(在线状态等运行字段不允许被前端覆盖) + /// + public async Task UpdateAsync(IotDeviceDto dto) + { + var entity = dto?.ToEntity(); + if (entity == null || entity.Id <= 0) + return Result.Error("设备Id无效"); + + try + { + // 编号变更时校验唯一性 + bool exists = await SqlSugarContext.DbContext.Queryable() + .AnyAsync(x => x.Code == entity.Code && x.IsDel == 0 && x.Id != entity.Id); + if (exists) + return Result.Error($"设备编号【{entity.Code}】已存在"); + + await SqlSugarContext.DbContext.Updateable(entity) + .IgnoreColumns(x => new { x.CreateTime, x.IsDel, x.OnlineStatus, x.LastCollectTime, x.LastError }) + .ExecuteCommandAsync(); + return Result.Success(); + } + catch (Exception ex) + { + return Result.Error("修改设备失败", ex); + } + } + + /// + /// 删除设备(软删除:IsDel 置 1) + /// + public async Task DeleteAsync(long id) + { + if (id <= 0) + return Result.Error("设备Id无效"); + + try + { + await SqlSugarContext.DbContext.Updateable() + .SetColumns(x => x.IsDel == 1) + .Where(x => x.Id == id) + .ExecuteCommandAsync(); + return Result.Success(); + } + catch (Exception ex) + { + return Result.Error("删除设备失败", ex); + } + } + + /// + /// 分页查询指定设备的日志(设备独享日志) + /// + public async Task>> GetDeviceLogsAsync(long deviceId, int pageIndex, int pageSize, RefAsync total) + { + try + { + var list = await SqlSugarContext.DbContext.Queryable() + .Where(x => x.DeviceId == deviceId && x.IsDel == 0) + .OrderBy(x => x.LogTime, OrderByType.Desc) + .ToPageListAsync(pageIndex, pageSize, total); + + return Result>.Success(list.ToDtoList()); + } + catch (Exception ex) + { + return Result>.Error("查询设备日志失败", ex); + } + } + } +} diff --git a/Service/Implement/Config/IotDeviceService.cs b/Service/Implement/Config/IotDeviceService.cs deleted file mode 100644 index 7c5f911..0000000 --- a/Service/Implement/Config/IotDeviceService.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Service.Interface; - -namespace Service.Implement -{ - /// - /// IOT设备管理 服务实现 - /// - public class IotDeviceService : IIotDeviceService - { - // TODO: 实现 IOT设备管理 相关方法 - } -} diff --git a/Service/Interface/Config/IDeviceService.cs b/Service/Interface/Config/IDeviceService.cs new file mode 100644 index 0000000..34c2e2c --- /dev/null +++ b/Service/Interface/Config/IDeviceService.cs @@ -0,0 +1,44 @@ +using Model; +using Model.Dto.Config; +using SqlSugar; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Service.Interface.Config +{ + /// + /// IOT设备管理 服务接口 + /// + public interface IDeviceService + { + /// + /// 分页查询设备列表(支持关键字搜索编号/名称/类型) + /// + Task>> GetPagedAsync(int pageIndex, int pageSize, RefAsync total, string? keyword); + + /// + /// 根据 Id 获取设备详情 + /// + Task> GetByIdAsync(long id); + + /// + /// 新增设备(校验编号唯一性) + /// + Task AddAsync(IotDeviceDto dto); + + /// + /// 修改设备 + /// + Task UpdateAsync(IotDeviceDto dto); + + /// + /// 删除设备(软删除) + /// + Task DeleteAsync(long id); + + /// + /// 分页查询指定设备的日志(设备独享日志) + /// + Task>> GetDeviceLogsAsync(long deviceId, int pageIndex, int pageSize, RefAsync total); + } +} diff --git a/Service/Interface/Config/IIotDeviceService.cs b/Service/Interface/Config/IIotDeviceService.cs deleted file mode 100644 index 73a6566..0000000 --- a/Service/Interface/Config/IIotDeviceService.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Service.Interface -{ - /// - /// IOT设备管理 服务接口 - /// - public interface IIotDeviceService - { - // TODO: 定义 IOT设备管理 相关方法 - } -}