Prechádzať zdrojové kódy

10分钟在数据库保存一次心跳信号。

zhengqiang 5 rokov pred
rodič
commit
b28cb11ad7

+ 2 - 0
common/src/main/java/com/jpsoft/smart/modules/lapi/channel/LapiAttrKeys.java

@@ -1,7 +1,9 @@
 package com.jpsoft.smart.modules.lapi.channel;
 
 import io.netty.util.AttributeKey;
+import org.joda.time.DateTime;
 
 public class LapiAttrKeys {
     public static final AttributeKey<String> DEVICE_NO = AttributeKey.newInstance("deviceNo");
+    public static final AttributeKey<DateTime> LAST_HEART_TIME = AttributeKey.newInstance("lastHearTime");
 }

+ 12 - 12
lapi/pom.xml

@@ -38,16 +38,16 @@
             <version>1.0.0</version>
         </dependency>
     </dependencies>
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.springframework.boot</groupId>
-                <artifactId>spring-boot-maven-plugin</artifactId>
-                <configuration>
-                    <fork>true</fork>
-                    <addResources>true</addResources>
-                </configuration>
-            </plugin>
-        </plugins>
-    </build>
+<!--    <build>-->
+<!--        <plugins>-->
+<!--            <plugin>-->
+<!--                <groupId>org.springframework.boot</groupId>-->
+<!--                <artifactId>spring-boot-maven-plugin</artifactId>-->
+<!--                <configuration>-->
+<!--                    <fork>true</fork>-->
+<!--                    <addResources>true</addResources>-->
+<!--                </configuration>-->
+<!--            </plugin>-->
+<!--        </plugins>-->
+<!--    </build>-->
 </project>

+ 31 - 9
lapi/src/main/java/com/jpsoft/smart/lapi/handler/HeartReportHandler.java

@@ -10,6 +10,8 @@ import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.SimpleChannelInboundHandler;
 import lombok.extern.slf4j.Slf4j;
+import org.joda.time.DateTime;
+import org.joda.time.Minutes;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -41,16 +43,36 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
         log.warn("收到设备心跳,deviceNo=" + deviceNo);
         log.warn("ip =="+ip);
 
-        SysLog sysLog = new SysLog();
-        sysLog.setPointcut(deviceNo);
-        sysLog.setUrl("/LAPI/V1.0/PACS/Controller/HeartReportInfo");
-        sysLog.setData(heartReportInfo.toString());
-        sysLog.setRemark("收到设备心跳");
-        SocketAddress remoteAddress = ctx.channel().remoteAddress();
-        sysLog.setRemoteIp(remoteAddress.toString());
-        sysLog.setCreateTime(new Date());
+        DateTime lastHeartTime = ctx.channel().attr(LapiAttrKeys.LAST_HEART_TIME).get();
 
-        sysLogService.insert(sysLog);
+        boolean needWrite = false;
+
+        if(lastHeartTime==null){
+            ctx.channel().attr(LapiAttrKeys.LAST_HEART_TIME).set(DateTime.now());
+            needWrite = true;
+        }
+        else{
+            int minutes = Minutes.minutesBetween(lastHeartTime,DateTime.now()).getMinutes();
+
+            if(minutes>=10){
+                ctx.channel().attr(LapiAttrKeys.LAST_HEART_TIME).set(DateTime.now());
+                needWrite = true;
+            }
+        }
+
+        if(needWrite) {
+            new Thread(()->{
+                SysLog sysLog = new SysLog();
+                sysLog.setPointcut(deviceNo);
+                sysLog.setUrl("/LAPI/V1.0/PACS/Controller/HeartReportInfo");
+                sysLog.setData(heartReportInfo.toString());
+                sysLog.setRemark("收到设备心跳");
+                SocketAddress remoteAddress = ctx.channel().remoteAddress();
+                sysLog.setRemoteIp(remoteAddress.toString());
+                sysLog.setCreateTime(new Date());
+                sysLogService.insert(sysLog);
+            }).start();
+        }
 
         deviceInfoService.updateByDeviceNo(deviceNo,ip);