Kaynağa Gözat

优化电度
1:新增电度记录时,判断当前小时内是否以及采集过
2:发送电度召唤时,判断需要采集的电度总数是否和当前小时内已采集数量相等,不等才发送召唤

hfxc226 2 yıl önce
ebeveyn
işleme
fcdb754962

+ 1 - 1
platform-iec/pom.xml

@@ -28,7 +28,7 @@
         </dependency>
         <dependency>
             <groupId>com.platform</groupId>
-            <artifactId>platform-common</artifactId>
+            <artifactId>platform-dao</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
         <dependency>

+ 4 - 1
platform-iec/src/main/java/com/platform/iec/IecApplication.java

@@ -1,5 +1,6 @@
 package com.platform.iec;
 
+import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
 import com.platform.iec.master.MasterSysDataHandler;
 import com.ydl.iec.iec104.config.Iec104Config;
 import com.ydl.iec.iec104.server.Iec104MasterFactory;
@@ -8,6 +9,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.core.annotation.Order;
 import org.springframework.scheduling.annotation.EnableScheduling;
 
@@ -19,7 +21,8 @@ import java.util.TimeZone;
  * @Date 2019/7/22
  * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
  */
-@SpringBootApplication
+@SpringBootApplication(scanBasePackages = {"com.platform.common","com.platform.dao", "com.platform.iec"}, exclude = {
+        DataSourceAutoConfiguration.class, PageHelperAutoConfiguration.class})
 @EnableScheduling
 @Order(1)
 @Slf4j

+ 27 - 12
platform-iec/src/main/java/com/ydl/iec/iec104/core/ScheduledTaskPool.java

@@ -4,14 +4,19 @@ import com.alibaba.fastjson.JSON;
 import com.ydl.iec.iec104.common.BasicInstruction104;
 import com.ydl.iec.iec104.message.MessageDetail;
 import com.ydl.iec.util.ByteUtil;
+import com.ydl.iec.util.MySqlUtil;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
 import lombok.extern.slf4j.Slf4j;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.util.CollectionUtils;
 
+import java.time.LocalDateTime;
 import java.time.LocalTime;
+import java.util.ArrayList;
+import java.util.List;
 
 
 /**
@@ -54,7 +59,7 @@ public class ScheduledTaskPool {
     /**
      * 记录每小时是否发送过电度总召唤-累计数量,超过十次没响应后,需要重启监听
      */
-    private int sendDegreeCallNum = 0;
+    private int sendDegreeCallNum = 1;
     /**
      * 发送总召唤指令状态
      */
@@ -185,15 +190,23 @@ public class ScheduledTaskPool {
                     while (sendTestStatus) {
                         try {
                             // ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104());
-                            Thread.sleep(1000 * 60 * 2);// 2分钟判断一次是否发送一次电度总召唤,确保一个小时必须发一次
+                            Thread.sleep(1000 * 60 * 5);// 5分钟判断一次是否发送一次电度总召唤,确保一个小时所有点位数据都获取了
                             // 判断是否需要发送电度
-                            if (isNeedSendDegree()) {
-                                if (sendDegreeCallNum == 0) {
-                                    LOGGER.info("电度召唤指令,并累加" + sendDegreeCallNum++);
+                            int count = MySqlUtil.selectCountDegree();
+                            LocalDateTime time = LocalDateTime.now();
+                            int year = time.getYear();
+                            int month = time.getMonthValue();
+                            int day = time.getDayOfMonth();
+                            int hour = time.getHour();
+                            List<Integer> list = MySqlUtil.selectExistDegree(year,month, day, hour,null);
+                            if(!CollectionUtils.isEmpty(list)){
+                                if(list.size() < count){
+                                    LOGGER.info("电度已采集数量:" + list.size() + ", 总电度数量:" + count);
                                     ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104Degree());
-                                } else {
-                                    LOGGER.info("电度召唤指令该小时内,已经发送,不再发送");
                                 }
+                            }else{
+                                LOGGER.info("电度已采集数量:0" + ", 总电度数量:" + count);
+                                ctx.channel().writeAndFlush(BasicInstruction104.getGeneralCallRuleDetail104Degree());
                             }
                         } catch (Exception e) {
                             e.printStackTrace();
@@ -212,18 +225,20 @@ public class ScheduledTaskPool {
      *
      * @return
      */
-    private boolean isNeedSendDegree() {
+    /*private boolean isNeedSendDegree() {
+        LOGGER.error("count: " + count);
+        LOGGER.error("degreeList.size(): " + degreeList.size());
         LocalTime time = LocalTime.now();
         int minute = time.getMinute();
         System.out.println(minute);
-        if (minute >= 3 && minute < 10) {
+        if (minute >= 3 && minute < 50 && count>degreeList.size()) {
             return true;
         }
-        if (minute >= 10) {
-            sendDegreeCallNum = 0;
+        if (minute >= 50) {
+            degreeList.clear();
         }
         return false;
-    }
+    }*/
 
     public static void main(String[] args) {
         // MessageDetail messageDetail = BasicInstruction104.getGeneralCallRuleDetail104();

+ 111 - 13
platform-iec/src/main/java/com/ydl/iec/util/MySqlUtil.java

@@ -7,6 +7,9 @@ import com.platform.common.util.DateUtils;
 import com.platform.common.util.IdGeneratorUtils;
 import com.platform.common.util.SecurityUtils;
 import com.platform.common.util.StringUtils;
+import com.platform.dao.vo.query.qykh.HotspotHelpVO;
+import com.platform.dao.vo.sb.SbInfoScreenDetailVO;
+import com.ydl.iec.iec104.core.Iec104ThreadLocal;
 import com.ydl.iec.iec104.message.MessageDetail;
 import com.ydl.iec.iec104.message.MessageInfo;
 import lombok.extern.slf4j.Slf4j;
@@ -16,27 +19,25 @@ import org.springframework.util.CollectionUtils;
 import java.math.BigDecimal;
 import java.sql.*;
 import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @Slf4j
 public class MySqlUtil {
     //是否连接数据库
     public static String mysql_open = "false";
     //数据库连接地址
-    //public static String mysql_url = "jdbc:mysql://123.60.19.203:5006/hitch-sb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&allowMultiQueries=true";
+    public static String mysql_url = "jdbc:mysql://123.60.19.203:5006/hitch-sb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&allowMultiQueries=true";
     //用户名
-    //public static String mysql_username = "root";
+    public static String mysql_username = "root";
     //密码
-    //public static String mysql_password = "mysql?MYSQLmoniu123";
+    public static String mysql_password = "mysql?MYSQLmoniu123";
     //数据库连接地址
-    public static String mysql_url = "jdbc:mysql://192.168.16.222:3306/hitch-sb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&allowMultiQueries=true";
+    //public static String mysql_url = "jdbc:mysql://192.168.16.222:3306/hitch-sb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&allowMultiQueries=true";
     //用户名
-    public static String mysql_username = "root";
+    //public static String mysql_username = "root";
     //密码
-    public static String mysql_password = "mydm888";
+    //public static String mysql_password = "mydm888";
     //驱动名称
     public static String mysql_driver = "com.mysql.cj.jdbc.Driver";
     // 采集数据,结构:遥测最低,遥测最高;电度最低,电度最高
@@ -76,7 +77,7 @@ public class MySqlUtil {
      * @param ruleDetail104
      */
     public static void dealMessage(MessageDetail ruleDetail104) {
-        log.info("处理数据中……………………");
+        // log.info("处理数据中……………………");
         LocalDateTime time = LocalDateTime.now();
         int year = time.getYear();
         int month = time.getMonthValue();
@@ -159,6 +160,90 @@ public class MySqlUtil {
         }
     }
 
+    /**
+     * 查询电度数量
+     */
+    public static int selectCountDegree() {
+        PreparedStatement ps = null;
+        int count = 0;
+        try {
+            conn = getConnection();
+            String selectSql = "select count(*) from t_remote_degree";
+            ps = conn.prepareStatement(selectSql);
+            ResultSet rs = ps.executeQuery();
+            if (rs.next()) {
+                count = rs.getInt(1);
+            }
+            ps.close();
+            closeConn();
+            return count;
+        } catch (SQLException e1) {
+            e1.printStackTrace();
+            throw new DeniedException("获取乾元数据库连接失败" + e1.getCause().getMessage());
+        } finally {
+            try {
+                if (ps != null) {
+                    ps.close();
+                }
+                if (conn != null) {
+                    closeConn();
+                }
+            } catch (Exception e) {
+
+            }
+        }
+    }
+
+    /**
+     * 查询电度在当前小时内是否存在
+     */
+    public static List<Integer> selectExistDegree(int year, int month, int day, int hour, List<Integer> positionNumList) {
+        PreparedStatement ps = null;
+        try {
+            conn = getConnection();
+            StringBuffer selectSql = new StringBuffer("select position_num from t_remote_degree_log where ");
+            if(CollectionUtils.isEmpty(positionNumList)){
+                selectSql.append(" year = " + year);
+            }else{
+                selectSql.append(" position_num in ( ");
+                for (int i = 0; i < positionNumList.size(); i++) {
+                    if (i == positionNumList.size() - 1) {
+                        selectSql.append(positionNumList.get(i) + ") ");
+                    } else {
+                        selectSql.append(positionNumList.get(i) + ", ");
+                    }
+                }
+                selectSql.append(" and year = " + year);
+            }
+            selectSql.append(" and month = " + month);
+            selectSql.append(" and day = " + day);
+            selectSql.append(" and hour = " + hour);
+            System.out.println(selectSql.toString());
+            ps = conn.prepareStatement(selectSql.toString());
+            ResultSet rs = ps.executeQuery();
+            List<Integer> list = new ArrayList<>();
+            while (rs.next()) {
+                list.add(rs.getInt(1));
+            }
+            ps.close();
+            closeConn();
+            return list;
+        } catch (SQLException e1) {
+            e1.printStackTrace();
+            throw new DeniedException("获取乾元数据库连接失败" + e1.getCause().getMessage());
+        } finally {
+            try {
+                if (ps != null) {
+                    ps.close();
+                }
+                if (conn != null) {
+                    closeConn();
+                }
+            } catch (Exception e) {
+
+            }
+        }
+    }
 
     /**
      * 记录电度数据-每一条新增保存
@@ -171,6 +256,7 @@ public class MySqlUtil {
      * @param hour
      */
     public static void addBatch(List<MessageInfo> list, LocalDateTime time, Integer year, Integer month, Integer day, Integer hour, Integer minute) {
+        List<Integer> checkList = selectExistDegree(year, month, day, hour, list.stream().map(MessageInfo::getMessageAddress).collect(Collectors.toList()));
         PreparedStatement ps = null;
         try {
             String sql = "insert into t_remote_degree_log (id,position_num,year,month,day,hour,minute,result,info,created_time,created_user_id) values (?,?,?,?,?,?,?,?,?,?,?)";
@@ -178,6 +264,10 @@ public class MySqlUtil {
             ps = conn.prepareStatement(sql);
             for (int i = 0; i < list.size(); i++) {
                 MessageInfo messageInfo = list.get(i);
+                if (checkList.contains(messageInfo.getMessageAddress())) {
+                    log.info("点位已存在,不处理。" + year + ", " + month + ", " + day + ", " + hour + ", " + messageInfo.getMessageAddress());
+                    continue;
+                }
                 ps.setObject(1, IdGeneratorUtils.getObjectId());
                 ps.setObject(2, messageInfo.getMessageAddress());
                 ps.setObject(3, year);
@@ -300,8 +390,16 @@ public class MySqlUtil {
     }
 
     public static void main(String[] args) {
-        String str = "68FA020002000D1E140001000140000000000000024000000000000003400000000000000440000000000000054000000000000006400000000000000740000000000000084000000000000009400000000000000A400000000000000B400000000000000C400000000000000D400000000000000E400000000000000F4000000000000010400000000000001140000000000000124000000000000013400000000000001440000000000000154000000000000016400000000000001740000000000000184000000000000019400000000000001A400000000000001B400000000000001C400000000000001D400000000000001E40000000000000";
-        System.out.println(ByteUtil.testStr(str));
+        List<Integer> result = selectExistDegree(2022, 11, 25, 22, Arrays.asList(25601, 25602, 25603));
+        for (Integer ss : result) {
+            System.out.println("ss: " + ss);
+        }
+        System.out.println("result: " + result.contains(25601));
+        List<Integer> result1 = selectExistDegree(2022, 11, 25, 22, null);
+        for (Integer ss : result1) {
+            System.out.println("ss: " + ss);
+        }
+        System.out.println("result: " + result1.contains(25601));
     }
 
     private static float changeMeassInfosToFloat(byte[] bytes) {

+ 1 - 1
platform-iec/src/main/resources/application.yml

@@ -1,5 +1,5 @@
 spring:
   profiles:
-    active: daoDev,serviceDev,dev
+    active: daoDev,dev
   main:
     web-application-type: none