【代码新增】IoT:实现 device 下行属性获取、设置的下行消息
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package cn.iocoder.yudao.module.iot.plugin.common.downstream;
|
||||
|
||||
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;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
@@ -24,8 +26,12 @@ public class IotDeviceDownstreamServer {
|
||||
// 创建 Router 实例
|
||||
Router router = Router.router(vertx);
|
||||
router.route().handler(BodyHandler.create()); // 处理 Body
|
||||
router.post(IotDeviceServiceInvokeVertxHandler.PATH).handler(
|
||||
new IotDeviceServiceInvokeVertxHandler(deviceDownstreamHandler)); // 处理 Service Invoke
|
||||
router.post(IotDeviceServiceInvokeVertxHandler.PATH)
|
||||
.handler(new IotDeviceServiceInvokeVertxHandler(deviceDownstreamHandler));
|
||||
router.post(IotDevicePropertySetVertxHandler.PATH)
|
||||
.handler(new IotDevicePropertySetVertxHandler(deviceDownstreamHandler));
|
||||
router.post(IotDevicePropertyGetVertxHandler.PATH)
|
||||
.handler(new IotDevicePropertyGetVertxHandler(deviceDownstreamHandler));
|
||||
// 创建 HttpServer 实例
|
||||
this.server = vertx.createHttpServer().requestHandler(router);
|
||||
}
|
||||
|
@@ -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.IotDevicePropertyGetReqDTO;
|
||||
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.List;
|
||||
|
||||
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 IotDevicePropertyGetVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
public static final String PATH = "/sys/:productKey/:deviceName/thing/service/property/get";
|
||||
|
||||
private final IotDeviceDownstreamHandler deviceDownstreamHandler;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void handle(RoutingContext routingContext) {
|
||||
// 1. 解析参数
|
||||
IotDevicePropertyGetReqDTO reqDTO;
|
||||
try {
|
||||
String productKey = routingContext.pathParam("productKey");
|
||||
String deviceName = routingContext.pathParam("deviceName");
|
||||
JsonObject body = routingContext.body().asJsonObject();
|
||||
String requestId = body.getString("requestId");
|
||||
List<String> identifiers = (List<String>) body.getMap().get("identifiers");
|
||||
reqDTO = ((IotDevicePropertyGetReqDTO) new IotDevicePropertyGetReqDTO()
|
||||
.setRequestId(requestId).setProductKey(productKey).setDeviceName(deviceName))
|
||||
.setIdentifiers(identifiers);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][路径参数({}) 解析参数失败]", routingContext.pathParams(), e);
|
||||
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(BAD_REQUEST));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.getDeviceProperty(reqDTO);
|
||||
IotPluginCommonUtils.writeJson(routingContext, result);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) 属性获取异常]", reqDTO, e);
|
||||
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -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.IotDevicePropertySetReqDTO;
|
||||
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 IotDevicePropertySetVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
public static final String PATH = "/sys/:productKey/:deviceName/thing/service/property/set";
|
||||
|
||||
private final IotDeviceDownstreamHandler deviceDownstreamHandler;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void handle(RoutingContext routingContext) {
|
||||
// 1. 解析参数
|
||||
IotDevicePropertySetReqDTO 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> properties = (Map<String, Object>) body.getMap().get("properties");
|
||||
reqDTO = ((IotDevicePropertySetReqDTO) new IotDevicePropertySetReqDTO()
|
||||
.setRequestId(requestId).setProductKey(productKey).setDeviceName(deviceName))
|
||||
.setProperties(properties);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][路径参数({}) 解析参数失败]", routingContext.pathParams(), e);
|
||||
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(BAD_REQUEST));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.setDeviceProperty(reqDTO);
|
||||
IotPluginCommonUtils.writeJson(routingContext, result);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) 属性设置异常]", reqDTO, e);
|
||||
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -14,6 +14,11 @@ 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 IotDeviceServiceInvokeVertxHandler implements Handler<RoutingContext> {
|
||||
@@ -26,7 +31,7 @@ public class IotDeviceServiceInvokeVertxHandler implements Handler<RoutingContex
|
||||
@SuppressWarnings("unchecked")
|
||||
public void handle(RoutingContext routingContext) {
|
||||
// 1. 解析参数
|
||||
IotDeviceServiceInvokeReqDTO invokeReqDTO;
|
||||
IotDeviceServiceInvokeReqDTO reqDTO;
|
||||
try {
|
||||
String productKey = routingContext.pathParam("productKey");
|
||||
String deviceName = routingContext.pathParam("deviceName");
|
||||
@@ -34,21 +39,21 @@ public class IotDeviceServiceInvokeVertxHandler implements Handler<RoutingContex
|
||||
JsonObject body = routingContext.body().asJsonObject();
|
||||
String requestId = body.getString("requestId");
|
||||
Map<String, Object> params = (Map<String, Object>) body.getMap().get("params");
|
||||
invokeReqDTO = ((IotDeviceServiceInvokeReqDTO) new IotDeviceServiceInvokeReqDTO()
|
||||
reqDTO = ((IotDeviceServiceInvokeReqDTO) new IotDeviceServiceInvokeReqDTO()
|
||||
.setRequestId(requestId).setProductKey(productKey).setDeviceName(deviceName))
|
||||
.setIdentifier(identifier).setParams(params);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][解析参数失败]", e);
|
||||
log.error("[handle][路径参数({}) 解析参数失败]", routingContext.pathParams(), e);
|
||||
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(BAD_REQUEST));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用下游处理器
|
||||
// 2. 调用处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.invokeDeviceService(invokeReqDTO);
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.invokeDeviceService(reqDTO);
|
||||
IotPluginCommonUtils.writeJson(routingContext, result);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) 服务调用异常]", invokeReqDTO, e);
|
||||
log.error("[handle][请求参数({}) 服务调用异常]", reqDTO, e);
|
||||
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user