648540858
2022-09-09 221f99c764b51c3cd284c6e5a41492c26c5c7ffc
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java
@@ -1,20 +1,22 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd;
import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.IMessageHandler;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.NotifyMessageHandler;
import com.genersoft.iot.vmp.service.IDeviceService;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
import com.genersoft.iot.vmp.utils.DateUtil;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.sip.InvalidArgumentException;
@@ -24,23 +26,20 @@
import javax.sip.message.Response;
import java.text.ParseException;
/**
 * 状态信息(心跳)报送
 */
@Component
public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
    private Logger logger = LoggerFactory.getLogger(KeepaliveNotifyMessageHandler.class);
    private final String cmdType = "Keepalive";
    private final static String cmdType = "Keepalive";
    @Autowired
    private NotifyMessageHandler notifyMessageHandler;
    @Autowired
    private EventPublisher publisher;
    @Autowired
    private IVideoManagerStorage videoManagerStorager;
    @Autowired
    private IRedisCatchStorage redisCatchStorage;
    private IDeviceService deviceService;
    @Override
    public void afterPropertiesSet() throws Exception {
@@ -49,29 +48,34 @@
    @Override
    public void handForDevice(RequestEvent evt, Device device, Element element) {
        // 检查设备是否存在并在线, 不在线则设置为在线
        if (device == null) {
            // 未注册的设备不做处理
            return;
        }
        try {
            if (device != null ) {
                // 回复200 OK
                responseAck(evt, Response.OK);
                // 判断RPort是否改变,改变则说明路由nat信息变化,修改设备信息
                // 获取到通信地址等信息
                ViaHeader viaHeader = (ViaHeader) evt.getRequest().getHeader(ViaHeader.NAME);
                String received = viaHeader.getReceived();
                int rPort = viaHeader.getRPort();
                // 解析本地地址替代
                if (StringUtils.isEmpty(received) || rPort == -1) {
                    received = viaHeader.getHost();
                    rPort = viaHeader.getPort();
                }
                if (device.getPort() != rPort) {
                    device.setPort(rPort);
                    device.setHostAddress(received.concat(":").concat(String.valueOf(rPort)));
                    videoManagerStorager.updateDevice(device);
                    redisCatchStorage.updateDevice(device);
                }
                if (!redisCatchStorage.deviceIsOnline(device.getDeviceId())) {
                    publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
            // 判断RPort是否改变,改变则说明路由nat信息变化,修改设备信息
            // 获取到通信地址等信息
            ViaHeader viaHeader = (ViaHeader) evt.getRequest().getHeader(ViaHeader.NAME);
            String received = viaHeader.getReceived();
            int rPort = viaHeader.getRPort();
            // 解析本地地址替代
            if (ObjectUtils.isEmpty(received) || rPort == -1) {
                received = viaHeader.getHost();
                rPort = viaHeader.getPort();
            }
            if (device.getPort() != rPort) {
                device.setPort(rPort);
                device.setHostAddress(received.concat(":").concat(String.valueOf(rPort)));
            }
            device.setKeepaliveTime(DateUtil.getNow());
            // 回复200 OK
            responseAck(evt, Response.OK);
            if (device.getOnline() == 1) {
                deviceService.updateDevice(device);
            }else {
                // 对于已经离线的设备判断他的注册是否已经过期
                if (!deviceService.expire(device)){
                    deviceService.online(device);
                }
            }
        } catch (SipException e) {