1. 统一包名修改
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>zt-module-iot-plugins</artifactId>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>zt-module-iot-plugin-common</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>
|
||||
<!-- TODO @芋艿:注释 -->
|
||||
物联网 插件 模块 - 通用功能
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<artifactId>zt-module-iot-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Web 相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 工具类相关 -->
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 参数校验 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.zt.plat.module.iot.plugin.common.config;
|
||||
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamServer;
|
||||
import com.zt.plat.module.iot.plugin.common.heartbeat.IotPluginInstanceHeartbeatJob;
|
||||
import com.zt.plat.module.iot.plugin.common.upstream.IotDeviceUpstreamClient;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* IoT 插件的通用自动配置类
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(IotPluginCommonProperties.class)
|
||||
@EnableScheduling // 开启定时任务,因为 IotPluginInstanceHeartbeatJob 是一个定时任务
|
||||
public class IotPluginCommonAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate(IotPluginCommonProperties properties) {
|
||||
return new RestTemplateBuilder()
|
||||
.connectTimeout(properties.getUpstreamConnectTimeout())
|
||||
.readTimeout(properties.getUpstreamReadTimeout())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IotDeviceUpstreamApi deviceUpstreamApi(IotPluginCommonProperties properties,
|
||||
RestTemplate restTemplate) {
|
||||
return new IotDeviceUpstreamClient(properties, restTemplate);
|
||||
}
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||
public IotDeviceDownstreamServer deviceDownstreamServer(IotPluginCommonProperties properties,
|
||||
IotDeviceDownstreamHandler deviceDownstreamHandler) {
|
||||
return new IotDeviceDownstreamServer(properties, deviceDownstreamHandler);
|
||||
}
|
||||
|
||||
@Bean(initMethod = "init", destroyMethod = "stop")
|
||||
public IotPluginInstanceHeartbeatJob pluginInstanceHeartbeatJob(IotDeviceUpstreamApi deviceDataApi,
|
||||
IotDeviceDownstreamServer deviceDownstreamServer,
|
||||
IotPluginCommonProperties commonProperties) {
|
||||
return new IotPluginInstanceHeartbeatJob(deviceDataApi, deviceDownstreamServer, commonProperties);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.zt.plat.module.iot.plugin.common.config;
|
||||
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* IoT 插件的通用配置类
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "cloud.iot.plugin.common")
|
||||
@Validated
|
||||
@Data
|
||||
public class IotPluginCommonProperties {
|
||||
|
||||
/**
|
||||
* 上行连接超时的默认值
|
||||
*/
|
||||
public static final Duration UPSTREAM_CONNECT_TIMEOUT_DEFAULT = Duration.ofSeconds(30);
|
||||
/**
|
||||
* 上行读取超时的默认值
|
||||
*/
|
||||
public static final Duration UPSTREAM_READ_TIMEOUT_DEFAULT = Duration.ofSeconds(30);
|
||||
|
||||
/**
|
||||
* 下行端口 - 随机
|
||||
*/
|
||||
public static final Integer DOWNSTREAM_PORT_RANDOM = 0;
|
||||
|
||||
/**
|
||||
* 上行 URL
|
||||
*/
|
||||
@NotEmpty(message = "上行 URL 不能为空")
|
||||
private String upstreamUrl;
|
||||
/**
|
||||
* 上行连接超时
|
||||
*/
|
||||
private Duration upstreamConnectTimeout = UPSTREAM_CONNECT_TIMEOUT_DEFAULT;
|
||||
/**
|
||||
* 上行读取超时
|
||||
*/
|
||||
private Duration upstreamReadTimeout = UPSTREAM_READ_TIMEOUT_DEFAULT;
|
||||
|
||||
/**
|
||||
* 下行端口
|
||||
*/
|
||||
private Integer downstreamPort = DOWNSTREAM_PORT_RANDOM;
|
||||
|
||||
/**
|
||||
* 插件包标识符
|
||||
*/
|
||||
@NotEmpty(message = "插件包标识符不能为空")
|
||||
private String pluginKey;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.zt.plat.module.iot.plugin.common.downstream;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.downstream.*;
|
||||
|
||||
/**
|
||||
* IoT 设备下行处理器
|
||||
*
|
||||
* 目的:每个 plugin 需要实现,用于处理 server 下行的指令(请求),从而实现从 server => plugin => device 的下行流程
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* 设置设备配置
|
||||
*
|
||||
* @param setReqDTO 设置设备配置的请求
|
||||
* @return 是否成功
|
||||
*/
|
||||
CommonResult<Boolean> setDeviceConfig(IotDeviceConfigSetReqDTO setReqDTO);
|
||||
|
||||
/**
|
||||
* 升级设备 OTA
|
||||
*
|
||||
* @param upgradeReqDTO 升级设备 OTA 的请求
|
||||
* @return 是否成功
|
||||
*/
|
||||
CommonResult<Boolean> upgradeDeviceOta(IotDeviceOtaUpgradeReqDTO upgradeReqDTO);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.zt.plat.module.iot.plugin.common.downstream;
|
||||
|
||||
import com.zt.plat.module.iot.plugin.common.config.IotPluginCommonProperties;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.router.*;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.handler.BodyHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* IoT 设备下行服务端,接收来自 server 服务器的请求,转发给 device 设备
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
@Slf4j
|
||||
public class IotDeviceDownstreamServer {
|
||||
|
||||
private final Vertx vertx;
|
||||
private final HttpServer server;
|
||||
private final IotPluginCommonProperties properties;
|
||||
|
||||
public IotDeviceDownstreamServer(IotPluginCommonProperties properties,
|
||||
IotDeviceDownstreamHandler deviceDownstreamHandler) {
|
||||
this.properties = properties;
|
||||
// 创建 Vertx 实例
|
||||
this.vertx = Vertx.vertx();
|
||||
// 创建 Router 实例
|
||||
Router router = Router.router(vertx);
|
||||
router.route().handler(BodyHandler.create()); // 处理 Body
|
||||
router.post(IotDeviceServiceInvokeVertxHandler.PATH)
|
||||
.handler(new IotDeviceServiceInvokeVertxHandler(deviceDownstreamHandler));
|
||||
router.post(IotDevicePropertySetVertxHandler.PATH)
|
||||
.handler(new IotDevicePropertySetVertxHandler(deviceDownstreamHandler));
|
||||
router.post(IotDevicePropertyGetVertxHandler.PATH)
|
||||
.handler(new IotDevicePropertyGetVertxHandler(deviceDownstreamHandler));
|
||||
router.post(IotDeviceConfigSetVertxHandler.PATH)
|
||||
.handler(new IotDeviceConfigSetVertxHandler(deviceDownstreamHandler));
|
||||
router.post(IotDeviceOtaUpgradeVertxHandler.PATH)
|
||||
.handler(new IotDeviceOtaUpgradeVertxHandler(deviceDownstreamHandler));
|
||||
// 创建 HttpServer 实例
|
||||
this.server = vertx.createHttpServer().requestHandler(router);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动 HTTP 服务器
|
||||
*/
|
||||
public void start() {
|
||||
log.info("[start][开始启动]");
|
||||
server.listen(properties.getDownstreamPort())
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
log.info("[start][启动完成,端口({})]", this.server.actualPort());
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止所有
|
||||
*/
|
||||
public void stop() {
|
||||
log.info("[stop][开始关闭]");
|
||||
try {
|
||||
// 关闭 HTTP 服务器
|
||||
if (server != null) {
|
||||
server.close()
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
}
|
||||
|
||||
// 关闭 Vertx 实例
|
||||
if (vertx != null) {
|
||||
vertx.close()
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
}
|
||||
log.info("[stop][关闭完成]");
|
||||
} catch (Exception e) {
|
||||
log.error("[stop][关闭异常]", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得端口
|
||||
*
|
||||
* @return 端口
|
||||
*/
|
||||
public int getPort() {
|
||||
return this.server.actualPort();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.zt.plat.module.iot.plugin.common.downstream.router;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.downstream.IotDeviceConfigSetReqDTO;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import com.zt.plat.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||
import com.zt.plat.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 com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
|
||||
/**
|
||||
* IoT 设备配置设置 Vertx Handler
|
||||
*
|
||||
* ZT源码
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class IotDeviceConfigSetVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
// TODO @haohao:是不是可以把 PATH、Method 所有的,抽到一个枚举类里?因为 topic、path、method 相当于不同的几个表达?
|
||||
public static final String PATH = "/sys/:productKey/:deviceName/thing/service/config/set";
|
||||
public static final String METHOD = "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);
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
null, METHOD, BAD_REQUEST.getCode(), BAD_REQUEST.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.setDeviceConfig(reqDTO);
|
||||
|
||||
// 3. 响应结果
|
||||
IotStandardResponse response = result.isSuccess() ?
|
||||
IotStandardResponse.success(reqDTO.getRequestId(), METHOD, result.getData())
|
||||
: IotStandardResponse.error(reqDTO.getRequestId(), METHOD, result.getCode(), result.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, response);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) 配置设置异常]", reqDTO, e);
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
reqDTO.getRequestId(), METHOD, INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.zt.plat.module.iot.plugin.common.downstream.router;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.downstream.IotDeviceOtaUpgradeReqDTO;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import com.zt.plat.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||
import com.zt.plat.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 static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
|
||||
/**
|
||||
* IoT 设备 OTA 升级 Vertx Handler
|
||||
* <p>
|
||||
* ZT源码
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class IotDeviceOtaUpgradeVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
public static final String PATH = "/ota/:productKey/:deviceName/upgrade";
|
||||
public static final String METHOD = "ota.device.upgrade";
|
||||
|
||||
private final IotDeviceDownstreamHandler deviceDownstreamHandler;
|
||||
|
||||
@Override
|
||||
public void handle(RoutingContext routingContext) {
|
||||
// 1. 解析参数
|
||||
IotDeviceOtaUpgradeReqDTO reqDTO;
|
||||
try {
|
||||
String productKey = routingContext.pathParam("productKey");
|
||||
String deviceName = routingContext.pathParam("deviceName");
|
||||
JsonObject body = routingContext.body().asJsonObject();
|
||||
String requestId = body.getString("requestId");
|
||||
Long firmwareId = body.getLong("firmwareId");
|
||||
String version = body.getString("version");
|
||||
String signMethod = body.getString("signMethod");
|
||||
String fileSign = body.getString("fileSign");
|
||||
Long fileSize = body.getLong("fileSize");
|
||||
String fileUrl = body.getString("fileUrl");
|
||||
String information = body.getString("information");
|
||||
reqDTO = ((IotDeviceOtaUpgradeReqDTO) new IotDeviceOtaUpgradeReqDTO()
|
||||
.setRequestId(requestId).setProductKey(productKey).setDeviceName(deviceName))
|
||||
.setFirmwareId(firmwareId).setVersion(version)
|
||||
.setSignMethod(signMethod).setFileSign(fileSign).setFileSize(fileSize).setFileUrl(fileUrl)
|
||||
.setInformation(information);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][路径参数({}) 解析参数失败]", routingContext.pathParams(), e);
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
null, METHOD, BAD_REQUEST.getCode(), BAD_REQUEST.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.upgradeDeviceOta(reqDTO);
|
||||
|
||||
// 3. 响应结果
|
||||
// TODO @haohao:可以考虑 IotStandardResponse.of(requestId, method, CommonResult)
|
||||
IotStandardResponse response = result.isSuccess() ?
|
||||
IotStandardResponse.success(reqDTO.getRequestId(), METHOD, result.getData())
|
||||
:IotStandardResponse.error(reqDTO.getRequestId(), METHOD, result.getCode(), result.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, response);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) OTA 升级异常]", reqDTO, e);
|
||||
// TODO @haohao:可以考虑 IotStandardResponse.of(requestId, method, ErrorCode)
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
reqDTO.getRequestId(), METHOD, INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.zt.plat.module.iot.plugin.common.downstream.router;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.downstream.IotDevicePropertyGetReqDTO;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import com.zt.plat.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||
import com.zt.plat.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 com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
|
||||
/**
|
||||
* IoT 设备服务获取 Vertx Handler
|
||||
*
|
||||
* ZT源码
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class IotDevicePropertyGetVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
public static final String PATH = "/sys/:productKey/:deviceName/thing/service/property/get";
|
||||
public static final String METHOD = "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);
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
null, METHOD, BAD_REQUEST.getCode(), BAD_REQUEST.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.getDeviceProperty(reqDTO);
|
||||
|
||||
// 3. 响应结果
|
||||
IotStandardResponse response;
|
||||
if (result.isSuccess()) {
|
||||
response = IotStandardResponse.success(reqDTO.getRequestId(), METHOD, result.getData());
|
||||
} else {
|
||||
response = IotStandardResponse.error(reqDTO.getRequestId(), METHOD, result.getCode(), result.getMsg());
|
||||
}
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, response);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) 属性获取异常]", reqDTO, e);
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
reqDTO.getRequestId(), METHOD, INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.zt.plat.module.iot.plugin.common.downstream.router;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.downstream.IotDevicePropertySetReqDTO;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import com.zt.plat.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||
import com.zt.plat.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 com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
|
||||
/**
|
||||
* IoT 设置设备属性 Vertx Handler
|
||||
*
|
||||
* ZT源码
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class IotDevicePropertySetVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
public static final String PATH = "/sys/:productKey/:deviceName/thing/service/property/set";
|
||||
public static final String METHOD = "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);
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
null, METHOD, BAD_REQUEST.getCode(), BAD_REQUEST.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.setDeviceProperty(reqDTO);
|
||||
|
||||
// 3. 响应结果
|
||||
IotStandardResponse response;
|
||||
if (result.isSuccess()) {
|
||||
response = IotStandardResponse.success(reqDTO.getRequestId(), METHOD, result.getData());
|
||||
} else {
|
||||
response = IotStandardResponse.error(reqDTO.getRequestId(), METHOD, result.getCode(), result.getMsg());
|
||||
}
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, response);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) 属性设置异常]", reqDTO, e);
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
reqDTO.getRequestId(), METHOD, INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.zt.plat.module.iot.plugin.common.downstream.router;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.downstream.IotDeviceServiceInvokeReqDTO;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import com.zt.plat.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||
import com.zt.plat.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 com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
|
||||
/**
|
||||
* IoT 设备服务调用 Vertx Handler
|
||||
*
|
||||
* ZT源码
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class IotDeviceServiceInvokeVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
public static final String PATH = "/sys/:productKey/:deviceName/thing/service/:identifier";
|
||||
public static final String METHOD_PREFIX = "thing.service.";
|
||||
public static final String METHOD_SUFFIX = "";
|
||||
|
||||
private final IotDeviceDownstreamHandler deviceDownstreamHandler;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void handle(RoutingContext routingContext) {
|
||||
// 1. 解析参数
|
||||
IotDeviceServiceInvokeReqDTO reqDTO;
|
||||
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");
|
||||
reqDTO = ((IotDeviceServiceInvokeReqDTO) new IotDeviceServiceInvokeReqDTO()
|
||||
.setRequestId(requestId).setProductKey(productKey).setDeviceName(deviceName))
|
||||
.setIdentifier(identifier).setParams(params);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][路径参数({}) 解析参数失败]", routingContext.pathParams(), e);
|
||||
String method = METHOD_PREFIX + routingContext.pathParam("identifier") + METHOD_SUFFIX;
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
null, method, BAD_REQUEST.getCode(), BAD_REQUEST.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 调用处理器
|
||||
try {
|
||||
CommonResult<Boolean> result = deviceDownstreamHandler.invokeDeviceService(reqDTO);
|
||||
|
||||
// 3. 响应结果
|
||||
String method = METHOD_PREFIX + reqDTO.getIdentifier() + METHOD_SUFFIX;
|
||||
IotStandardResponse response;
|
||||
if (result.isSuccess()) {
|
||||
response = IotStandardResponse.success(reqDTO.getRequestId(), method, result.getData());
|
||||
} else {
|
||||
response = IotStandardResponse.error(reqDTO.getRequestId(), method, result.getCode(), result.getMsg());
|
||||
}
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, response);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][请求参数({}) 服务调用异常]", reqDTO, e);
|
||||
String method = METHOD_PREFIX + reqDTO.getIdentifier() + METHOD_SUFFIX;
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(
|
||||
reqDTO.getRequestId(), method, INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.zt.plat.module.iot.plugin.common.heartbeat;
|
||||
|
||||
import cn.hutool.system.SystemUtil;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.IotPluginInstanceHeartbeatReqDTO;
|
||||
import com.zt.plat.module.iot.plugin.common.config.IotPluginCommonProperties;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamServer;
|
||||
import com.zt.plat.module.iot.plugin.common.util.IotPluginCommonUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* IoT 插件实例心跳 Job
|
||||
*
|
||||
* 用于定时发送心跳给服务端
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class IotPluginInstanceHeartbeatJob {
|
||||
|
||||
private final IotDeviceUpstreamApi deviceUpstreamApi;
|
||||
private final IotDeviceDownstreamServer deviceDownstreamServer;
|
||||
private final IotPluginCommonProperties commonProperties;
|
||||
|
||||
public void init() {
|
||||
CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(true));
|
||||
log.info("[init][上线结果:{})]", result);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(false));
|
||||
log.info("[stop][下线结果:{})]", result);
|
||||
}
|
||||
|
||||
@Scheduled(initialDelay = 3, fixedRate = 3, timeUnit = TimeUnit.MINUTES) // 3 分钟执行一次
|
||||
public void execute() {
|
||||
CommonResult<Boolean> result = deviceUpstreamApi.heartbeatPluginInstance(buildPluginInstanceHeartbeatReqDTO(true));
|
||||
log.info("[execute][心跳结果:{})]", result);
|
||||
}
|
||||
|
||||
private IotPluginInstanceHeartbeatReqDTO buildPluginInstanceHeartbeatReqDTO(Boolean online) {
|
||||
return new IotPluginInstanceHeartbeatReqDTO()
|
||||
.setPluginKey(commonProperties.getPluginKey()).setProcessId(IotPluginCommonUtils.getProcessId())
|
||||
.setHostIp(SystemUtil.getHostInfo().getAddress()).setDownstreamPort(deviceDownstreamServer.getPort())
|
||||
.setOnline(online);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// TODO @芋艿:注释
|
||||
package com.zt.plat.module.iot.plugin.common;
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.zt.plat.module.iot.plugin.common.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
// TODO @芋艿:1)后续考虑,要不要叫 IoT 网关之类的 Response;2)包名 pojo
|
||||
/**
|
||||
* IoT 标准协议响应实体类
|
||||
* <p>
|
||||
* 用于统一 MQTT 和 HTTP 的响应格式
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Data
|
||||
public class IotStandardResponse {
|
||||
|
||||
/**
|
||||
* 消息ID
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 状态码
|
||||
*/
|
||||
private Integer code;
|
||||
|
||||
/**
|
||||
* 响应数据
|
||||
*/
|
||||
private Object data;
|
||||
|
||||
/**
|
||||
* 响应消息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
/**
|
||||
* 方法名
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
* 协议版本
|
||||
*/
|
||||
private String version;
|
||||
|
||||
/**
|
||||
* 创建成功响应
|
||||
*
|
||||
* @param id 消息ID
|
||||
* @param method 方法名
|
||||
* @return 成功响应
|
||||
*/
|
||||
public static IotStandardResponse success(String id, String method) {
|
||||
return success(id, method, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建成功响应
|
||||
*
|
||||
* @param id 消息ID
|
||||
* @param method 方法名
|
||||
* @param data 响应数据
|
||||
* @return 成功响应
|
||||
*/
|
||||
public static IotStandardResponse success(String id, String method, Object data) {
|
||||
return new IotStandardResponse()
|
||||
.setId(id)
|
||||
.setCode(200)
|
||||
.setData(data)
|
||||
.setMessage("success")
|
||||
.setMethod(method)
|
||||
.setVersion("1.0");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建错误响应
|
||||
*
|
||||
* @param id 消息ID
|
||||
* @param method 方法名
|
||||
* @param code 错误码
|
||||
* @param message 错误消息
|
||||
* @return 错误响应
|
||||
*/
|
||||
public static IotStandardResponse error(String id, String method, Integer code, String message) {
|
||||
return new IotStandardResponse()
|
||||
.setId(id)
|
||||
.setCode(code)
|
||||
.setData(null)
|
||||
.setMessage(message)
|
||||
.setMethod(method)
|
||||
.setVersion("1.0");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.zt.plat.module.iot.plugin.common.upstream;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.*;
|
||||
import com.zt.plat.module.iot.plugin.common.config.IotPluginCommonProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
|
||||
/**
|
||||
* 设备数据 Upstream 上行客户端
|
||||
*
|
||||
* 通过 HTTP 调用远程的 IotDeviceUpstreamApi 接口
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class IotDeviceUpstreamClient implements IotDeviceUpstreamApi {
|
||||
|
||||
public static final String URL_PREFIX = "/rpc-api/iot/device/upstream";
|
||||
|
||||
private final IotPluginCommonProperties properties;
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateDeviceState(IotDeviceStateUpdateReqDTO updateReqDTO) {
|
||||
String url = properties.getUpstreamUrl() + URL_PREFIX + "/update-state";
|
||||
return doPost(url, updateReqDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> reportDeviceEvent(IotDeviceEventReportReqDTO reportReqDTO) {
|
||||
String url = properties.getUpstreamUrl() + URL_PREFIX + "/report-event";
|
||||
return doPost(url, reportReqDTO);
|
||||
}
|
||||
|
||||
// TODO @芋艿:待实现
|
||||
@Override
|
||||
public CommonResult<Boolean> registerDevice(IotDeviceRegisterReqDTO registerReqDTO) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO @芋艿:待实现
|
||||
@Override
|
||||
public CommonResult<Boolean> registerSubDevice(IotDeviceRegisterSubReqDTO registerReqDTO) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO @芋艿:待实现
|
||||
@Override
|
||||
public CommonResult<Boolean> addDeviceTopology(IotDeviceTopologyAddReqDTO addReqDTO) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> authenticateEmqxConnection(IotDeviceEmqxAuthReqDTO authReqDTO) {
|
||||
String url = properties.getUpstreamUrl() + URL_PREFIX + "/authenticate-emqx-connection";
|
||||
return doPost(url, authReqDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> reportDeviceProperty(IotDevicePropertyReportReqDTO reportReqDTO) {
|
||||
String url = properties.getUpstreamUrl() + URL_PREFIX + "/report-property";
|
||||
return doPost(url, reportReqDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> heartbeatPluginInstance(IotPluginInstanceHeartbeatReqDTO heartbeatReqDTO) {
|
||||
String url = properties.getUpstreamUrl() + URL_PREFIX + "/heartbeat-plugin-instance";
|
||||
return doPost(url, heartbeatReqDTO);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> CommonResult<Boolean> doPost(String url, T requestBody) {
|
||||
try {
|
||||
CommonResult<Boolean> result = restTemplate.postForObject(url, requestBody,
|
||||
(Class<CommonResult<Boolean>>) (Class<?>) CommonResult.class);
|
||||
log.info("[doPost][url({}) requestBody({}) result({})]", url, requestBody, result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("[doPost][url({}) requestBody({}) 发生异常]", url, requestBody, e);
|
||||
return CommonResult.error(INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.zt.plat.module.iot.plugin.common.util;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.system.SystemUtil;
|
||||
import com.zt.plat.framework.common.util.json.JsonUtils;
|
||||
import com.zt.plat.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||
import io.vertx.core.http.HttpHeaders;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* IoT 插件的通用工具类
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
public class IotPluginCommonUtils {
|
||||
|
||||
/**
|
||||
* 流程实例的进程编号
|
||||
*/
|
||||
private static String processId;
|
||||
|
||||
public static String getProcessId() {
|
||||
if (StrUtil.isEmpty(processId)) {
|
||||
initProcessId();
|
||||
}
|
||||
return processId;
|
||||
}
|
||||
|
||||
private synchronized static void initProcessId() {
|
||||
processId = String.format("%s@%d@%s", // IP@PID@${uuid}
|
||||
SystemUtil.getHostInfo().getAddress(), SystemUtil.getCurrentPID(), IdUtil.fastSimpleUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象转换为JSON字符串后写入HTTP响应
|
||||
*
|
||||
* @param routingContext 路由上下文
|
||||
* @param data 数据对象
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void writeJsonResponse(RoutingContext routingContext, Object data) {
|
||||
routingContext.response()
|
||||
.setStatusCode(200)
|
||||
.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
.end(JsonUtils.toJsonString(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成标准JSON格式的响应并写入HTTP响应(基于IotStandardResponse)
|
||||
* <p>
|
||||
* 推荐使用此方法,统一MQTT和HTTP的响应格式。使用方式:
|
||||
*
|
||||
* <pre>
|
||||
* // 成功响应
|
||||
* IotStandardResponse response = IotStandardResponse.success(requestId, method, data);
|
||||
* IotPluginCommonUtils.writeJsonResponse(routingContext, response);
|
||||
*
|
||||
* // 错误响应
|
||||
* IotStandardResponse errorResponse = IotStandardResponse.error(requestId, method, code, message);
|
||||
* IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
* </pre>
|
||||
*
|
||||
* @param routingContext 路由上下文
|
||||
* @param response IotStandardResponse响应对象
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void writeJsonResponse(RoutingContext routingContext, IotStandardResponse response) {
|
||||
routingContext.response()
|
||||
.setStatusCode(200)
|
||||
.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
.end(JsonUtils.toJsonString(response));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
com.zt.plat.module.iot.plugin.common.config.IotPluginCommonAutoConfiguration
|
||||
@@ -0,0 +1,6 @@
|
||||
plugin.id=cloud-module-iot-plugin-emqx
|
||||
plugin.class=com.zt.plat.module.iot.plugin.emqx.config.IotEmqxPlugin
|
||||
plugin.version=1.0.0
|
||||
plugin.provider=cloud
|
||||
plugin.dependencies=
|
||||
plugin.description=cloud-module-iot-plugin-emqx-1.0.0
|
||||
@@ -0,0 +1,169 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="
|
||||
http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>zt-module-iot-plugins</artifactId>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<artifactId>zt-module-iot-plugin-emqx</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<!-- TODO @芋艿:待整理 -->
|
||||
<description>
|
||||
物联网 插件模块 - emqx 插件
|
||||
</description>
|
||||
|
||||
<properties>
|
||||
<!-- 插件相关 -->
|
||||
<plugin.id>emqx-plugin</plugin.id>
|
||||
<plugin.class>com.zt.plat.module.iot.plugin.emqx.config.IotEmqxPlugin</plugin.class>
|
||||
<plugin.version>${project.version}</plugin.version>
|
||||
<plugin.provider>zt</plugin.provider>
|
||||
<plugin.description>${project.artifactId}-${project.version}</plugin.description>
|
||||
<plugin.dependencies/>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- 插件模式 zip -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-antrun-plugin</artifactId>
|
||||
<version>1.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>unzip jar file</id>
|
||||
<phase>package</phase>
|
||||
<configuration>
|
||||
<target>
|
||||
<unzip src="target/${project.artifactId}-${project.version}.${project.packaging}"
|
||||
dest="target/plugin-classes"/>
|
||||
</target>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>
|
||||
src/main/assembly/assembly.xml
|
||||
</descriptor>
|
||||
</descriptors>
|
||||
<appendAssemblyId>false</appendAssemblyId>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>attached</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- 插件模式 jar -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries>
|
||||
<Plugin-Id>${plugin.id}</Plugin-Id>
|
||||
<Plugin-Class>${plugin.class}</Plugin-Class>
|
||||
<Plugin-Version>${plugin.version}</Plugin-Version>
|
||||
<Plugin-Provider>${plugin.provider}</Plugin-Provider>
|
||||
<Plugin-Description>${plugin.description}</Plugin-Description>
|
||||
<Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
|
||||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!-- <artifactId>maven-shade-plugin</artifactId>-->
|
||||
<!-- <version>3.6.0</version>-->
|
||||
<!-- <executions>-->
|
||||
<!-- <execution>-->
|
||||
<!-- <phase>package</phase>-->
|
||||
<!-- <goals>-->
|
||||
<!-- <goal>shade</goal>-->
|
||||
<!-- </goals>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <minimizeJar>true</minimizeJar>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </execution>-->
|
||||
<!-- </executions>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <archive>-->
|
||||
<!-- <manifestEntries>-->
|
||||
<!-- <Plugin-Id>${plugin.id}</Plugin-Id>-->
|
||||
<!-- <Plugin-Class>${plugin.class}</Plugin-Class>-->
|
||||
<!-- <Plugin-Version>${plugin.version}</Plugin-Version>-->
|
||||
<!-- <Plugin-Provider>${plugin.provider}</Plugin-Provider>-->
|
||||
<!-- <Plugin-Description>${plugin.description}</Plugin-Description>-->
|
||||
<!-- <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>-->
|
||||
<!-- </manifestEntries>-->
|
||||
<!-- </archive>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </plugin>-->
|
||||
|
||||
<!-- 独立模式 -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>-standalone</classifier>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<artifactId>zt-module-iot-plugin-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Web 相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 工具类相关 -->
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-mqtt</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,31 @@
|
||||
<assembly>
|
||||
<id>plugin</id>
|
||||
<formats>
|
||||
<format>zip</format>
|
||||
</formats>
|
||||
<includeBaseDirectory>false</includeBaseDirectory>
|
||||
<dependencySets>
|
||||
<dependencySet>
|
||||
<useProjectArtifact>false</useProjectArtifact>
|
||||
<scope>runtime</scope>
|
||||
<outputDirectory>lib</outputDirectory>
|
||||
<includes>
|
||||
<include>*:jar:*</include>
|
||||
</includes>
|
||||
</dependencySet>
|
||||
</dependencySets>
|
||||
<!--
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>target/classes</directory>
|
||||
<outputDirectory>classes</outputDirectory>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
-->
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>target/plugin-classes</directory>
|
||||
<outputDirectory>classes</outputDirectory>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
</assembly>
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* IoT Emqx 插件的独立运行入口
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootApplication
|
||||
public class IotEmqxPluginApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication application = new SpringApplication(IotEmqxPluginApplication.class);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
application.run(args);
|
||||
log.info("[main][独立模式启动完成]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx.config;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.PluginWrapper;
|
||||
import org.pf4j.spring.SpringPlugin;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
/**
|
||||
* EMQX 插件实现类
|
||||
*
|
||||
* 基于 PF4J 插件框架,实现 EMQX 消息中间件的集成:负责插件的生命周期管理,包括启动、停止和应用上下文的创建
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Slf4j
|
||||
public class IotEmqxPlugin extends SpringPlugin {
|
||||
|
||||
public IotEmqxPlugin(PluginWrapper wrapper) {
|
||||
super(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
log.info("[EmqxPlugin][EmqxPlugin 插件启动开始...]");
|
||||
try {
|
||||
log.info("[EmqxPlugin][EmqxPlugin 插件启动成功...]");
|
||||
} catch (Exception e) {
|
||||
log.error("[EmqxPlugin][EmqxPlugin 插件开启动异常...]", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
log.info("[EmqxPlugin][EmqxPlugin 插件停止开始...]");
|
||||
try {
|
||||
log.info("[EmqxPlugin][EmqxPlugin 插件停止成功...]");
|
||||
} catch (Exception e) {
|
||||
log.error("[EmqxPlugin][EmqxPlugin 插件停止异常...]", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ApplicationContext createApplicationContext() {
|
||||
// 创建插件自己的 ApplicationContext
|
||||
AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext();
|
||||
// 设置父容器为主应用的 ApplicationContext (确保主应用中提供的类可用)
|
||||
pluginContext.setParent(SpringUtil.getApplicationContext());
|
||||
// 继续使用插件自己的 ClassLoader 以加载插件内部的类
|
||||
pluginContext.setClassLoader(getWrapper().getPluginClassLoader());
|
||||
// 扫描当前插件的自动配置包
|
||||
// TODO @芋艿:是不是要配置下包
|
||||
pluginContext.scan("com.zt.plat.module.iot.plugin.emqx.config");
|
||||
pluginContext.refresh();
|
||||
return pluginContext;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx.config;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import com.zt.plat.module.iot.plugin.emqx.downstream.IotDeviceDownstreamHandlerImpl;
|
||||
import com.zt.plat.module.iot.plugin.emqx.upstream.IotDeviceUpstreamServer;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.mqtt.MqttClient;
|
||||
import io.vertx.mqtt.MqttClientOptions;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* IoT 插件 EMQX 的专用自动配置类
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(IotPluginEmqxProperties.class)
|
||||
public class IotPluginEmqxAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public Vertx vertx() {
|
||||
return Vertx.vertx();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MqttClient mqttClient(Vertx vertx, IotPluginEmqxProperties emqxProperties) {
|
||||
MqttClientOptions options = new MqttClientOptions()
|
||||
.setClientId("cloud-iot-downstream-" + IdUtil.fastSimpleUUID())
|
||||
.setUsername(emqxProperties.getMqttUsername())
|
||||
.setPassword(emqxProperties.getMqttPassword())
|
||||
.setSsl(emqxProperties.getMqttSsl());
|
||||
return MqttClient.create(vertx, options);
|
||||
}
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||
public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi,
|
||||
IotPluginEmqxProperties emqxProperties,
|
||||
Vertx vertx,
|
||||
MqttClient mqttClient) {
|
||||
return new IotDeviceUpstreamServer(emqxProperties, deviceUpstreamApi, vertx, mqttClient);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IotDeviceDownstreamHandler deviceDownstreamHandler(MqttClient mqttClient) {
|
||||
return new IotDeviceDownstreamHandlerImpl(mqttClient);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
* 物联网插件 - EMQX 配置
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "cloud.iot.plugin.emqx")
|
||||
@Validated
|
||||
@Data
|
||||
public class IotPluginEmqxProperties {
|
||||
|
||||
// TODO @haohao:参数校验,加下,啊哈
|
||||
|
||||
/**
|
||||
* 服务主机
|
||||
*/
|
||||
private String mqttHost;
|
||||
/**
|
||||
* 服务端口
|
||||
*/
|
||||
private Integer mqttPort;
|
||||
/**
|
||||
* 服务用户名
|
||||
*/
|
||||
private String mqttUsername;
|
||||
/**
|
||||
* 服务密码
|
||||
*/
|
||||
private String mqttPassword;
|
||||
/**
|
||||
* 是否启用 SSL
|
||||
*/
|
||||
private Boolean mqttSsl;
|
||||
|
||||
/**
|
||||
* 订阅的主题列表
|
||||
*/
|
||||
private String[] mqttTopics;
|
||||
|
||||
/**
|
||||
* 认证端口
|
||||
*/
|
||||
private Integer authPort;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx.downstream;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.downstream.*;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.mqtt.MqttClient;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static com.zt.plat.module.iot.enums.ErrorCodeConstants.MQTT_TOPIC_ILLEGAL;
|
||||
|
||||
/**
|
||||
* EMQX 插件的 {@link IotDeviceDownstreamHandler} 实现类
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
@Slf4j
|
||||
public class IotDeviceDownstreamHandlerImpl implements IotDeviceDownstreamHandler {
|
||||
|
||||
private static final String SYS_TOPIC_PREFIX = "/sys/";
|
||||
|
||||
// TODO @haohao:是不是可以类似 IotDeviceConfigSetVertxHandler 的建议,抽到统一的枚举类
|
||||
// TODO @haohao:讨论,感觉 mqtt 和 http,可以做个相对统一的格式哈。;回复 都使用 Alink 格式,方便后续扩展。
|
||||
// 设备服务调用 标准 JSON
|
||||
// 请求Topic:/sys/${productKey}/${deviceName}/thing/service/${tsl.service.identifier}
|
||||
// 响应Topic:/sys/${productKey}/${deviceName}/thing/service/${tsl.service.identifier}_reply
|
||||
private static final String SERVICE_TOPIC_PREFIX = "/thing/service/";
|
||||
|
||||
// 设置设备属性 标准 JSON
|
||||
// 请求Topic:/sys/${productKey}/${deviceName}/thing/service/property/set
|
||||
// 响应Topic:/sys/${productKey}/${deviceName}/thing/service/property/set_reply
|
||||
private static final String PROPERTY_SET_TOPIC = "/thing/service/property/set";
|
||||
|
||||
private final MqttClient mqttClient;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param mqttClient MQTT客户端
|
||||
*/
|
||||
public IotDeviceDownstreamHandlerImpl(MqttClient mqttClient) {
|
||||
this.mqttClient = mqttClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> invokeDeviceService(IotDeviceServiceInvokeReqDTO reqDTO) {
|
||||
log.info("[invokeService][开始调用设备服务][reqDTO: {}]", JSONUtil.toJsonStr(reqDTO));
|
||||
|
||||
// 验证参数
|
||||
if (reqDTO.getProductKey() == null || reqDTO.getDeviceName() == null || reqDTO.getIdentifier() == null) {
|
||||
log.error("[invokeService][参数不完整][reqDTO: {}]", JSONUtil.toJsonStr(reqDTO));
|
||||
return CommonResult.error(MQTT_TOPIC_ILLEGAL.getCode(), MQTT_TOPIC_ILLEGAL.getMsg());
|
||||
}
|
||||
|
||||
try {
|
||||
// 构建请求主题
|
||||
String topic = buildServiceTopic(reqDTO.getProductKey(), reqDTO.getDeviceName(), reqDTO.getIdentifier());
|
||||
// 构建请求消息
|
||||
String requestId = reqDTO.getRequestId() != null ? reqDTO.getRequestId() : generateRequestId();
|
||||
JSONObject request = buildServiceRequest(requestId, reqDTO.getIdentifier(), reqDTO.getParams());
|
||||
// 发送消息
|
||||
publishMessage(topic, request);
|
||||
|
||||
log.info("[invokeService][调用设备服务成功][requestId: {}][topic: {}]", requestId, topic);
|
||||
return CommonResult.success(true);
|
||||
} catch (Exception e) {
|
||||
log.error("[invokeService][调用设备服务异常][reqDTO: {}]", JSONUtil.toJsonStr(reqDTO), e);
|
||||
return CommonResult.error(MQTT_TOPIC_ILLEGAL.getCode(), MQTT_TOPIC_ILLEGAL.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> getDeviceProperty(IotDevicePropertyGetReqDTO getReqDTO) {
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> setDeviceProperty(IotDevicePropertySetReqDTO reqDTO) {
|
||||
// 验证参数
|
||||
log.info("[setProperty][开始设置设备属性][reqDTO: {}]", JSONUtil.toJsonStr(reqDTO));
|
||||
if (reqDTO.getProductKey() == null || reqDTO.getDeviceName() == null) {
|
||||
log.error("[setProperty][参数不完整][reqDTO: {}]", JSONUtil.toJsonStr(reqDTO));
|
||||
return CommonResult.error(MQTT_TOPIC_ILLEGAL.getCode(), MQTT_TOPIC_ILLEGAL.getMsg());
|
||||
}
|
||||
|
||||
try {
|
||||
// 构建请求主题
|
||||
String topic = buildPropertySetTopic(reqDTO.getProductKey(), reqDTO.getDeviceName());
|
||||
// 构建请求消息
|
||||
String requestId = reqDTO.getRequestId() != null ? reqDTO.getRequestId() : generateRequestId();
|
||||
JSONObject request = buildPropertySetRequest(requestId, reqDTO.getProperties());
|
||||
// 发送消息
|
||||
publishMessage(topic, request);
|
||||
|
||||
log.info("[setProperty][设置设备属性成功][requestId: {}][topic: {}]", requestId, topic);
|
||||
return CommonResult.success(true);
|
||||
} catch (Exception e) {
|
||||
log.error("[setProperty][设置设备属性异常][reqDTO: {}]", JSONUtil.toJsonStr(reqDTO), e);
|
||||
return CommonResult.error(MQTT_TOPIC_ILLEGAL.getCode(), MQTT_TOPIC_ILLEGAL.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> setDeviceConfig(IotDeviceConfigSetReqDTO setReqDTO) {
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> upgradeDeviceOta(IotDeviceOtaUpgradeReqDTO upgradeReqDTO) {
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建服务调用主题
|
||||
*/
|
||||
private String buildServiceTopic(String productKey, String deviceName, String serviceIdentifier) {
|
||||
return SYS_TOPIC_PREFIX + productKey + "/" + deviceName + SERVICE_TOPIC_PREFIX + serviceIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建属性设置主题
|
||||
*/
|
||||
private String buildPropertySetTopic(String productKey, String deviceName) {
|
||||
return SYS_TOPIC_PREFIX + productKey + "/" + deviceName + PROPERTY_SET_TOPIC;
|
||||
}
|
||||
|
||||
// TODO @haohao:这个,后面搞个对象,会不会好点哈?
|
||||
/**
|
||||
* 构建服务调用请求
|
||||
*/
|
||||
private JSONObject buildServiceRequest(String requestId, String serviceIdentifier, Map<String, Object> params) {
|
||||
return new JSONObject()
|
||||
.set("id", requestId)
|
||||
.set("version", "1.0")
|
||||
.set("method", "thing.service." + serviceIdentifier)
|
||||
.set("params", params != null ? params : new JSONObject());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建属性设置请求
|
||||
*/
|
||||
private JSONObject buildPropertySetRequest(String requestId, Map<String, Object> properties) {
|
||||
return new JSONObject()
|
||||
.set("id", requestId)
|
||||
.set("version", "1.0")
|
||||
.set("method", "thing.service.property.set")
|
||||
.set("params", properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布 MQTT 消息
|
||||
*/
|
||||
private void publishMessage(String topic, JSONObject payload) {
|
||||
mqttClient.publish(
|
||||
topic,
|
||||
Buffer.buffer(payload.toString()),
|
||||
MqttQoS.AT_LEAST_ONCE,
|
||||
false,
|
||||
false);
|
||||
log.info("[publishMessage][发送消息成功][topic: {}][payload: {}]", topic, payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成请求 ID
|
||||
*/
|
||||
private String generateRequestId() {
|
||||
return IdUtil.fastSimpleUUID();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx.upstream;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.plugin.emqx.config.IotPluginEmqxProperties;
|
||||
import com.zt.plat.module.iot.plugin.emqx.upstream.router.IotDeviceAuthVertxHandler;
|
||||
import com.zt.plat.module.iot.plugin.emqx.upstream.router.IotDeviceMqttMessageHandler;
|
||||
import com.zt.plat.module.iot.plugin.emqx.upstream.router.IotDeviceWebhookVertxHandler;
|
||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.handler.BodyHandler;
|
||||
import io.vertx.mqtt.MqttClient;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* IoT 设备下行服务端,接收来自 device 设备的请求,转发给 server 服务器
|
||||
* <p>
|
||||
* 协议:HTTP、MQTT
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Slf4j
|
||||
public class IotDeviceUpstreamServer {
|
||||
|
||||
/**
|
||||
* 重连延迟时间(毫秒)
|
||||
*/
|
||||
private static final int RECONNECT_DELAY_MS = 5000;
|
||||
/**
|
||||
* 连接超时时间(毫秒)
|
||||
*/
|
||||
private static final int CONNECTION_TIMEOUT_MS = 10000;
|
||||
/**
|
||||
* 默认 QoS 级别
|
||||
*/
|
||||
private static final MqttQoS DEFAULT_QOS = MqttQoS.AT_LEAST_ONCE;
|
||||
|
||||
private final Vertx vertx;
|
||||
private final HttpServer server;
|
||||
private final MqttClient client;
|
||||
private final IotPluginEmqxProperties emqxProperties;
|
||||
private final IotDeviceMqttMessageHandler mqttMessageHandler;
|
||||
|
||||
/**
|
||||
* 服务运行状态标志
|
||||
*/
|
||||
private volatile boolean isRunning = false;
|
||||
|
||||
public IotDeviceUpstreamServer(IotPluginEmqxProperties emqxProperties,
|
||||
IotDeviceUpstreamApi deviceUpstreamApi,
|
||||
Vertx vertx,
|
||||
MqttClient client) {
|
||||
this.vertx = vertx;
|
||||
this.emqxProperties = emqxProperties;
|
||||
this.client = client;
|
||||
|
||||
// 创建 Router 实例
|
||||
Router router = Router.router(vertx);
|
||||
router.route().handler(BodyHandler.create()); // 处理 Body
|
||||
router.post(IotDeviceAuthVertxHandler.PATH)
|
||||
// TODO @haohao:疑问,mqtt 的认证,需要通过 http 呀?
|
||||
// 回复:MQTT 认证不必须通过 HTTP 进行,但 HTTP 认证是 EMQX 等 MQTT 服务器支持的一种灵活的认证方式
|
||||
.handler(new IotDeviceAuthVertxHandler(deviceUpstreamApi));
|
||||
// 添加 Webhook 处理器,用于处理设备连接和断开连接事件
|
||||
router.post(IotDeviceWebhookVertxHandler.PATH)
|
||||
.handler(new IotDeviceWebhookVertxHandler(deviceUpstreamApi));
|
||||
// 创建 HttpServer 实例
|
||||
this.server = vertx.createHttpServer().requestHandler(router);
|
||||
this.mqttMessageHandler = new IotDeviceMqttMessageHandler(deviceUpstreamApi, client);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动 HTTP 服务器、MQTT 客户端
|
||||
*/
|
||||
public void start() {
|
||||
if (isRunning) {
|
||||
log.warn("[start][服务已经在运行中,请勿重复启动]");
|
||||
return;
|
||||
}
|
||||
log.info("[start][开始启动服务]");
|
||||
|
||||
// TODO @haohao:建议先启动 MQTT Broker,再启动 HTTP Server。类似 jdbc 先连接了,在启动 tomcat 的味道
|
||||
// 1. 启动 HTTP 服务器
|
||||
CompletableFuture<Void> httpFuture = server.listen(emqxProperties.getAuthPort())
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.thenAccept(v -> log.info("[start][HTTP 服务器启动完成,端口: {}]", server.actualPort()));
|
||||
|
||||
// 2. 连接 MQTT Broker
|
||||
CompletableFuture<Void> mqttFuture = connectMqtt()
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.thenAccept(v -> {
|
||||
// 2.1 添加 MQTT 断开重连监听器
|
||||
client.closeHandler(closeEvent -> {
|
||||
log.warn("[closeHandler][MQTT 连接已断开,准备重连]");
|
||||
reconnectWithDelay();
|
||||
});
|
||||
// 2.2 设置 MQTT 消息处理器
|
||||
setupMessageHandler();
|
||||
});
|
||||
|
||||
// 3. 等待所有服务启动完成
|
||||
CompletableFuture.allOf(httpFuture, mqttFuture)
|
||||
.orTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS) // TODO @芋艿:JDK8 不兼容
|
||||
.whenComplete((result, error) -> {
|
||||
if (error != null) {
|
||||
log.error("[start][服务启动失败]", error);
|
||||
} else {
|
||||
isRunning = true;
|
||||
log.info("[start][所有服务启动完成]");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 MQTT 消息处理器
|
||||
*/
|
||||
private void setupMessageHandler() {
|
||||
client.publishHandler(mqttMessageHandler::handle);
|
||||
log.debug("[setupMessageHandler][MQTT 消息处理器设置完成]");
|
||||
}
|
||||
|
||||
/**
|
||||
* 重连 MQTT 客户端
|
||||
*/
|
||||
private void reconnectWithDelay() {
|
||||
if (!isRunning) {
|
||||
log.info("[reconnectWithDelay][服务已停止,不再尝试重连]");
|
||||
return;
|
||||
}
|
||||
|
||||
vertx.setTimer(RECONNECT_DELAY_MS, id -> {
|
||||
log.info("[reconnectWithDelay][开始重新连接 MQTT]");
|
||||
connectMqtt();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接 MQTT Broker 并订阅主题
|
||||
*
|
||||
* @return 连接结果的Future
|
||||
*/
|
||||
private Future<Void> connectMqtt() {
|
||||
return client.connect(emqxProperties.getMqttPort(), emqxProperties.getMqttHost())
|
||||
.compose(connAck -> {
|
||||
log.info("[connectMqtt][MQTT客户端连接成功]");
|
||||
return subscribeToTopics();
|
||||
})
|
||||
.recover(error -> {
|
||||
log.error("[connectMqtt][连接MQTT Broker失败:]", error);
|
||||
reconnectWithDelay();
|
||||
return Future.failedFuture(error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅设备上行消息主题
|
||||
*
|
||||
* @return 订阅结果的 Future
|
||||
*/
|
||||
private Future<Void> subscribeToTopics() {
|
||||
String[] topics = emqxProperties.getMqttTopics();
|
||||
if (ArrayUtil.isEmpty(topics)) {
|
||||
log.warn("[subscribeToTopics][未配置MQTT主题,跳过订阅]");
|
||||
return Future.succeededFuture();
|
||||
}
|
||||
log.info("[subscribeToTopics][开始订阅设备上行消息主题]");
|
||||
|
||||
Future<Void> compositeFuture = Future.succeededFuture();
|
||||
for (String topic : topics) {
|
||||
String trimmedTopic = topic.trim();
|
||||
if (trimmedTopic.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
compositeFuture = compositeFuture.compose(v -> client.subscribe(trimmedTopic, DEFAULT_QOS.value())
|
||||
.<Void>map(ack -> {
|
||||
log.info("[subscribeToTopics][成功订阅主题: {}]", trimmedTopic);
|
||||
return null;
|
||||
})
|
||||
.recover(error -> {
|
||||
log.error("[subscribeToTopics][订阅主题失败: {}]", trimmedTopic, error);
|
||||
return Future.<Void>succeededFuture(); // 继续订阅其他主题
|
||||
}));
|
||||
}
|
||||
return compositeFuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止所有服务
|
||||
*/
|
||||
public void stop() {
|
||||
if (!isRunning) {
|
||||
log.warn("[stop][服务未运行,无需停止]");
|
||||
return;
|
||||
}
|
||||
log.info("[stop][开始关闭服务]");
|
||||
isRunning = false;
|
||||
|
||||
try {
|
||||
// 关闭 HTTP 服务器
|
||||
if (server != null) {
|
||||
server.close()
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
}
|
||||
|
||||
// 关闭 MQTT 客户端
|
||||
if (client != null) {
|
||||
client.disconnect()
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
}
|
||||
|
||||
// 关闭 Vertx 实例
|
||||
if (vertx!= null) {
|
||||
vertx.close()
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
}
|
||||
log.info("[stop][关闭完成]");
|
||||
} catch (Exception e) {
|
||||
log.error("[stop][关闭服务异常]", e);
|
||||
throw new RuntimeException("关闭 IoT 设备上行服务失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx.upstream.router;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.IotDeviceEmqxAuthReqDTO;
|
||||
import com.zt.plat.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.Collections;
|
||||
|
||||
/**
|
||||
* IoT EMQX 连接认证的 Vert.x Handler
|
||||
*
|
||||
* 参考:<a href="https://docs.emqx.com/zh/emqx/latest/access-control/authn/http.html">EMQX HTTP</a>
|
||||
*
|
||||
* 注意:该处理器需要返回特定格式:{"result": "allow"} 或 {"result": "deny"},
|
||||
* 以符合 EMQX 认证插件的要求,因此不使用 IotStandardResponse 实体类
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class IotDeviceAuthVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
public static final String PATH = "/mqtt/auth";
|
||||
|
||||
private final IotDeviceUpstreamApi deviceUpstreamApi;
|
||||
|
||||
@Override
|
||||
public void handle(RoutingContext routingContext) {
|
||||
try {
|
||||
// 构建认证请求 DTO
|
||||
JsonObject json = routingContext.body().asJsonObject();
|
||||
String clientId = json.getString("clientid");
|
||||
String username = json.getString("username");
|
||||
String password = json.getString("password");
|
||||
IotDeviceEmqxAuthReqDTO authReqDTO = new IotDeviceEmqxAuthReqDTO()
|
||||
.setClientId(clientId)
|
||||
.setUsername(username)
|
||||
.setPassword(password);
|
||||
|
||||
// 调用认证 API
|
||||
CommonResult<Boolean> authResult = deviceUpstreamApi.authenticateEmqxConnection(authReqDTO);
|
||||
if (authResult.getCode() != 0 || !authResult.getData()) {
|
||||
// 注意:这里必须返回 {"result": "deny"} 格式,以符合 EMQX 认证插件的要求
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, Collections.singletonMap("result", "deny"));
|
||||
return;
|
||||
}
|
||||
|
||||
// 响应结果
|
||||
// 注意:这里必须返回 {"result": "allow"} 格式,以符合 EMQX 认证插件的要求
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, Collections.singletonMap("result", "allow"));
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][EMQX 认证异常]", e);
|
||||
// 注意:这里必须返回 {"result": "deny"} 格式,以符合 EMQX 认证插件的要求
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, Collections.singletonMap("result", "deny"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx.upstream.router;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.zt.plat.framework.common.util.json.JsonUtils;
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.IotDeviceEventReportReqDTO;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO;
|
||||
import com.zt.plat.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||
import com.zt.plat.module.iot.plugin.common.util.IotPluginCommonUtils;
|
||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.mqtt.MqttClient;
|
||||
import io.vertx.mqtt.messages.MqttPublishMessage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* IoT 设备 MQTT 消息处理器
|
||||
*
|
||||
* 参考:<a href="https://help.aliyun.com/zh/iot/user-guide/device-properties-events-and-services">设备属性、事件、服务</a>
|
||||
*/
|
||||
@Slf4j
|
||||
public class IotDeviceMqttMessageHandler {
|
||||
|
||||
// TODO @haohao:讨论,感觉 mqtt 和 http,可以做个相对统一的格式哈;回复 都使用 Alink 格式,方便后续扩展。
|
||||
// 设备上报属性 标准 JSON
|
||||
// 请求 Topic:/sys/${productKey}/${deviceName}/thing/event/property/post
|
||||
// 响应 Topic:/sys/${productKey}/${deviceName}/thing/event/property/post_reply
|
||||
|
||||
// 设备上报事件 标准 JSON
|
||||
// 请求 Topic:/sys/${productKey}/${deviceName}/thing/event/${tsl.event.identifier}/post
|
||||
// 响应 Topic:/sys/${productKey}/${deviceName}/thing/event/${tsl.event.identifier}/post_reply
|
||||
|
||||
private static final String SYS_TOPIC_PREFIX = "/sys/";
|
||||
private static final String PROPERTY_POST_TOPIC = "/thing/event/property/post";
|
||||
private static final String EVENT_POST_TOPIC_PREFIX = "/thing/event/";
|
||||
private static final String EVENT_POST_TOPIC_SUFFIX = "/post";
|
||||
private static final String REPLY_SUFFIX = "_reply";
|
||||
private static final String PROPERTY_METHOD = "thing.event.property.post";
|
||||
private static final String EVENT_METHOD_PREFIX = "thing.event.";
|
||||
private static final String EVENT_METHOD_SUFFIX = ".post";
|
||||
|
||||
private final IotDeviceUpstreamApi deviceUpstreamApi;
|
||||
private final MqttClient mqttClient;
|
||||
|
||||
public IotDeviceMqttMessageHandler(IotDeviceUpstreamApi deviceUpstreamApi, MqttClient mqttClient) {
|
||||
this.deviceUpstreamApi = deviceUpstreamApi;
|
||||
this.mqttClient = mqttClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理MQTT消息
|
||||
*
|
||||
* @param message MQTT发布消息
|
||||
*/
|
||||
public void handle(MqttPublishMessage message) {
|
||||
String topic = message.topicName();
|
||||
String payload = message.payload().toString();
|
||||
log.info("[messageHandler][接收到消息][topic: {}][payload: {}]", topic, payload);
|
||||
|
||||
try {
|
||||
if (StrUtil.isEmpty(payload)) {
|
||||
log.warn("[messageHandler][消息内容为空][topic: {}]", topic);
|
||||
return;
|
||||
}
|
||||
handleMessage(topic, payload);
|
||||
} catch (Exception e) {
|
||||
log.error("[messageHandler][处理消息失败][topic: {}][payload: {}]", topic, payload, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主题类型处理消息
|
||||
*
|
||||
* @param topic 主题
|
||||
* @param payload 消息内容
|
||||
*/
|
||||
private void handleMessage(String topic, String payload) {
|
||||
// 校验前缀
|
||||
if (!topic.startsWith(SYS_TOPIC_PREFIX)) {
|
||||
log.warn("[handleMessage][未知的消息类型][topic: {}]", topic);
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理设备属性上报消息
|
||||
if (topic.endsWith(PROPERTY_POST_TOPIC)) {
|
||||
log.info("[handleMessage][接收到设备属性上报][topic: {}]", topic);
|
||||
handlePropertyPost(topic, payload);
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理设备事件上报消息
|
||||
if (topic.contains(EVENT_POST_TOPIC_PREFIX) && topic.endsWith(EVENT_POST_TOPIC_SUFFIX)) {
|
||||
log.info("[handleMessage][接收到设备事件上报][topic: {}]", topic);
|
||||
handleEventPost(topic, payload);
|
||||
return;
|
||||
}
|
||||
|
||||
// 未知消息类型
|
||||
log.warn("[handleMessage][未知的消息类型][topic: {}]", topic);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理设备属性上报消息
|
||||
*
|
||||
* @param topic 主题
|
||||
* @param payload 消息内容
|
||||
*/
|
||||
private void handlePropertyPost(String topic, String payload) {
|
||||
try {
|
||||
// 解析消息内容
|
||||
JSONObject jsonObject = JSONUtil.parseObj(payload);
|
||||
String[] topicParts = parseTopic(topic);
|
||||
if (topicParts == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 构建设备属性上报请求对象
|
||||
IotDevicePropertyReportReqDTO reportReqDTO = buildPropertyReportDTO(jsonObject, topicParts);
|
||||
|
||||
// 调用上游 API 处理设备上报数据
|
||||
deviceUpstreamApi.reportDeviceProperty(reportReqDTO);
|
||||
log.info("[handlePropertyPost][处理设备属性上报成功][topic: {}]", topic);
|
||||
|
||||
// 发送响应消息
|
||||
sendResponse(topic, jsonObject, PROPERTY_METHOD, null);
|
||||
} catch (Exception e) {
|
||||
log.error("[handlePropertyPost][处理设备属性上报失败][topic: {}][payload: {}]", topic, payload, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理设备事件上报消息
|
||||
*
|
||||
* @param topic 主题
|
||||
* @param payload 消息内容
|
||||
*/
|
||||
private void handleEventPost(String topic, String payload) {
|
||||
try {
|
||||
// 解析消息内容
|
||||
JSONObject jsonObject = JSONUtil.parseObj(payload);
|
||||
String[] topicParts = parseTopic(topic);
|
||||
if (topicParts == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 构建设备事件上报请求对象
|
||||
IotDeviceEventReportReqDTO reportReqDTO = buildEventReportDTO(jsonObject, topicParts);
|
||||
|
||||
// 调用上游 API 处理设备上报数据
|
||||
deviceUpstreamApi.reportDeviceEvent(reportReqDTO);
|
||||
log.info("[handleEventPost][处理设备事件上报成功][topic: {}]", topic);
|
||||
|
||||
// 从 topic 中获取事件标识符
|
||||
String eventIdentifier = getEventIdentifier(topicParts, topic);
|
||||
if (eventIdentifier == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送响应消息
|
||||
String method = EVENT_METHOD_PREFIX + eventIdentifier + EVENT_METHOD_SUFFIX;
|
||||
sendResponse(topic, jsonObject, method, null);
|
||||
} catch (Exception e) {
|
||||
log.error("[handleEventPost][处理设备事件上报失败][topic: {}][payload: {}]", topic, payload, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析主题,获取主题各部分
|
||||
*
|
||||
* @param topic 主题
|
||||
* @return 主题各部分数组,如果解析失败返回null
|
||||
*/
|
||||
private String[] parseTopic(String topic) {
|
||||
String[] topicParts = topic.split("/");
|
||||
if (topicParts.length < 7) {
|
||||
log.warn("[parseTopic][主题格式不正确][topic: {}]", topic);
|
||||
return null;
|
||||
}
|
||||
return topicParts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从主题部分中获取事件标识符
|
||||
*
|
||||
* @param topicParts 主题各部分
|
||||
* @param topic 原始主题,用于日志
|
||||
* @return 事件标识符,如果获取失败返回null
|
||||
*/
|
||||
private String getEventIdentifier(String[] topicParts, String topic) {
|
||||
try {
|
||||
return topicParts[6];
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
log.warn("[getEventIdentifier][无法从主题中获取事件标识符][topic: {}][topicParts: {}]",
|
||||
topic, Arrays.toString(topicParts));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送响应消息
|
||||
*
|
||||
* @param topic 原始主题
|
||||
* @param jsonObject 原始消息JSON对象
|
||||
* @param method 响应方法
|
||||
* @param customData 自定义数据,可为 null
|
||||
*/
|
||||
private void sendResponse(String topic, JSONObject jsonObject, String method, Object customData) {
|
||||
String replyTopic = topic + REPLY_SUFFIX;
|
||||
|
||||
// 响应结果
|
||||
IotStandardResponse response = IotStandardResponse.success(
|
||||
jsonObject.getStr("id"), method, customData);
|
||||
try {
|
||||
mqttClient.publish(replyTopic, Buffer.buffer(JsonUtils.toJsonString(response)),
|
||||
MqttQoS.AT_LEAST_ONCE, false, false);
|
||||
log.info("[sendResponse][发送响应消息成功][topic: {}]", replyTopic);
|
||||
} catch (Exception e) {
|
||||
log.error("[sendResponse][发送响应消息失败][topic: {}][response: {}]", replyTopic, response, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建设备属性上报请求对象
|
||||
*
|
||||
* @param jsonObject 消息内容
|
||||
* @param topicParts 主题部分
|
||||
* @return 设备属性上报请求对象
|
||||
*/
|
||||
private IotDevicePropertyReportReqDTO buildPropertyReportDTO(JSONObject jsonObject, String[] topicParts) {
|
||||
IotDevicePropertyReportReqDTO reportReqDTO = new IotDevicePropertyReportReqDTO();
|
||||
reportReqDTO.setRequestId(jsonObject.getStr("id"));
|
||||
reportReqDTO.setProcessId(IotPluginCommonUtils.getProcessId());
|
||||
reportReqDTO.setReportTime(LocalDateTime.now());
|
||||
reportReqDTO.setProductKey(topicParts[2]);
|
||||
reportReqDTO.setDeviceName(topicParts[3]);
|
||||
|
||||
// 只使用标准JSON格式处理属性数据
|
||||
JSONObject params = jsonObject.getJSONObject("params");
|
||||
if (params == null) {
|
||||
log.warn("[buildPropertyReportDTO][消息格式不正确,缺少params字段][jsonObject: {}]", jsonObject);
|
||||
params = new JSONObject();
|
||||
}
|
||||
|
||||
// 将标准格式的params转换为平台需要的properties格式
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object valueObj = entry.getValue();
|
||||
|
||||
// 如果是复杂结构(包含value和time)
|
||||
if (valueObj instanceof JSONObject valueJson) {
|
||||
properties.put(key, valueJson.getOrDefault("value", valueObj));
|
||||
} else {
|
||||
properties.put(key, valueObj);
|
||||
}
|
||||
}
|
||||
reportReqDTO.setProperties(properties);
|
||||
|
||||
return reportReqDTO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建设备事件上报请求对象
|
||||
*
|
||||
* @param jsonObject 消息内容
|
||||
* @param topicParts 主题部分
|
||||
* @return 设备事件上报请求对象
|
||||
*/
|
||||
private IotDeviceEventReportReqDTO buildEventReportDTO(JSONObject jsonObject, String[] topicParts) {
|
||||
IotDeviceEventReportReqDTO reportReqDTO = new IotDeviceEventReportReqDTO();
|
||||
reportReqDTO.setRequestId(jsonObject.getStr("id"));
|
||||
reportReqDTO.setProcessId(IotPluginCommonUtils.getProcessId());
|
||||
reportReqDTO.setReportTime(LocalDateTime.now());
|
||||
reportReqDTO.setProductKey(topicParts[2]);
|
||||
reportReqDTO.setDeviceName(topicParts[3]);
|
||||
reportReqDTO.setIdentifier(topicParts[6]);
|
||||
|
||||
// 只使用标准JSON格式处理事件参数
|
||||
JSONObject params = jsonObject.getJSONObject("params");
|
||||
if (params == null) {
|
||||
log.warn("[buildEventReportDTO][消息格式不正确,缺少params字段][jsonObject: {}]", jsonObject);
|
||||
params = new JSONObject();
|
||||
}
|
||||
reportReqDTO.setParams(params);
|
||||
|
||||
return reportReqDTO;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package com.zt.plat.module.iot.plugin.emqx.upstream.router;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO;
|
||||
import com.zt.plat.module.iot.enums.device.IotDeviceStateEnum;
|
||||
import com.zt.plat.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.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* IoT EMQX Webhook 事件处理的 Vert.x Handler
|
||||
*
|
||||
* 参考:<a href="https://docs.emqx.com/zh/emqx/latest/data-integration/webhook.html">EMQX Webhook</a>
|
||||
*
|
||||
* 注意:该处理器需要返回特定格式:{"result": "success"} 或 {"result": "error"},
|
||||
* 以符合 EMQX Webhook 插件的要求,因此不使用 IotStandardResponse 实体类。
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class IotDeviceWebhookVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
public static final String PATH = "/mqtt/webhook";
|
||||
|
||||
private final IotDeviceUpstreamApi deviceUpstreamApi;
|
||||
|
||||
@Override
|
||||
public void handle(RoutingContext routingContext) {
|
||||
try {
|
||||
// 解析请求体
|
||||
JsonObject json = routingContext.body().asJsonObject();
|
||||
String event = json.getString("event");
|
||||
String clientId = json.getString("clientid");
|
||||
String username = json.getString("username");
|
||||
|
||||
// 处理不同的事件类型
|
||||
switch (event) {
|
||||
case "client.connected":
|
||||
handleClientConnected(clientId, username);
|
||||
break;
|
||||
case "client.disconnected":
|
||||
handleClientDisconnected(clientId, username);
|
||||
break;
|
||||
default:
|
||||
log.info("[handle][未处理的 Webhook 事件] event={}, clientId={}, username={}", event, clientId, username);
|
||||
break;
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
// 注意:这里必须返回 {"result": "success"} 格式,以符合 EMQX Webhook 插件的要求
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, Collections.singletonMap("result", "success"));
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][处理 Webhook 事件异常]", e);
|
||||
// 注意:这里必须返回 {"result": "error"} 格式,以符合 EMQX Webhook 插件的要求
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, Collections.singletonMap("result", "error"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端连接事件
|
||||
*
|
||||
* @param clientId 客户端ID
|
||||
* @param username 用户名
|
||||
*/
|
||||
private void handleClientConnected(String clientId, String username) {
|
||||
// 解析产品标识和设备名称
|
||||
if (StrUtil.isEmpty(username) || "undefined".equals(username)) {
|
||||
log.warn("[handleClientConnected][客户端连接事件,但用户名为空] clientId={}", clientId);
|
||||
return;
|
||||
}
|
||||
String[] parts = parseUsername(username);
|
||||
if (parts == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新设备状态为在线
|
||||
IotDeviceStateUpdateReqDTO updateReqDTO = new IotDeviceStateUpdateReqDTO();
|
||||
updateReqDTO.setProductKey(parts[1]);
|
||||
updateReqDTO.setDeviceName(parts[0]);
|
||||
updateReqDTO.setState(IotDeviceStateEnum.ONLINE.getState());
|
||||
updateReqDTO.setProcessId(IotPluginCommonUtils.getProcessId());
|
||||
updateReqDTO.setReportTime(LocalDateTime.now());
|
||||
CommonResult<Boolean> result = deviceUpstreamApi.updateDeviceState(updateReqDTO);
|
||||
if (result.getCode() != 0 || !result.getData()) {
|
||||
log.error("[handleClientConnected][更新设备状态为在线失败] clientId={}, username={}, code={}, msg={}",
|
||||
clientId, username, result.getCode(), result.getMsg());
|
||||
} else {
|
||||
log.info("[handleClientConnected][更新设备状态为在线成功] clientId={}, username={}", clientId, username);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端断开连接事件
|
||||
*
|
||||
* @param clientId 客户端ID
|
||||
* @param username 用户名
|
||||
*/
|
||||
private void handleClientDisconnected(String clientId, String username) {
|
||||
// 解析产品标识和设备名称
|
||||
if (StrUtil.isEmpty(username) || "undefined".equals(username)) {
|
||||
log.warn("[handleClientDisconnected][客户端断开连接事件,但用户名为空] clientId={}", clientId);
|
||||
return;
|
||||
}
|
||||
String[] parts = parseUsername(username);
|
||||
if (parts == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新设备状态为离线
|
||||
IotDeviceStateUpdateReqDTO offlineReqDTO = new IotDeviceStateUpdateReqDTO();
|
||||
offlineReqDTO.setProductKey(parts[1]);
|
||||
offlineReqDTO.setDeviceName(parts[0]);
|
||||
offlineReqDTO.setState(IotDeviceStateEnum.OFFLINE.getState());
|
||||
offlineReqDTO.setProcessId(IotPluginCommonUtils.getProcessId());
|
||||
offlineReqDTO.setReportTime(LocalDateTime.now());
|
||||
CommonResult<Boolean> offlineResult = deviceUpstreamApi.updateDeviceState(offlineReqDTO);
|
||||
if (offlineResult.getCode() != 0 || !offlineResult.getData()) {
|
||||
log.error("[handleClientDisconnected][更新设备状态为离线失败] clientId={}, username={}, code={}, msg={}",
|
||||
clientId, username, offlineResult.getCode(), offlineResult.getMsg());
|
||||
} else {
|
||||
log.info("[handleClientDisconnected][更新设备状态为离线成功] clientId={}, username={}", clientId, username);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析用户名,格式为 deviceName&productKey
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return 解析结果,[0] 为 deviceName,[1] 为 productKey,解析失败返回 null
|
||||
*/
|
||||
private String[] parseUsername(String username) {
|
||||
if (StrUtil.isEmpty(username)) {
|
||||
return null;
|
||||
}
|
||||
String[] parts = username.split("&");
|
||||
if (parts.length != 2) {
|
||||
log.warn("[parseUsername][用户名格式({})不正确,无法解析产品标识和设备名称]", username);
|
||||
return null;
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
application:
|
||||
name: cloud-module-iot-plugin-emqx
|
||||
|
||||
cloud:
|
||||
iot:
|
||||
plugin:
|
||||
common:
|
||||
upstream-url: http://127.0.0.1:48080
|
||||
downstream-port: 8100
|
||||
plugin-key: cloud-module-iot-plugin-emqx
|
||||
emqx:
|
||||
mqtt-host: 127.0.0.1
|
||||
mqtt-port: 1883
|
||||
mqtt-ssl: false
|
||||
mqtt-username: cloud
|
||||
mqtt-password: 123456
|
||||
mqtt-topics:
|
||||
- "/sys/#"
|
||||
auth-port: 8101
|
||||
@@ -0,0 +1,6 @@
|
||||
plugin.id=cloud-module-iot-plugin-http
|
||||
plugin.class=com.zt.plat.module.iot.plugin.http.config.IotHttpVertxPlugin
|
||||
plugin.version=1.0.0
|
||||
plugin.provider=cloud
|
||||
plugin.dependencies=
|
||||
plugin.description=cloud-module-iot-plugin-http-1.0.0
|
||||
@@ -0,0 +1,165 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="
|
||||
http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>zt-module-iot-plugins</artifactId>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<artifactId>zt-module-iot-plugin-http</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>
|
||||
<!-- TODO @芋艿:注释 -->
|
||||
物联网 插件模块 - http 插件
|
||||
</description>
|
||||
|
||||
<properties>
|
||||
<!-- 插件相关 -->
|
||||
<plugin.id>${project.artifactId}</plugin.id>
|
||||
<plugin.class>com.zt.plat.module.iot.plugin.http.config.IotHttpVertxPlugin</plugin.class>
|
||||
<plugin.version>${project.version}</plugin.version>
|
||||
<plugin.provider>zt</plugin.provider>
|
||||
<plugin.description>${project.artifactId}-${project.version}</plugin.description>
|
||||
<plugin.dependencies/>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- 插件模式 zip -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-antrun-plugin</artifactId>
|
||||
<version>1.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>unzip jar file</id>
|
||||
<phase>package</phase>
|
||||
<configuration>
|
||||
<target>
|
||||
<unzip src="target/${project.artifactId}-${project.version}.${project.packaging}"
|
||||
dest="target/plugin-classes"/>
|
||||
</target>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>
|
||||
src/main/assembly/assembly.xml
|
||||
</descriptor>
|
||||
</descriptors>
|
||||
<appendAssemblyId>false</appendAssemblyId>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>attached</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- 插件模式 jar -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries>
|
||||
<Plugin-Id>${plugin.id}</Plugin-Id>
|
||||
<Plugin-Class>${plugin.class}</Plugin-Class>
|
||||
<Plugin-Version>${plugin.version}</Plugin-Version>
|
||||
<Plugin-Provider>${plugin.provider}</Plugin-Provider>
|
||||
<Plugin-Description>${plugin.description}</Plugin-Description>
|
||||
<Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
|
||||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!-- <artifactId>maven-shade-plugin</artifactId>-->
|
||||
<!-- <version>3.6.0</version>-->
|
||||
<!-- <executions>-->
|
||||
<!-- <execution>-->
|
||||
<!-- <phase>package</phase>-->
|
||||
<!-- <goals>-->
|
||||
<!-- <goal>shade</goal>-->
|
||||
<!-- </goals>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <minimizeJar>true</minimizeJar>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </execution>-->
|
||||
<!-- </executions>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <archive>-->
|
||||
<!-- <manifestEntries>-->
|
||||
<!-- <Plugin-Id>${plugin.id}</Plugin-Id>-->
|
||||
<!-- <Plugin-Class>${plugin.class}</Plugin-Class>-->
|
||||
<!-- <Plugin-Version>${plugin.version}</Plugin-Version>-->
|
||||
<!-- <Plugin-Provider>${plugin.provider}</Plugin-Provider>-->
|
||||
<!-- <Plugin-Description>${plugin.description}</Plugin-Description>-->
|
||||
<!-- <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>-->
|
||||
<!-- </manifestEntries>-->
|
||||
<!-- </archive>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </plugin>-->
|
||||
|
||||
<!-- 独立模式 -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>-standalone</classifier>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<artifactId>zt-module-iot-plugin-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Web 相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 工具类相关 -->
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,24 @@
|
||||
<assembly>
|
||||
<id>plugin</id>
|
||||
<formats>
|
||||
<format>zip</format>
|
||||
</formats>
|
||||
<includeBaseDirectory>false</includeBaseDirectory>
|
||||
<dependencySets>
|
||||
<dependencySet>
|
||||
<useProjectArtifact>false</useProjectArtifact>
|
||||
<scope>runtime</scope>
|
||||
<outputDirectory>lib</outputDirectory>
|
||||
<includes>
|
||||
<include>*:jar:*</include>
|
||||
</includes>
|
||||
</dependencySet>
|
||||
</dependencySets>
|
||||
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>target/plugin-classes</directory>
|
||||
<outputDirectory>classes</outputDirectory>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
</assembly>
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.zt.plat.module.iot.plugin.http;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* 独立运行入口
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootApplication
|
||||
public class IotHttpPluginApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication application = new SpringApplication(IotHttpPluginApplication.class);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
application.run(args);
|
||||
log.info("[main][独立模式启动完成]");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.zt.plat.module.iot.plugin.http.config;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.PluginWrapper;
|
||||
import org.pf4j.spring.SpringPlugin;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
// TODO @芋艿:完善注释
|
||||
/**
|
||||
* 负责插件的启动和停止,与 Vert.x 的生命周期管理
|
||||
*/
|
||||
@Slf4j
|
||||
public class IotHttpVertxPlugin extends SpringPlugin {
|
||||
|
||||
public IotHttpVertxPlugin(PluginWrapper wrapper) {
|
||||
super(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动开始...]");
|
||||
try {
|
||||
ApplicationContext pluginContext = getApplicationContext();
|
||||
Assert.notNull(pluginContext, "pluginContext 不能为空");
|
||||
log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动成功...]");
|
||||
} catch (Exception e) {
|
||||
log.error("[HttpVertxPlugin][HttpVertxPlugin 插件开启动异常...]", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止开始...]");
|
||||
try {
|
||||
log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止成功...]");
|
||||
} catch (Exception e) {
|
||||
log.error("[HttpVertxPlugin][HttpVertxPlugin 插件停止异常...]", e);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO @芋艿:思考下,未来要不要。。。
|
||||
@Override
|
||||
protected ApplicationContext createApplicationContext() {
|
||||
// 创建插件自己的 ApplicationContext
|
||||
AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext();
|
||||
// 设置父容器为主应用的 ApplicationContext (确保主应用中提供的类可用)
|
||||
pluginContext.setParent(SpringUtil.getApplicationContext());
|
||||
// 继续使用插件自己的 ClassLoader 以加载插件内部的类
|
||||
pluginContext.setClassLoader(getWrapper().getPluginClassLoader());
|
||||
// 扫描当前插件的自动配置包
|
||||
// TODO @芋艿:后续看看,怎么配置类包
|
||||
pluginContext.scan("com.zt.plat.module.iot.plugin.http.config");
|
||||
pluginContext.refresh();
|
||||
return pluginContext;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.zt.plat.module.iot.plugin.http.config;
|
||||
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
import com.zt.plat.module.iot.plugin.http.downstream.IotDeviceDownstreamHandlerImpl;
|
||||
import com.zt.plat.module.iot.plugin.http.upstream.IotDeviceUpstreamServer;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* IoT 插件 HTTP 的专用自动配置类
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(IotPluginHttpProperties.class)
|
||||
public class IotPluginHttpAutoConfiguration {
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||
public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi,
|
||||
IotPluginHttpProperties properties) {
|
||||
return new IotDeviceUpstreamServer(properties, deviceUpstreamApi);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IotDeviceDownstreamHandler deviceDownstreamHandler() {
|
||||
return new IotDeviceDownstreamHandlerImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.zt.plat.module.iot.plugin.http.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@ConfigurationProperties(prefix = "cloud.iot.plugin.http")
|
||||
@Validated
|
||||
@Data
|
||||
public class IotPluginHttpProperties {
|
||||
|
||||
/**
|
||||
* HTTP 服务端口
|
||||
*/
|
||||
private Integer serverPort;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.zt.plat.module.iot.plugin.http.downstream;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.downstream.*;
|
||||
import com.zt.plat.module.iot.plugin.common.downstream.IotDeviceDownstreamHandler;
|
||||
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
|
||||
|
||||
/**
|
||||
* HTTP 插件的 {@link IotDeviceDownstreamHandler} 实现类
|
||||
*
|
||||
* 但是:由于设备通过 HTTP 短链接接入,导致其实无法下行指导给 device 设备,所以基本都是直接返回失败!!!
|
||||
* 类似 MQTT、WebSocket、TCP 插件,是可以实现下行指令的。
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
public class IotDeviceDownstreamHandlerImpl implements IotDeviceDownstreamHandler {
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> invokeDeviceService(IotDeviceServiceInvokeReqDTO invokeReqDTO) {
|
||||
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持调用设备服务");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> getDeviceProperty(IotDevicePropertyGetReqDTO getReqDTO) {
|
||||
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持获取设备属性");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> setDeviceProperty(IotDevicePropertySetReqDTO setReqDTO) {
|
||||
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持设置设备属性");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> setDeviceConfig(IotDeviceConfigSetReqDTO setReqDTO) {
|
||||
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持设置设备属性");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> upgradeDeviceOta(IotDeviceOtaUpgradeReqDTO upgradeReqDTO) {
|
||||
return CommonResult.error(NOT_IMPLEMENTED.getCode(), "HTTP 不支持设置设备属性");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.zt.plat.module.iot.plugin.http.upstream;
|
||||
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.plugin.http.config.IotPluginHttpProperties;
|
||||
import com.zt.plat.module.iot.plugin.http.upstream.router.IotDeviceUpstreamVertxHandler;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.handler.BodyHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* IoT 设备下行服务端,接收来自 device 设备的请求,转发给 server 服务器
|
||||
*
|
||||
* 协议:HTTP
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@Slf4j
|
||||
public class IotDeviceUpstreamServer {
|
||||
|
||||
private final Vertx vertx;
|
||||
private final HttpServer server;
|
||||
private final IotPluginHttpProperties properties;
|
||||
|
||||
public IotDeviceUpstreamServer(IotPluginHttpProperties properties,
|
||||
IotDeviceUpstreamApi deviceUpstreamApi) {
|
||||
this.properties = properties;
|
||||
// 创建 Vertx 实例
|
||||
this.vertx = Vertx.vertx();
|
||||
// 创建 Router 实例
|
||||
Router router = Router.router(vertx);
|
||||
router.route().handler(BodyHandler.create()); // 处理 Body
|
||||
|
||||
// 使用统一的 Handler 处理所有上行请求
|
||||
IotDeviceUpstreamVertxHandler upstreamHandler = new IotDeviceUpstreamVertxHandler(deviceUpstreamApi);
|
||||
router.post(IotDeviceUpstreamVertxHandler.PROPERTY_PATH).handler(upstreamHandler);
|
||||
router.post(IotDeviceUpstreamVertxHandler.EVENT_PATH).handler(upstreamHandler);
|
||||
|
||||
// 创建 HttpServer 实例
|
||||
this.server = vertx.createHttpServer().requestHandler(router);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动 HTTP 服务器
|
||||
*/
|
||||
public void start() {
|
||||
log.info("[start][开始启动]");
|
||||
server.listen(properties.getServerPort())
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
log.info("[start][启动完成,端口({})]", this.server.actualPort());
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止所有
|
||||
*/
|
||||
public void stop() {
|
||||
log.info("[stop][开始关闭]");
|
||||
try {
|
||||
// 关闭 HTTP 服务器
|
||||
if (server != null) {
|
||||
server.close()
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
}
|
||||
|
||||
// 关闭 Vertx 实例
|
||||
if (vertx != null) {
|
||||
vertx.close()
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.join();
|
||||
}
|
||||
log.info("[stop][关闭完成]");
|
||||
} catch (Exception e) {
|
||||
log.error("[stop][关闭异常]", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package com.zt.plat.module.iot.plugin.http.upstream.router;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.iot.api.device.IotDeviceUpstreamApi;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.IotDeviceEventReportReqDTO;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.IotDevicePropertyReportReqDTO;
|
||||
import com.zt.plat.module.iot.api.device.dto.control.upstream.IotDeviceStateUpdateReqDTO;
|
||||
import com.zt.plat.module.iot.enums.device.IotDeviceStateEnum;
|
||||
import com.zt.plat.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||
import com.zt.plat.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.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
||||
import static com.zt.plat.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
|
||||
/**
|
||||
* IoT 设备上行统一处理的 Vert.x Handler
|
||||
* <p>
|
||||
* 统一处理设备属性上报和事件上报的请求
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||
|
||||
// TODO @haohao:要不要类似 IotDeviceConfigSetVertxHandler 写的,把这些 PATH、METHOD 之类的抽走
|
||||
/**
|
||||
* 属性上报路径
|
||||
*/
|
||||
public static final String PROPERTY_PATH = "/sys/:productKey/:deviceName/thing/event/property/post";
|
||||
/**
|
||||
* 事件上报路径
|
||||
*/
|
||||
public static final String EVENT_PATH = "/sys/:productKey/:deviceName/thing/event/:identifier/post";
|
||||
|
||||
private static final String PROPERTY_METHOD = "thing.event.property.post";
|
||||
private static final String EVENT_METHOD_PREFIX = "thing.event.";
|
||||
private static final String EVENT_METHOD_SUFFIX = ".post";
|
||||
|
||||
private final IotDeviceUpstreamApi deviceUpstreamApi;
|
||||
|
||||
// TODO @haohao:要不要分成多个 Handler?每个只解决一个问题哈。
|
||||
@Override
|
||||
public void handle(RoutingContext routingContext) {
|
||||
String path = routingContext.request().path();
|
||||
String requestId = IdUtil.fastSimpleUUID();
|
||||
|
||||
try {
|
||||
// 1. 解析通用参数
|
||||
String productKey = routingContext.pathParam("productKey");
|
||||
String deviceName = routingContext.pathParam("deviceName");
|
||||
JsonObject body = routingContext.body().asJsonObject();
|
||||
requestId = ObjUtil.defaultIfBlank(body.getString("id"), requestId);
|
||||
|
||||
// 2. 根据路径模式处理不同类型的请求
|
||||
CommonResult<Boolean> result;
|
||||
String method;
|
||||
if (path.matches(".*/thing/event/property/post")) {
|
||||
// 处理属性上报
|
||||
IotDevicePropertyReportReqDTO reportReqDTO = parsePropertyReportRequest(productKey, deviceName, requestId, body);
|
||||
|
||||
// 设备上线
|
||||
updateDeviceState(reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
|
||||
|
||||
// 属性上报
|
||||
result = deviceUpstreamApi.reportDeviceProperty(reportReqDTO);
|
||||
method = PROPERTY_METHOD;
|
||||
} else if (path.matches(".*/thing/event/.+/post")) {
|
||||
// 处理事件上报
|
||||
String identifier = routingContext.pathParam("identifier");
|
||||
IotDeviceEventReportReqDTO reportReqDTO = parseEventReportRequest(productKey, deviceName, identifier, requestId, body);
|
||||
|
||||
// 设备上线
|
||||
updateDeviceState(reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
|
||||
|
||||
// 事件上报
|
||||
result = deviceUpstreamApi.reportDeviceEvent(reportReqDTO);
|
||||
method = EVENT_METHOD_PREFIX + identifier + EVENT_METHOD_SUFFIX;
|
||||
} else {
|
||||
// 不支持的请求路径
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(requestId, "unknown", BAD_REQUEST.getCode(), "不支持的请求路径");
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 返回标准响应
|
||||
IotStandardResponse response;
|
||||
if (result.isSuccess()) {
|
||||
response = IotStandardResponse.success(requestId, method, result.getData());
|
||||
} else {
|
||||
response = IotStandardResponse.error(requestId, method, result.getCode(), result.getMsg());
|
||||
}
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, response);
|
||||
} catch (Exception e) {
|
||||
log.error("[handle][处理上行请求异常] path={}", path, e);
|
||||
String method = path.contains("/property/") ? PROPERTY_METHOD
|
||||
: EVENT_METHOD_PREFIX + (routingContext.pathParams().containsKey("identifier")
|
||||
? routingContext.pathParam("identifier")
|
||||
: "unknown") + EVENT_METHOD_SUFFIX;
|
||||
IotStandardResponse errorResponse = IotStandardResponse.error(requestId, method, INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg());
|
||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新设备状态
|
||||
*
|
||||
* @param productKey 产品 Key
|
||||
* @param deviceName 设备名称
|
||||
*/
|
||||
private void updateDeviceState(String productKey, String deviceName) {
|
||||
deviceUpstreamApi.updateDeviceState(((IotDeviceStateUpdateReqDTO) new IotDeviceStateUpdateReqDTO()
|
||||
.setRequestId(IdUtil.fastSimpleUUID()).setProcessId(IotPluginCommonUtils.getProcessId()).setReportTime(LocalDateTime.now())
|
||||
.setProductKey(productKey).setDeviceName(deviceName)).setState(IotDeviceStateEnum.ONLINE.getState()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析属性上报请求
|
||||
*
|
||||
* @param productKey 产品 Key
|
||||
* @param deviceName 设备名称
|
||||
* @param requestId 请求 ID
|
||||
* @param body 请求体
|
||||
* @return 属性上报请求 DTO
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private IotDevicePropertyReportReqDTO parsePropertyReportRequest(String productKey, String deviceName, String requestId, JsonObject body) {
|
||||
// 按照标准 JSON 格式处理属性数据
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
Map<String, Object> params = body.getJsonObject("params") != null ? body.getJsonObject("params").getMap() : null;
|
||||
if (params != null) {
|
||||
// 将标准格式的 params 转换为平台需要的 properties 格式
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object valueObj = entry.getValue();
|
||||
// 如果是复杂结构(包含 value 和 time)
|
||||
if (valueObj instanceof Map) {
|
||||
Map<String, Object> valueMap = (Map<String, Object>) valueObj;
|
||||
properties.put(key, valueMap.getOrDefault("value", valueObj));
|
||||
} else {
|
||||
properties.put(key, valueObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 构建属性上报请求 DTO
|
||||
return ((IotDevicePropertyReportReqDTO) new IotDevicePropertyReportReqDTO().setRequestId(requestId)
|
||||
.setProcessId(IotPluginCommonUtils.getProcessId()).setReportTime(LocalDateTime.now())
|
||||
.setProductKey(productKey).setDeviceName(deviceName)).setProperties(properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析事件上报请求
|
||||
*
|
||||
* @param productKey 产品K ey
|
||||
* @param deviceName 设备名称
|
||||
* @param identifier 事件标识符
|
||||
* @param requestId 请求 ID
|
||||
* @param body 请求体
|
||||
* @return 事件上报请求 DTO
|
||||
*/
|
||||
private IotDeviceEventReportReqDTO parseEventReportRequest(String productKey, String deviceName, String identifier, String requestId, JsonObject body) {
|
||||
// 按照标准 JSON 格式处理事件参数
|
||||
Map<String, Object> params;
|
||||
if (body.containsKey("params")) {
|
||||
params = body.getJsonObject("params").getMap();
|
||||
} else {
|
||||
// 兼容旧格式
|
||||
params = new HashMap<>();
|
||||
}
|
||||
|
||||
// 构建事件上报请求 DTO
|
||||
return ((IotDeviceEventReportReqDTO) new IotDeviceEventReportReqDTO().setRequestId(requestId)
|
||||
.setProcessId(IotPluginCommonUtils.getProcessId()).setReportTime(LocalDateTime.now())
|
||||
.setProductKey(productKey).setDeviceName(deviceName)).setIdentifier(identifier).setParams(params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
spring:
|
||||
application:
|
||||
name: cloud-module-iot-plugin-http
|
||||
|
||||
cloud:
|
||||
iot:
|
||||
plugin:
|
||||
common:
|
||||
upstream-url: http://127.0.0.1:48080
|
||||
downstream-port: 8093
|
||||
plugin-key: cloud-module-iot-plugin-http
|
||||
http:
|
||||
server-port: 8092
|
||||
@@ -0,0 +1,7 @@
|
||||
plugin.id=mqtt-plugin
|
||||
plugin.description=Vert.x MQTT plugin
|
||||
plugin.class=com.zt.plat.module.iot.plugin.MqttPlugin
|
||||
plugin.version=1.0.0
|
||||
plugin.requires=
|
||||
plugin.provider=ahh
|
||||
plugin.license=Apache-2.0
|
||||
@@ -0,0 +1,155 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="
|
||||
http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>zt-module-iot-plugins</artifactId>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<artifactId>zt-module-iot-plugin-mqtt</artifactId>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<!-- TODO @芋艿:待整理 -->
|
||||
<description>
|
||||
物联网 插件模块 - mqtt 插件
|
||||
</description>
|
||||
|
||||
<properties>
|
||||
<!-- 插件相关 -->
|
||||
<plugin.id>mqtt-plugin</plugin.id>
|
||||
<plugin.class>com.zt.plat.module.iot.plugin.MqttPlugin</plugin.class>
|
||||
<plugin.version>0.0.1</plugin.version>
|
||||
<plugin.provider>ahh</plugin.provider>
|
||||
<plugin.description>mqtt-plugin-0.0.1</plugin.description>
|
||||
<plugin.dependencies/>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- DOESN'T WORK WITH MAVEN 3 (I defined the plugin metadata in properties section)
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>properties-maven-plugin</artifactId>
|
||||
<version>1.0-alpha-2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>initialize</phase>
|
||||
<goals>
|
||||
<goal>read-project-properties</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<files>
|
||||
<file>plugin.properties</file>
|
||||
</files>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
-->
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-antrun-plugin</artifactId>
|
||||
<version>1.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>unzip jar file</id>
|
||||
<phase>package</phase>
|
||||
<configuration>
|
||||
<target>
|
||||
<unzip src="target/${project.artifactId}-${project.version}.${project.packaging}"
|
||||
dest="target/plugin-classes"/>
|
||||
</target>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>
|
||||
src/main/assembly/assembly.xml
|
||||
</descriptor>
|
||||
</descriptors>
|
||||
<appendAssemblyId>false</appendAssemblyId>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>attached</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestEntries>
|
||||
<Plugin-Id>${plugin.id}</Plugin-Id>
|
||||
<Plugin-Class>${plugin.class}</Plugin-Class>
|
||||
<Plugin-Version>${plugin.version}</Plugin-Version>
|
||||
<Plugin-Provider>${plugin.provider}</Plugin-Provider>
|
||||
<Plugin-Description>${plugin.description}</Plugin-Description>
|
||||
<Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
|
||||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- 其他依赖项 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- PF4J Spring 集成 -->
|
||||
<dependency>
|
||||
<groupId>org.pf4j</groupId>
|
||||
<artifactId>pf4j-spring</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- 项目依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<artifactId>zt-module-iot-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Vert.x MQTT -->
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-mqtt</artifactId>
|
||||
<version>4.5.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,31 @@
|
||||
<assembly>
|
||||
<id>plugin</id>
|
||||
<formats>
|
||||
<format>zip</format>
|
||||
</formats>
|
||||
<includeBaseDirectory>false</includeBaseDirectory>
|
||||
<dependencySets>
|
||||
<dependencySet>
|
||||
<useProjectArtifact>false</useProjectArtifact>
|
||||
<scope>runtime</scope>
|
||||
<outputDirectory>lib</outputDirectory>
|
||||
<includes>
|
||||
<include>*:jar:*</include>
|
||||
</includes>
|
||||
</dependencySet>
|
||||
</dependencySets>
|
||||
<!--
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>target/classes</directory>
|
||||
<outputDirectory>classes</outputDirectory>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
-->
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>target/plugin-classes</directory>
|
||||
<outputDirectory>classes</outputDirectory>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
</assembly>
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.zt.plat.module.iot.plugin;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.Plugin;
|
||||
import org.pf4j.PluginWrapper;
|
||||
|
||||
// TODO @芋艿:暂未实现
|
||||
@Slf4j
|
||||
public class MqttPlugin extends Plugin {
|
||||
|
||||
private MqttServerExtension mqttServerExtension;
|
||||
|
||||
public MqttPlugin(PluginWrapper wrapper) {
|
||||
super(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
log.info("MQTT Plugin started.");
|
||||
mqttServerExtension = new MqttServerExtension();
|
||||
mqttServerExtension.startMqttServer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
log.info("MQTT Plugin stopped.");
|
||||
if (mqttServerExtension != null) {
|
||||
mqttServerExtension.stopMqttServer().onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
log.info("Stopped MQTT Server successfully");
|
||||
} else {
|
||||
log.error("Failed to stop MQTT Server: {}", ar.cause().getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package com.zt.plat.module.iot.plugin;
|
||||
|
||||
import io.netty.handler.codec.mqtt.MqttProperties;
|
||||
import io.netty.handler.codec.mqtt.MqttQoS;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.mqtt.MqttEndpoint;
|
||||
import io.vertx.mqtt.MqttServer;
|
||||
import io.vertx.mqtt.MqttServerOptions;
|
||||
import io.vertx.mqtt.MqttTopicSubscription;
|
||||
import io.vertx.mqtt.messages.MqttDisconnectMessage;
|
||||
import io.vertx.mqtt.messages.MqttPublishMessage;
|
||||
import io.vertx.mqtt.messages.MqttSubscribeMessage;
|
||||
import io.vertx.mqtt.messages.MqttUnsubscribeMessage;
|
||||
import io.vertx.mqtt.messages.codes.MqttSubAckReasonCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.Extension;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// TODO @芋艿:暂未实现
|
||||
/**
|
||||
* 根据官方示例,整合常见 MQTT 功能到 PF4J 的 Extension 类中
|
||||
*/
|
||||
@Slf4j
|
||||
@Extension
|
||||
public class MqttServerExtension {
|
||||
|
||||
private Vertx vertx;
|
||||
private MqttServer mqttServer;
|
||||
|
||||
/**
|
||||
* 启动 MQTT 服务端
|
||||
* 可根据需要决定是否启用 SSL/TLS、WebSocket、多实例部署等
|
||||
*/
|
||||
public void startMqttServer() {
|
||||
// 初始化 Vert.x
|
||||
vertx = Vertx.vertx();
|
||||
|
||||
// ========== 如果需要 SSL/TLS,请参考下面注释,启用注释并替换端口、证书路径等 ==========
|
||||
// MqttServerOptions options = new MqttServerOptions()
|
||||
// .setPort(8883)
|
||||
// .setKeyCertOptions(new PemKeyCertOptions()
|
||||
// .setKeyPath("./src/test/resources/tls/server-key.pem")
|
||||
// .setCertPath("./src/test/resources/tls/server-cert.pem"))
|
||||
// .setSsl(true);
|
||||
|
||||
// ========== 如果需要 WebSocket,请设置 setUseWebSocket(true) ==========
|
||||
// options.setUseWebSocket(true);
|
||||
|
||||
// ========== 默认不启用 SSL 的示例 ==========
|
||||
MqttServerOptions options = new MqttServerOptions()
|
||||
.setPort(1883)
|
||||
.setHost("0.0.0.0")
|
||||
.setUseWebSocket(false); // 如果需要 WebSocket,请改为 true
|
||||
|
||||
mqttServer = MqttServer.create(vertx, options);
|
||||
|
||||
// 指定 endpointHandler,处理客户端连接等
|
||||
mqttServer.endpointHandler(endpoint -> {
|
||||
handleClientConnect(endpoint);
|
||||
handleDisconnect(endpoint);
|
||||
handleSubscribe(endpoint);
|
||||
handleUnsubscribe(endpoint);
|
||||
handlePublish(endpoint);
|
||||
handlePing(endpoint);
|
||||
});
|
||||
|
||||
// 启动监听
|
||||
mqttServer.listen(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
log.info("MQTT server is listening on port {}", mqttServer.actualPort());
|
||||
} else {
|
||||
log.error("Error on starting the server", ar.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 优雅关闭 MQTT 服务端
|
||||
*/
|
||||
public Future<Void> stopMqttServer() {
|
||||
if (mqttServer != null) {
|
||||
return mqttServer.close().onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
log.info("MQTT server closed.");
|
||||
if (vertx != null) {
|
||||
vertx.close();
|
||||
log.info("Vert.x instance closed.");
|
||||
}
|
||||
} else {
|
||||
log.error("Failed to close MQTT server: {}", ar.cause().getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
return Future.succeededFuture();
|
||||
}
|
||||
|
||||
// ==================== 以下为官方示例中常见事件的处理封装 ====================
|
||||
|
||||
/**
|
||||
* 处理客户端连接 (CONNECT)
|
||||
*/
|
||||
private void handleClientConnect(MqttEndpoint endpoint) {
|
||||
// 打印 CONNECT 的主要信息
|
||||
log.info("MQTT client [{}] request to connect, clean session = {}",
|
||||
endpoint.clientIdentifier(), endpoint.isCleanSession());
|
||||
|
||||
if (endpoint.auth() != null) {
|
||||
log.info("[username = {}, password = {}]", endpoint.auth().getUsername(), endpoint.auth().getPassword());
|
||||
}
|
||||
log.info("[properties = {}]", endpoint.connectProperties());
|
||||
|
||||
if (endpoint.will() != null) {
|
||||
log.info("[will topic = {}, msg = {}, QoS = {}, isRetain = {}]",
|
||||
endpoint.will().getWillTopic(),
|
||||
new String(endpoint.will().getWillMessageBytes()),
|
||||
endpoint.will().getWillQos(),
|
||||
endpoint.will().isWillRetain());
|
||||
}
|
||||
|
||||
log.info("[keep alive timeout = {}]", endpoint.keepAliveTimeSeconds());
|
||||
|
||||
// 接受远程客户端的连接
|
||||
endpoint.accept(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端主动断开 (DISCONNECT)
|
||||
*/
|
||||
private void handleDisconnect(MqttEndpoint endpoint) {
|
||||
endpoint.disconnectMessageHandler((MqttDisconnectMessage disconnectMessage) -> {
|
||||
log.info("Received disconnect from client [{}], reason code = {}",
|
||||
endpoint.clientIdentifier(), disconnectMessage.code());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端订阅 (SUBSCRIBE)
|
||||
*/
|
||||
private void handleSubscribe(MqttEndpoint endpoint) {
|
||||
endpoint.subscribeHandler((MqttSubscribeMessage subscribe) -> {
|
||||
List<MqttSubAckReasonCode> reasonCodes = new ArrayList<>();
|
||||
for (MqttTopicSubscription s : subscribe.topicSubscriptions()) {
|
||||
log.info("Subscription for {} with QoS {}", s.topicName(), s.qualityOfService());
|
||||
// 将客户端请求的 QoS 转换为返回给客户端的 reason code(可能是错误码或实际 granted QoS)
|
||||
reasonCodes.add(MqttSubAckReasonCode.qosGranted(s.qualityOfService()));
|
||||
}
|
||||
// 回复 SUBACK,MQTT 5.0 时可指定 reasonCodes、properties
|
||||
endpoint.subscribeAcknowledge(subscribe.messageId(), reasonCodes, MqttProperties.NO_PROPERTIES);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端取消订阅 (UNSUBSCRIBE)
|
||||
*/
|
||||
private void handleUnsubscribe(MqttEndpoint endpoint) {
|
||||
endpoint.unsubscribeHandler((MqttUnsubscribeMessage unsubscribe) -> {
|
||||
for (String topic : unsubscribe.topics()) {
|
||||
log.info("Unsubscription for {}", topic);
|
||||
}
|
||||
// 回复 UNSUBACK,MQTT 5.0 时可指定 reasonCodes、properties
|
||||
endpoint.unsubscribeAcknowledge(unsubscribe.messageId());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端发布的消息 (PUBLISH)
|
||||
*/
|
||||
private void handlePublish(MqttEndpoint endpoint) {
|
||||
// 接收 PUBLISH 消息
|
||||
endpoint.publishHandler((MqttPublishMessage message) -> {
|
||||
String payload = message.payload().toString(Charset.defaultCharset());
|
||||
log.info("Received message [{}] on topic [{}] with QoS [{}]",
|
||||
payload, message.topicName(), message.qosLevel());
|
||||
|
||||
// 根据不同 QoS,回复客户端
|
||||
if (message.qosLevel() == MqttQoS.AT_LEAST_ONCE) {
|
||||
endpoint.publishAcknowledge(message.messageId());
|
||||
} else if (message.qosLevel() == MqttQoS.EXACTLY_ONCE) {
|
||||
endpoint.publishReceived(message.messageId());
|
||||
}
|
||||
});
|
||||
|
||||
// 如果 QoS = 2,需要处理 PUBREL
|
||||
endpoint.publishReleaseHandler(messageId -> {
|
||||
endpoint.publishComplete(messageId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客户端 PINGREQ
|
||||
*/
|
||||
private void handlePing(MqttEndpoint endpoint) {
|
||||
endpoint.pingHandler(v -> {
|
||||
// 这里仅做日志, PINGRESP 已自动发送
|
||||
log.info("Ping received from client [{}]", endpoint.clientIdentifier());
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 如果需要服务端向客户端发布消息,可用以下示例 ====================
|
||||
|
||||
/**
|
||||
* 服务端主动向已连接的某个 endpoint 发布消息的示例
|
||||
* 如果使用 MQTT 5.0,可以传递更多消息属性
|
||||
*/
|
||||
public void publishToClient(MqttEndpoint endpoint, String topic, String content) {
|
||||
endpoint.publish(topic,
|
||||
Buffer.buffer(content),
|
||||
MqttQoS.AT_LEAST_ONCE, // QoS 自行选择
|
||||
false,
|
||||
false);
|
||||
|
||||
// 处理 QoS 1 和 QoS 2 的 ACK
|
||||
endpoint.publishAcknowledgeHandler(messageId -> {
|
||||
log.info("Received PUBACK from client [{}] for messageId = {}", endpoint.clientIdentifier(), messageId);
|
||||
}).publishReceivedHandler(messageId -> {
|
||||
endpoint.publishRelease(messageId);
|
||||
}).publishCompletionHandler(messageId -> {
|
||||
log.info("Received PUBCOMP from client [{}] for messageId = {}", endpoint.clientIdentifier(), messageId);
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 如果需要多实例部署,用于多核扩展,可参考以下思路 ====================
|
||||
// 例如,在宿主应用或插件中循环启动多个 MqttServerExtension 实例,或使用 Vert.x 的 deployVerticle:
|
||||
// DeploymentOptions options = new DeploymentOptions().setInstances(10);
|
||||
// vertx.deployVerticle(() -> new MyMqttVerticle(), options);
|
||||
|
||||
}
|
||||
27
zt-module-iot/zt-module-iot-plugins/pom.xml
Normal file
27
zt-module-iot/zt-module-iot-plugins/pom.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>zt-module-iot</artifactId>
|
||||
<groupId>com.zt.plat</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modules>
|
||||
<module>zt-module-iot-plugin-common</module>
|
||||
<module>zt-module-iot-plugin-http</module>
|
||||
<module>zt-module-iot-plugin-mqtt</module>
|
||||
<module>zt-module-iot-plugin-emqx</module>
|
||||
</modules>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>zt-module-iot-plugins</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
<description>
|
||||
物联网 插件 模块
|
||||
</description>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user