xiwa 3 роки тому
батько
коміт
48b6ec7eb3

+ 2 - 0
.gitignore

@@ -26,3 +26,5 @@ target
 *.iml
 *.yml
 log
+components
+data

BIN
components/.DS_Store


BIN
data/elasticsearch/nodes/0/_state/_3.cfe


BIN
data/elasticsearch/nodes/0/_state/_3.cfs


BIN
data/elasticsearch/nodes/0/_state/_3.si


BIN
data/elasticsearch/nodes/0/_state/manifest-0.st


BIN
data/elasticsearch/nodes/0/_state/node-0.st


BIN
data/elasticsearch/nodes/0/_state/segments_5


+ 0 - 0
data/elasticsearch/nodes/0/_state/write.lock


+ 0 - 0
data/elasticsearch/nodes/0/node.lock


+ 43 - 13
manager/src/main/java/cc/iotkit/manager/config/ElasticSearchConfig.java

@@ -1,6 +1,7 @@
 package cc.iotkit.manager.controller;
 
 import cc.iotkit.common.exception.BizException;
+import cc.iotkit.common.utils.JsonUtil;
 import cc.iotkit.common.utils.ReflectUtil;
 import cc.iotkit.comp.CompConfig;
 import cc.iotkit.comp.mqtt.MqttComponent;
@@ -56,21 +57,29 @@ public class ProtocolController {
     @Autowired
     private ComponentManager componentManager;
 
-    private Path getFilePath(String comId, String type) {
-        return Paths.get(String.format("%s/%s/%s", componentDir, comId, type))
+    private Path getFilePath(String comId) {
+        return Paths.get(String.format("%s/%s", componentDir, comId))
                 .toAbsolutePath().normalize();
     }
 
     @PostMapping("/uploadJar")
-    public String uploadJar(@RequestParam("file") MultipartFile file) {
+    public String uploadJar(@RequestParam("file") MultipartFile file, String id) {
         if (file == null) {
             throw new BizException("file is null");
         }
         log.info("saving upload jar file:{}", file.getName());
         String fileName = StringUtils.cleanPath(file.getOriginalFilename());
         try {
-            String id = UUID.randomUUID().toString();
-            Path jarFilePath = getFilePath(id, "jar");
+            if (StringUtils.hasLength(id)) {
+                Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(id);
+                if (!optComponent.isPresent()) {
+                    throw new BizException("the protocol component does not exists");
+                }
+                dataOwnerService.checkOwner(optComponent.get());
+            } else {
+                id = UUID.randomUUID().toString();
+            }
+            Path jarFilePath = getFilePath(id);
             Files.createDirectories(jarFilePath);
             Path targetLocation = jarFilePath.resolve(fileName);
             Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
@@ -86,7 +95,7 @@ public class ProtocolController {
         if (!StringUtils.hasLength(id)) {
             throw new BizException("component id is blank");
         }
-        Path jarPath = getFilePath(id, "jar");
+        Path jarPath = getFilePath(id);
         if (!jarPath.resolve(component.getJarFile()).toFile().exists()) {
             throw new BizException("component jar file does not exist");
         }
@@ -115,7 +124,7 @@ public class ProtocolController {
         if (!StringUtils.hasLength(id)) {
             throw new BizException("component id is blank");
         }
-        Path jarPath = getFilePath(id, "jar");
+        Path jarPath = getFilePath(id);
         if (!jarPath.resolve(component.getJarFile()).toFile().exists()) {
             throw new BizException("component jar file does not exist");
         }
@@ -139,18 +148,39 @@ public class ProtocolController {
         }
     }
 
-    @PostMapping("/saveComponentScript")
-    public void saveComponentScript(@RequestBody ProtocolComponent component) {
-        Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(component.getId());
+    @GetMapping("/getComponentScript/{id}")
+    public String getComponentScript(@PathVariable("id") String id) {
+        Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(id);
         if (!optComponent.isPresent()) {
             throw new BizException("the component does not exists");
         }
+        ProtocolComponent component = optComponent.get();
         dataOwnerService.checkOwner(component);
+        try {
+            Path path = getFilePath(id);
+            File file = path.resolve(ProtocolComponent.SCRIPT_FILE_NAME).toFile();
+            return FileUtils.readFileToString(file, "UTF-8");
+        } catch (Throwable e) {
+            log.error("read component script file error", e);
+            return "";
+        }
+    }
+
+    @PostMapping("/saveComponentScript/{id}")
+    public void saveComponentScript(
+            @PathVariable("id") String id,
+            @RequestBody String script) {
+        Optional<ProtocolComponent> optComponent = protocolComponentRepository.findById(id);
+        if (!optComponent.isPresent()) {
+            throw new BizException("the component does not exists");
+        }
         ProtocolComponent oldComponent = optComponent.get();
-        oldComponent.setScriptFile(component.getScriptFile());
+        dataOwnerService.checkOwner(oldComponent);
         try {
-//            gatewayService.saveFunction(oldGateway.getUuid(), oldGateway.getId(),
-//                    "new (function (){" + oldGateway.getScript() + "})()", functionJar);
+            Path path = getFilePath(id);
+            File file = path.resolve(ProtocolComponent.SCRIPT_FILE_NAME).toFile();
+            script = JsonUtil.parse(script, String.class);
+            FileUtils.writeStringToFile(file, script, "UTF-8", false);
             protocolComponentRepository.save(oldComponent);
         } catch (Throwable e) {
             throw new BizException("save protocol component script error", e);

+ 2 - 2
model/src/main/java/cc/iotkit/model/protocol/ProtocolComponent.java

@@ -12,6 +12,8 @@ public class ProtocolComponent implements Owned {
     public static final String STATE_STOPPED = "stopped";
     public static final String STATE_RUNNING = "running";
 
+    public static final String SCRIPT_FILE_NAME = "component.js";
+
     @Id
     private String id;
 
@@ -28,8 +30,6 @@ public class ProtocolComponent implements Owned {
 
     private String config;
 
-    private String scriptFile;
-
     private String state;
 
     private Long createAt;

Різницю між файлами не показано, бо вона завелика
+ 0 - 1
protocol-gateway/component-server/src/main/java/cc/iotkit/comps/ComponentClassLoader.java


+ 0 - 0
protocol-gateway/mqtt-component/src/main/resources/component.spi


Деякі файли не було показано, через те що забагато файлів було змінено