diff --git a/dao/src/main/java/cc/iotkit/dao/ProtocolComponentRepository.java b/dao/src/main/java/cc/iotkit/dao/ProtocolComponentRepository.java new file mode 100755 index 00000000..68d51cbb --- /dev/null +++ b/dao/src/main/java/cc/iotkit/dao/ProtocolComponentRepository.java @@ -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 { +} diff --git a/dao/src/main/java/cc/iotkit/dao/ProtocolGatewayRepository.java b/dao/src/main/java/cc/iotkit/dao/ProtocolGatewayRepository.java index d304423c..8248a283 100755 --- a/dao/src/main/java/cc/iotkit/dao/ProtocolGatewayRepository.java +++ b/dao/src/main/java/cc/iotkit/dao/ProtocolGatewayRepository.java @@ -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 { +public interface ProtocolGatewayRepository extends MongoRepository { } diff --git a/data/elasticsearch/nodes/0/_state/_3.cfe b/data/elasticsearch/nodes/0/_state/_3.cfe new file mode 100644 index 00000000..cf99088b Binary files /dev/null and b/data/elasticsearch/nodes/0/_state/_3.cfe differ diff --git a/data/elasticsearch/nodes/0/_state/_3.cfs b/data/elasticsearch/nodes/0/_state/_3.cfs new file mode 100644 index 00000000..4d78d246 Binary files /dev/null and b/data/elasticsearch/nodes/0/_state/_3.cfs differ diff --git a/data/elasticsearch/nodes/0/_state/_3.si b/data/elasticsearch/nodes/0/_state/_3.si new file mode 100644 index 00000000..5df35ca1 Binary files /dev/null and b/data/elasticsearch/nodes/0/_state/_3.si differ diff --git a/data/elasticsearch/nodes/0/_state/manifest-0.st b/data/elasticsearch/nodes/0/_state/manifest-0.st new file mode 100644 index 00000000..030f9cbe Binary files /dev/null and b/data/elasticsearch/nodes/0/_state/manifest-0.st differ diff --git a/data/elasticsearch/nodes/0/_state/node-0.st b/data/elasticsearch/nodes/0/_state/node-0.st new file mode 100644 index 00000000..a819c66f Binary files /dev/null and b/data/elasticsearch/nodes/0/_state/node-0.st differ diff --git a/data/elasticsearch/nodes/0/_state/segments_5 b/data/elasticsearch/nodes/0/_state/segments_5 new file mode 100644 index 00000000..ef389e57 Binary files /dev/null and b/data/elasticsearch/nodes/0/_state/segments_5 differ diff --git a/data/elasticsearch/nodes/0/_state/write.lock b/data/elasticsearch/nodes/0/_state/write.lock new file mode 100644 index 00000000..e69de29b diff --git a/data/elasticsearch/nodes/0/node.lock b/data/elasticsearch/nodes/0/node.lock new file mode 100644 index 00000000..e69de29b diff --git a/manager/src/main/java/cc/iotkit/manager/config/ElasticSearchConfig.java b/manager/src/main/java/cc/iotkit/manager/config/ElasticSearchConfig.java new file mode 100644 index 00000000..e67249e1 --- /dev/null +++ b/manager/src/main/java/cc/iotkit/manager/config/ElasticSearchConfig.java @@ -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); + } + } +} diff --git a/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java b/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java index 03a02fdc..13cf7f48 100755 --- a/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java +++ b/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java @@ -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 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 optComponent = protocolComponentRepository.findById(component.getId()); + if (optComponent.isPresent()) { + throw new BizException("component already exists"); } try { Optional 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 optGateway = gatewayRepository.findById(gateway.getId()); - if (!optGateway.isPresent()) { - throw new BizException("the gateway does not exists"); + @PostMapping("/saveComponent") + public void saveComponent(ProtocolComponent component) { + Optional optComponent = protocolComponentRepository.findById(component.getId()); + if (!optComponent.isPresent()) { + throw new BizException("the protocol component does not exists"); } Optional 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 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 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 getGateways( + @PostMapping("/components/{size}/{page}") + public Paging getComponents( @PathVariable("size") int size, @PathVariable("page") int page) { - Page gateways = gatewayRepository.findAll( + Page 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") diff --git a/manager/src/main/resources/application-dev.yml b/manager/src/main/resources/application-dev.yml index 2a9c0ec1..ebc395c7 100755 --- a/manager/src/main/resources/application-dev.yml +++ b/manager/src/main/resources/application-dev.yml @@ -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 diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index 8973fa15..1b6731d8 100755 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -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 diff --git a/model/src/main/java/cc/iotkit/model/protocol/ProtocolGateway.java b/model/src/main/java/cc/iotkit/model/protocol/ProtocolComponent.java similarity index 71% rename from model/src/main/java/cc/iotkit/model/protocol/ProtocolGateway.java rename to model/src/main/java/cc/iotkit/model/protocol/ProtocolComponent.java index 38752ddf..e80098a1 100755 --- a/model/src/main/java/cc/iotkit/model/protocol/ProtocolGateway.java +++ b/model/src/main/java/cc/iotkit/model/protocol/ProtocolComponent.java @@ -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; diff --git a/pom.xml b/pom.xml index b9873bf3..d150113b 100755 --- a/pom.xml +++ b/pom.xml @@ -139,7 +139,7 @@ co.elastic.clients elasticsearch-java - 8.1.0 + 7.17 diff --git a/protocol-gateway/component-server/src/main/java/cc/iotkit/comps/ComponentClassLoader.java b/protocol-gateway/component-server/src/main/java/cc/iotkit/comps/ComponentClassLoader.java new file mode 100644 index 00000000..0f8cbc02 --- /dev/null +++ b/protocol-gateway/component-server/src/main/java/cc/iotkit/comps/ComponentClassLoader.java @@ -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 findClass(String name) throws ClassNotFoundException { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + return (Class) 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); + } +} diff --git a/protocol-gateway/mqtt-component/.DS_Store b/protocol-gateway/mqtt-component/.DS_Store old mode 100644 new mode 100755 diff --git a/protocol-gateway/mqtt-component/dependency-reduced-pom.xml b/protocol-gateway/mqtt-component/dependency-reduced-pom.xml new file mode 100644 index 00000000..6278059c --- /dev/null +++ b/protocol-gateway/mqtt-component/dependency-reduced-pom.xml @@ -0,0 +1,79 @@ + + + + protocol-gateway + cc.iotkit + 0.0.1-SNAPSHOT + + 4.0.0 + mqtt-component + + + + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + + io.vertx:vertx-core + io.vertx:vertx-mqtt + + + + + + maven-compiler-plugin + + 8 + 8 + + + + + + + io.vertx + vertx-core + 4.2.6 + provided + + + io.vertx + vertx-mqtt + 4.2.6 + provided + + + org.projectlombok + lombok + 1.18.22 + compile + + + org.slf4j + slf4j-api + 1.7.32 + compile + + + cc.iotkit + common + 0.0.1-SNAPSHOT + compile + + + cc.iotkit + component + 0.0.1-SNAPSHOT + compile + + +