通讯组件加载和上传功能

V0.5.x
xiwa 2022-03-29 05:12:40 +08:00
parent 758f6e12f8
commit 2d74d0fb91
19 changed files with 293 additions and 52 deletions

View File

@ -0,0 +1,9 @@
package cc.iotkit.dao;
import cc.iotkit.model.protocol.ProtocolComponent;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProtocolComponentRepository extends MongoRepository<ProtocolComponent, String> {
}

View File

@ -1,11 +1,11 @@
package cc.iotkit.dao;
import cc.iotkit.model.protocol.ProtocolGateway;
import cc.iotkit.model.protocol.ProtocolComponent;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProtocolGatewayRepository extends MongoRepository<ProtocolGateway, String> {
public interface ProtocolGatewayRepository extends MongoRepository<ProtocolComponent, String> {
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

View File

@ -0,0 +1,76 @@
package cc.iotkit.manager.config;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.InternalSettingsPreparer;
import org.elasticsearch.node.Node;
import org.elasticsearch.transport.Netty4Plugin;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.Collections;
@Slf4j
@Configuration
public class ElasticSearchConfig {
static {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
@SneakyThrows
@Bean
public EmbeddedElasticSearch getEmbeddedElasticSearch(ConfigProperty configProperty) {
if (configProperty.enabled) {
EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(configProperty);
embeddedElasticSearch.start();
return embeddedElasticSearch;
}
return null;
}
@Component
@ConfigurationProperties(prefix = "elasticsearch.embedded")
public static class ConfigProperty {
private boolean enabled;
private String dataPath = "./data/elasticsearch";
private String homePath = "./";
private int port = 9200;
private String host = "0.0.0.0";
public Settings.Builder applySetting(Settings.Builder settings) {
return settings.put("network.host", host)
.put("http.port", port)
.put("path.data", dataPath)
.put("path.home", homePath);
}
}
public static class EmbeddedElasticSearch extends Node {
@SneakyThrows
public EmbeddedElasticSearch(ConfigProperty properties) {
super(InternalSettingsPreparer.prepareEnvironment(
properties.applySetting(
Settings.builder()
.put("node.name", "test")
.put("discovery.type", "single-node")
.put("transport.type", Netty4Plugin.NETTY_TRANSPORT_NAME)
.put("http.type", Netty4Plugin.NETTY_HTTP_TRANSPORT_NAME)
.put("network.host", "0.0.0.0")
.put("http.port", 9200)
).build(), Collections.emptyMap(), null, () -> "default"),
Collections.singleton(Netty4Plugin.class), false);
}
}
}

View File

@ -6,13 +6,14 @@ import cc.iotkit.comp.CompConfig;
import cc.iotkit.comp.mqtt.MqttComponent;
import cc.iotkit.comps.ComponentManager;
import cc.iotkit.converter.ScriptConverter;
import cc.iotkit.dao.ProtocolGatewayRepository;
import cc.iotkit.dao.ProtocolComponentRepository;
import cc.iotkit.dao.UserInfoRepository;
import cc.iotkit.manager.service.DataOwnerService;
import cc.iotkit.manager.utils.AuthUtil;
import cc.iotkit.model.Paging;
import cc.iotkit.model.UserInfo;
import cc.iotkit.model.protocol.ProtocolGateway;
import cc.iotkit.model.protocol.ProtocolComponent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -20,10 +21,17 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Optional;
@Slf4j
@ -34,8 +42,11 @@ public class ProtocolController {
@Value("${gateway.function-jar}")
private String functionJar;
@Value("${spring.servlet.multipart.upload-dir}")
private String uploadDir;
@Autowired
private ProtocolGatewayRepository gatewayRepository;
private ProtocolComponentRepository protocolComponentRepository;
@Autowired
private DataOwnerService dataOwnerService;
@ -46,11 +57,35 @@ public class ProtocolController {
@Autowired
private ComponentManager componentManager;
@PostMapping("/addGateway")
public void addGateway(ProtocolGateway gateway) {
Optional<ProtocolGateway> optGateway = gatewayRepository.findById(gateway.getId());
if (optGateway.isPresent()) {
throw new BizException("gateway already exists");
private Path fileStorageLocation;
@SneakyThrows
@PostConstruct
public void init() {
this.fileStorageLocation = Paths.get(uploadDir).toAbsolutePath().normalize();
Files.createDirectories(this.fileStorageLocation);
}
@PostMapping("/uploadJar")
public void uploadJar(@RequestParam("file") MultipartFile file) {
if (file == null) {
throw new BizException("file is null");
}
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
try {
Path targetLocation = this.fileStorageLocation.resolve(fileName);
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
throw new BizException("upload jar error", ex);
}
}
@PostMapping("/addComponent")
public void addComponent(ProtocolComponent component) {
Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(component.getId());
if (optComponent.isPresent()) {
throw new BizException("component already exists");
}
try {
Optional<UserInfo> optUser = userInfoRepository.findById(AuthUtil.getUserId());
@ -58,72 +93,71 @@ public class ProtocolController {
throw new BizException("user does not exists");
}
gateway.setScript("new (function () {this.decode = function (msg) {return null; };})().decode(msg)");
gateway.setCreateAt(System.currentTimeMillis());
gateway.setUid(AuthUtil.getUserId());
gateway.setUuid(optUser.get().getUid());
gatewayRepository.save(gateway);
component.setScript("new (function () {this.decode = function (msg) {return null; };})().decode(msg)");
component.setCreateAt(System.currentTimeMillis());
component.setUid(AuthUtil.getUserId());
protocolComponentRepository.save(component);
} catch (Throwable e) {
throw new BizException("add protocol gateway error", e);
throw new BizException("add protocol component error", e);
}
}
@PostMapping("/saveGateway")
public void saveGateway(ProtocolGateway gateway) {
Optional<ProtocolGateway> optGateway = gatewayRepository.findById(gateway.getId());
if (!optGateway.isPresent()) {
throw new BizException("the gateway does not exists");
@PostMapping("/saveComponent")
public void saveComponent(ProtocolComponent component) {
Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(component.getId());
if (!optComponent.isPresent()) {
throw new BizException("the protocol component does not exists");
}
Optional<UserInfo> optUser = userInfoRepository.findById(AuthUtil.getUserId());
if (!optUser.isPresent()) {
throw new BizException("user does not exists");
}
ProtocolGateway oldGateway = optGateway.get();
gateway = ReflectUtil.copyNoNulls(gateway, oldGateway);
dataOwnerService.checkOwner(gateway);
ProtocolComponent oldComponent = optComponent.get();
component = ReflectUtil.copyNoNulls(component, oldComponent);
dataOwnerService.checkOwner(component);
try {
gatewayRepository.save(gateway);
protocolComponentRepository.save(component);
} catch (Throwable e) {
throw new BizException("add protocol gateway error", e);
throw new BizException("add protocol component error", e);
}
}
@PostMapping("/saveGatewayScript")
public void saveGatewayScript(@RequestBody ProtocolGateway gateway) {
Optional<ProtocolGateway> optGateway = gatewayRepository.findById(gateway.getId());
if (!optGateway.isPresent()) {
throw new BizException("the gateway does not exists");
@PostMapping("/saveComponentScript")
public void saveComponentScript(@RequestBody ProtocolComponent component) {
Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(component.getId());
if (!optComponent.isPresent()) {
throw new BizException("the component does not exists");
}
dataOwnerService.checkOwner(gateway);
ProtocolGateway oldGateway = optGateway.get();
oldGateway.setScript(gateway.getScript());
dataOwnerService.checkOwner(component);
ProtocolComponent oldComponent = optComponent.get();
oldComponent.setScript(component.getScript());
try {
// gatewayService.saveFunction(oldGateway.getUuid(), oldGateway.getId(),
// "new (function (){" + oldGateway.getScript() + "})()", functionJar);
gatewayRepository.save(oldGateway);
protocolComponentRepository.save(oldComponent);
} catch (Throwable e) {
throw new BizException("save protocol gateway script error", e);
throw new BizException("save protocol component script error", e);
}
}
@PostMapping("/deleteGateway/{id}")
public void deleteGateway(@PathVariable("id") String id) {
dataOwnerService.checkOwner(gatewayRepository, id);
@PostMapping("/deleteComponent/{id}")
public void deleteComponent(@PathVariable("id") String id) {
dataOwnerService.checkOwner(protocolComponentRepository, id);
try {
gatewayRepository.deleteById(id);
protocolComponentRepository.deleteById(id);
} catch (Throwable e) {
throw new BizException("delete protocol gateway error", e);
throw new BizException("delete protocol component error", e);
}
}
@PostMapping("/gateways/{size}/{page}")
public Paging<ProtocolGateway> getGateways(
@PostMapping("/components/{size}/{page}")
public Paging<ProtocolComponent> getComponents(
@PathVariable("size") int size,
@PathVariable("page") int page) {
Page<ProtocolGateway> gateways = gatewayRepository.findAll(
Page<ProtocolComponent> components = protocolComponentRepository.findAll(
PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("createAt"))));
return new Paging<>(gateways.getTotalElements(), gateways.getContent());
return new Paging<>(components.getTotalElements(), components.getContent());
}
@GetMapping("/registerMqtt")

View File

@ -1,4 +1,11 @@
spring:
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 12MB
upload-dir: ./component_jar
data:
mongodb:
uri: mongodb://填写mongodb地址/admin

View File

@ -1,4 +1,11 @@
spring:
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 12MB
upload-dir: ./component_jar
data:
mongodb:
uri: mongodb://填写mongodb地址/admin

View File

@ -3,9 +3,11 @@ package cc.iotkit.model.protocol;
import cc.iotkit.model.Owned;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
public class ProtocolGateway implements Owned {
@Document
public class ProtocolComponent implements Owned {
@Id
private String id;
@ -15,15 +17,12 @@ public class ProtocolGateway implements Owned {
*/
private String uid;
/**
* ID
*/
private String uuid;
private String name;
private String protocol;
private String jarFile;
private String config;
private String script;

View File

@ -139,7 +139,7 @@
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.1.0</version>
<version>7.17</version>
</dependency>
<dependency>

View File

@ -0,0 +1,30 @@
package cc.iotkit.comps;
import cc.iotkit.comp.CompConfig;
import cc.iotkit.comp.IComponent;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class ComponentClassLoader {
protected Class<IComponent> findClass(String name) throws ClassNotFoundException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return (Class<IComponent>) classLoader.loadClass("cc.iotkit.comp.mqtt.MqttComponent");
}
public void addUrl(File jarPath) throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException, MalformedURLException {
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
if (!method.isAccessible()) {
method.setAccessible(true);
}
URL url = jarPath.toURI().toURL();
method.invoke(classLoader, url);
}
}

0
protocol-gateway/mqtt-component/.DS_Store vendored Normal file → Executable file
View File

View File

@ -0,0 +1,79 @@
<?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/maven-v4_0_0.xsd">
<parent>
<artifactId>protocol-gateway</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mqtt-component</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactSet>
<includes>
<include>io.vertx:vertx-core</include>
<include>io.vertx:vertx-mqtt</include>
</includes>
</artifactSet>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>4.2.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mqtt</artifactId>
<version>4.2.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>component</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>