添加设备类、设备日志类、设备服务类、设备控制器

This commit is contained in:
2026-08-28 17:28:16 +08:00
parent 6c20630a40
commit cfa8e78d33
11 changed files with 667 additions and 23 deletions
@@ -1,4 +1,9 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Model;
using Model.Dto.Config;
using Service.Interface.Config;
using SqlSugar;
using System.Threading.Tasks;
namespace WebAPI.Controllers namespace WebAPI.Controllers
{ {
@@ -9,6 +14,83 @@ namespace WebAPI.Controllers
[Route("api/config/device")] [Route("api/config/device")]
public class IotDeviceController : ControllerBase public class IotDeviceController : ControllerBase
{ {
// TODO: 实现 IOT设备管理 相关接口 private readonly IDeviceService _deviceService;
public IotDeviceController(IDeviceService deviceService)
{
_deviceService = deviceService;
}
/// <summary>
/// 设备列表(分页查询,支持关键字搜索)
/// </summary>
/// <param name="pageIndex">页码(从1开始,默认1</param>
/// <param name="pageSize">每页数量(默认10</param>
/// <param name="keyword">关键字(模糊匹配设备编号/名称/类型)</param>
[HttpGet("list")]
public async Task<IActionResult> GetList(int pageIndex = 1, int pageSize = 10, string? keyword = null)
{
RefAsync<int> total = 0;
var result = await _deviceService.GetPagedAsync(pageIndex, pageSize, total, keyword);
Response.Headers["X-Total-Count"] = total.Value.ToString();
return result.IsSuccess
? Ok(Result<List<IotDeviceDto>>.Success(result.Data))
: Ok(Result<List<IotDeviceDto>>.Error(result.Msg));
}
/// <summary>
/// 设备详情
/// </summary>
/// <param name="id">设备主键 Id</param>
[HttpGet("{id}")]
public async Task<IActionResult> GetById(long id)
{
return Ok(await _deviceService.GetByIdAsync(id));
}
/// <summary>
/// 新增设备
/// </summary>
[HttpPost]
public async Task<IActionResult> Add([FromBody] IotDeviceDto dto)
{
return Ok(await _deviceService.AddAsync(dto));
}
/// <summary>
/// 修改设备
/// </summary>
[HttpPut]
public async Task<IActionResult> Update([FromBody] IotDeviceDto dto)
{
return Ok(await _deviceService.UpdateAsync(dto));
}
/// <summary>
/// 删除设备(软删除)
/// </summary>
/// <param name="id">设备主键 Id</param>
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(long id)
{
return Ok(await _deviceService.DeleteAsync(id));
}
/// <summary>
/// 设备日志(设备独享日志,分页查询)
/// </summary>
/// <param name="id">设备主键 Id</param>
/// <param name="pageIndex">页码(默认1</param>
/// <param name="pageSize">每页数量(默认20</param>
[HttpGet("{id}/logs")]
public async Task<IActionResult> GetLogs(long id, int pageIndex = 1, int pageSize = 20)
{
RefAsync<int> total = 0;
var result = await _deviceService.GetDeviceLogsAsync(id, pageIndex, pageSize, total);
Response.Headers["X-Total-Count"] = total.Value.ToString();
return result.IsSuccess
? Ok(Result<List<DeviceLogDto>>.Success(result.Data))
: Ok(Result<List<DeviceLogDto>>.Error(result.Msg));
}
} }
} }
+32
View File
@@ -0,0 +1,32 @@
namespace Model.Dto.Config
{
/// <summary>
/// 设备日志 DTOId 由 long 改为 string,避免前端精度丢失)
/// </summary>
public class DeviceLogDto
{
/// <summary>主键 Idlong → string</summary>
public string Id { get; set; }
/// <summary>设备Id</summary>
public string DeviceId { get; set; }
/// <summary>设备编号</summary>
public string DeviceCode { get; set; }
/// <summary>日志级别(Info/Warn/Error</summary>
public string Level { get; set; }
/// <summary>日志类型(连接/通讯/采集/指令/系统)</summary>
public string LogType { get; set; }
/// <summary>日志内容</summary>
public string Message { get; set; }
/// <summary>记录时间</summary>
public DateTime LogTime { get; set; }
/// <summary>创建时间</summary>
public DateTime? CreateTime { get; set; }
}
}
+55
View File
@@ -0,0 +1,55 @@
using Model.Entity.Config;
namespace Model.Dto.Config
{
/// <summary>
/// IOT 设备 DTOId 由 long 改为 string,避免前端精度丢失)
/// </summary>
public class IotDeviceDto
{
/// <summary>主键 Idlong → string</summary>
public string Id { get; set; }
/// <summary>设备编号</summary>
public string Code { get; set; }
/// <summary>设备名称</summary>
public string Name { get; set; }
/// <summary>设备类型(型号,对应 DeviceCommand 驱动类)</summary>
public string DeviceType { get; set; }
/// <summary>所属网关Code</summary>
public string GatewayCode { get; set; }
/// <summary>从站地址</summary>
public byte SlaveId { get; set; }
/// <summary>制造商</summary>
public string? Manufacturer { get; set; }
/// <summary>设备描述</summary>
public string? Description { get; set; }
/// <summary>通讯协议类型</summary>
public IotDeviceProtocolEnum ProtocolType { get; set; }
/// <summary>是否启用采集</summary>
public bool IsEnabled { get; set; } = true;
/// <summary>在线状态</summary>
public IotDeviceOnlineStatusEnum OnlineStatus { get; set; }
/// <summary>最后采集时间</summary>
public DateTime? LastCollectTime { get; set; }
/// <summary>最近错误信息</summary>
public string? LastError { get; set; }
/// <summary>备注</summary>
public string? Remark { get; set; }
/// <summary>创建时间</summary>
public DateTime? CreateTime { get; set; }
}
}
+47
View File
@@ -0,0 +1,47 @@
using SqlSugar;
namespace Model.Entity.Config
{
/// <summary>
/// 设备日志(每台设备独享的日志记录)
/// 记录设备的通讯、连接、数据采集等运行日志,按设备查询
/// </summary>
public class DeviceLogEntity : BaseEntity
{
/// <summary>
/// 设备Id(关联 IotDeviceEntity.Id
/// </summary>
[SugarColumn(ColumnDescription = "设备Id")]
public long DeviceId { get; set; }
/// <summary>
/// 设备编号(冗余便于查询展示)
/// </summary>
[SugarColumn(ColumnDescription = "设备编号", Length = 64)]
public string? DeviceCode { get; set; }
/// <summary>
/// 日志级别(Info/Warn/Error
/// </summary>
[SugarColumn(ColumnDescription = "日志级别", Length = 16)]
public string? Level { get; set; } = "Info";
/// <summary>
/// 日志类型(连接/通讯/采集/指令/系统)
/// </summary>
[SugarColumn(ColumnDescription = "日志类型", Length = 32)]
public string? LogType { get; set; }
/// <summary>
/// 日志内容
/// </summary>
[SugarColumn(ColumnDescription = "日志内容", ColumnDataType = "text")]
public string? Message { get; set; }
/// <summary>
/// 记录时间(日志产生时间,用于时间线展示)
/// </summary>
[SugarColumn(ColumnDescription = "记录时间")]
public DateTime LogTime { get; set; }
}
}
+97
View File
@@ -0,0 +1,97 @@
using SqlSugar;
namespace Model.Entity.Config
{
/// <summary>
/// IOT 设备(IOT设备注册与管理 - 设备管理)
/// 一台设备 = 一个从站实例,通过所属网关(IP:端口 或 串口)通讯
/// </summary>
public class IotDeviceEntity : BaseEntity
{
#region
/// <summary>
/// 设备编号(唯一)
/// </summary>
[SugarColumn(ColumnDescription = "设备编号", Length = 64)]
public string Code { get; set; }
/// <summary>
/// 设备名称
/// </summary>
[SugarColumn(ColumnDescription = "设备名称", Length = 100)]
public string Name { get; set; }
/// <summary>
/// 设备类型(型号,如 温度箱/充放电柜,对应 DeviceCommand 驱动类)
/// </summary>
[SugarColumn(ColumnDescription = "设备类型", Length = 64)]
public string DeviceType { get; set; }
/// <summary>
/// 所属网关Code(对应网关实体/通道的标识,连接时据此刻查找 IP/端口/串口)
/// </summary>
[SugarColumn(ColumnDescription = "所属网关Code", Length = 64)]
public string GatewayCode { get; set; }
/// <summary>
/// 从站地址(协议从站号,网关下区分设备)
/// </summary>
[SugarColumn(ColumnDescription = "从站地址")]
public byte SlaveId { get; set; }
/// <summary>
/// 制造商
/// </summary>
[SugarColumn(ColumnDescription = "制造商", Length = 100, IsNullable = true)]
public string? Manufacturer { get; set; }
/// <summary>
/// 设备描述
/// </summary>
[SugarColumn(ColumnDescription = "设备描述", Length = 500, IsNullable = true)]
public string? Description { get; set; }
#endregion
#region
/// <summary>
/// 通讯协议类型(ModbusTcp/ModbusRtu/S7/Tcp/Serial
/// </summary>
[SugarColumn(ColumnDescription = "通讯协议类型")]
public IotDeviceProtocolEnum ProtocolType { get; set; } = IotDeviceProtocolEnum.ModbusTcp;
/// <summary>
/// 是否启用采集(停用则采集调度跳过该设备)
/// </summary>
[SugarColumn(ColumnDescription = "是否启用")]
public bool IsEnabled { get; set; } = true;
#endregion
#region
/// <summary>
/// 在线状态
/// </summary>
[SugarColumn(ColumnDescription = "在线状态")]
public IotDeviceOnlineStatusEnum OnlineStatus { get; set; } = IotDeviceOnlineStatusEnum.Offline;
/// <summary>
/// 最后采集时间
/// </summary>
[SugarColumn(ColumnDescription = "最后采集时间", IsNullable = true)]
public DateTime? LastCollectTime { get; set; }
/// <summary>
/// 最近错误信息(通讯异常时记录)
/// </summary>
[SugarColumn(ColumnDescription = "最近错误", Length = 500, IsNullable = true)]
public string? LastError { get; set; }
#endregion
#region
/// <summary>
/// 备注
/// </summary>
[SugarColumn(ColumnDescription = "备注", Length = 500, IsNullable = true)]
public string? Remark { get; set; }
#endregion
}
}
+59
View File
@@ -0,0 +1,59 @@
namespace Model.Entity.Config
{
/// <summary>
/// 设备通讯协议类型(决定使用 DeviceCommand 中哪类驱动)
/// </summary>
public enum IotDeviceProtocolEnum
{
/// <summary>
/// Modbus TCP(网口,IP + 端口 + 从站地址)
/// </summary>
ModbusTcp = 1,
/// <summary>
/// Modbus RTU(串口,COM 口 + 波特率 + 从站地址)
/// </summary>
ModbusRtu = 2,
/// <summary>
/// 西门子 S7 协议
/// </summary>
S7 = 3,
/// <summary>
/// 自定义 TCP 协议
/// </summary>
Tcp = 4,
/// <summary>
/// 串口自定义协议
/// </summary>
Serial = 5
}
/// <summary>
/// 设备在线状态
/// </summary>
public enum IotDeviceOnlineStatusEnum
{
/// <summary>
/// 离线
/// </summary>
Offline = 0,
/// <summary>
/// 在线
/// </summary>
Online = 1,
/// <summary>
/// 连接中(网关已连,设备待确认)
/// </summary>
Connecting = 2,
/// <summary>
/// 通讯异常
/// </summary>
Fault = 3
}
}
+89
View File
@@ -1,6 +1,8 @@
using Model.Dto.Asset; using Model.Dto.Asset;
using Model.Dto.Config;
using Model.Dto.Inspection; using Model.Dto.Inspection;
using Model.Entity.Asset; using Model.Entity.Asset;
using Model.Entity.Config;
using Model.Entity.Inspection; using Model.Entity.Inspection;
namespace Model.Mapper namespace Model.Mapper
@@ -387,5 +389,92 @@ namespace Model.Mapper
}; };
} }
#endregion #endregion
#region IOT设备
/// <summary>
/// IotDeviceEntity → IotDeviceDto
/// </summary>
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
};
}
/// <summary>
/// List&lt;IotDeviceEntity&gt; → List&lt;IotDeviceDto&gt;
/// </summary>
public static List<IotDeviceDto> ToDtoList(this List<IotDeviceEntity> entities)
{
return entities?.Select(e => e.ToDto()).ToList() ?? new List<IotDeviceDto>();
}
/// <summary>
/// IotDeviceDto → IotDeviceEntity(入参映射,IsDel/CreateTime/OnlineStatus 等服务端管控字段不映射)
/// </summary>
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
/// <summary>
/// DeviceLogEntity → DeviceLogDto
/// </summary>
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
};
}
/// <summary>
/// List&lt;DeviceLogEntity&gt; → List&lt;DeviceLogDto&gt;
/// </summary>
public static List<DeviceLogDto> ToDtoList(this List<DeviceLogEntity> entities)
{
return entities?.Select(e => e.ToDto()).ToList() ?? new List<DeviceLogDto>();
}
#endregion
} }
} }
+161
View File
@@ -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
{
/// <summary>
/// IOT设备管理 服务实现
/// </summary>
public class DeviceService : IDeviceService
{
/// <summary>
/// 分页查询设备列表(支持关键字搜索编号/名称/类型)
/// </summary>
public async Task<Result<List<IotDeviceDto>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, string? keyword)
{
try
{
var list = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
.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<List<IotDeviceDto>>.Success(list.ToDtoList());
}
catch (Exception ex)
{
return Result<List<IotDeviceDto>>.Error("查询设备列表失败", ex);
}
}
/// <summary>
/// 根据 Id 获取设备详情
/// </summary>
public async Task<Result<IotDeviceDto>> GetByIdAsync(long id)
{
try
{
var entity = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
.Where(x => x.Id == id && x.IsDel == 0)
.FirstAsync();
if (entity == null)
return Result<IotDeviceDto>.Error("设备不存在或已被删除");
return Result<IotDeviceDto>.Success(entity.ToDto());
}
catch (Exception ex)
{
return Result<IotDeviceDto>.Error("查询设备详情失败", ex);
}
}
/// <summary>
/// 新增设备(校验编号唯一性)
/// </summary>
public async Task<Result> 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<IotDeviceEntity>()
.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);
}
}
/// <summary>
/// 修改设备(在线状态等运行字段不允许被前端覆盖)
/// </summary>
public async Task<Result> UpdateAsync(IotDeviceDto dto)
{
var entity = dto?.ToEntity();
if (entity == null || entity.Id <= 0)
return Result.Error("设备Id无效");
try
{
// 编号变更时校验唯一性
bool exists = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
.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);
}
}
/// <summary>
/// 删除设备(软删除:IsDel 置 1)
/// </summary>
public async Task<Result> DeleteAsync(long id)
{
if (id <= 0)
return Result.Error("设备Id无效");
try
{
await SqlSugarContext.DbContext.Updateable<IotDeviceEntity>()
.SetColumns(x => x.IsDel == 1)
.Where(x => x.Id == id)
.ExecuteCommandAsync();
return Result.Success();
}
catch (Exception ex)
{
return Result.Error("删除设备失败", ex);
}
}
/// <summary>
/// 分页查询指定设备的日志(设备独享日志)
/// </summary>
public async Task<Result<List<DeviceLogDto>>> GetDeviceLogsAsync(long deviceId, int pageIndex, int pageSize, RefAsync<int> total)
{
try
{
var list = await SqlSugarContext.DbContext.Queryable<DeviceLogEntity>()
.Where(x => x.DeviceId == deviceId && x.IsDel == 0)
.OrderBy(x => x.LogTime, OrderByType.Desc)
.ToPageListAsync(pageIndex, pageSize, total);
return Result<List<DeviceLogDto>>.Success(list.ToDtoList());
}
catch (Exception ex)
{
return Result<List<DeviceLogDto>>.Error("查询设备日志失败", ex);
}
}
}
}
@@ -1,12 +0,0 @@
using Service.Interface;
namespace Service.Implement
{
/// <summary>
/// IOT设备管理 服务实现
/// </summary>
public class IotDeviceService : IIotDeviceService
{
// TODO: 实现 IOT设备管理 相关方法
}
}
@@ -0,0 +1,44 @@
using Model;
using Model.Dto.Config;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Service.Interface.Config
{
/// <summary>
/// IOT设备管理 服务接口
/// </summary>
public interface IDeviceService
{
/// <summary>
/// 分页查询设备列表(支持关键字搜索编号/名称/类型)
/// </summary>
Task<Result<List<IotDeviceDto>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, string? keyword);
/// <summary>
/// 根据 Id 获取设备详情
/// </summary>
Task<Result<IotDeviceDto>> GetByIdAsync(long id);
/// <summary>
/// 新增设备(校验编号唯一性)
/// </summary>
Task<Result> AddAsync(IotDeviceDto dto);
/// <summary>
/// 修改设备
/// </summary>
Task<Result> UpdateAsync(IotDeviceDto dto);
/// <summary>
/// 删除设备(软删除)
/// </summary>
Task<Result> DeleteAsync(long id);
/// <summary>
/// 分页查询指定设备的日志(设备独享日志)
/// </summary>
Task<Result<List<DeviceLogDto>>> GetDeviceLogsAsync(long deviceId, int pageIndex, int pageSize, RefAsync<int> total);
}
}
@@ -1,10 +0,0 @@
namespace Service.Interface
{
/// <summary>
/// IOT设备管理 服务接口
/// </summary>
public interface IIotDeviceService
{
// TODO: 定义 IOT设备管理 相关方法
}
}