【功能新增】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,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;