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());
|
}
|
|
}
|
}
|