Browse Source

bug修改

Lifan 1 year ago
parent
commit
6c834160e7

+ 19 - 1
application/src/main/java/org/thingsboard/server/config/ReceiveHandler.java

@@ -14,7 +14,9 @@ import org.springframework.amqp.core.Message;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.cache.CacheManager;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
@@ -37,6 +39,7 @@ import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.time.Duration;
 import java.util.*;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.stream.Collectors;
@@ -70,6 +73,10 @@ public class ReceiveHandler {
 
     @Autowired
     private JqsjConfig jqsjConfig;
+    @Value("#{'${bridge.deviceId}'.split(',')}")
+    private List<String> codes;
+    @Autowired
+    private CacheManager cacheManager;
 
     private ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtils.getThreadPool();
 
@@ -109,6 +116,7 @@ public class ReceiveHandler {
                 Date generationTime = StringUtils.hasText(generationTimeStr)
                         ? DateUtil.parse(generationTimeStr, YYYY_MM_DD_HH_MM_SS) : new Date();
                 String deviceName = jsonObject.get("device_id").toString();
+
                 for (IndexTypeEnum anEnum : IndexTypeEnum.values()) {
                     Object o = jsonObject.get(anEnum.getCode());
                     if(o!=null){
@@ -170,7 +178,14 @@ public class ReceiveHandler {
                         }
                         scadaHistoryList.add(scadaHistory);
                     }
-                    scadaHistoryService.saveBatch(scadaHistoryList);
+                    if(codes.contains(deviceName)){
+                        if(cacheManager.getCache(org.thingsboard.server.glossary.constants.FastIotConstants.CacheConfigKey.CACHE_BRIDGE_DATA_KEY).get(deviceName, Long.class) == null || timestamp2Timestamp(generationTime.getTime(), cacheManager.getCache(org.thingsboard.server.glossary.constants.FastIotConstants.CacheConfigKey.CACHE_BRIDGE_DATA_KEY).get(deviceName, Long.class))) {
+                            cacheManager.getCache(org.thingsboard.server.glossary.constants.FastIotConstants.CacheConfigKey.CACHE_BRIDGE_DATA_KEY).put(deviceName, generationTime.getTime());
+                            scadaHistoryService.saveBatch(scadaHistoryList);
+                        }
+                    }else{
+                        scadaHistoryService.saveBatch(scadaHistoryList);
+                    }
                     if (CollUtil.isNotEmpty(scadaHistoryList)) {
                         //实时数据先清空该设备数据再新增
                         scadaMonitorService.deleteByCode(deviceName);
@@ -368,5 +383,8 @@ public class ReceiveHandler {
         return val;
     }
 
+    private boolean timestamp2Timestamp(Long nowTimestamp, Long oldTimestamp){
+        return Duration.ofMillis(nowTimestamp - oldTimestamp).toMinutes() > 5;
+    }
 
 }

+ 34 - 5
application/src/main/java/org/thingsboard/server/controller/nanxi/BridgeController.java

@@ -21,6 +21,7 @@ import org.thingsboard.server.domain.*;
 import org.thingsboard.server.glossary.enums.YuntiTypeEnums;
 import org.thingsboard.server.service.nanxi.*;
 import org.thingsboard.server.utils.MinioUtils;
+import org.thingsboard.server.utils.WordUtils;
 
 import java.io.BufferedReader;
 import java.io.InputStream;
@@ -28,10 +29,7 @@ import java.io.InputStreamReader;
 import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 
 @Slf4j
 @RestController
@@ -50,7 +48,7 @@ public class BridgeController {
     private INxAnnexService annexService;
     @Autowired
     private NxMonitorWarnDataService monitorWarnDataService;
-    @RequestMapping("/test")
+//    @RequestMapping("/test")
     public String test(@RequestParam(value = "ANPR", required = false) MultipartFile ANPR, @RequestParam(value = "detectionPicture1", required = false) MultipartFile file){
         try {
             InputStreamReader ISR = new InputStreamReader(ANPR.getInputStream(), "utf-8");
@@ -80,4 +78,35 @@ public class BridgeController {
 
         return "200";
     }
+    @RequestMapping("/template")
+    public void template() throws Exception {
+        Map<String, Object> data = new HashMap<>();
+        data.put("pushTime", "2023-01-02");
+        data.put("areaName", "xxxx");
+        data.put("eventTypeName", "qqq");
+        data.put("eventLevelName", "wwwww");
+        data.put("domainName", "eeeee");
+        data.put("industryName", "rrrrr");
+        data.put("eventName", "ddddd");
+        data.put("eventAddress", "ffffff");
+        data.put("longitude", "aaaaaa");
+        data.put("latitude", "ssssss");
+        data.put("remark", "dddddff");
+        List<Map<String, Object>> obj = new ArrayList<>();
+        data.put("contacts", obj);
+        Map<String, Object> tmp = null;
+        for (int i = 0; i < 5; i++) {
+            tmp = new HashMap<>();
+            tmp.put("name", "name" + i);
+            tmp.put("type", "type");
+            tmp.put("areaName", "areaName");
+            tmp.put("contactName", "contactName");
+            tmp.put("contactPhone", "contactPhone");
+            tmp.put("contactDuty", "contactDuty");
+            tmp.put("distance", "distance");
+            obj.add(tmp);
+        }
+        String templateName = "事件报告.ftl";
+        WordUtils.generateWord(data, templateName, "/tmp/demo.docx");
+    }
 }

+ 1 - 0
application/src/main/java/org/thingsboard/server/glossary/constants/FastIotConstants.java

@@ -7,5 +7,6 @@ public interface FastIotConstants {
   interface CacheConfigKey {
     String CACHE_TRIGGER_DATA_KEY = "triggerData";
     String CACHE_REAL_TIME_DATA_KEY = "realTimeData";
+    String CACHE_BRIDGE_DATA_KEY = "bridgeData";
   }
 }

+ 17 - 2
application/src/main/java/org/thingsboard/server/service/nanxi/impl/YunTiAsyncServiceImpl.java

@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.RandomUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.CacheManager;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
@@ -19,6 +20,7 @@ import org.thingsboard.server.domain.*;
 import org.thingsboard.server.domain.dto.RealTimeDataObj;
 import org.thingsboard.server.domain.dto.TriggerDataObj;
 import org.thingsboard.server.domain.vo.UserInfoViews;
+import org.thingsboard.server.glossary.constants.FastIotConstants;
 import org.thingsboard.server.glossary.enums.YuntiTypeEnums;
 import org.thingsboard.server.mapper.CommonMapper;
 import org.thingsboard.server.mapper.SysDicItemMapper;
@@ -27,6 +29,7 @@ import org.thingsboard.server.service.nanxi.*;
 import org.thingsboard.server.utils.NumberUtils;
 
 import java.text.SimpleDateFormat;
+import java.time.Duration;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
@@ -53,6 +56,8 @@ public class YunTiAsyncServiceImpl implements IYunTiAsyncService {
     private ScadaHistoryService scadaHistoryService;
     @Autowired
     private ScadaMonitorService scadaMonitorService;
+    @Autowired
+    private CacheManager cacheManager;
 
     @Override
     @Async("yunTiAsyncExecutor")
@@ -97,7 +102,10 @@ public class YunTiAsyncServiceImpl implements IYunTiAsyncService {
                     }
                     scadaHistoryList.add(scadaHistory);
                 }
-                scadaHistoryService.saveBatch(scadaHistoryList);
+                if(cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_REAL_TIME_DATA_KEY).get(messageBody.getRegisterCode(), Long.class) == 0l || timestamp2Timestamp(json.getLong("timestamp"), cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_REAL_TIME_DATA_KEY).get(messageBody.getRegisterCode(), Long.class))) {
+                    cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_REAL_TIME_DATA_KEY).put(messageBody.getRegisterCode(), json.getLong("timestamp"));
+                    scadaHistoryService.saveBatch(scadaHistoryList);
+                }
                 if (CollUtil.isNotEmpty(scadaHistoryList)) {
                     //实时数据先清空该设备数据再新增
 //                    scadaMonitorService.deleteByCode(messageBody.getRegisterCode());
@@ -156,7 +164,10 @@ public class YunTiAsyncServiceImpl implements IYunTiAsyncService {
                     }
                     scadaHistoryList.add(scadaHistory);
                 }
-                scadaHistoryService.saveBatch(scadaHistoryList);
+                if(cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_TRIGGER_DATA_KEY).get(messageBody.getRegisterCode(), Long.class) == 0l || timestamp2Timestamp(json.getLong("timestamp"), cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_TRIGGER_DATA_KEY).get(messageBody.getRegisterCode(), Long.class))){
+                    cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_TRIGGER_DATA_KEY).put(messageBody.getRegisterCode(), json.getLong("timestamp"));
+                    scadaHistoryService.saveBatch(scadaHistoryList);
+                }
                 if (CollUtil.isNotEmpty(scadaHistoryList)) {
                     //实时数据先清空该设备数据再新增
 //                    scadaMonitorService.deleteByCode(messageBody.getRegisterCode());
@@ -190,6 +201,10 @@ public class YunTiAsyncServiceImpl implements IYunTiAsyncService {
         return history;
     }
 
+    private boolean timestamp2Timestamp(Long nowTimestamp, Long oldTimestamp){
+        return Duration.ofMillis(nowTimestamp - oldTimestamp).toMinutes() > 5;
+    }
+
 }
 
 

+ 7 - 9
application/src/main/java/org/thingsboard/server/service/nanxi/impl/YunTiDataHandlerServiceImpl.java

@@ -22,26 +22,25 @@ public class YunTiDataHandlerServiceImpl extends YunTiDataHandlerService {
 
     @Autowired
     private IYunTiAsyncService yunTiAsyncService;
-//    @Autowired
-//    private CacheManager cacheManager;
+    @Autowired
+    private CacheManager cacheManager;
 
     @Override
     public void dataHandler(String data) {
         JSONObject json = JSON.parseObject(data);
         if(YunTiMsgTypeEnum.SUBSCRIBE_LIFT_ACK.getCode().equals(json.get("messageType"))){
             SubLiftAckObj messageBody = json.getObject("messageBody", SubLiftAckObj.class);
-            log.info("订阅电梯数据:{}", JSON.toJSONString(messageBody));
-            /*for (String registerCode : messageBody.getRegisterCodes()) {
+//            log.info("订阅电梯数据:{}", JSON.toJSONString(messageBody));
+            for (String registerCode : messageBody.getRegisterCodes()) {
                 cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_REAL_TIME_DATA_KEY).put(registerCode, 0l);
                 cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_TRIGGER_DATA_KEY).put(registerCode, 0l);
-            }*/
+            }
 
         }else if(YunTiMsgTypeEnum.TRIGGER_DATA.getCode().equals(json.get("messageType"))){
             TriggerDataObj messageBody = json.getObject("messageBody", TriggerDataObj.class);
             if(!Objects.isNull(messageBody)){
 //                if(cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_TRIGGER_DATA_KEY).get(messageBody.getRegisterCode(), Long.class) == 0l || timestamp2Timestamp(json.getLong("timestamp"), cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_TRIGGER_DATA_KEY).get(messageBody.getRegisterCode(), Long.class))){
-                    log.info("电梯触发数据:{}", JSON.toJSONString(messageBody));
-//                    cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_TRIGGER_DATA_KEY).put(messageBody.getRegisterCode(), json.getLong("timestamp"));
+//                    log.info("电梯触发数据:{}", JSON.toJSONString(messageBody));
                     yunTiAsyncService.dealTriggerDataObj(messageBody, data);
 //                }
             }
@@ -49,8 +48,7 @@ public class YunTiDataHandlerServiceImpl extends YunTiDataHandlerService {
             RealTimeDataObj messageBody = json.getObject("messageBody", RealTimeDataObj.class);
             if(!Objects.isNull(messageBody)){
 //                if(cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_REAL_TIME_DATA_KEY).get(messageBody.getRegisterCode(), Long.class) == 0l || timestamp2Timestamp(json.getLong("timestamp"), cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_REAL_TIME_DATA_KEY).get(messageBody.getRegisterCode(), Long.class))) {
-                    log.info("电梯实时数据:{}", JSON.toJSONString(messageBody));
-//                    cacheManager.getCache(FastIotConstants.CacheConfigKey.CACHE_REAL_TIME_DATA_KEY).put(messageBody.getRegisterCode(), json.getLong("timestamp"));
+//                    log.info("电梯实时数据:{}", JSON.toJSONString(messageBody));
                     yunTiAsyncService.dealRealTimeDataObj(messageBody, data);
 //                }
             }

+ 41 - 0
application/src/main/java/org/thingsboard/server/utils/WordUtils.java

@@ -0,0 +1,41 @@
+package org.thingsboard.server.utils;
+
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.Version;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+
+public class WordUtils {
+
+    /**
+     * 生成 word 文档方法
+     *
+     * @param dataMap      要填充的数据
+     * @param templateName 模版名称
+     * @param fileName     要输出的文件路径
+     * @throws Exception 抛出的异常
+     */
+    public static void generateWord(Map<String, Object> dataMap, String templateName, String fileName) throws Exception {
+
+        // 设置FreeMarker的版本和编码格式
+        Configuration configuration = new Configuration(new Version("2.3.30"));
+        configuration.setDefaultEncoding("UTF-8");
+
+        // 设置FreeMarker生成Word文档所需要的模板的路径
+        // configuration.setDirectoryForTemplateLoading(new File("/Users/xxx/Desktop/"));
+        // 此处把模版文件都放在 resources 下的 templates 中
+        configuration.setClassForTemplateLoading(WordUtils.class, "/templates");
+
+        // 设置FreeMarker生成Word文档所需要的模板
+        Template tem = configuration.getTemplate(templateName, "UTF-8");
+        // 创建一个Word文档的输出流
+        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), StandardCharsets.UTF_8));
+        // FreeMarker使用Word模板和数据生成Word文档
+        tem.process(dataMap, out);
+        out.flush();
+        out.close();
+    }
+}

+ 3 - 1
application/src/main/resources/bootstrap-dev.yml

@@ -38,4 +38,6 @@ jqsj:
   x1: 1294.971924
   y1: 560.378012
   x2: 2784.739632
-  y2: 1491.618101
+  y2: 1491.618101
+bridge:
+  deviceId: "20451150,DCSP2000,MS3P1473043158,MS3P1473043161,A103202303080001,LN3S225404"

+ 3 - 1
application/src/main/resources/bootstrap-test.yml

@@ -37,4 +37,6 @@ jqsj:
   x1: 1294.971924
   y1: 560.378012
   x2: 2784.739632
-  y2: 1491.618101
+  y2: 1491.618101
+bridge:
+  deviceId: "20451150,DCSP2000,MS3P1473043158,MS3P1473043161,A103202303080001,LN3S225404"

+ 3 - 0
application/src/main/resources/bootstrap.yml

@@ -331,6 +331,9 @@ caffeine:
     realTimeData:
       timeToLiveInMinutes: 1440000
       maxSize: 500
+    bridgeData:
+      timeToLiveInMinutes: 1440000
+      maxSize: 500
 
 redis:
   # standalone or cluster

File diff suppressed because it is too large
+ 1704 - 0
application/src/main/resources/templates/事件报告.ftl