【功能新增】IoT:增加插件支持,包含插件API和示例控制器

This commit is contained in:
安浩浩
2024-12-16 18:18:53 +08:00
parent 92c2717d46
commit 290fcd94d5
18 changed files with 637 additions and 92 deletions

View File

@@ -0,0 +1,5 @@
plugin.id=iot-plugin-hppt
plugin.class=cn.iocoder.yudao.module.iot.plugin.WelcomePlugin
plugin.version=0.0.1
plugin.provider=ahh
plugin.dependencies=

View File

@@ -1,31 +1,149 @@
<?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>yudao-module-iot</artifactId>
<groupId>cn.iocoder.boot</groupId>
<version>${revision}</version>
</parent>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-iot-plugin</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>
物联网 模块 - 插件
</description>
<description>物联网 模块 - 插件</description>
<properties>
<!-- 插件相关 -->
<plugin.id>iot-plugin-http</plugin.id>
<plugin.class>cn.iocoder.yudao.module.iot.plugin.WelcomePlugin</plugin.class>
<plugin.version>0.0.1</plugin.version>
<plugin.provider>ahh</plugin.provider>
<plugin.dependencies/>
<!-- Maven 相关 -->
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven-antrun-plugin.version>1.6</maven-antrun-plugin.version>
<maven-assembly-plugin.version>2.3</maven-assembly-plugin.version>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
<pf4j-spring.version>0.9.0</pf4j-spring.version>
<!-- 看看咋放到 bom 里 -->
<spring.boot.version>3.3.1</spring.boot.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 其他依赖项 -->
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-common</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
<!-- PF4J Spring 集成 -->
<dependency>
<groupId>org.pf4j</groupId>
<artifactId>pf4j-spring</artifactId>
<version>${pf4j-spring.version}</version>
<scope>provided</scope>
</dependency>
<!-- 项目依赖 -->
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-iot-plugin-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
<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>${maven-antrun-plugin.version}</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>${maven-assembly-plugin.version}</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>${maven-jar-plugin.version}</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-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,37 @@
<!--
Describes the plugin archive
@author Decebal Suiu
@version 1.0
-->
<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>

View File

@@ -0,0 +1,22 @@
package cn.iocoder.yudao.module.iot.plugin;
import org.pf4j.Extension;
import org.pf4j.ExtensionPoint;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/iot/plugin-demo")
@Extension
public class IoTHttpPluginController implements ExtensionPoint {
@GetMapping("/greet")
public String greet() {
return "Hello from MyPlugin!";
}
@PostMapping("/message")
public void receiveMessage(@RequestBody String message) {
System.out.println("Received message: " + message);
}
}

View File

@@ -0,0 +1,33 @@
package cn.iocoder.yudao.module.iot.plugin;
import org.pf4j.PluginWrapper;
import org.pf4j.spring.SpringPlugin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class IoTPlugin extends SpringPlugin {
public IoTPlugin(PluginWrapper wrapper) {
super(wrapper);
}
@Override
public void start() {
System.out.println("IoTPlugin 启动");
}
@Override
public void stop() {
System.out.println("IoTPlugin 停止");
super.stop(); // to close applicationContext
}
@Override
protected ApplicationContext createApplicationContext() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.setClassLoader(getWrapper().getPluginClassLoader());
applicationContext.register(IoTHttpPluginController.class); // 注册 IoTPluginConfig
applicationContext.refresh();
System.out.println("IoTPlugin 加载完成");
return applicationContext;
}
}

View File

@@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.iot.plugin;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.context.annotation.Bean;
@Configuration
public class IoTPluginConfig {
@Bean
public IoTHttpPluginController ioTHttpPluginController() {
return new IoTHttpPluginController();
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.iocoder.yudao.module.iot.plugin;
import cn.iocoder.yudao.module.iot.api.Greeting;
import org.apache.commons.lang.StringUtils;
import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;
import org.pf4j.RuntimeMode;
/**
* 打招呼 测试用例
*/
public class WelcomePlugin extends Plugin {
public WelcomePlugin(PluginWrapper wrapper) {
super(wrapper);
}
@Override
public void start() {
System.out.println("WelcomePlugin.start()");
// for testing the development mode
if (RuntimeMode.DEVELOPMENT.equals(wrapper.getRuntimeMode())) {
System.out.println(StringUtils.upperCase("WelcomePlugin"));
}
}
@Override
public void stop() {
System.out.println("WelcomePlugin.stop()");
}
@Extension
public static class WelcomeGreeting implements Greeting {
@Override
public String getGreeting() {
return "Welcome to PF4J";
}
}
}

View File

@@ -1,6 +0,0 @@
/**
* 占位
*
* TODO 芋艿:后续删除
*/
package cn.iocoder.yudao.module.iot.plugin;