Merge branch 'feature/iot' of https://gitee.com/zhijiantianya/ruoyi-vue-pro into feature/iot
This commit is contained in:
@@ -1,22 +1,20 @@
|
||||
package cn.iocoder.yudao.module.iot.framework.plugin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum;
|
||||
import cn.iocoder.yudao.module.iot.service.plugin.PluginInfoService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.spring.SpringPluginManager;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.service.plugin.PluginInfoService;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
// TODO @芋艿:需要 review 下
|
||||
@Component
|
||||
@Slf4j
|
||||
public class PluginStart implements ApplicationRunner {
|
||||
|
||||
@@ -1,42 +1,35 @@
|
||||
package cn.iocoder.yudao.module.iot.framework.plugin;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.api.ServiceRegistry;
|
||||
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
|
||||
import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.spring.SpringPluginManager;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
// TODO @芋艿:需要 review 下
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class UnifiedConfiguration {
|
||||
|
||||
private static final String SERVICE_REGISTRY_INITIALIZED_MARKER = "serviceRegistryInitializedMarker";
|
||||
|
||||
@Resource
|
||||
private DeviceDataApi deviceDataApi;
|
||||
|
||||
@Bean(SERVICE_REGISTRY_INITIALIZED_MARKER)
|
||||
public Object serviceRegistryInitializedMarker() {
|
||||
ServiceRegistry.registerService(DeviceDataApi.class, deviceDataApi);
|
||||
log.info("[init][将 DeviceDataApi 实例注册到 ServiceRegistry 中]");
|
||||
return new Object();
|
||||
}
|
||||
@Value("${pf4j.pluginsDir:pluginsDir}")
|
||||
private String pluginsDir;
|
||||
|
||||
@Bean
|
||||
@DependsOn(SERVICE_REGISTRY_INITIALIZED_MARKER)
|
||||
// @DependsOn("deviceDataApiImpl")
|
||||
public SpringPluginManager pluginManager() {
|
||||
log.info("[init][实例化 SpringPluginManager]");
|
||||
SpringPluginManager springPluginManager = new SpringPluginManager() {
|
||||
SpringPluginManager springPluginManager = new SpringPluginManager(Paths.get(pluginsDir)) {
|
||||
// SpringPluginManager springPluginManager = new SpringPluginManager() {
|
||||
|
||||
@Override
|
||||
public void startPlugins() {
|
||||
// 禁用插件启动,避免插件启动时,启动所有插件
|
||||
log.info("[init][禁用默认启动所有插件]");
|
||||
}
|
||||
|
||||
};
|
||||
springPluginManager.addPluginStateListener(new CustomPluginStateListener());
|
||||
return springPluginManager;
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.pf4j.PluginStateEvent;
|
||||
import org.pf4j.PluginStateListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
// TODO @芋艿:需要 review 下
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CustomPluginStateListener implements PluginStateListener {
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
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;
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
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;
|
||||
|
||||
// TODO @芋艿:server 逻辑,再瞅瞅;
|
||||
// TODO @haohao:如果只写在 iot biz 里,那么后续 server => client 貌似不方便?微信再讨论下~;
|
||||
@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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user