Selaa lähdekoodia

添加转换器

xiwa 3 vuotta sitten
vanhempi
commit
a0d7d73a74

BIN
.DS_Store


BIN
components/.DS_Store


+ 9 - 0
dao/src/main/java/cc/iotkit/dao/ProtocolConverterRepository.java

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

+ 136 - 28
manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java

@@ -8,12 +8,13 @@ import cc.iotkit.comp.mqtt.MqttComponent;
 import cc.iotkit.comps.ComponentManager;
 import cc.iotkit.converter.ScriptConverter;
 import cc.iotkit.dao.ProtocolComponentRepository;
+import cc.iotkit.dao.ProtocolConverterRepository;
 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.ProtocolComponent;
+import cc.iotkit.model.protocol.ProtocolConverter;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,10 +28,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 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.nio.file.*;
 import java.util.Optional;
 import java.util.UUID;
 
@@ -39,15 +37,18 @@ import java.util.UUID;
 @RequestMapping("/protocol")
 public class ProtocolController {
 
-    @Value("${gateway.function-jar}")
-    private String functionJar;
-
-    @Value("${component.dir}")
+    @Value("${component.dir:./data/components}")
     private String componentDir;
 
+    @Value("${converter.dir:./data/converters}")
+    private String converterDir;
+
     @Autowired
     private ProtocolComponentRepository protocolComponentRepository;
 
+    @Autowired
+    private ProtocolConverterRepository protocolConverterRepository;
+
     @Autowired
     private DataOwnerService dataOwnerService;
 
@@ -57,11 +58,16 @@ public class ProtocolController {
     @Autowired
     private ComponentManager componentManager;
 
-    private Path getFilePath(String comId) {
+    private Path getComponentFilePath(String comId) {
         return Paths.get(String.format("%s/%s", componentDir, comId))
                 .toAbsolutePath().normalize();
     }
 
+    private Path getConverterFilePath(String conId) {
+        return Paths.get(String.format("%s/%s", converterDir, conId))
+                .toAbsolutePath().normalize();
+    }
+
     @PostMapping("/uploadJar")
     public String uploadJar(@RequestParam("file") MultipartFile file, String id) {
         if (file == null) {
@@ -79,7 +85,7 @@ public class ProtocolController {
             } else {
                 id = UUID.randomUUID().toString();
             }
-            Path jarFilePath = getFilePath(id);
+            Path jarFilePath = getComponentFilePath(id);
             Files.createDirectories(jarFilePath);
             Path targetLocation = jarFilePath.resolve(fileName);
             Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
@@ -95,7 +101,7 @@ public class ProtocolController {
         if (!StringUtils.hasLength(id)) {
             throw new BizException("component id is blank");
         }
-        Path jarPath = getFilePath(id);
+        Path jarPath = getComponentFilePath(id);
         if (!jarPath.resolve(component.getJarFile()).toFile().exists()) {
             throw new BizException("component jar file does not exist");
         }
@@ -105,11 +111,6 @@ public class ProtocolController {
             throw new BizException("component already exists");
         }
         try {
-            Optional<UserInfo> optUser = userInfoRepository.findById(AuthUtil.getUserId());
-            if (!optUser.isPresent()) {
-                throw new BizException("user does not exists");
-            }
-
             component.setCreateAt(System.currentTimeMillis());
             component.setUid(AuthUtil.getUserId());
             protocolComponentRepository.save(component);
@@ -124,7 +125,7 @@ public class ProtocolController {
         if (!StringUtils.hasLength(id)) {
             throw new BizException("component id is blank");
         }
-        Path jarPath = getFilePath(id);
+        Path jarPath = getComponentFilePath(id);
         if (!jarPath.resolve(component.getJarFile()).toFile().exists()) {
             throw new BizException("component jar file does not exist");
         }
@@ -133,10 +134,6 @@ public class ProtocolController {
         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");
-        }
 
         ProtocolComponent oldComponent = optComponent.get();
         component = ReflectUtil.copyNoNulls(component, oldComponent);
@@ -157,7 +154,7 @@ public class ProtocolController {
         ProtocolComponent component = optComponent.get();
         dataOwnerService.checkOwner(component);
         try {
-            Path path = getFilePath(id);
+            Path path = getComponentFilePath(id);
             File file = path.resolve(ProtocolComponent.SCRIPT_FILE_NAME).toFile();
             return FileUtils.readFileToString(file, "UTF-8");
         } catch (Throwable e) {
@@ -177,7 +174,7 @@ public class ProtocolController {
         ProtocolComponent oldComponent = optComponent.get();
         dataOwnerService.checkOwner(oldComponent);
         try {
-            Path path = getFilePath(id);
+            Path path = getComponentFilePath(id);
             File file = path.resolve(ProtocolComponent.SCRIPT_FILE_NAME).toFile();
             script = JsonUtil.parse(script, String.class);
             FileUtils.writeStringToFile(file, script, "UTF-8", false);
@@ -194,10 +191,14 @@ public class ProtocolController {
             Path path = Paths.get(String.format("%s/%s", componentDir, id))
                     .toAbsolutePath().normalize();
             File file = path.toFile();
-            if (file.isDirectory()) {
-                FileUtils.deleteDirectory(file);
-            } else {
-                FileUtils.delete(file);
+            try {
+                if (file.isDirectory()) {
+                    FileUtils.deleteDirectory(file);
+                } else {
+                    FileUtils.delete(file);
+                }
+            } catch (NoSuchFileException e) {
+                log.warn("delete component script error", e);
             }
             protocolComponentRepository.deleteById(id);
         } catch (Throwable e) {
@@ -214,6 +215,113 @@ public class ProtocolController {
         return new Paging<>(components.getTotalElements(), components.getContent());
     }
 
+    @PostMapping("/converters/{size}/{page}")
+    public Paging<ProtocolConverter> getConverters(
+            @PathVariable("size") int size,
+            @PathVariable("page") int page) {
+        protocolConverterRepository.deleteById("");
+        protocolConverterRepository.deleteById("null");
+        Page<ProtocolConverter> converters = protocolConverterRepository.findAll(
+                PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("createAt"))));
+        return new Paging<>(converters.getTotalElements(), converters.getContent());
+    }
+
+    @PostMapping("/addConverter")
+    public void addConverter(ProtocolConverter converter) {
+        try {
+            converter.setId(null);
+            converter.setCreateAt(System.currentTimeMillis());
+            converter.setUid(AuthUtil.getUserId());
+            protocolConverterRepository.save(converter);
+        } catch (Throwable e) {
+            throw new BizException("add protocol converter error", e);
+        }
+    }
+
+    @PostMapping("/saveConverter")
+    public void saveConverter(ProtocolConverter converter) {
+        Optional<ProtocolConverter> optConverter = protocolConverterRepository.findById(converter.getId());
+        if (!optConverter.isPresent()) {
+            throw new BizException("the protocol converter does not exists");
+        }
+
+        ProtocolConverter oldConverter = optConverter.get();
+        converter = ReflectUtil.copyNoNulls(converter, oldConverter);
+        dataOwnerService.checkOwner(converter);
+        try {
+            protocolConverterRepository.save(converter);
+        } catch (Throwable e) {
+            throw new BizException("add protocol converter error", e);
+        }
+    }
+
+    @GetMapping("/getConverterScript/{id}")
+    public String getConverterScript(@PathVariable("id") String id) {
+        Optional<ProtocolConverter> optConverter = protocolConverterRepository.findById(id);
+        if (!optConverter.isPresent()) {
+            throw new BizException("the converter does not exists");
+        }
+        ProtocolConverter converter = optConverter.get();
+        dataOwnerService.checkOwner(converter);
+        try {
+            Path path = getConverterFilePath(id);
+            File file = path.resolve(ProtocolConverter.SCRIPT_FILE_NAME).toFile();
+            return FileUtils.readFileToString(file, "UTF-8");
+        } catch (Throwable e) {
+            log.error("read converter script file error", e);
+            return "";
+        }
+    }
+
+    @PostMapping("/saveConverterScript/{id}")
+    public void saveConverterScript(
+            @PathVariable("id") String id,
+            @RequestBody String script) {
+        Optional<ProtocolConverter> optConverter = protocolConverterRepository.findById(id);
+        if (!optConverter.isPresent()) {
+            throw new BizException("the converter does not exists");
+        }
+        ProtocolConverter oldConverter = optConverter.get();
+        dataOwnerService.checkOwner(oldConverter);
+        try {
+            Path path = getConverterFilePath(id);
+            File file = path.resolve(ProtocolConverter.SCRIPT_FILE_NAME).toFile();
+            script = JsonUtil.parse(script, String.class);
+            FileUtils.writeStringToFile(file, script, "UTF-8", false);
+        } catch (Throwable e) {
+            throw new BizException("save protocol converter script error", e);
+        }
+    }
+
+    @PostMapping("/deleteConverter/{id}")
+    public void deleteConverter(@PathVariable("id") String id) {
+        dataOwnerService.checkOwner(protocolConverterRepository, id);
+        try {
+            Path path = Paths.get(String.format("%s/%s", componentDir, id))
+                    .toAbsolutePath().normalize();
+            File file = path.toFile();
+            try {
+                if (file.isDirectory()) {
+                    FileUtils.deleteDirectory(file);
+                } else {
+                    FileUtils.delete(file);
+                }
+            } catch (NoSuchFileException e) {
+                log.warn("delete converter script error", e);
+            }
+            protocolConverterRepository.deleteById(id);
+        } catch (Throwable e) {
+            throw new BizException("delete protocol converter error", e);
+        }
+    }
+
+    @PostMapping("/component/{id}/{state}")
+    public void startComponent(@PathVariable("id") String id,
+                               @PathVariable("state") String state) {
+        
+    }
+
+
     @GetMapping("/registerMqtt")
     public void registerMqtt() throws IOException {
         MqttComponent component = new MqttComponent();

+ 0 - 5
manager/src/main/resources/application-dev.yml

@@ -69,8 +69,3 @@ app:
 mqtt:
   url: tcp://填写mqtt连接地址
 
-component:
-  dir: ./components
-
-gateway:
-  function-jar: 填写protocol-function打包的jar存放路径:/xx/xx.jar

+ 0 - 5
manager/src/main/resources/application.yml

@@ -67,8 +67,3 @@ app:
 mqtt:
   url: tcp://填写mqtt连接地址
 
-component:
-  dir: ./components
-
-gateway:
-  function-jar: 填写protocol-function打包的jar存放路径:/xx/xx.jar

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

@@ -30,6 +30,8 @@ public class ProtocolComponent implements Owned {
 
     private String config;
 
+    private String converter;
+
     private String state;
 
     private Long createAt;

+ 27 - 0
model/src/main/java/cc/iotkit/model/protocol/ProtocolConverter.java

@@ -0,0 +1,27 @@
+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;
+
+@Document
+@Data
+public class ProtocolConverter implements Owned {
+
+    public static final String SCRIPT_FILE_NAME = "converter.js";
+
+    @Id
+    private String id;
+
+    /**
+     * 所属性用户id
+     */
+    private String uid;
+
+    private String name;
+
+    private String desc;
+
+    private Long createAt;
+}

+ 47 - 50
protocol-gateway/mqtt-component/src/main/resources/converter.js

@@ -1,51 +1,48 @@
-new (function () {
-    this.decode = function (msg) {
-      //对msg进行解析,并返回物模型数据
-      var mqttMsg = JSON.parse(msg.content);
-      var topic = mqttMsg.topic;
-      var payload = mqttMsg.payload;
-  
-      if (topic.endsWith("/property/post")) {
-        //属性上报
-        return {
-          mid: msg.mid,
-          productKey: msg.productKey, //可根据消息内容判断填写不同产品
-          deviceName: msg.deviceName,
-          type:"property",
-          identifier: "report", //属性上报
-          occur: new Date().getTime(), //时间戳,设备上的事件或数据产生的本地时间
-          time: new Date().getTime(), //时间戳,消息上报时间
-          data: payload.params,
-        };
-      } else if (topic.indexOf("/event/") > 0) {
-        var identifier = topic.substring(topic.lastIndexOf("/") + 1);
-        //事件上报
-        return {
-          mid: msg.mid,
-          productKey: msg.productKey,
-          deviceName: msg.deviceName,
-          type:"event",
-          identifier: identifier,
-          occur: new Date().getTime(),
-          time: new Date().getTime(),
-          data: payload.params,
-        };
-      } else if (topic.endsWith("_reply")) {
-        var identifier = topic.substring(topic.lastIndexOf("/") + 1);
-        //服务回复
-        return {
-          mid: msg.mid,
-          productKey: msg.productKey,
-          deviceName: msg.deviceName,
-          type:"service",
-          identifier: identifier.replace("_reply", "Reply"),
-          occur: new Date().getTime(),
-          time: new Date().getTime(),
-          code: payload.code,
-          data: payload.data,
-        };
-      }
-      return null;
+this.decode = function (msg) {
+  //对msg进行解析,并返回物模型数据
+  var mqttMsg = JSON.parse(msg.content);
+  var topic = mqttMsg.topic;
+  var payload = mqttMsg.payload;
+
+  if (topic.endsWith("/property/post")) {
+    //属性上报
+    return {
+      mid: msg.mid,
+      productKey: msg.productKey, //可根据消息内容判断填写不同产品
+      deviceName: msg.deviceName,
+      type:"property",
+      identifier: "report", //属性上报
+      occur: new Date().getTime(), //时间戳,设备上的事件或数据产生的本地时间
+      time: new Date().getTime(), //时间戳,消息上报时间
+      data: payload.params,
     };
-  })()
-  
+  } else if (topic.indexOf("/event/") > 0) {
+    var identifier = topic.substring(topic.lastIndexOf("/") + 1);
+    //事件上报
+    return {
+      mid: msg.mid,
+      productKey: msg.productKey,
+      deviceName: msg.deviceName,
+      type:"event",
+      identifier: identifier,
+      occur: new Date().getTime(),
+      time: new Date().getTime(),
+      data: payload.params,
+    };
+  } else if (topic.endsWith("_reply")) {
+    var identifier = topic.substring(topic.lastIndexOf("/") + 1);
+    //服务回复
+    return {
+      mid: msg.mid,
+      productKey: msg.productKey,
+      deviceName: msg.deviceName,
+      type:"service",
+      identifier: identifier.replace("_reply", "Reply"),
+      occur: new Date().getTime(),
+      time: new Date().getTime(),
+      code: payload.code,
+      data: payload.data,
+    };
+  }
+  return null;
+};