【功能完善】IoT: 新增 MQTT RPC 支持,包含请求和响应模型、序列化工具、MQTT 配置及客户端/服务器实现,提供示例服务和控制器接口,优化插件结构以支持 HTTP 插件的集成。
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package cn.iocoder.yudao.module.iot.mqttrpc.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "mqtt")
|
||||
public class MqttConfig {
|
||||
/**
|
||||
* MQTT 代理地址
|
||||
*/
|
||||
private String broker;
|
||||
|
||||
/**
|
||||
* MQTT 用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* MQTT 密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* MQTT 客户端 ID
|
||||
*/
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* MQTT 请求主题
|
||||
*/
|
||||
private String requestTopic;
|
||||
|
||||
/**
|
||||
* MQTT 响应主题前缀
|
||||
*/
|
||||
private String responseTopicPrefix;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package cn.iocoder.yudao.module.iot.mqttrpc.server;
|
||||
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.common.RpcRequest;
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.common.RpcResponse;
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.common.SerializationUtils;
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.config.MqttConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class RpcServer {
|
||||
|
||||
private final MqttConfig mqttConfig;
|
||||
private final MqttClient mqttClient;
|
||||
private final Map<String, MethodInvoker> methodRegistry = new HashMap<>();
|
||||
|
||||
public RpcServer(MqttConfig mqttConfig) throws MqttException {
|
||||
this.mqttConfig = mqttConfig;
|
||||
this.mqttClient = new MqttClient(mqttConfig.getBroker(), "rpc-server-" + UUID.randomUUID(), new MemoryPersistence());
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setAutomaticReconnect(true);
|
||||
options.setCleanSession(true);
|
||||
options.setUserName(mqttConfig.getUsername());
|
||||
options.setPassword(mqttConfig.getPassword().toCharArray());
|
||||
this.mqttClient.connect(options);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws MqttException {
|
||||
mqttClient.subscribe(mqttConfig.getRequestTopic(), this::handleRequest);
|
||||
log.info("RPC Server subscribed to topic: {}", mqttConfig.getRequestTopic());
|
||||
}
|
||||
|
||||
private void handleRequest(String topic, MqttMessage message) {
|
||||
RpcRequest request = SerializationUtils.deserialize(new String(message.getPayload()), RpcRequest.class);
|
||||
RpcResponse response = new RpcResponse();
|
||||
response.setCorrelationId(request.getCorrelationId());
|
||||
|
||||
try {
|
||||
MethodInvoker invoker = methodRegistry.get(request.getMethod());
|
||||
if (invoker == null) {
|
||||
throw new NoSuchMethodException("Unknown method: " + request.getMethod());
|
||||
}
|
||||
Object result = invoker.invoke(request.getParams());
|
||||
response.setResult(result);
|
||||
} catch (Exception e) {
|
||||
response.setError(e.getMessage());
|
||||
log.error("Error processing RPC request: {}", e.getMessage(), e);
|
||||
}
|
||||
|
||||
String replyPayload = SerializationUtils.serialize(response);
|
||||
MqttMessage replyMessage = new MqttMessage(replyPayload.getBytes());
|
||||
replyMessage.setQos(1);
|
||||
try {
|
||||
mqttClient.publish(request.getReplyTo(), replyMessage);
|
||||
log.info("Published response to {}", request.getReplyTo());
|
||||
} catch (MqttException e) {
|
||||
log.error("Failed to publish response: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册可调用的方法
|
||||
*
|
||||
* @param methodName 方法名称
|
||||
* @param invoker 方法调用器
|
||||
*/
|
||||
public void registerMethod(String methodName, MethodInvoker invoker) {
|
||||
methodRegistry.put(methodName, invoker);
|
||||
log.info("Registered method: {}", methodName);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void cleanup() throws MqttException {
|
||||
mqttClient.disconnect();
|
||||
log.info("RPC Server disconnected");
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法调用器接口
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface MethodInvoker {
|
||||
Object invoke(Object[] params) throws Exception;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cn.iocoder.yudao.module.iot.service.plugin;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.server.RpcServer;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ExampleService {
|
||||
|
||||
private final RpcServer rpcServer;
|
||||
|
||||
@PostConstruct
|
||||
public void registerMethods() {
|
||||
rpcServer.registerMethod("add", params -> {
|
||||
if (params.length != 2) {
|
||||
throw new IllegalArgumentException("add方法需要两个参数");
|
||||
}
|
||||
int a = ((Number) params[0]).intValue();
|
||||
int b = ((Number) params[1]).intValue();
|
||||
return add(a, b);
|
||||
});
|
||||
|
||||
rpcServer.registerMethod("concat", params -> {
|
||||
if (params.length != 2) {
|
||||
throw new IllegalArgumentException("concat方法需要两个参数");
|
||||
}
|
||||
String str1 = params[0].toString();
|
||||
String str2 = params[1].toString();
|
||||
return concat(str1, str2);
|
||||
});
|
||||
}
|
||||
|
||||
private int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
private String concat(String a, String b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
|
||||
Reference in New Issue
Block a user