From 221f99c764b51c3cd284c6e5a41492c26c5c7ffc Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期五, 09 九月 2022 17:06:19 +0800
Subject: [PATCH] 优化sip消息

---
 src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java |  290 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 268 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
index 4220d81..1198c26 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
@@ -1,15 +1,12 @@
 package com.genersoft.iot.vmp.gb28181.utils;
 
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-
+import com.genersoft.iot.vmp.gb28181.bean.Device;
+import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
+import com.genersoft.iot.vmp.gb28181.bean.TreeType;
+import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
+import com.genersoft.iot.vmp.utils.DateUtil;
 import org.dom4j.Attribute;
 import org.dom4j.Document;
 import org.dom4j.DocumentException;
@@ -17,24 +14,31 @@
 import org.dom4j.io.SAXReader;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
+
+import javax.sip.RequestEvent;
+import javax.sip.message.Request;
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+import java.util.*;
 
 /**
  * 鍩轰簬dom4j鐨勫伐鍏峰寘
- * 
- * 
+ *
+ *
  */
 public class XmlUtil {
     /**
      * 鏃ュ織鏈嶅姟
      */
-    private static Logger LOG = LoggerFactory.getLogger(XmlUtil.class);
+    private static Logger logger = LoggerFactory.getLogger(XmlUtil.class);
 
     /**
      * 瑙f瀽XML涓篋ocument瀵硅薄
-     * 
+     *
      * @param xml 琚В鏋愮殑XMl
-     * 
+     *
      * @return Document
      */
     public static Element parseXml(String xml) {
@@ -45,14 +49,14 @@
         try {
             document = saxReader.read(sr);
         } catch (DocumentException e) {
-            LOG.error("瑙f瀽澶辫触", e);
+            logger.error("瑙f瀽澶辫触", e);
         }
         return null == document ? null : document.getRootElement();
     }
 
     /**
      * 鑾峰彇element瀵硅薄鐨則ext鐨勫��
-     * 
+     *
      * @param em  鑺傜偣鐨勫璞�
      * @param tag 鑺傜偣鐨則ag
      * @return 鑺傜偣
@@ -63,12 +67,12 @@
         }
         Element e = em.element(tag);
         //
-        return null == e ? null : e.getText();
+        return null == e ? null : e.getText().trim();
     }
 
     /**
      * 閫掑綊瑙f瀽xml鑺傜偣锛岄�傜敤浜� 澶氳妭鐐规暟鎹�
-     * 
+     *
      * @param node     node
      * @param nodeName nodeName
      * @return List<Map<String, Object>>
@@ -107,7 +111,7 @@
 
     /**
      * xml杞琷son
-     * 
+     *
      * @param element
      * @param json
      */
@@ -115,12 +119,12 @@
         // 濡傛灉鏄睘鎬�
         for (Object o : element.attributes()) {
             Attribute attr = (Attribute) o;
-            if (!StringUtils.isEmpty(attr.getValue())) {
+            if (!ObjectUtils.isEmpty(attr.getValue())) {
                 json.put("@" + attr.getName(), attr.getValue());
             }
         }
         List<Element> chdEl = element.elements();
-        if (chdEl.isEmpty() && !StringUtils.isEmpty(element.getText())) {// 濡傛灉娌℃湁瀛愬厓绱�,鍙湁涓�涓��
+        if (chdEl.isEmpty() && !ObjectUtils.isEmpty(element.getText())) {// 濡傛灉娌℃湁瀛愬厓绱�,鍙湁涓�涓��
             json.put(element.getName(), element.getText());
         }
 
@@ -151,7 +155,7 @@
             } else { // 瀛愬厓绱犳病鏈夊瓙鍏冪礌
                 for (Object o : element.attributes()) {
                     Attribute attr = (Attribute) o;
-                    if (!StringUtils.isEmpty(attr.getValue())) {
+                    if (!ObjectUtils.isEmpty(attr.getValue())) {
                         json.put("@" + attr.getName(), attr.getValue());
                     }
                 }
@@ -161,4 +165,246 @@
             }
         }
     }
-}
+    public static  Element getRootElement(RequestEvent evt) throws DocumentException {
+
+        return getRootElement(evt, "gb2312");
+    }
+
+    public static Element getRootElement(RequestEvent evt, String charset) throws DocumentException {
+        Request request = evt.getRequest();
+        return getRootElement(request.getRawContent(), charset);
+    }
+
+    public static Element getRootElement(byte[] content, String charset) throws DocumentException {
+        if (charset == null) {
+            charset = "gb2312";
+        }
+        SAXReader reader = new SAXReader();
+        reader.setEncoding(charset);
+        Document xml = reader.read(new ByteArrayInputStream(content));
+        return xml.getRootElement();
+    }
+
+    private enum ChannelType{
+        CivilCode, BusinessGroup,VirtualOrganization,Other
+    }
+
+    public static DeviceChannel channelContentHander(Element itemDevice, Device device, String event){
+        DeviceChannel deviceChannel = new DeviceChannel();
+        deviceChannel.setDeviceId(device.getDeviceId());
+        Element channdelIdElement = itemDevice.element("DeviceID");
+        if (channdelIdElement == null) {
+            logger.warn("瑙f瀽Catalog娑堟伅鏃跺彂鐜扮己灏� DeviceID");
+            return null;
+        }
+        String channelId = channdelIdElement.getTextTrim();
+        if (ObjectUtils.isEmpty(channelId)) {
+            logger.warn("瑙f瀽Catalog娑堟伅鏃跺彂鐜扮己灏� DeviceID");
+            return null;
+        }
+        deviceChannel.setChannelId(channelId);
+        if (event != null && !event.equals(CatalogEvent.ADD) && !event.equals(CatalogEvent.UPDATE)) {
+            // 闄や簡ADD鍜寀pdate鎯呭喌涓嬮渶瑕佽瘑鍒叏閮ㄥ唴瀹癸紝
+            return deviceChannel;
+        }
+
+        ChannelType channelType = ChannelType.Other;
+        if (channelId.length() <= 8) {
+            channelType = ChannelType.CivilCode;
+            deviceChannel.setHasAudio(false);
+        }else {
+            if (channelId.length() == 20) {
+                int code = Integer.parseInt(channelId.substring(10, 13));
+                switch (code){
+                    case 215:
+                        channelType = ChannelType.BusinessGroup;
+                        deviceChannel.setHasAudio(false);
+                        break;
+                    case 216:
+                        channelType = ChannelType.VirtualOrganization;
+                        deviceChannel.setHasAudio(false);
+                        break;
+                    case 136:
+                    case 137:
+                    case 138:
+                        deviceChannel.setHasAudio(true);
+                        break;
+                    default:
+                        deviceChannel.setHasAudio(false);
+                        break;
+
+                }
+            }
+        }
+
+        Element channdelNameElement = itemDevice.element("Name");
+        String channelName = channdelNameElement != null ? channdelNameElement.getTextTrim() : "";
+        deviceChannel.setName(channelName);
+
+        String civilCode = XmlUtil.getText(itemDevice, "CivilCode");
+        deviceChannel.setCivilCode(civilCode);
+        if (channelType == ChannelType.CivilCode && civilCode == null) {
+            deviceChannel.setParental(1);
+            // 琛屾斂鍖哄垝濡傛灉娌℃湁浼犻�掑叿浣撳�硷紝鍒欐帹娴嬩竴涓�
+            if (channelId.length() > 2) {
+                deviceChannel.setCivilCode(channelId.substring(0, channelId.length() - 2));
+            }
+        }
+        if (channelType.equals(ChannelType.CivilCode)) {
+            // 琛屾斂鍖哄垝鍏朵粬瀛楁娌″繀瑕佽瘑鍒簡锛岄粯璁ゅ湪绾垮嵆鍙�
+            deviceChannel.setStatus(1);
+            deviceChannel.setParental(1);
+            deviceChannel.setCreateTime(DateUtil.getNow());
+            deviceChannel.setUpdateTime(DateUtil.getNow());
+            return deviceChannel;
+        }
+        /**
+         * 琛屾斂鍖哄垝灞曠ず璁惧鏍戜笌涓氬姟鍒嗙粍灞曠ず璁惧鏍戞槸涓ょ涓嶅悓鐨勬ā寮�
+         * 琛屾斂鍖哄垝灞曠ず璁惧鏍� 鍚勪釜鐩綍涔嬮棿涓昏闈燿eviceId鍋氬叧鑱�,鎽勫儚澶撮�氳繃CivilCode鎸囧畾鍏跺睘浜庨偅涓鏀垮尯鍒�;閮芥槸涓嶈秴杩囧崄浣嶇殑缂栧彿; 缁撴瀯濡備笅:
+         * 娌冲寳鐪�
+         *    --> 鐭冲搴勫競
+         *          --> 鎽勫儚澶�
+         *String parentId = XmlUtil.getText(itemDevice, "ParentID");
+         if (parentId != null) {
+         if (parentId.contains("/")) {
+         String lastParentId = parentId.substring(parentId.lastIndexOf("/") + 1);
+         String businessGroup = parentId.substring(0, parentId.indexOf("/"));
+         deviceChannel.setParentId(lastParentId);
+         }else {
+         deviceChannel.setParentId(parentId);
+         }
+         }
+         deviceCh          --> 姝e畾鍘�
+         *                  --> 鎽勫儚澶�
+         *                  --> 鎽勫儚澶�
+         *
+         * 涓氬姟鍒嗙粍灞曠ず璁惧鏍戞槸椤剁骇鏄笟鍔″垎缁�,鍏朵笅鐨勮櫄鎷熺粍缁囬潬BusinessGroupID鎸囧畾鍏舵墍灞炵殑涓氬姟鍒嗙粍;鎽勫儚澶撮�氳繃ParentId鏉ユ寚瀹氬叾鎵�灞炰簬鐨勮櫄鎷熺粍缁�:
+         * 涓氬姟鍒嗙粍
+         *    --> 铏氭嫙缁勭粐
+         *         --> 鎽勫儚澶�
+         *         --> 铏氭嫙缁勭粐
+         *             --> 鎽勫儚澶�
+         *             --> 鎽勫儚澶�
+         */
+        String parentId = XmlUtil.getText(itemDevice, "ParentID");
+        String businessGroupID = XmlUtil.getText(itemDevice, "BusinessGroupID");
+        if (parentId != null) {
+            if (parentId.contains("/")) {
+                String lastParentId = parentId.substring(parentId.lastIndexOf("/") + 1);
+                if (businessGroupID == null) {
+                    businessGroupID = parentId.substring(0, parentId.indexOf("/"));
+                }
+                deviceChannel.setParentId(lastParentId);
+            }else {
+                deviceChannel.setParentId(parentId);
+            }
+        }
+        deviceChannel.setBusinessGroupId(businessGroupID);
+        if (channelType.equals(ChannelType.BusinessGroup) || channelType.equals(ChannelType.VirtualOrganization)) {
+            // 涓氬姟鍒嗙粍鍜岃櫄鎷熺粍缁� 鍏朵粬瀛楁娌″繀瑕佽瘑鍒簡锛岄粯璁ゅ湪绾垮嵆鍙�
+            deviceChannel.setStatus(1);
+            deviceChannel.setParental(1);
+            deviceChannel.setCreateTime(DateUtil.getNow());
+            deviceChannel.setUpdateTime(DateUtil.getNow());
+            return deviceChannel;
+        }
+
+        Element statusElement = itemDevice.element("Status");
+
+        if (statusElement != null) {
+            String status = statusElement.getTextTrim().trim();
+            // ONLINE OFFLINE HIKVISION DS-7716N-E4 NVR鐨勫吋瀹规�у鐞�
+            if (status.equals("ON") || status.equals("On") || status.equals("ONLINE") || status.equals("OK")) {
+                deviceChannel.setStatus(1);
+            }
+            if (status.equals("OFF") || status.equals("Off") || status.equals("OFFLINE")) {
+                deviceChannel.setStatus(0);
+            }
+        }else {
+            deviceChannel.setStatus(1);
+        }
+        // 璇嗗埆鑷甫鐨勭洰褰曟爣璇�
+        String parental = XmlUtil.getText(itemDevice, "Parental");
+        // 鐢变簬娴峰悍浼氶敊璇殑鍙戦��65535浣滀负杩欓噷鐨勫彇鍊�,鎵�浠ヨ繖閲岄櫎闈炴槸0鍚﹀垯璁や负鏄�1
+        if (!ObjectUtils.isEmpty(parental) && parental.length() == 1 && Integer.parseInt(parental) == 0) {
+            deviceChannel.setParental(0);
+        }else {
+            deviceChannel.setParental(1);
+        }
+
+
+        deviceChannel.setManufacture(XmlUtil.getText(itemDevice, "Manufacturer"));
+        deviceChannel.setModel(XmlUtil.getText(itemDevice, "Model"));
+        deviceChannel.setOwner(XmlUtil.getText(itemDevice, "Owner"));
+        deviceChannel.setCertNum(XmlUtil.getText(itemDevice, "CertNum"));
+        deviceChannel.setBlock(XmlUtil.getText(itemDevice, "Block"));
+        deviceChannel.setAddress(XmlUtil.getText(itemDevice, "Address"));
+        deviceChannel.setPassword(XmlUtil.getText(itemDevice, "Password"));
+
+        String safetyWay = XmlUtil.getText(itemDevice, "SafetyWay");
+        if (ObjectUtils.isEmpty(safetyWay)) {
+            deviceChannel.setSafetyWay(0);
+        } else {
+            deviceChannel.setSafetyWay(Integer.parseInt(safetyWay));
+        }
+
+        String registerWay = XmlUtil.getText(itemDevice, "RegisterWay");
+        if (ObjectUtils.isEmpty(registerWay)) {
+            deviceChannel.setRegisterWay(1);
+        } else {
+            deviceChannel.setRegisterWay(Integer.parseInt(registerWay));
+        }
+
+        if (XmlUtil.getText(itemDevice, "Certifiable") == null
+                || XmlUtil.getText(itemDevice, "Certifiable") == "") {
+            deviceChannel.setCertifiable(0);
+        } else {
+            deviceChannel.setCertifiable(Integer.parseInt(XmlUtil.getText(itemDevice, "Certifiable")));
+        }
+
+        if (XmlUtil.getText(itemDevice, "ErrCode") == null
+                || XmlUtil.getText(itemDevice, "ErrCode") == "") {
+            deviceChannel.setErrCode(0);
+        } else {
+            deviceChannel.setErrCode(Integer.parseInt(XmlUtil.getText(itemDevice, "ErrCode")));
+        }
+
+        deviceChannel.setEndTime(XmlUtil.getText(itemDevice, "EndTime"));
+        deviceChannel.setSecrecy(XmlUtil.getText(itemDevice, "Secrecy"));
+        deviceChannel.setIpAddress(XmlUtil.getText(itemDevice, "IPAddress"));
+        if (XmlUtil.getText(itemDevice, "Port") == null || XmlUtil.getText(itemDevice, "Port") == "") {
+            deviceChannel.setPort(0);
+        } else {
+            deviceChannel.setPort(Integer.parseInt(XmlUtil.getText(itemDevice, "Port")));
+        }
+
+
+        String longitude = XmlUtil.getText(itemDevice, "Longitude");
+        if (NumericUtil.isDouble(longitude)) {
+            deviceChannel.setLongitude(Double.parseDouble(longitude));
+        } else {
+            deviceChannel.setLongitude(0.00);
+        }
+        String latitude = XmlUtil.getText(itemDevice, "Latitude");
+        if (NumericUtil.isDouble(latitude)) {
+            deviceChannel.setLatitude(Double.parseDouble(latitude));
+        } else {
+            deviceChannel.setLatitude(0.00);
+        }
+        deviceChannel.setGpsTime(DateUtil.getNow());
+
+
+        if (XmlUtil.getText(itemDevice, "PTZType") == null || "".equals(XmlUtil.getText(itemDevice, "PTZType"))) {
+            //鍏煎INFO涓殑淇℃伅
+            Element info = itemDevice.element("Info");
+            if(XmlUtil.getText(info, "PTZType") == null || "".equals(XmlUtil.getText(info, "PTZType"))){
+                deviceChannel.setPTZType(0);
+            }else{
+                deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(info, "PTZType")));
+            }
+        } else {
+            deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(itemDevice, "PTZType")));
+        }
+        return deviceChannel;
+    }
+}
\ No newline at end of file

--
Gitblit v1.8.0