liusuyi
2026-06-01 a2e7e8ff9cfaa69b001d483710bddbda50d55c91
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.ard.gb28181.transmit.event.request.impl.message.response.cmd;
 
import com.ard.gb28181.api.domain.Device;
import com.ard.gb28181.domain.GbDevice;
import com.ard.gb28181.service.IDeviceService;
import com.ard.gb28181.service.IGbDeviceService;
import com.ard.gb28181.transmit.event.request.SIPRequestProcessorParent;
import com.ard.gb28181.transmit.event.request.impl.message.IMessageHandler;
import com.ard.gb28181.transmit.event.request.impl.message.response.ResponseMessageHandler;
import com.ard.gb28181.api.utils.XmlUtil;
import gov.nist.javax.sip.message.SIPRequest;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
 
import javax.sip.InvalidArgumentException;
import javax.sip.RequestEvent;
import javax.sip.SipException;
import javax.sip.message.Response;
import java.text.ParseException;
 
/**
 * @author lin
 */
@Slf4j
@Component
public class DeviceInfoResponseMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
 
    private final String cmdType = "DeviceInfo";
 
    @Autowired
    private ResponseMessageHandler responseMessageHandler;
 
 
    @Autowired
    private IDeviceService deviceService;
 
    @Autowired
    private IGbDeviceService gbDeviceService;
 
    @Override
    public void afterPropertiesSet() throws Exception {
        responseMessageHandler.addHandler(cmdType, this);
    }
 
    @Override
    public void handForDevice(RequestEvent evt, Device device, Element rootElement) {
        log.debug("接收到DeviceInfo应答消息");
        // 检查设备是否存在, 不存在则不回复
        if (device == null || !device.isOnLine()) {
            log.warn("[接收到DeviceInfo应答消息,但是设备已经离线]:" + (device != null ? device.getDeviceId() : ""));
            return;
        }
        SIPRequest request = (SIPRequest) evt.getRequest();
        try {
            rootElement = getRootElement(evt, device.getCharset());
 
            if (rootElement == null) {
                log.warn("[ 接收到DeviceInfo应答消息 ] content cannot be null, {}", evt.getRequest());
                try {
                    responseAck((SIPRequest) evt.getRequest(), Response.BAD_REQUEST);
                } catch (SipException | InvalidArgumentException | ParseException e) {
                    log.error("[命令发送失败] DeviceInfo应答消息 BAD_REQUEST: {}", e.getMessage());
                }
                return;
            }
            device.setName(XmlUtil.getText(rootElement, "DeviceName"));
 
            device.setManufacturer(XmlUtil.getText(rootElement, "Manufacturer"));
            device.setModel(XmlUtil.getText(rootElement, "Model"));
            device.setFirmware(XmlUtil.getText(rootElement, "Firmware"));
            if (ObjectUtils.isEmpty(device.getStreamMode())) {
                device.setStreamMode("TCP-PASSIVE");
            }
            deviceService.updateDevice(device);
            // 同步更新 ard_gb_device 表中的设备名称、IP、端口、厂商、型号、固件
            try {
                GbDevice gbDevice = gbDeviceService.selectGbDeviceByGbDeviceId(device.getDeviceId());
                if (gbDevice != null) {
                    boolean needUpdate = false;
                    if (device.getName() != null && !device.getName().equals(gbDevice.getDeviceName())) {
                        gbDevice.setDeviceName(device.getName());
                        needUpdate = true;
                    }
                    if (device.getIp() != null && !device.getIp().equals(gbDevice.getIp())) {
                        gbDevice.setIp(device.getIp());
                        needUpdate = true;
                    }
                    if (gbDevice.getPort() == null || device.getPort() != gbDevice.getPort()) {
                        gbDevice.setPort(device.getPort());
                        needUpdate = true;
                    }
                    if (device.getManufacturer() != null && !device.getManufacturer().equals(gbDevice.getManufacturer())) {
                        gbDevice.setManufacturer(device.getManufacturer());
                        needUpdate = true;
                    }
                    if (device.getModel() != null && !device.getModel().equals(gbDevice.getModel())) {
                        gbDevice.setModel(device.getModel());
                        needUpdate = true;
                    }
                    if (device.getFirmware() != null && !device.getFirmware().equals(gbDevice.getFirmware())) {
                        gbDevice.setFirmware(device.getFirmware());
                        needUpdate = true;
                    }
                    if (needUpdate) {
                        gbDeviceService.updateGbDevice(gbDevice);
                        log.info("[DeviceInfo] 更新设备信息: {} -> name={}, ip={}, port={}, manufacturer={}, model={}, firmware={}",
                                device.getDeviceId(), device.getName(), device.getIp(), device.getPort(),
                                device.getManufacturer(), device.getModel(), device.getFirmware());
                    }
                }
            } catch (Exception e) {
                log.error("[DeviceInfo] 更新GbDevice信息失败: {}", device.getDeviceId(), e);
            }
            responseMessageHandler.handMessageEvent(rootElement, device);
 
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        try {
            // 回复200 OK
            responseAck(request, Response.OK);
        } catch (SipException | InvalidArgumentException | ParseException e) {
            log.error("[命令发送失败] DeviceInfo应答消息 200: {}", e.getMessage());
        }
 
    }
}