diff --git a/.gitignore b/.gitignore index 8da0c3c1..d9c4d48c 100755 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ target *.iml *.yml log +components +data diff --git a/components/.DS_Store b/components/.DS_Store old mode 100644 new mode 100755 index 612370d1..d35af108 Binary files a/components/.DS_Store and b/components/.DS_Store differ diff --git a/data/elasticsearch/nodes/0/_state/_3.cfe b/data/elasticsearch/nodes/0/_state/_3.cfe deleted file mode 100644 index cf99088b..00000000 Binary files a/data/elasticsearch/nodes/0/_state/_3.cfe and /dev/null differ diff --git a/data/elasticsearch/nodes/0/_state/_3.cfs b/data/elasticsearch/nodes/0/_state/_3.cfs deleted file mode 100644 index 4d78d246..00000000 Binary files a/data/elasticsearch/nodes/0/_state/_3.cfs and /dev/null differ diff --git a/data/elasticsearch/nodes/0/_state/_3.si b/data/elasticsearch/nodes/0/_state/_3.si deleted file mode 100644 index 5df35ca1..00000000 Binary files a/data/elasticsearch/nodes/0/_state/_3.si and /dev/null differ diff --git a/data/elasticsearch/nodes/0/_state/manifest-0.st b/data/elasticsearch/nodes/0/_state/manifest-0.st deleted file mode 100644 index 030f9cbe..00000000 Binary files a/data/elasticsearch/nodes/0/_state/manifest-0.st and /dev/null differ diff --git a/data/elasticsearch/nodes/0/_state/node-0.st b/data/elasticsearch/nodes/0/_state/node-0.st deleted file mode 100644 index a819c66f..00000000 Binary files a/data/elasticsearch/nodes/0/_state/node-0.st and /dev/null differ diff --git a/data/elasticsearch/nodes/0/_state/segments_5 b/data/elasticsearch/nodes/0/_state/segments_5 deleted file mode 100644 index ef389e57..00000000 Binary files a/data/elasticsearch/nodes/0/_state/segments_5 and /dev/null differ diff --git a/data/elasticsearch/nodes/0/_state/write.lock b/data/elasticsearch/nodes/0/_state/write.lock deleted file mode 100644 index e69de29b..00000000 diff --git a/data/elasticsearch/nodes/0/node.lock b/data/elasticsearch/nodes/0/node.lock deleted file mode 100644 index e69de29b..00000000 diff --git a/manager/src/main/java/cc/iotkit/manager/config/ElasticSearchConfig.java b/manager/src/main/java/cc/iotkit/manager/config/ElasticSearchConfig.java old mode 100644 new mode 100755 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 9fabe171..dee6ae4a 100755 --- a/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java +++ b/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.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 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 optComponent = protocolComponentRepository.findById(component.getId()); + @GetMapping("/getComponentScript/{id}") + public String getComponentScript(@PathVariable("id") String id) { + Optional optComponent = protocolComponentRepository.findById(id); if (!optComponent.isPresent()) { throw new BizException("the component does not exists"); } + ProtocolComponent component = optComponent.get(); dataOwnerService.checkOwner(component); - ProtocolComponent oldComponent = optComponent.get(); - oldComponent.setScriptFile(component.getScriptFile()); 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(); + 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 optComponent = protocolComponentRepository.findById(id); + if (!optComponent.isPresent()) { + throw new BizException("the component does not exists"); + } + ProtocolComponent oldComponent = optComponent.get(); + dataOwnerService.checkOwner(oldComponent); + try { + 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); diff --git a/model/src/main/java/cc/iotkit/model/protocol/ProtocolComponent.java b/model/src/main/java/cc/iotkit/model/protocol/ProtocolComponent.java index 40ee8bf7..97b1aa0c 100755 --- a/model/src/main/java/cc/iotkit/model/protocol/ProtocolComponent.java +++ b/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; 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 old mode 100644 new mode 100755 diff --git a/protocol-gateway/emqx-component/src/main/resources/component.spi b/protocol-gateway/emqx-component/src/main/resources/component.spi old mode 100644 new mode 100755 diff --git a/protocol-gateway/mqtt-component/src/main/resources/component.js b/protocol-gateway/mqtt-component/src/main/resources/component.js index b9c8a840..cd505874 100755 --- a/protocol-gateway/mqtt-component/src/main/resources/component.js +++ b/protocol-gateway/mqtt-component/src/main/resources/component.js @@ -1,84 +1,77 @@ -new (function () { - !function(n){"use strict";function d(n,t){var r=(65535&n)+(65535&t);return(n>>16)+(t>>16)+(r>>16)<<16|65535&r}function f(n,t,r,e,o,u){return d((u=d(d(t,n),d(e,u)))<>>32-o,r)}function l(n,t,r,e,o,u,c){return f(t&r|~t&e,n,t,o,u,c)}function g(n,t,r,e,o,u,c){return f(t&e|r&~e,n,t,o,u,c)}function v(n,t,r,e,o,u,c){return f(t^r^e,n,t,o,u,c)}function m(n,t,r,e,o,u,c){return f(r^(t|~e),n,t,o,u,c)}function c(n,t){var r,e,o,u;n[t>>5]|=128<>>9<<4)]=t;for(var c=1732584193,f=-271733879,i=-1732584194,a=271733878,h=0;h>5]>>>e%32&255);return t}function a(n){var t=[];for(t[(n.length>>2)-1]=void 0,e=0;e>5]|=(255&n.charCodeAt(e/8))<>>4&15)+r.charAt(15&t);return e}function r(n){return unescape(encodeURIComponent(n))}function o(n){return i(c(a(n=r(n)),8*n.length))}function u(n,t){return function(n,t){var r,e=a(n),o=[],u=[];for(o[15]=u[15]=void 0,16>16)+(t>>16)+(r>>16)<<16|65535&r}function f(n,t,r,e,o,u){return d((u=d(d(t,n),d(e,u)))<>>32-o,r)}function l(n,t,r,e,o,u,c){return f(t&r|~t&e,n,t,o,u,c)}function g(n,t,r,e,o,u,c){return f(t&e|r&~e,n,t,o,u,c)}function v(n,t,r,e,o,u,c){return f(t^r^e,n,t,o,u,c)}function m(n,t,r,e,o,u,c){return f(r^(t|~e),n,t,o,u,c)}function c(n,t){var r,e,o,u;n[t>>5]|=128<>>9<<4)]=t;for(var c=1732584193,f=-271733879,i=-1732584194,a=271733878,h=0;h>5]>>>e%32&255);return t}function a(n){var t=[];for(t[(n.length>>2)-1]=void 0,e=0;e>5]|=(255&n.charCodeAt(e/8))<>>4&15)+r.charAt(15&t);return e}function r(n){return unescape(encodeURIComponent(n))}function o(n){return i(c(a(n=r(n)),8*n.length))}function u(n,t){return function(n,t){var r,e=a(n),o=[],u=[];for(o[15]=u[15]=void 0,16