【代码新增】IoT:实现 device 下行服务调用的逻辑
This commit is contained in:
@@ -14,10 +14,28 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceSe
|
||||
*/
|
||||
public interface IotDeviceDownstreamHandler {
|
||||
|
||||
/**
|
||||
* 调用设备服务
|
||||
*
|
||||
* @param invokeReqDTO 调用设备服务的请求
|
||||
* @return 是否成功
|
||||
*/
|
||||
CommonResult<Boolean> invokeDeviceService(IotDeviceServiceInvokeReqDTO invokeReqDTO);
|
||||
|
||||
/**
|
||||
* 获取设备属性
|
||||
*
|
||||
* @param getReqDTO 获取设备属性的请求
|
||||
* @return 是否成功
|
||||
*/
|
||||
CommonResult<Boolean> getDeviceProperty(IotDevicePropertyGetReqDTO getReqDTO);
|
||||
|
||||
/**
|
||||
* 设置设备属性
|
||||
*
|
||||
* @param setReqDTO 设置设备属性的请求
|
||||
* @return 是否成功
|
||||
*/
|
||||
CommonResult<Boolean> setDeviceProperty(IotDevicePropertySetReqDTO setReqDTO);
|
||||
|
||||
}
|
||||
|
@@ -1,10 +1,18 @@
|
||||
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.IotDeviceServiceInvokeReqDTO;
|
||||
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.ext.web.RoutingContext;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@@ -15,9 +23,34 @@ public class IotDeviceServiceInvokeVertxHandler implements Handler<RoutingContex
|
||||
private final IotDeviceDownstreamHandler deviceDownstreamHandler;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void handle(RoutingContext routingContext) {
|
||||
// TODO 芋艿:这里没实现
|
||||
deviceDownstreamHandler.invokeDeviceService(null);
|
||||
// 1. 解析参数
|
||||
IotDeviceServiceInvokeReqDTO invokeReqDTO;
|
||||
try {
|
||||
String productKey = routingContext.pathParam("productKey");
|
||||
String deviceName = routingContext.pathParam("deviceName");
|
||||
String identifier = routingContext.pathParam("identifier");
|
||||
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()
|
||||
.setRequestId(requestId).setProductKey(productKey).setDeviceName(deviceName))
|
||||
.setIdentifier(identifier).setParams(params);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][解析参数失败]", e);
|
||||
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(BAD_REQUEST));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用下游处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.invokeDeviceService(invokeReqDTO);
|
||||
IotPluginCommonUtils.writeJson(routingContext, result);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) 服务调用异常]", invokeReqDTO, e);
|
||||
IotPluginCommonUtils.writeJson(routingContext, CommonResult.error(INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,6 +3,11 @@ package cn.iocoder.yudao.module.iot.plugin.common.util;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.system.SystemUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import io.vertx.core.http.HttpHeaders;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* IoT 插件的通用工具类
|
||||
@@ -28,4 +33,12 @@ public class IotPluginCommonUtils {
|
||||
SystemUtil.getHostInfo().getAddress(), SystemUtil.getCurrentPID(), IdUtil.fastSimpleUUID());
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void writeJson(RoutingContext routingContext, CommonResult<?> result) {
|
||||
routingContext.response()
|
||||
.setStatusCode(200)
|
||||
.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
.end(JsonUtils.toJsonString(result));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package cn.iocoder.yudao.module.iot.plugin.http.config;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import cn.iocoder.yudao.module.iot.plugin.http.downstream.IotDeviceDownstreamHandlerImpl;
|
||||
import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -26,4 +28,9 @@ public class IotPluginHttpAutoConfiguration {
|
||||
return new IotDeviceUpstreamServer(port, deviceUpstreamApi);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IotDeviceDownstreamHandler deviceDownstreamHandler() {
|
||||
return new IotDeviceDownstreamHandlerImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -5,7 +5,8 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePr
|
||||
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDevicePropertySetReqDTO;
|
||||
import cn.iocoder.yudao.module.iot.api.device.dto.control.downstream.IotDeviceServiceInvokeReqDTO;
|
||||
import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
|
||||
|
||||
/**
|
||||
* HTTP 插件的 {@link IotDeviceDownstreamHandler} 实现类
|
||||
@@ -15,28 +16,21 @@ import org.springframework.stereotype.Component;
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Component // TODO @芋艿:后续统一处理
|
||||
public class IotDeviceDownstreamHandlerImpl implements IotDeviceDownstreamHandler {
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> invokeDeviceService(IotDeviceServiceInvokeReqDTO invokeReqDTO) {
|
||||
// TODO @芋艿:待实现
|
||||
System.out.println();
|
||||
return null;
|
||||
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持调用设备服务");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> getDeviceProperty(IotDevicePropertyGetReqDTO getReqDTO) {
|
||||
// TODO @芋艿:待实现
|
||||
System.out.println();
|
||||
return null;
|
||||
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持获取设备属性");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> setDeviceProperty(IotDevicePropertySetReqDTO setReqDTO) {
|
||||
// TODO @芋艿:待实现
|
||||
System.out.println();
|
||||
return null;
|
||||
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持设置设备属性");
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user