TRX 1 год назад
Родитель
Сommit
693997b043

+ 23 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/TestController.java

@@ -1,7 +1,9 @@
 package com.zhongshu.iot.server.core.controller;
 
+import com.github.microservice.core.util.script.GroovyUtil;
 import com.zhongshu.iot.client.model.mqtt.SendMessageModel;
 import com.zhongshu.iot.server.core.service.artemis.OperationMessageService;
+import com.zhongshu.iot.server.core.service.groovy.GroovySupport;
 import com.zhongshu.iot.server.core.service.iot.IotServiceImpl;
 import com.zhongshu.iot.server.core.service.mqtt.DeviceInfoService;
 import com.zhongshu.iot.server.core.service.openApi.OpenAPIRegisterService;
@@ -9,6 +11,7 @@ import com.zhongshu.iot.server.core.service.other.ScanExecuteService;
 import com.github.microservice.net.ResultContent;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -16,6 +19,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  *
  */
@@ -23,6 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
 @RestController
 @Validated
 @Tag(name = "测试接口")
+@Slf4j
 public class TestController {
 
     @Autowired
@@ -40,6 +47,9 @@ public class TestController {
     @Autowired
     private DeviceInfoService deviceInfoService;
 
+    @Autowired
+    private GroovySupport groovySupport;
+
     @Operation(summary = "发送指令")
     @RequestMapping(value = "free/sendMessage", method = {RequestMethod.POST})
     public ResultContent sendMessage(@RequestBody SendMessageModel param) {
@@ -78,5 +88,18 @@ public class TestController {
         return deviceInfoService.initSyncAllDevice();
     }
 
+    @Operation(summary = "Groovy测试")
+    @RequestMapping(value = "testGroovy", method = {RequestMethod.GET})
+    public ResultContent testGroovy() {
+        String template = "Hi, ${name} 我的名字是:${app.getName()} ${app.getCurrentTime()}";
+
+        Map<String, Object> var = new HashMap<String, Object>();
+        var.put("name", "张三");
+        var.put("app", groovySupport);
+        String templateStr = GroovyUtil.textTemplate(var, template);
+        log.info("templateStr: {}", templateStr);
+        return ResultContent.buildSuccess();
+    }
+
 }
 

+ 50 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/groovy/GroovySupport.java

@@ -0,0 +1,50 @@
+package com.zhongshu.iot.server.core.service.groovy;
+
+import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
+import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
+import com.zhongshu.iot.server.core.service.base.SuperService;
+import com.zhongshu.iot.server.core.util.DateUtils;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * Groovy 支持类
+ *
+ * @author TRX
+ * @date 2024/10/31
+ */
+@Slf4j
+@Service
+public class GroovySupport extends SuperService {
+
+    @Autowired
+    private DeviceInfoDao deviceInfoDao;
+
+    public String getName() {
+        return "测试";
+    }
+
+    /**
+     * 更加设备deviceId得到名称
+     * @param deviceId
+     * @return
+     */
+    public String getDeviceNameById(String deviceId) {
+        DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
+        if (ObjectUtils.isNotEmpty(deviceId)) {
+            return deviceInfo.getDeviceName();
+        }
+        return "";
+    }
+
+    /**
+     * 得到当前时间,如:2024-10-31 14:45:21
+     * @return
+     */
+    public String getCurrentTime() {
+        return DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG);
+    }
+}

+ 10 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/util/test/TestReplace.java

@@ -1,10 +1,13 @@
 package com.zhongshu.iot.server.core.util.test;
 
 import cn.hutool.json.JSONObject;
+import com.github.microservice.core.util.script.GroovyUtil;
 import com.zhongshu.iot.client.type.DataType;
 
 import java.math.BigDecimal;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.regex.Pattern;
 
 /**
@@ -28,6 +31,12 @@ public class TestReplace {
         System.out.println(ob instanceof String);
 
         boolean b = List.of(DataType.String, DataType.Boolean).contains(DataType.Object);
-        System.out.println(b);
+
+        // GroovyUtil
+        String template = "Hi, ${name}";
+        Map<String, Object> var = new HashMap<String, Object>();
+        var.put("name", "张三");
+        String templateStr = GroovyUtil.textTemplate(var, template);
+        System.out.println("templateStr: " + templateStr);
     }
 }