【代码新增】IoT:增加 device 配置下发(设置)实现

This commit is contained in:
YunaiV
2025-01-31 23:14:09 +08:00
parent 47c281d933
commit f46a2fb011
9 changed files with 175 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.iot.plugin.common.downstream;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceConfigSetReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertyGetReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertySetReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceServiceInvokeReqDTO;
@@ -38,4 +39,12 @@ public interface IotDeviceDownstreamHandler {
*/
CommonResult<Boolean> setDeviceProperty(IotDevicePropertySetReqDTO setReqDTO);
/**
* 设置设备配置
*
* @param setReqDTO 设置设备配置的请求
* @return 是否成功
*/
CommonResult<Boolean> setDeviceConfig(IotDeviceConfigSetReqDTO setReqDTO);
}

View File

@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.iot.plugin.common.downstream;
import cn.iocoder.yudao.module.iot.plugin.common.config.IotPluginCommonProperties;
import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDeviceConfigSetVertxHandler;
import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDevicePropertyGetVertxHandler;
import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDevicePropertySetVertxHandler;
import cn.iocoder.yudao.module.iot.plugin.common.downstream.router.IotDeviceServiceInvokeVertxHandler;
@@ -36,6 +37,8 @@ public class IotDeviceDownstreamServer {
.handler(new IotDevicePropertySetVertxHandler(deviceDownstreamHandler));
router.post(IotDevicePropertyGetVertxHandler.PATH)
.handler(new IotDevicePropertyGetVertxHandler(deviceDownstreamHandler));
router.post(IotDeviceConfigSetVertxHandler.PATH)
.handler(new IotDeviceConfigSetVertxHandler(deviceDownstreamHandler));
// 创建 HttpServer 实例
this.server = vertx.createHttpServer().requestHandler(router);
}

View File

@@ -0,0 +1,61 @@
package cn.iocoder.yudao.module.iot.plugin.common.downstream.router;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceConfigSetReqDTO;
import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
import cn.iocoder.yudao.module.iot.plugin.common.util.IotPluginCommonUtils;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
/**
* IOT 设备配置设置 Vertx Handler
*
* 芋道源码
*/
@Slf4j
@RequiredArgsConstructor
public class IotDeviceConfigSetVertxHandler implements Handler<RoutingContext> {
public static final String PATH = "/sys/:productKey/:deviceName/thing/service/config/set";
private final IotDeviceDownstreamHandler deviceDownstreamHandler;
@Override
@SuppressWarnings("unchecked")
public void handle(RoutingContext routingContext) {
// 1. 解析参数
IotDeviceConfigSetReqDTO reqDTO;
try {
String productKey = routingContext.pathParam("productKey");
String deviceName = routingContext.pathParam("deviceName");
JsonObject body = routingContext.body().asJsonObject();
String requestId = body.getString("requestId");
Map<String, Object> config = (Map<String, Object>) body.getMap().get("config");
reqDTO = ((IotDeviceConfigSetReqDTO) new IotDeviceConfigSetReqDTO()
.setRequestId(requestId).setProductKey(productKey).setDeviceName(deviceName))
.setConfig(config);
} catch (Exception e) {
log.error("[handle][路径参数({}) 解析参数失败]", routingContext.pathParams(), e);
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(BAD_REQUEST));
return;
}
// 2. 调用处理器
try {
CommonResult<Boolean> result = deviceDownstreamHandler.setDeviceConfig(reqDTO);
IotPluginCommonUtils.writeJson(routingContext, result);
} catch (Exception e) {
log.error("[handle][请求参数({}) 配置设置异常]", reqDTO, e);
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(INTERNAL_SERVER_ERROR));
}
}
}

View File

@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.iot.plugin.http.downstream;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceConfigSetReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertyGetReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertySetReqDTO;
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceServiceInvokeReqDTO;
@@ -33,4 +34,9 @@ public class IotDeviceDownstreamHandlerImpl implements IotDeviceDownstreamHandle
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持设置设备属性");
}
@Override
public CommonResult<Boolean> setDeviceConfig(IotDeviceConfigSetReqDTO setReqDTO) {
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持设置设备属性");
}
}