【功能完善】IoT: 添加 MQTT 插件支持,重构插件管理,新增获取运行状态插件信息接口,优化插件信息存储逻辑,移除不必要的 Spring 注解。
This commit is contained in:
parent
24a660b5c2
commit
dc1f9338f1
|
@ -1,2 +1 @@
|
||||||
http-plugin
|
http-plugin
|
||||||
http-plugin@0.0.1
|
|
||||||
|
|
|
@ -4,6 +4,9 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import cn.iocoder.yudao.module.iot.controller.admin.plugin.vo.*;
|
import cn.iocoder.yudao.module.iot.controller.admin.plugin.vo.*;
|
||||||
|
|
||||||
|
@ -22,4 +25,10 @@ public interface PluginInfoMapper extends BaseMapperX<PluginInfoDO> {
|
||||||
.orderByDesc(PluginInfoDO::getId));
|
.orderByDesc(PluginInfoDO::getId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default List<PluginInfoDO> selectListByStatus(Integer status) {
|
||||||
|
return selectList(new LambdaQueryWrapperX<PluginInfoDO>()
|
||||||
|
.eq(PluginInfoDO::getStatus, status)
|
||||||
|
.orderByAsc(PluginInfoDO::getId));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -17,7 +17,7 @@ import org.springframework.stereotype.Component;
|
||||||
* @author ahh
|
* @author ahh
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
//@Component
|
||||||
public class EmqxCallback implements MqttCallbackExtended {
|
public class EmqxCallback implements MqttCallbackExtended {
|
||||||
|
|
||||||
@Lazy
|
@Lazy
|
||||||
|
|
|
@ -19,7 +19,7 @@ import org.springframework.stereotype.Component;
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Data
|
@Data
|
||||||
@Component
|
//@Component
|
||||||
public class EmqxClient {
|
public class EmqxClient {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
|
|
@ -12,8 +12,8 @@ import org.springframework.stereotype.Component;
|
||||||
* @author ahh
|
* @author ahh
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Component
|
//@Component
|
||||||
@ConfigurationProperties("iot.emq")
|
//@ConfigurationProperties("iot.emq")
|
||||||
public class MqttConfig {
|
public class MqttConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -16,7 +16,7 @@ import org.springframework.stereotype.Service;
|
||||||
* @author ahh
|
* @author ahh
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
//@Service
|
||||||
public class EmqxServiceImpl implements EmqxService {
|
public class EmqxServiceImpl implements EmqxService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
|
|
@ -13,7 +13,7 @@ import org.springframework.stereotype.Component;
|
||||||
*
|
*
|
||||||
* @author ahh
|
* @author ahh
|
||||||
*/
|
*/
|
||||||
@Component
|
//@Component
|
||||||
public class EmqxStart implements ApplicationRunner {
|
public class EmqxStart implements ApplicationRunner {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
|
|
@ -76,4 +76,11 @@ public interface PluginInfoService {
|
||||||
* @return 插件信息列表
|
* @return 插件信息列表
|
||||||
*/
|
*/
|
||||||
List<PluginInfoDO> getPluginInfoList();
|
List<PluginInfoDO> getPluginInfoList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得运行状态的插件信息列表
|
||||||
|
*
|
||||||
|
* @return 运行状态的插件信息列表
|
||||||
|
*/
|
||||||
|
List<PluginInfoDO> getRunningPluginInfoList();
|
||||||
}
|
}
|
|
@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
|
@ -186,20 +187,19 @@ public class PluginInfoServiceImpl implements PluginInfoService {
|
||||||
if (pluginWrapper == null) {
|
if (pluginWrapper == null) {
|
||||||
throw exception(PLUGIN_INSTALL_FAILED);
|
throw exception(PLUGIN_INSTALL_FAILED);
|
||||||
}
|
}
|
||||||
String pluginInfo = pluginKeyNew + "@" + pluginWrapper.getDescriptor().getVersion();
|
|
||||||
List<String> targetLines = Files.exists(targetFilePath) ? Files.readAllLines(targetFilePath)
|
List<String> targetLines = Files.exists(targetFilePath) ? Files.readAllLines(targetFilePath)
|
||||||
: new ArrayList<>();
|
: new ArrayList<>();
|
||||||
List<String> oppositeLines = Files.exists(oppositeFilePath) ? Files.readAllLines(oppositeFilePath)
|
List<String> oppositeLines = Files.exists(oppositeFilePath) ? Files.readAllLines(oppositeFilePath)
|
||||||
: new ArrayList<>();
|
: new ArrayList<>();
|
||||||
|
|
||||||
if (!targetLines.contains(pluginInfo)) {
|
if (!targetLines.contains(pluginKeyNew)) {
|
||||||
targetLines.add(pluginInfo);
|
targetLines.add(pluginKeyNew);
|
||||||
Files.write(targetFilePath, targetLines, StandardOpenOption.CREATE,
|
Files.write(targetFilePath, targetLines, StandardOpenOption.CREATE,
|
||||||
StandardOpenOption.TRUNCATE_EXISTING);
|
StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oppositeLines.contains(pluginInfo)) {
|
if (oppositeLines.contains(pluginKeyNew)) {
|
||||||
oppositeLines.remove(pluginInfo);
|
oppositeLines.remove(pluginKeyNew);
|
||||||
Files.write(oppositeFilePath, oppositeLines, StandardOpenOption.CREATE,
|
Files.write(oppositeFilePath, oppositeLines, StandardOpenOption.CREATE,
|
||||||
StandardOpenOption.TRUNCATE_EXISTING);
|
StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
}
|
}
|
||||||
|
@ -267,4 +267,8 @@ public class PluginInfoServiceImpl implements PluginInfoService {
|
||||||
return pluginInfoMapper.selectList(null);
|
return pluginInfoMapper.selectList(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PluginInfoDO> getRunningPluginInfoList() {
|
||||||
|
return pluginInfoMapper.selectListByStatus(IotPluginStatusEnum.RUNNING.getStatus());
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -2,11 +2,6 @@
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
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">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<!-- <modelVersion>4.0.0</modelVersion>-->
|
|
||||||
<!-- <groupId>cn.iocoder.boot</groupId>-->
|
|
||||||
<!-- <artifactId>yudao-module-iot-plugin</artifactId>-->
|
|
||||||
<!-- <version>0.0.1</version>-->
|
|
||||||
<!-- <packaging>pom</packaging>-->
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>yudao-module-iot</artifactId>
|
<artifactId>yudao-module-iot</artifactId>
|
||||||
<groupId>cn.iocoder.boot</groupId>
|
<groupId>cn.iocoder.boot</groupId>
|
||||||
|
@ -15,6 +10,7 @@
|
||||||
<modules>
|
<modules>
|
||||||
<module>yudao-module-iot-demo-plugin</module>
|
<module>yudao-module-iot-demo-plugin</module>
|
||||||
<module>yudao-module-iot-http-plugin</module>
|
<module>yudao-module-iot-http-plugin</module>
|
||||||
|
<module>yudao-module-iot-mqtt-plugin</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
plugin.id=mqtt-plugin
|
||||||
|
plugin.class=cn.iocoder.yudao.module.iot.plugin.MqttPlugin
|
||||||
|
plugin.version=0.0.1
|
||||||
|
plugin.provider=ahh
|
||||||
|
plugin.dependencies=
|
||||||
|
plugin.description=mqtt-plugin-0.0.1
|
|
@ -0,0 +1,154 @@
|
||||||
|
<?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>yudao-module-iot-plugin</artifactId>
|
||||||
|
<groupId>cn.iocoder.boot</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<artifactId>yudao-module-iot-mqtt-plugin</artifactId>
|
||||||
|
|
||||||
|
<name>${project.artifactId}</name>
|
||||||
|
<description>
|
||||||
|
物联网 插件模块 - mqtt 插件
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- 插件相关 -->
|
||||||
|
<plugin.id>mqtt-plugin</plugin.id>
|
||||||
|
<plugin.class>cn.iocoder.yudao.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>cn.iocoder.boot</groupId>
|
||||||
|
<artifactId>yudao-module-iot-api</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.netty</groupId>
|
||||||
|
<artifactId>netty-all</artifactId>
|
||||||
|
<version>4.1.63.Final</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,48 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
|
||||||
|
import cn.iocoder.yudao.module.iot.api.ServiceRegistry;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.pf4j.PluginWrapper;
|
||||||
|
import org.pf4j.Plugin;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class MqttPlugin extends Plugin {
|
||||||
|
|
||||||
|
private ExecutorService executorService;
|
||||||
|
private DeviceDataApi deviceDataApi;
|
||||||
|
|
||||||
|
public MqttPlugin(PluginWrapper wrapper) {
|
||||||
|
super(wrapper);
|
||||||
|
// 初始化线程池
|
||||||
|
this.executorService = Executors.newSingleThreadExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() {
|
||||||
|
log.info("MqttPlugin.start()");
|
||||||
|
|
||||||
|
// 重新初始化线程池,确保它是活跃的
|
||||||
|
if (executorService.isShutdown() || executorService.isTerminated()) {
|
||||||
|
executorService = Executors.newSingleThreadExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从 ServiceRegistry 中获取主程序暴露的 DeviceDataApi 接口实例
|
||||||
|
deviceDataApi = ServiceRegistry.getService(DeviceDataApi.class);
|
||||||
|
if (deviceDataApi == null) {
|
||||||
|
log.error("未能从 ServiceRegistry 获取 DeviceDataApi 实例,请确保主程序已正确注册!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
log.info("MqttPlugin.stop()");
|
||||||
|
// 停止线程池
|
||||||
|
executorService.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue