通讯组件加载和上传功能

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; 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.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@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.comp.mqtt.MqttComponent;
import cc.iotkit.comps.ComponentManager; import cc.iotkit.comps.ComponentManager;
import cc.iotkit.converter.ScriptConverter; import cc.iotkit.converter.ScriptConverter;
import cc.iotkit.dao.ProtocolGatewayRepository; import cc.iotkit.dao.ProtocolComponentRepository;
import cc.iotkit.dao.UserInfoRepository; import cc.iotkit.dao.UserInfoRepository;
import cc.iotkit.manager.service.DataOwnerService; import cc.iotkit.manager.service.DataOwnerService;
import cc.iotkit.manager.utils.AuthUtil; import cc.iotkit.manager.utils.AuthUtil;
import cc.iotkit.model.Paging; import cc.iotkit.model.Paging;
import cc.iotkit.model.UserInfo; 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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired; 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.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
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.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Optional; import java.util.Optional;
@Slf4j @Slf4j
@ -34,8 +42,11 @@ public class ProtocolController {
@Value("${gateway.function-jar}") @Value("${gateway.function-jar}")
private String functionJar; private String functionJar;
@Value("${spring.servlet.multipart.upload-dir}")
private String uploadDir;
@Autowired @Autowired
private ProtocolGatewayRepository gatewayRepository; private ProtocolComponentRepository protocolComponentRepository;
@Autowired @Autowired
private DataOwnerService dataOwnerService; private DataOwnerService dataOwnerService;
@ -46,11 +57,35 @@ public class ProtocolController {
@Autowired @Autowired
private ComponentManager componentManager; private ComponentManager componentManager;
@PostMapping("/addGateway") private Path fileStorageLocation;
public void addGateway(ProtocolGateway gateway) {
Optional<ProtocolGateway> optGateway = gatewayRepository.findById(gateway.getId()); @SneakyThrows
if (optGateway.isPresent()) { @PostConstruct
throw new BizException("gateway already exists"); 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 { try {
Optional<UserInfo> optUser = userInfoRepository.findById(AuthUtil.getUserId()); Optional<UserInfo> optUser = userInfoRepository.findById(AuthUtil.getUserId());
@ -58,72 +93,71 @@ public class ProtocolController {
throw new BizException("user does not exists"); throw new BizException("user does not exists");
} }
gateway.setScript("new (function () {this.decode = function (msg) {return null; };})().decode(msg)"); component.setScript("new (function () {this.decode = function (msg) {return null; };})().decode(msg)");
gateway.setCreateAt(System.currentTimeMillis()); component.setCreateAt(System.currentTimeMillis());
gateway.setUid(AuthUtil.getUserId()); component.setUid(AuthUtil.getUserId());
gateway.setUuid(optUser.get().getUid()); protocolComponentRepository.save(component);
gatewayRepository.save(gateway);
} catch (Throwable e) { } catch (Throwable e) {
throw new BizException("add protocol gateway error", e); throw new BizException("add protocol component error", e);
} }
} }
@PostMapping("/saveGateway") @PostMapping("/saveComponent")
public void saveGateway(ProtocolGateway gateway) { public void saveComponent(ProtocolComponent component) {
Optional<ProtocolGateway> optGateway = gatewayRepository.findById(gateway.getId()); Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(component.getId());
if (!optGateway.isPresent()) { if (!optComponent.isPresent()) {
throw new BizException("the gateway does not exists"); throw new BizException("the protocol component does not exists");
} }
Optional<UserInfo> optUser = userInfoRepository.findById(AuthUtil.getUserId()); Optional<UserInfo> optUser = userInfoRepository.findById(AuthUtil.getUserId());
if (!optUser.isPresent()) { if (!optUser.isPresent()) {
throw new BizException("user does not exists"); throw new BizException("user does not exists");
} }
ProtocolGateway oldGateway = optGateway.get(); ProtocolComponent oldComponent = optComponent.get();
gateway = ReflectUtil.copyNoNulls(gateway, oldGateway); component = ReflectUtil.copyNoNulls(component, oldComponent);
dataOwnerService.checkOwner(gateway); dataOwnerService.checkOwner(component);
try { try {
gatewayRepository.save(gateway); protocolComponentRepository.save(component);
} catch (Throwable e) { } catch (Throwable e) {
throw new BizException("add protocol gateway error", e); throw new BizException("add protocol component error", e);
} }
} }
@PostMapping("/saveGatewayScript") @PostMapping("/saveComponentScript")
public void saveGatewayScript(@RequestBody ProtocolGateway gateway) { public void saveComponentScript(@RequestBody ProtocolComponent component) {
Optional<ProtocolGateway> optGateway = gatewayRepository.findById(gateway.getId()); Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(component.getId());
if (!optGateway.isPresent()) { if (!optComponent.isPresent()) {
throw new BizException("the gateway does not exists"); throw new BizException("the component does not exists");
} }
dataOwnerService.checkOwner(gateway); dataOwnerService.checkOwner(component);
ProtocolGateway oldGateway = optGateway.get(); ProtocolComponent oldComponent = optComponent.get();
oldGateway.setScript(gateway.getScript()); oldComponent.setScript(component.getScript());
try { try {
// gatewayService.saveFunction(oldGateway.getUuid(), oldGateway.getId(), // gatewayService.saveFunction(oldGateway.getUuid(), oldGateway.getId(),
// "new (function (){" + oldGateway.getScript() + "})()", functionJar); // "new (function (){" + oldGateway.getScript() + "})()", functionJar);
gatewayRepository.save(oldGateway); protocolComponentRepository.save(oldComponent);
} catch (Throwable e) { } catch (Throwable e) {
throw new BizException("save protocol gateway script error", e); throw new BizException("save protocol component script error", e);
} }
} }
@PostMapping("/deleteGateway/{id}") @PostMapping("/deleteComponent/{id}")
public void deleteGateway(@PathVariable("id") String id) { public void deleteComponent(@PathVariable("id") String id) {
dataOwnerService.checkOwner(gatewayRepository, id); dataOwnerService.checkOwner(protocolComponentRepository, id);
try { try {
gatewayRepository.deleteById(id); protocolComponentRepository.deleteById(id);
} catch (Throwable e) { } catch (Throwable e) {
throw new BizException("delete protocol gateway error", e); throw new BizException("delete protocol component error", e);
} }
} }
@PostMapping("/gateways/{size}/{page}") @PostMapping("/components/{size}/{page}")
public Paging<ProtocolGateway> getGateways( public Paging<ProtocolComponent> getComponents(
@PathVariable("size") int size, @PathVariable("size") int size,
@PathVariable("page") int page) { @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")))); 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") @GetMapping("/registerMqtt")

View File

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

View File

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

View File

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

View File

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