From 363606d8453c027429ea66a6036c62b01ff8e06c Mon Sep 17 00:00:00 2001 From: 648540858 <456panlinlin> Date: 星期二, 26 四月 2022 18:29:47 +0800 Subject: [PATCH] 优化级联目录发送 --- src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java | 187 ++++++++++++++++++++++++++++++++++++---------- 1 files changed, 145 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java index 299799f..3fcf2fb 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java @@ -5,10 +5,7 @@ import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.conf.SipConfig; import com.genersoft.iot.vmp.conf.UserSetting; -import com.genersoft.iot.vmp.gb28181.bean.Device; -import com.genersoft.iot.vmp.gb28181.bean.InviteStreamCallback; -import com.genersoft.iot.vmp.gb28181.bean.InviteStreamInfo; -import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction; +import com.genersoft.iot.vmp.gb28181.bean.*; import com.genersoft.iot.vmp.gb28181.event.SipSubscribe; import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander; @@ -23,6 +20,7 @@ import com.genersoft.iot.vmp.storager.IVideoManagerStorage; import gov.nist.javax.sip.SipProviderImpl; import gov.nist.javax.sip.SipStackImpl; +import gov.nist.javax.sip.message.MessageFactoryImpl; import gov.nist.javax.sip.message.SIPRequest; import gov.nist.javax.sip.stack.SIPDialog; import org.slf4j.Logger; @@ -35,10 +33,7 @@ import javax.sip.*; import javax.sip.address.SipURI; -import javax.sip.header.CallIdHeader; -import javax.sip.header.ContentTypeHeader; -import javax.sip.header.ExpiresHeader; -import javax.sip.header.ViaHeader; +import javax.sip.header.*; import javax.sip.message.Request; import java.lang.reflect.Field; import java.text.ParseException; @@ -235,7 +230,7 @@ String cmdStr= cmdString(leftRight, upDown, inOut, moveSpeed, zoomSpeed); StringBuffer ptzXml = new StringBuffer(200); String charset = device.getCharset(); - ptzXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + ptzXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); ptzXml.append("<Control>\r\n"); ptzXml.append("<CmdType>DeviceControl</CmdType>\r\n"); ptzXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -278,7 +273,7 @@ logger.debug("鎺у埗瀛楃涓诧細" + cmdStr); StringBuffer ptzXml = new StringBuffer(200); String charset = device.getCharset(); - ptzXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + ptzXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); ptzXml.append("<Control>\r\n"); ptzXml.append("<CmdType>DeviceControl</CmdType>\r\n"); ptzXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -314,7 +309,7 @@ try { StringBuffer ptzXml = new StringBuffer(200); String charset = device.getCharset(); - ptzXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + ptzXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); ptzXml.append("<Control>\r\n"); ptzXml.append("<CmdType>DeviceControl</CmdType>\r\n"); ptzXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -778,7 +773,7 @@ try { StringBuffer broadcastXml = new StringBuffer(200); String charset = device.getCharset(); - broadcastXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + broadcastXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); broadcastXml.append("<Notify>\r\n"); broadcastXml.append("<CmdType>Broadcast</CmdType>\r\n"); broadcastXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -804,7 +799,7 @@ try { StringBuffer broadcastXml = new StringBuffer(200); String charset = device.getCharset(); - broadcastXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + broadcastXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); broadcastXml.append("<Notify>\r\n"); broadcastXml.append("<CmdType>Broadcast</CmdType>\r\n"); broadcastXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -837,7 +832,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Control>\r\n"); cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -873,7 +868,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Control>\r\n"); cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -906,7 +901,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Control>\r\n"); cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -938,7 +933,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Control>\r\n"); cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -983,7 +978,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Control>\r\n"); cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1022,7 +1017,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Control>\r\n"); cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1091,7 +1086,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Control>\r\n"); cmdXml.append("<CmdType>DeviceConfig</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1146,7 +1141,7 @@ try { String charset = device.getCharset(); StringBuffer catalogXml = new StringBuffer(200); - catalogXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + catalogXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); catalogXml.append("<Query>\r\n"); catalogXml.append("<CmdType>DeviceStatus</CmdType>\r\n"); catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1179,7 +1174,7 @@ try { StringBuffer catalogXml = new StringBuffer(200); String charset = device.getCharset(); - catalogXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + catalogXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); catalogXml.append("<Query>\r\n"); catalogXml.append("<CmdType>DeviceInfo</CmdType>\r\n"); catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1208,14 +1203,14 @@ * @param device 瑙嗛璁惧 */ @Override - public boolean catalogQuery(Device device, SipSubscribe.Event errorEvent) { + public boolean catalogQuery(Device device, int sn, SipSubscribe.Event errorEvent) { try { StringBuffer catalogXml = new StringBuffer(200); String charset = device.getCharset(); - catalogXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + catalogXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); catalogXml.append("<Query>\r\n"); catalogXml.append("<CmdType>Catalog</CmdType>\r\n"); - catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); + catalogXml.append("<SN>" + sn + "</SN>\r\n"); catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n"); catalogXml.append("</Query>\r\n"); @@ -1252,7 +1247,7 @@ try { StringBuffer recordInfoXml = new StringBuffer(200); String charset = device.getCharset(); - recordInfoXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + recordInfoXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); recordInfoXml.append("<Query>\r\n"); recordInfoXml.append("<CmdType>RecordInfo</CmdType>\r\n"); recordInfoXml.append("<SN>" + sn + "</SN>\r\n"); @@ -1306,7 +1301,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Query>\r\n"); cmdXml.append("<CmdType>Alarm</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1357,7 +1352,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Query>\r\n"); cmdXml.append("<CmdType>ConfigDownload</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1393,7 +1388,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Query>\r\n"); cmdXml.append("<CmdType>PresetQuery</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1428,7 +1423,7 @@ try { StringBuffer mobilePostitionXml = new StringBuffer(200); String charset = device.getCharset(); - mobilePostitionXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + mobilePostitionXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); mobilePostitionXml.append("<Query>\r\n"); mobilePostitionXml.append("<CmdType>MobilePosition</CmdType>\r\n"); mobilePostitionXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1462,7 +1457,7 @@ try { StringBuffer subscribePostitionXml = new StringBuffer(200); String charset = device.getCharset(); - subscribePostitionXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + subscribePostitionXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); subscribePostitionXml.append("<Query>\r\n"); subscribePostitionXml.append("<CmdType>MobilePosition</CmdType>\r\n"); subscribePostitionXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1513,7 +1508,7 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Query>\r\n"); cmdXml.append("<CmdType>Alarm</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); @@ -1559,24 +1554,35 @@ try { StringBuffer cmdXml = new StringBuffer(200); String charset = device.getCharset(); - cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + cmdXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); cmdXml.append("<Query>\r\n"); cmdXml.append("<CmdType>Catalog</CmdType>\r\n"); cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n"); cmdXml.append("</Query>\r\n"); - String tm = Long.toString(System.currentTimeMillis()); - CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId() - : udpSipProvider.getNewCallId(); + Request request; + if (dialog != null) { + logger.info("鍙戦�佺洰褰曡闃呮秷鎭椂 dialog鐨勭姸鎬佷负锛� {}", dialog.getState()); + request = dialog.createRequest(Request.SUBSCRIBE); + ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml"); + request.setContent(cmdXml.toString(), contentTypeHeader); + ExpiresHeader expireHeader = sipFactory.createHeaderFactory().createExpiresHeader(device.getSubscribeCycleForMobilePosition()); + request.addHeader(expireHeader); + }else { + String tm = Long.toString(System.currentTimeMillis()); - // 鏈夋晥鏃堕棿榛樿涓�60绉掍互涓� - Request request = headerProvider.createSubscribeRequest(device, cmdXml.toString(), "z9hG4bK-viaPos-" + tm, - "fromTagPos" + tm, null, device.getSubscribeCycleForCatalog(), "Catalog" , - callIdHeader); + CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId() + : udpSipProvider.getNewCallId(); + + // 鏈夋晥鏃堕棿榛樿涓�60绉掍互涓� + request = headerProvider.createSubscribeRequest(device, cmdXml.toString(), "z9hG4bK-viaPos-" + tm, + "fromTagPos" + tm, null, device.getSubscribeCycleForCatalog(), "Catalog" , + callIdHeader); + + } transmitRequest(device, request, errorEvent, okEvent); - return true; } catch ( NumberFormatException | ParseException | InvalidArgumentException | SipException e) { @@ -1590,7 +1596,7 @@ try { StringBuffer dragXml = new StringBuffer(200); String charset = device.getCharset(); - dragXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\" ?>\r\n"); + dragXml.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\r\n"); dragXml.append("<Control>\r\n"); dragXml.append("<CmdType>DeviceControl</CmdType>\r\n"); dragXml.append("<SN>" + (int) ((Math.random() * 9 + 1) * 100000) + "</SN>\r\n"); @@ -1766,4 +1772,101 @@ e.printStackTrace(); } } + + @Override + public boolean sendAlarmMessage(Device device, DeviceAlarm deviceAlarm) { + if (device == null) { + return false; + } + logger.info("[鍙戦�� 鎶ヨ閫氱煡] {}/{}->{},{}", device.getDeviceId(), deviceAlarm.getChannelId(), + deviceAlarm.getLongitude(), deviceAlarm.getLatitude()); + try { + String characterSet = device.getCharset(); + StringBuffer deviceStatusXml = new StringBuffer(600); + deviceStatusXml.append("<?xml version=\"1.0\" encoding=\"" + characterSet + "\"?>\r\n"); + deviceStatusXml.append("<Notify>\r\n"); + deviceStatusXml.append("<CmdType>Alarm</CmdType>\r\n"); + deviceStatusXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n"); + deviceStatusXml.append("<DeviceID>" + deviceAlarm.getChannelId() + "</DeviceID>\r\n"); + deviceStatusXml.append("<AlarmPriority>" + deviceAlarm.getAlarmPriority() + "</AlarmPriority>\r\n"); + deviceStatusXml.append("<AlarmMethod>" + deviceAlarm.getAlarmMethod() + "</AlarmMethod>\r\n"); + deviceStatusXml.append("<AlarmTime>" + deviceAlarm.getAlarmTime() + "</AlarmTime>\r\n"); + deviceStatusXml.append("<AlarmDescription>" + deviceAlarm.getAlarmDescription() + "</AlarmDescription>\r\n"); + deviceStatusXml.append("<Longitude>" + deviceAlarm.getLongitude() + "</Longitude>\r\n"); + deviceStatusXml.append("<Latitude>" + deviceAlarm.getLatitude() + "</Latitude>\r\n"); + deviceStatusXml.append("<info>\r\n"); + deviceStatusXml.append("<AlarmType>" + deviceAlarm.getAlarmType() + "</AlarmType>\r\n"); + deviceStatusXml.append("</info>\r\n"); + deviceStatusXml.append("</Notify>\r\n"); + + CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId() + : udpSipProvider.getNewCallId(); + String tm = Long.toString(System.currentTimeMillis()); + Request request = headerProvider.createMessageRequest(device, deviceStatusXml.toString(), "z9hG4bK-ViaPtz-" + tm, "FromPtz" + tm, null, callIdHeader); + transmitRequest(device, request); + + + } catch (SipException | ParseException e) { + e.printStackTrace(); + return false; + } catch (InvalidArgumentException e) { + throw new RuntimeException(e); + } + return true; + } + + private void sendNotify(Device device, String catalogXmlContent, + SubscribeInfo subscribeInfo, SipSubscribe.Event errorEvent, SipSubscribe.Event okEvent ) + throws NoSuchFieldException, IllegalAccessException, SipException, ParseException { + MessageFactoryImpl messageFactory = (MessageFactoryImpl) sipFactory.createMessageFactory(); + String characterSet = device.getCharset(); + // 璁剧疆缂栫爜锛� 闃叉涓枃涔辩爜 + messageFactory.setDefaultContentEncodingCharset(characterSet); + Dialog dialog = subscribeInfo.getDialog(); + if (dialog == null || !dialog.getState().equals(DialogState.CONFIRMED)) return; + SIPRequest notifyRequest = (SIPRequest)dialog.createRequest(Request.NOTIFY); + ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml"); + notifyRequest.setContent(catalogXmlContent, contentTypeHeader); + + SubscriptionStateHeader subscriptionState = sipFactory.createHeaderFactory() + .createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE); + notifyRequest.addHeader(subscriptionState); + + EventHeader event = sipFactory.createHeaderFactory().createEventHeader(subscribeInfo.getEventType()); + if (subscribeInfo.getEventId() != null) { + event.setEventId(subscribeInfo.getEventId()); + } + notifyRequest.addHeader(event); + + SipURI sipURI = (SipURI) notifyRequest.getRequestURI(); + if (subscribeInfo.getTransaction() != null) { + SIPRequest request = (SIPRequest) subscribeInfo.getTransaction().getRequest(); + sipURI.setHost(request.getRemoteAddress().getHostAddress()); + sipURI.setPort(request.getRemotePort()); + }else { + sipURI.setHost(device.getIp()); + sipURI.setPort(device.getPort()); + } + + ClientTransaction transaction = null; + if ("TCP".equals(device.getTransport())) { + transaction = tcpSipProvider.getNewClientTransaction(notifyRequest); + } else if ("UDP".equals(device.getTransport())) { + transaction = udpSipProvider.getNewClientTransaction(notifyRequest); + } + // 娣诲姞閿欒璁㈤槄 + if (errorEvent != null) { + sipSubscribe.addErrorSubscribe(subscribeInfo.getCallId(), errorEvent); + } + // 娣诲姞璁㈤槄 + if (okEvent != null) { + sipSubscribe.addOkSubscribe(subscribeInfo.getCallId(), okEvent); + } + if (transaction == null) { + logger.error("骞冲彴{}鐨凾ransport閿欒锛歿}",device.getDeviceId(), device.getTransport()); + return; + } + dialog.sendRequest(transaction); + + } } -- Gitblit v1.8.0