Sfoglia il codice sorgente

1.添加异常信息回传

wangtao 3 anni fa
parent
commit
721eebdfda

+ 6 - 1
ipfcst-client/pom.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
@@ -79,6 +79,11 @@
             <version>RELEASE</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-collections4</artifactId>
+            <version>4.4</version>
+        </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>

+ 1 - 1
ipfcst-client/src/main/java/com/jiayue/client/job/SendDataToDcJob.java

@@ -60,7 +60,7 @@ public class SendDataToDcJob {
     /**
      * 每天凌晨2点执行
      */
-    @Scheduled(cron = "0 0/5 * * * ?")
+    @Scheduled(cron = "10 1 2 * * ?")
     public void sendJob() {
         log.info("时间 【" + System.currentTimeMillis() + "】 向数据中心发送青海集中功率预测前一天的实际功率等数据。数据范围:【{}--{}】", getDayStartTime(), getDayLastTime());
         sendDataService.send(getDayStartTime(), getDayLastTime(), "all");

+ 87 - 0
ipfcst-client/src/main/java/com/jiayue/client/job/SendWeChartMsgJob.java

@@ -0,0 +1,87 @@
+package com.jiayue.client.job;
+
+import cn.hutool.core.codec.Base64;
+import cn.hutool.core.convert.Convert;
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.core.util.ZipUtil;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONUtil;
+import com.jiayue.client.service.QueryDataService;
+import com.jiayue.client.util.Constants;
+import com.jiayue.client.util.MD5Util;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
+import org.springframework.core.io.PathResource;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+
+/**
+ * 向微信发送警告通知
+ */
+@Service
+@Slf4j
+public class SendWeChartMsgJob {
+    protected static Properties properties;
+
+    static {
+        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
+        yaml.setResources(new PathResource("./focus-client.yml"));
+        properties = yaml.getObject();
+    }
+
+    public final QueryDataService queryDataService;
+
+    public SendWeChartMsgJob(QueryDataService queryDataService) {
+        this.queryDataService = queryDataService;
+    }
+
+    /*
+     * 将时间戳转换为时间
+     * @param s
+     */
+    public static String stampToDateStr(String s) {
+        String res;
+        long lt = new Long(s);
+        Date date = new Date(lt);
+        res = Constants.formatter.format(date);
+        return res;
+    }
+
+    /**
+     * 每天凌晨2点执行
+     */
+    @Scheduled(cron = "50 0/5 * * * ?")
+    public void sendJob() {
+        log.info("时间 {} 公众号推送异常消息", System.currentTimeMillis());
+        List<Map<String, Object>> testList = queryDataService.getWarnigMsg();
+        List<Map<String, Object>> valueList = new ArrayList<>();
+        for (Map<String, Object> strInfo : testList) {
+            Map<String, Object> valueMap = new HashMap<>();
+            valueMap.put("info", strInfo.get("C_NAME"));
+            valueMap.put("stationCode", strInfo.get("C_STATION_CODE"));
+            valueMap.put("createTime", stampToDateStr(Convert.toDate(strInfo.get("C_CREATE_TIME")).getTime() + ""));
+            valueList.add(valueMap);
+        }
+        String reqBodyStr = JSONUtil.toJsonStr(valueList);
+        Map<String, Object> resultMap = new HashMap<>();
+        resultMap.put("type", "warningMsg");
+        resultMap.put("body", reqBodyStr);
+        String sign = MD5Util.encode(reqBodyStr + "Syjy*3377", "MD5");
+        resultMap.put("sign", sign);
+
+        String reqStr = JSONUtil.toJsonStr(resultMap);
+        log.debug("回传数据给数据中心,请求原报文:{}", reqStr);
+
+        Map<String, Object> reqMap = new HashMap<>();
+        reqMap.put("reqMsg", Base64.encode(ZipUtil.gzip(reqStr, CharsetUtil.CHARSET_UTF_8.name())));
+        log.debug("回传数据给数据中心,压缩加密后报文:{}", reqMap);
+        String resp = HttpUtil.post(properties.getProperty("remote.dcUrl"), reqMap);
+        if (resp.length() > 0 && (0 == JSONUtil.parseObj(resp).getInt("code"))) {
+            log.info("回传警告数据给数据中心成功,获得响应报文:{}", resp);
+        } else {
+            log.error("回传警告数据给数据中心错误:{}", resp);
+        }
+    }
+}

+ 18 - 0
ipfcst-client/src/main/java/com/jiayue/client/service/QueryDataService.java

@@ -1,11 +1,13 @@
 package com.jiayue.client.service;
 
+import com.jiayue.client.util.Constants;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Repository;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -183,4 +185,20 @@ public class QueryDataService {
         log.info("风机设备信息查询sql:{}", fjsSql);
         return jdbcTemplate.queryForList(fjsSql);
     }
+
+    /**
+     * 查询最近5分钟的告警信息
+     *
+     * @return
+     */
+    public List<Map<String, Object>> getWarnigMsg() {
+        Calendar date = Calendar.getInstance();
+        date.setTime(new Date());
+        Date dateEnd = date.getTime();
+        date.add(Calendar.MINUTE, -6);
+        String fjsSql = "SELECT C_NAME,C_STATION_CODE,C_CREATE_TIME " +
+                "from t_sys_alarm where C_CREATE_TIME between '" + Constants.formatter.format(date.getTime()) + "' and '" + Constants.formatter.format(dateEnd) + "'";
+        log.info("告警信息信息查询sql:{}", fjsSql);
+        return jdbcTemplate.queryForList(fjsSql);
+    }
 }

+ 1 - 2
ipfcst-client/src/main/java/com/jiayue/client/service/SendDataService.java

@@ -321,13 +321,12 @@ public class SendDataService {
         Map<String, Object> reqMap = new HashMap<>();
         reqMap.put("reqMsg", Base64.encode(ZipUtil.gzip(reqStr, CharsetUtil.CHARSET_UTF_8.name())));
         log.debug("回传数据给数据中心,压缩加密后报文:{}", reqMap);
-        String resp = HttpUtil.get(properties.getProperty("remote.dcUrl"), reqMap);
+        String resp = HttpUtil.post(properties.getProperty("remote.dcUrl"), reqMap);
         if (resp.length() > 0 && (0 == JSONUtil.parseObj(resp).getInt("code"))) {
             log.info("回传数据给数据中心成功,获得响应报文:{}", resp);
         } else {
             log.error("回传数据给数据中心错误:{}", resp);
         }
-
     }
 
 }

+ 4 - 0
ipfcst-client/src/main/java/com/jiayue/client/util/Constants.java

@@ -1,5 +1,7 @@
 package com.jiayue.client.util;
 
+import java.text.SimpleDateFormat;
+
 public class Constants {
     public static final String C_STATION_CODE_FIELD = "C_STATION_CODE";
     public static final String C_ABLE_VALUE_FIELD = "C_ABLE_VALUE";
@@ -10,4 +12,6 @@ public class Constants {
     public static final String C_EQUIPMENT_NO_FIELD = "C_EQUIPMENT_NO";
     public static final String stationCode_FIELD = "stationCode";
     public static final String C_TIME_FIELD = "C_TIME";
+
+    public static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 }

+ 4 - 0
ipfcst-client/src/main/resources/application.yml

@@ -1,5 +1,6 @@
 server:
   port: 81
+  max-http-header-size: 102400
 spring:
   thymeleaf:
     mode: HTML5
@@ -17,8 +18,11 @@ spring:
     #mysql
   datasource:
     url: jdbc:mysql://192.168.35.43:3306/focus?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&autoReconnect=true&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai
+    #url: jdbc:mysql://192.168.1.205:3306/focus?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&autoReconnect=true&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai
     username: root
     password: '!QAZ2root'
+  hikari:
+    maxLifetime: 0
 logging:
   config: classpath:logback-spring.xml
 

+ 13 - 0
ipfcst-client/src/test/java/com/jiayue/client/IpfcstClientApplicationTests.java

@@ -0,0 +1,13 @@
+package com.jiayue.client;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class IpfcstClientApplicationTests {
+
+    @Test
+    void contextLoads() {
+    }
+
+}

+ 62 - 12
ipfcst-client/src/test/java/com/jiayue/client/service/QueryDataServiceTest.java

@@ -1,6 +1,14 @@
 package com.jiayue.client.service;
 
+import cn.hutool.core.codec.Base64;
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.core.util.ZipUtil;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONUtil;
 import com.jiayue.client.IpfcstClientApplication;
+import com.jiayue.client.util.Constants;
+import com.jiayue.client.util.MD5Util;
+import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -8,9 +16,9 @@ import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
+@Slf4j
 @RunWith(SpringJUnit4ClassRunner.class)
 @SpringBootTest(classes = IpfcstClientApplication.class, properties = "spring.config.location=classpath:/application.yml")
 public class QueryDataServiceTest {
@@ -20,18 +28,60 @@ public class QueryDataServiceTest {
     @Autowired
     private QueryDataService queryDataService;
 
+    /*
+     * 将时间戳转换为时间
+     * @param s
+     */
+    public static String stampToDateStr(String s) {
+        String res;
+        long lt = new Long(s);
+        Date date = new Date(lt);
+        res = Constants.formatter.format(date);
+        return res;
+    }
+
     @Test
     public void test() {
-        String starTime = "2021-01-01 12:00:21";
-        String endTime = "2022-01-01 12:00:21";
-        //List<Map<String, Object>> testList = queryDataService.getRps(starTime, endTime, "T00003");
-        // List<Map<String, Object>> testList = queryDataService.getDqs(starTime, endTime, "T00003");
-        //List<Map<String, Object>> testList = queryDataService.getCdqs(starTime, endTime, "1");
-        //List<Map<String, Object>> testList = queryDataService.getCfts(starTime, endTime, "6");
-        // List<Map<String, Object>> testList = queryDataService.getFjs(starTime, endTime, "1");
-        List<Map<String, Object>> testList = queryDataService.getWeatherds(starTime, endTime, "7");
-
-        System.out.println(testList.size());
+        log.info("时间 {} 公众号推送异常消息", System.currentTimeMillis());
+        List<Map<String, Object>> testList = queryDataService.getWarnigMsg();
+        List<Map<String, Object>> valueList = new ArrayList<>();
+        for (Map<String, Object> strInfo : testList) {
+            Map<String, Object> valueMap = new HashMap<>();
+            valueMap.put("info", strInfo.get("C_NAME"));
+            valueMap.put("stationCode", strInfo.get("C_STATION_CODE"));
+            // valueMap.put("createTime", WechatMsgUtils.stampToDateStr(Convert.toDate(strInfo.get("C_CREATE_TIME")).getTime() + ""));
+            valueList.add(valueMap);
+        }
+        String reqBodyStr = JSONUtil.toJsonStr(valueList);
+        Map<String, Object> resultMap = new HashMap<>();
+        resultMap.put("type", "warningMsg");
+        resultMap.put("body", reqBodyStr);
+        String sign = MD5Util.encode(reqBodyStr + "Syjy*3377", "MD5");
+        resultMap.put("sign", sign);
+
+        String reqStr = JSONUtil.toJsonStr(resultMap);
+        log.debug("回传数据给数据中心,请求原报文:{}", reqStr);
+
+        Map<String, Object> reqMap = new HashMap<>();
+        reqMap.put("reqMsg", Base64.encode(ZipUtil.gzip(reqStr, CharsetUtil.CHARSET_UTF_8.name())));
+        log.debug("回传数据给数据中心,压缩加密后报文:{}", reqMap);
+        String resp = HttpUtil.post("http://127.0.0.1:9001/qinghaiComeBackApi/datas", reqMap);
+        if (resp.length() > 0 && (0 == JSONUtil.parseObj(resp).getInt("code"))) {
+            log.info("回传警告数据给数据中心成功,获得响应报文:{}", resp);
+        } else {
+            log.error("回传警告数据给数据中心错误:{}", resp);
+        }
+    }
+
+    /**
+     * 获取当前时间
+     *
+     * @return
+     */
+    public String getCurentTime() {
+        Calendar date = Calendar.getInstance();
+        date.setTime(new Date());
+        return Constants.formatter.format(date.getTime());
     }
 
     public List<Map<String, Object>> getRp(String startTime, String endTime, String stationCode) {