From 809cb452923c862e6223613fd3687360262e036c Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 18 Jan 2026 00:08:58 +0800 Subject: [PATCH] feat:【iot】modbus-tcp 协议接入 100%:和管理后台对接,并修复少量 bug --- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java | 78 ++---------------------------------------------------------------------------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusConfigController.java | 13 ++----------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusPointController.java | 10 +++++----- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigService.java | 7 ------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigServiceImpl.java | 17 ----------------- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusPointServiceImpl.java | 3 ++- yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpBinaryDeviceMessageCodec.java | 2 +- yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpJsonDeviceMessageCodec.java | 2 +- yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-binary-packet-examples.md | 2 +- yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-json-packet-examples.md | 2 +- 10 files changed, 15 insertions(+), 121 deletions(-) diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java index a1aca2a..4a4d194 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.enums.RpcConstants; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; import cn.iocoder.yudao.module.iot.core.biz.IotDeviceCommonApi; import cn.iocoder.yudao.module.iot.core.biz.dto.*; import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; @@ -22,7 +23,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -74,12 +74,8 @@ public class IoTDeviceApiImpl implements IotDeviceCommonApi { @Override @PostMapping(RpcConstants.RPC_API_PREFIX + "/iot/modbus/enabled-configs") @PermitAll + @TenantIgnore public CommonResult> getEnabledModbusDeviceConfigs() { - // TODO @芋艿:临时 mock 数据,用于测试 ModbusTcpSlaveSimulator - if (true) { - return success(buildMockModbusConfigs()); - } - // 1. 获取所有启用的 Modbus 连接配置 List configList = modbusConfigService.getEnabledDeviceModbusConfigList(); if (CollUtil.isEmpty(configList)) { @@ -111,74 +107,4 @@ public class IoTDeviceApiImpl implements IotDeviceCommonApi { return success(result); } - /** - * 构建 Mock Modbus 配置,对接 ModbusTcpSlaveSimulator - * - * 设备:productKey=4aymZgOTOOCrDKRT, deviceName=small - * 物模型字段:width, height, oneThree - */ - private List buildMockModbusConfigs() { - List configs = new ArrayList<>(); - - // 设备配置 - IotModbusDeviceConfigRespDTO config = new IotModbusDeviceConfigRespDTO(); - config.setDeviceId(1L); - config.setProductKey("4aymZgOTOOCrDKRT"); - config.setDeviceName("small"); - config.setIp("127.0.0.1"); - config.setPort(5020); // 对应 ModbusTcpSlaveSimulator 的端口 - config.setSlaveId(1); - config.setTimeout(3000); - config.setRetryInterval(5000); - - // 点位配置(对应物模型字段:width, height, oneThree) - List points = new ArrayList<>(); - - // 点位 1:width - 读取保持寄存器地址 0(功能码 03) - IotModbusPointRespDTO point1 = new IotModbusPointRespDTO(); - point1.setId(1L); - point1.setIdentifier("width"); - point1.setName("宽度"); - point1.setFunctionCode(3); // 读保持寄存器 - point1.setRegisterAddress(0); - point1.setRegisterCount(1); - point1.setByteOrder("AB"); - point1.setRawDataType("INT16"); - point1.setScale(BigDecimal.ONE); - point1.setPollInterval(3000); // 3 秒轮询 - points.add(point1); - - // 点位 2:height - 读取保持寄存器地址 1(功能码 03) - IotModbusPointRespDTO point2 = new IotModbusPointRespDTO(); - point2.setId(2L); - point2.setIdentifier("height"); - point2.setName("高度"); - point2.setFunctionCode(3); // 读保持寄存器 - point2.setRegisterAddress(1); - point2.setRegisterCount(1); - point2.setByteOrder("AB"); - point2.setRawDataType("INT16"); - point2.setScale(BigDecimal.ONE); - point2.setPollInterval(3000); // 3 秒轮询 - points.add(point2); - - // 点位 3:oneThree - 读取保持寄存器地址 2(功能码 03) - IotModbusPointRespDTO point3 = new IotModbusPointRespDTO(); - point3.setId(3L); - point3.setIdentifier("oneThree"); - point3.setName("一三"); - point3.setFunctionCode(3); // 读保持寄存器 - point3.setRegisterAddress(2); - point3.setRegisterCount(1); - point3.setByteOrder("AB"); - point3.setRawDataType("INT16"); - point3.setScale(BigDecimal.ONE); - point3.setPollInterval(3000); // 3 秒轮询 - points.add(point3); - - config.setPoints(points); - configs.add(config); - return configs; - } - } \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusConfigController.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusConfigController.java index bd42a6f..576d904 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusConfigController.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusConfigController.java @@ -28,26 +28,17 @@ public class IotDeviceModbusConfigController { @PostMapping("/save") @Operation(summary = "保存设备 Modbus 连接配置") - @PreAuthorize("@ss.hasPermission('iot:device-modbus-config:create')") + @PreAuthorize("@ss.hasPermission('iot:device:update')") public CommonResult saveDeviceModbusConfig(@Valid @RequestBody IotDeviceModbusConfigSaveReqVO saveReqVO) { modbusConfigService.saveDeviceModbusConfig(saveReqVO); return success(true); } - @DeleteMapping("/delete") - @Operation(summary = "删除设备 Modbus 连接配置") - @Parameter(name = "id", description = "编号", required = true) - @PreAuthorize("@ss.hasPermission('iot:device-modbus-config:delete')") - public CommonResult deleteDeviceModbusConfig(@RequestParam("id") Long id) { - modbusConfigService.deleteDeviceModbusConfig(id); - return success(true); - } - @GetMapping("/get") @Operation(summary = "获得设备 Modbus 连接配置") @Parameter(name = "id", description = "编号", example = "1024") @Parameter(name = "deviceId", description = "设备编号", example = "2048") - @PreAuthorize("@ss.hasPermission('iot:device-modbus-config:query')") + @PreAuthorize("@ss.hasPermission('iot:device:query')") public CommonResult getDeviceModbusConfig( @RequestParam(value = "id", required = false) Long id, @RequestParam(value = "deviceId", required = false) Long deviceId) { diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusPointController.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusPointController.java index 22d1743..4e813d8 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusPointController.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceModbusPointController.java @@ -30,14 +30,14 @@ public class IotDeviceModbusPointController { @PostMapping("/create") @Operation(summary = "创建设备 Modbus 点位配置") - @PreAuthorize("@ss.hasPermission('iot:device-modbus-point:create')") + @PreAuthorize("@ss.hasPermission('iot:device:update')") public CommonResult createDeviceModbusPoint(@Valid @RequestBody IotDeviceModbusPointSaveReqVO createReqVO) { return success(modbusPointService.createDeviceModbusPoint(createReqVO)); } @PutMapping("/update") @Operation(summary = "更新设备 Modbus 点位配置") - @PreAuthorize("@ss.hasPermission('iot:device-modbus-point:update')") + @PreAuthorize("@ss.hasPermission('iot:device:update')") public CommonResult updateDeviceModbusPoint(@Valid @RequestBody IotDeviceModbusPointSaveReqVO updateReqVO) { modbusPointService.updateDeviceModbusPoint(updateReqVO); return success(true); @@ -46,7 +46,7 @@ public class IotDeviceModbusPointController { @DeleteMapping("/delete") @Operation(summary = "删除设备 Modbus 点位配置") @Parameter(name = "id", description = "编号", required = true) - @PreAuthorize("@ss.hasPermission('iot:device-modbus-point:delete')") + @PreAuthorize("@ss.hasPermission('iot:device:update')") public CommonResult deleteDeviceModbusPoint(@RequestParam("id") Long id) { modbusPointService.deleteDeviceModbusPoint(id); return success(true); @@ -55,7 +55,7 @@ public class IotDeviceModbusPointController { @GetMapping("/get") @Operation(summary = "获得设备 Modbus 点位配置") @Parameter(name = "id", description = "编号", required = true, example = "1024") - @PreAuthorize("@ss.hasPermission('iot:device-modbus-point:query')") + @PreAuthorize("@ss.hasPermission('iot:device:query')") public CommonResult getDeviceModbusPoint(@RequestParam("id") Long id) { IotDeviceModbusPointDO modbusPoint = modbusPointService.getDeviceModbusPoint(id); return success(BeanUtils.toBean(modbusPoint, IotDeviceModbusPointRespVO.class)); @@ -63,7 +63,7 @@ public class IotDeviceModbusPointController { @GetMapping("/page") @Operation(summary = "获得设备 Modbus 点位配置分页") - @PreAuthorize("@ss.hasPermission('iot:device-modbus-point:query')") + @PreAuthorize("@ss.hasPermission('iot:device:query')") public CommonResult> getDeviceModbusPointPage(@Valid IotDeviceModbusPointPageReqVO pageReqVO) { PageResult pageResult = modbusPointService.getDeviceModbusPointPage(pageReqVO); return success(BeanUtils.toBean(pageResult, IotDeviceModbusPointRespVO.class)); diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigService.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigService.java index 0c2c3e2..1bd17e4 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigService.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigService.java @@ -21,13 +21,6 @@ public interface IotDeviceModbusConfigService { void saveDeviceModbusConfig(@Valid IotDeviceModbusConfigSaveReqVO saveReqVO); /** - * 删除设备 Modbus 连接配置 - * - * @param id 编号 - */ - void deleteDeviceModbusConfig(Long id); - - /** * 获得设备 Modbus 连接配置 * * @param id 编号 diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigServiceImpl.java index 2447e46..91388d2 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusConfigServiceImpl.java @@ -11,9 +11,6 @@ import org.springframework.validation.annotation.Validated; import java.util.List; -import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_MODBUS_CONFIG_NOT_EXISTS; - /** * IoT 设备 Modbus 连接配置 Service 实现类 * @@ -47,20 +44,6 @@ public class IotDeviceModbusConfigServiceImpl implements IotDeviceModbusConfigSe } @Override - public void deleteDeviceModbusConfig(Long id) { - // 校验存在 - validateDeviceModbusConfigExists(id); - // 删除 - modbusConfigMapper.deleteById(id); - } - - private void validateDeviceModbusConfigExists(Long id) { - if (modbusConfigMapper.selectById(id) == null) { - throw exception(DEVICE_MODBUS_CONFIG_NOT_EXISTS); - } - } - - @Override public IotDeviceModbusConfigDO getDeviceModbusConfig(Long id) { return modbusConfigMapper.selectById(id); } diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusPointServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusPointServiceImpl.java index 242e1c9..906697b 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusPointServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceModbusPointServiceImpl.java @@ -70,7 +70,8 @@ public class IotDeviceModbusPointServiceImpl implements IotDeviceModbusPointServ validateDeviceModbusPointUnique(updateReqVO.getDeviceId(), thingModel.getIdentifier(), updateReqVO.getId()); // 2. 更新 - IotDeviceModbusPointDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceModbusPointDO.class); + IotDeviceModbusPointDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceModbusPointDO.class, + o -> o.setIdentifier(thingModel.getIdentifier()).setName(thingModel.getName())); modbusPointMapper.updateById(updateObj); } diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpBinaryDeviceMessageCodec.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpBinaryDeviceMessageCodec.java index 4f42a8c..c1b6cc3 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpBinaryDeviceMessageCodec.java +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpBinaryDeviceMessageCodec.java @@ -41,7 +41,7 @@ import java.nio.charset.StandardCharsets; @Component public class IotTcpBinaryDeviceMessageCodec implements IotDeviceMessageCodec { - public static final String TYPE = "TCP_BINARY"; + public static final String TYPE = "TcpBinary"; /** * 协议魔术字,用于协议识别 diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpJsonDeviceMessageCodec.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpJsonDeviceMessageCodec.java index 10ffbdf..99082b4 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpJsonDeviceMessageCodec.java +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpJsonDeviceMessageCodec.java @@ -29,7 +29,7 @@ import org.springframework.stereotype.Component; @Component public class IotTcpJsonDeviceMessageCodec implements IotDeviceMessageCodec { - public static final String TYPE = "TCP_JSON"; + public static final String TYPE = "TcpJson"; @Data @NoArgsConstructor diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-binary-packet-examples.md b/yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-binary-packet-examples.md index d6b2b3f..8e20378 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-binary-packet-examples.md +++ b/yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-binary-packet-examples.md @@ -162,7 +162,7 @@ E8 AF 81 E6 88 90 E5 8A 9F 22 ## 4. 编解码器标识 ```java -public static final String TYPE = "TCP_BINARY"; +public static final String TYPE = "TcpBinary"; ``` ## 5. 协议优势 diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-json-packet-examples.md b/yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-json-packet-examples.md index 09ef50c..5bfa42b 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-json-packet-examples.md +++ b/yudao-module-iot/yudao-module-iot-gateway/src/test/resources/tcp-json-packet-examples.md @@ -161,7 +161,7 @@ TCP JSON 格式协议采用纯 JSON 格式进行数据传输,具有以下特ç‚ ## 4. 编解码器标识 ```java -public static final String TYPE = "TCP_JSON"; +public static final String TYPE = "TcpJson"; ``` ## 5. 协议优势 -- libgit2 0.21.4