Commit f5640fddb711cb05defe691e9817057e35c03c6e

Authored by 648540858
1 parent 8f9a5302

优化国标级联的目录订阅

README.md
... ... @@ -105,6 +105,10 @@ https://gitee.com/pan648540858/wvp-GB28181-pro.git
105 105 - [X] WEB端支持播放H264与H265,音频支持G.711A/G.711U/AAC,覆盖国标常用编码格式。
106 106  
107 107 # docker快速体验
  108 +目前作者的docker-compose因为时间有限维护不及时,这里提供第三方提供的供大家使用,维护不易,大家记得给这位小伙伴点个star。
  109 +https://github.com/SaltFish001/wvp_pro_compose
  110 +[https://github.com/SaltFish001/wvp_pro_compose](https://github.com/SaltFish001/wvp_pro_compose)
  111 +这是作者维护的一个镜像,可能存在不及时的问题。
108 112 ```shell
109 113 docker pull 648540858/wvp_pro
110 114  
... ...
src/main/java/com/genersoft/iot/vmp/conf/Swagger3Config.java
... ... @@ -14,7 +14,7 @@ import springfox.documentation.spring.web.plugins.Docket;
14 14 @Configuration
15 15 public class Swagger3Config {
16 16  
17   - @Value("${swagger-ui.enabled}")
  17 + @Value("${swagger-ui.enabled: true}")
18 18 private boolean enable;
19 19  
20 20 @Bean
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/bean/SubscribeInfo.java
... ... @@ -21,6 +21,8 @@ public class SubscribeInfo {
21 21 EventHeader eventHeader = (EventHeader)request.getHeader(EventHeader.NAME);
22 22 this.eventId = eventHeader.getEventId();
23 23 this.eventType = eventHeader.getEventType();
  24 + ViaHeader viaHeader = (ViaHeader)request.getHeader(ViaHeader.NAME);
  25 + this.branch = viaHeader.getBranch();
24 26 }
25 27  
26 28 private String id;
... ... @@ -30,6 +32,7 @@ public class SubscribeInfo {
30 32 private String eventType;
31 33 private String fromTag;
32 34 private String toTag;
  35 + private String branch;
33 36  
34 37 public String getId() {
35 38 return id;
... ... @@ -86,4 +89,12 @@ public class SubscribeInfo {
86 89 public void setEventType(String eventType) {
87 90 this.eventType = eventType;
88 91 }
  92 +
  93 + public String getBranch() {
  94 + return branch;
  95 + }
  96 +
  97 + public void setBranch(String branch) {
  98 + this.branch = branch;
  99 + }
89 100 }
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/event/subscribe/catalog/CatalogEventLister.java
... ... @@ -106,7 +106,7 @@ public class CatalogEventLister implements ApplicationListener<CatalogEvent> {
106 106 }
107 107 if (deviceChannelList.size() > 0) {
108 108 logger.info("[Catalog事件: {}]平台:{},影响通道{}个", event.getType(), event.getPlatformId(), deviceChannelList.size());
109   - sipCommanderFroPlatform.sendNotifyForCatalogOther(event.getType(), parentPlatform, deviceChannelList, subscribe);
  109 + sipCommanderFroPlatform.sendNotifyForCatalogOther(event.getType(), parentPlatform, deviceChannelList, subscribe, null);
110 110 }
111 111 }else if (parentPlatformMap.keySet().size() > 0) {
112 112 for (String gbId : parentPlatformMap.keySet()) {
... ... @@ -121,7 +121,7 @@ public class CatalogEventLister implements ApplicationListener<CatalogEvent> {
121 121 DeviceChannel deviceChannel = new DeviceChannel();
122 122 deviceChannel.setChannelId(gbId);
123 123 deviceChannelList.add(deviceChannel);
124   - sipCommanderFroPlatform.sendNotifyForCatalogOther(event.getType(), platform, deviceChannelList, subscribeInfo);
  124 + sipCommanderFroPlatform.sendNotifyForCatalogOther(event.getType(), platform, deviceChannelList, subscribeInfo, null);
125 125 }
126 126 }
127 127 }
... ... @@ -163,7 +163,7 @@ public class CatalogEventLister implements ApplicationListener<CatalogEvent> {
163 163 GbStream gbStream = storager.queryStreamInParentPlatform(platform.getServerGBId(), gbId);
164 164 DeviceChannel deviceChannelByStream = gbStreamService.getDeviceChannelListByStream(gbStream, gbStream.getCatalogId(), platform.getDeviceGBId());
165 165 deviceChannelList.add(deviceChannelByStream);
166   - sipCommanderFroPlatform.sendNotifyForCatalogOther(event.getType(), platform, deviceChannelList, subscribeInfo);
  166 + sipCommanderFroPlatform.sendNotifyForCatalogOther(event.getType(), platform, deviceChannelList, subscribeInfo, null);
167 167 }
168 168 }
169 169 }
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java
... ... @@ -85,6 +85,6 @@ public interface ISIPCommanderForPlatform {
85 85 * @param parentPlatform
86 86 * @param deviceChannels
87 87 */
88   - boolean sendNotifyForCatalogOther(String type, ParentPlatform parentPlatform, List<DeviceChannel> deviceChannels, SubscribeInfo subscribeInfo);
  88 + boolean sendNotifyForCatalogOther(String type, ParentPlatform parentPlatform, List<DeviceChannel> deviceChannels, SubscribeInfo subscribeInfo, Integer index);
89 89  
90 90 }
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/SIPRequestHeaderPlarformProvider.java
... ... @@ -236,19 +236,18 @@ public class SIPRequestHeaderPlarformProvider {
236 236 // via
237 237 ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
238 238 ViaHeader viaHeader = sipFactory.createHeaderFactory().createViaHeader(parentPlatform.getDeviceIp(), Integer.parseInt(parentPlatform.getDevicePort()),
239   - parentPlatform.getTransport(), viaTag);
  239 + parentPlatform.getTransport(), subscribeInfo.getBranch());
240 240 viaHeader.setRPort();
241 241 viaHeaders.add(viaHeader);
242 242 // from
243 243 SipURI fromSipURI = sipFactory.createAddressFactory().createSipURI(parentPlatform.getDeviceGBId(),
244 244 parentPlatform.getDeviceIp() + ":" + parentPlatform.getDevicePort());
245 245 Address fromAddress = sipFactory.createAddressFactory().createAddress(fromSipURI);
246   - String tm = Long.toString(System.currentTimeMillis());
247   - FromHeader fromHeader = sipFactory.createHeaderFactory().createFromHeader(fromAddress, "fromtag" + tm);
  246 + FromHeader fromHeader = sipFactory.createHeaderFactory().createFromHeader(fromAddress, subscribeInfo.getToTag());
248 247 // to
249 248 SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(parentPlatform.getServerGBId(), parentPlatform.getServerGBDomain());
250 249 Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI);
251   - ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress, subscribeInfo.getToTag());
  250 + ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress, subscribeInfo.getFromTag());
252 251  
253 252 // Forwards
254 253 MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70);
... ... @@ -265,7 +264,10 @@ public class SIPRequestHeaderPlarformProvider {
265 264 request.addHeader(userAgentHeader);
266 265  
267 266 EventHeader event = sipFactory.createHeaderFactory().createEventHeader(subscribeInfo.getEventType());
268   - event.setEventId(subscribeInfo.getEventId());
  267 + if (subscribeInfo.getEventId() != null) {
  268 + event.setEventId(subscribeInfo.getEventId());
  269 + }
  270 +
269 271 request.addHeader(event);
270 272  
271 273 SubscriptionStateHeader active = sipFactory.createHeaderFactory().createSubscriptionStateHeader("active");
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java
... ... @@ -371,10 +371,10 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform {
371 371 }
372 372  
373 373 try {
374   - if (index == deviceChannels.size() - 1) {
  374 + if (index > deviceChannels.size() - 1) {
375 375 return true;
376 376 }
377   - Request request = getCatalogNotifyRequest(parentPlatform, deviceChannels.get(index), deviceChannels.size(), type, subscribeInfo);
  377 + Request request = getCatalogNotifyRequestForCatalogAddOrUpdate(parentPlatform, deviceChannels.get(index), deviceChannels.size(), type, subscribeInfo);
378 378 index += 1;
379 379 Integer finalIndex = index;
380 380 transmitRequest(parentPlatform, request, null, (eventResult -> {
... ... @@ -387,10 +387,10 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform {
387 387 return true;
388 388 }
389 389  
390   - private Request getCatalogNotifyRequest(ParentPlatform parentPlatform, DeviceChannel channel, int size, String type,
  390 + private Request getCatalogNotifyRequestForCatalogAddOrUpdate(ParentPlatform parentPlatform, DeviceChannel channel, int size, String type,
391 391 SubscribeInfo subscribeInfo) throws ParseException, InvalidArgumentException,
392 392 PeerUnavailableException {
393   - String catalogXmlContent = getCatalogXmlContent(parentPlatform, channel, size, type);
  393 + String catalogXmlContent = getCatalogXmlContentForCatalogAddOrUpdate(parentPlatform, channel, size, type, subscribeInfo);
394 394  
395 395 CallIdHeader callIdHeader = parentPlatform.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
396 396 : udpSipProvider.getNewCallId();
... ... @@ -400,14 +400,17 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform {
400 400 return request;
401 401 }
402 402  
403   - private String getCatalogXmlContent(ParentPlatform parentPlatform, DeviceChannel channel, int sumNum, String type) {
  403 + private String getCatalogXmlContentForCatalogAddOrUpdate(ParentPlatform parentPlatform, DeviceChannel channel, int sumNum, String type, SubscribeInfo subscribeInfo) {
404 404 StringBuffer catalogXml = new StringBuffer(600);
  405 + if (parentPlatform.getServerGBId().equals(channel.getParentId())) {
  406 + channel.setParentId(parentPlatform.getDeviceGBId());
  407 + }
405 408 catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
406 409 catalogXml.append("<Notify>\r\n");
407 410 catalogXml.append("<CmdType>Catalog</CmdType>\r\n");
408 411 catalogXml.append("<SN>" + (int) ((Math.random() * 9 + 1) * 100000) + "</SN>\r\n");
409 412 catalogXml.append("<DeviceID>" + parentPlatform.getDeviceGBId() + "</DeviceID>\r\n");
410   - catalogXml.append("<SumNum>" + sumNum + "</SumNum>\r\n");
  413 + catalogXml.append("<SumNum>1</SumNum>\r\n");
411 414 catalogXml.append("<DeviceList Num=\"1\">\r\n");
412 415 catalogXml.append("<Item>\r\n");
413 416 catalogXml.append("<DeviceID>" + channel.getChannelId() + "</DeviceID>\r\n");
... ... @@ -418,11 +421,15 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform {
418 421 catalogXml.append("<CivilCode>CivilCode</CivilCode>\r\n");
419 422 catalogXml.append("<Address>" + channel.getAddress() + "</Address>\r\n");
420 423 catalogXml.append("<Parental>" + channel.getParental() + "</Parental>\r\n");
421   - catalogXml.append("<ParentID>" + channel.getParentId() + "</ParentID>\r\n");
  424 + if (channel.getParentId() != null) {
  425 + catalogXml.append("<ParentID>" + channel.getParentId() + "</ParentID>\r\n");
  426 + }
422 427 catalogXml.append("<Secrecy>" + channel.getSecrecy() + "</Secrecy>\r\n");
423 428 catalogXml.append("<RegisterWay>" + channel.getRegisterWay() + "</RegisterWay>\r\n");
424 429 catalogXml.append("<Status>" + (channel.getStatus() == 0 ? "OFF" : "ON") + "</Status>\r\n");
425   - catalogXml.append("<Event>" + type + "</Event>\r\n");
  430 + if (!"presence".equals(subscribeInfo.getEventType())) {
  431 + catalogXml.append("<Event>" + type + "</Event>\r\n");
  432 + }
426 433 catalogXml.append("</Item>\r\n");
427 434 catalogXml.append("</DeviceList>\r\n");
428 435 catalogXml.append("</Notify>\r\n");
... ... @@ -430,7 +437,7 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform {
430 437 }
431 438  
432 439 @Override
433   - public boolean sendNotifyForCatalogOther(String type, ParentPlatform parentPlatform, List<DeviceChannel> deviceChannels, SubscribeInfo subscribeInfo) {
  440 + public boolean sendNotifyForCatalogOther(String type, ParentPlatform parentPlatform, List<DeviceChannel> deviceChannels, SubscribeInfo subscribeInfo, Integer index) {
434 441 if (parentPlatform == null
435 442 || deviceChannels == null
436 443 || deviceChannels.size() == 0
... ... @@ -438,42 +445,55 @@ public class SIPCommanderFroPlatform implements ISIPCommanderForPlatform {
438 445 return false;
439 446 }
440 447  
441   - for (DeviceChannel channel : deviceChannels) {
442   - try {
443   - StringBuffer catalogXml = new StringBuffer(600);
444   - catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
445   - catalogXml.append("<Notify>\r\n");
446   - catalogXml.append("<CmdType>Catalog</CmdType>\r\n");
447   - catalogXml.append("<SN>" + (int) ((Math.random() * 9 + 1) * 100000) + "</SN>\r\n");
448   - catalogXml.append("<DeviceID>" + parentPlatform.getDeviceGBId() + "</DeviceID>\r\n");
449   - catalogXml.append("<SumNum>" + deviceChannels.size() + "</SumNum>\r\n");
450   - catalogXml.append("<DeviceList Num=\"1\">\r\n");
451   - catalogXml.append("<Item>\r\n");
452   - catalogXml.append("<DeviceID>" + channel.getChannelId() + "</DeviceID>\r\n");
453   - catalogXml.append("<Event>" + type + "</Event>\r\n");
454   - catalogXml.append("</Item>\r\n");
455   - catalogXml.append("</DeviceList>\r\n");
456   - catalogXml.append("</Notify>\r\n");
  448 + if (index == null) {
  449 + index = 0;
  450 + }
457 451  
458   - CallIdHeader callIdHeader = parentPlatform.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
  452 + if (index > deviceChannels.size() - 1) {
  453 + return true;
  454 + }
  455 + try {
  456 + String catalogXml = getCatalogXmlContentForCatalogOther(deviceChannels.get(index), type, parentPlatform);
  457 + CallIdHeader callIdHeader = parentPlatform.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
459 458 : udpSipProvider.getNewCallId();
460   - callIdHeader.setCallId(subscribeInfo.getCallId());
461   -
462   - String tm = Long.toString(System.currentTimeMillis());
463   -
464   - Request request = headerProviderPlarformProvider.createNotifyRequest(parentPlatform, catalogXml.toString(),
  459 + Request request = headerProviderPlarformProvider.createNotifyRequest(parentPlatform, catalogXml,
465 460 callIdHeader,
466 461 "z9hG4bK-" + UUID.randomUUID().toString().replace("-", ""), subscribeInfo);
467   - transmitRequest(parentPlatform, request);
468   - Thread.sleep(200);
469   - } catch (SipException | ParseException | InvalidArgumentException e) {
470   - e.printStackTrace();
471   - return false;
472   - } catch (InterruptedException e) {
473   - e.printStackTrace();
474   - }
  462 + index += 1;
  463 + Integer finalIndex = index;
  464 + transmitRequest(parentPlatform, request, null, eventResult -> {
  465 + sendNotifyForCatalogOther(type, parentPlatform, deviceChannels, subscribeInfo, finalIndex);
  466 + });
  467 + } catch (SipException e) {
  468 + e.printStackTrace();
  469 + } catch (InvalidArgumentException e) {
  470 + e.printStackTrace();
  471 + } catch (ParseException e) {
  472 + e.printStackTrace();
475 473 }
  474 +
476 475 return true;
477 476 }
478 477  
  478 + private String getCatalogXmlContentForCatalogOther(DeviceChannel channel, String type, ParentPlatform parentPlatform) {
  479 + if (parentPlatform.getServerGBId().equals(channel.getParentId())) {
  480 + channel.setParentId(parentPlatform.getDeviceGBId());
  481 + }
  482 + StringBuffer catalogXml = new StringBuffer(600);
  483 + catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
  484 + catalogXml.append("<Notify>\r\n");
  485 + catalogXml.append("<CmdType>Catalog</CmdType>\r\n");
  486 + catalogXml.append("<SN>" + (int) ((Math.random() * 9 + 1) * 100000) + "</SN>\r\n");
  487 + catalogXml.append("<DeviceID>" + parentPlatform.getDeviceGBId() + "</DeviceID>\r\n");
  488 + catalogXml.append("<SumNum>1</SumNum>\r\n");
  489 + catalogXml.append("<DeviceList Num=\"1\">\r\n");
  490 + catalogXml.append("<Item>\r\n");
  491 + catalogXml.append("<DeviceID>" + channel.getChannelId() + "</DeviceID>\r\n");
  492 + catalogXml.append("<Event>" + type + "</Event>\r\n");
  493 + catalogXml.append("</Item>\r\n");
  494 + catalogXml.append("</DeviceList>\r\n");
  495 + catalogXml.append("</Notify>\r\n");
  496 + return catalogXml.toString();
  497 + }
  498 +
479 499 }
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
... ... @@ -191,7 +191,7 @@ public class XmlUtil {
191 191 String channelId = channdelIdElement != null ? channdelIdElement.getTextTrim().toString() : "";
192 192 deviceChannel.setChannelId(channelId);
193 193 // ONLINE OFFLINE HIKVISION DS-7716N-E4 NVR的兼容性处理
194   - if (status.equals("ON") || status.equals("On") || status.equals("ONLINE")) {
  194 + if (status.equals("ON") || status.equals("On") || status.equals("ONLINE") || status.equals("OK")) {
195 195 deviceChannel.setStatus(1);
196 196 }
197 197 if (status.equals("OFF") || status.equals("Off") || status.equals("OFFLINE")) {
... ... @@ -255,9 +255,14 @@ public class XmlUtil {
255 255 } else {
256 256 deviceChannel.setLatitude(0.00);
257 257 }
258   - if (XmlUtil.getText(itemDevice, "PTZType") == null
259   - || XmlUtil.getText(itemDevice, "PTZType") == "") {
260   - deviceChannel.setPTZType(0);
  258 + if (XmlUtil.getText(itemDevice, "PTZType") == null || "".equals(XmlUtil.getText(itemDevice, "PTZType"))) {
  259 + //兼容INFO中的信息
  260 + Element info = itemDevice.element("Info");
  261 + if(XmlUtil.getText(info, "PTZType") == null || "".equals(XmlUtil.getText(info, "PTZType"))){
  262 + deviceChannel.setPTZType(0);
  263 + }else{
  264 + deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(info, "PTZType")));
  265 + }
261 266 } else {
262 267 deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(itemDevice, "PTZType")));
263 268 }
... ...
src/main/java/com/genersoft/iot/vmp/storager/dao/GbStreamMapper.java
... ... @@ -61,7 +61,7 @@ public interface GbStreamMapper {
61 61 List<GbStream> selectByGBId(String gbId);
62 62  
63 63 @Select("SELECT gs.*, pgs.platformId as platformId, pgs.catalogId as catalogId FROM gb_stream gs " +
64   - "LEFT JOIN platform_gb_stream pgs ON gs.gbStreamId = pgs.catalogId " +
  64 + "LEFT JOIN platform_gb_stream pgs ON gs.gbStreamId = pgs.gbStreamId " +
65 65 "WHERE gs.gbId = '${gbId}' AND pgs.platformId = '${platformId}'")
66 66 GbStream queryStreamInPlatform(String platformId, String gbId);
67 67  
... ...
src/main/java/com/genersoft/iot/vmp/storager/dao/PlatformChannelMapper.java
... ... @@ -57,9 +57,9 @@ public interface PlatformChannelMapper {
57 57 @Select("SELECT dc.* FROM platform_gb_channel pgc left join device_channel dc on dc.id = pgc.deviceChannelId WHERE dc.channelId='${channelId}' and pgc.platformId='${platformId}'")
58 58 DeviceChannel queryChannelInParentPlatform(String platformId, String channelId);
59 59  
60   - @Select("select dc.channelId as id, dc.name as name, pgc.platformId as platformId, pgc.catalogId as parentId, 0 as childrenCount, 1 as type " +
61   - "from device_channel dc left join platform_gb_channel pgc on dc.id = pgc.deviceChannelId" +
62   - "where pgc.platformId=#{platformId} and pgc.catalogId=#{catalogId}")
  60 + @Select(" select dc.channelId as id, dc.name as name, pgc.platformId as platformId, pgc.catalogId as parentId, 0 as childrenCount, 1 as type " +
  61 + " from device_channel dc left join platform_gb_channel pgc on dc.id = pgc.deviceChannelId " +
  62 + " where pgc.platformId=#{platformId} and pgc.catalogId=#{catalogId}")
63 63 List<PlatformCatalog> queryChannelInParentPlatformAndCatalog(String platformId, String catalogId);
64 64  
65 65 @Select("select d.*\n" +
... ...
src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/platform/PlatformController.java
... ... @@ -211,26 +211,21 @@ public class PlatformController {
211 211 if (updateResult) {
212 212 // 保存时启用就发送注册
213 213 if (parentPlatform.isEnable()) {
214   - // 保存时启用就发送注册
215   - if (parentPlatform.isEnable()) {
216   - if (parentPlatformOld.isStatus()) {
217   - commanderForPlatform.unregister(parentPlatformOld, null, null);
218   - try {
219   - Thread.sleep(500);
220   - } catch (InterruptedException e) {
221   - e.printStackTrace();
222   - }
223   - // 只要保存就发送注册
224   - commanderForPlatform.register(parentPlatform, null, null);
225   - }else {
226   - // 只要保存就发送注册
227   - commanderForPlatform.register(parentPlatform, null, null);
228   - }
229   - } else if (parentPlatformOld != null && parentPlatformOld.isEnable() && !parentPlatform.isEnable()){ // 关闭启用时注销
  214 + if (parentPlatformOld.isStatus()) {
230 215 commanderForPlatform.unregister(parentPlatformOld, null, null);
  216 + try {
  217 + Thread.sleep(500);
  218 + } catch (InterruptedException e) {
  219 + e.printStackTrace();
  220 + }
  221 + // 只要保存就发送注册
  222 + commanderForPlatform.register(parentPlatform, null, null);
  223 + }else {
  224 + // 只要保存就发送注册
  225 + commanderForPlatform.register(parentPlatform, null, null);
231 226 }
232 227 } else if (parentPlatformOld != null && parentPlatformOld.isEnable() && !parentPlatform.isEnable()){ // 关闭启用时注销
233   - commanderForPlatform.unregister(parentPlatform, null, null);
  228 + commanderForPlatform.unregister(parentPlatformOld, null, null);
234 229 }
235 230 wvpResult.setCode(0);
236 231 wvpResult.setMsg("success");
... ...