648540858
2022-02-24 a42dda2bd3cc1cf8c20cc61e7ad9211eadecbaf3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.genersoft.iot.vmp.gb28181.event.platformKeepaliveExpire;
 
import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
 
import javax.sip.ResponseEvent;
import javax.sip.message.Response;
 
/**
 * @description: 平台心跳超时事件
 * @author: panll
 * @date: 2020年11月5日 10:00
 */
@Component
public class PlatformKeepaliveExpireEventLister implements ApplicationListener<PlatformKeepaliveExpireEvent> {
 
 
    private final static Logger logger = LoggerFactory.getLogger(PlatformKeepaliveExpireEventLister.class);
 
    @Autowired
    private IVideoManagerStorager storager;
 
    @Autowired
    private IRedisCatchStorage redisCatchStorage;
 
    @Autowired
    private ISIPCommanderForPlatform sipCommanderForPlatform;
 
    @Autowired
    private SipSubscribe sipSubscribe;
 
    @Autowired
    private EventPublisher publisher;
 
    @Override
    public void onApplicationEvent(@NotNull PlatformKeepaliveExpireEvent event) {
 
        if (logger.isDebugEnabled()) {
            logger.debug("平台心跳到期事件事件触发,平台国标ID:" + event.getPlatformGbID());
        }
        ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(event.getPlatformGbID());
        ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(event.getPlatformGbID());
        if (parentPlatformCatch == null) {
            return;
        }
        if (parentPlatform == null) {
            logger.debug("平台心跳到期事件事件触发,但平台已经删除!!! 平台国标ID:" + event.getPlatformGbID());
            return;
        }
        parentPlatformCatch.setParentPlatform(parentPlatform);
        // 发送心跳
        if (parentPlatformCatch.getKeepAliveReply() >= 3) {
            // 有3次未收到心跳回复, 设置平台状态为离线, 开始重新注册
            logger.warn("有3次未收到心跳回复,标记设置平台状态为离线, 并重新注册 平台国标ID:" + event.getPlatformGbID());
            storager.updateParentPlatformStatus(event.getPlatformGbID(), false);
            publisher.platformNotRegisterEventPublish(event.getPlatformGbID());
            parentPlatformCatch.setKeepAliveReply(0);
            redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
        }else {
            // 再次发送心跳
            String callId = sipCommanderForPlatform.keepalive(parentPlatform);
 
            parentPlatformCatch.setKeepAliveReply( parentPlatformCatch.getKeepAliveReply() + 1);
            // 存储心跳信息, 并设置状态为未回复, 如果多次过期仍未收到回复,则认为上级平台已经离线
            redisCatchStorage.updatePlatformKeepalive(parentPlatform);
            redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
 
            sipSubscribe.addOkSubscribe(callId, (SipSubscribe.EventResult eventResult) ->{
                if (eventResult.statusCode == Response.OK) {
                    // 收到心跳响应信息,
                    parentPlatformCatch.setKeepAliveReply(0);
                    redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
                }
            } );
        }
    }
}