From 0e9cb5ceb6baeed953bd64a4b71d9d6bacb1798c Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 06 九月 2023 09:31:43 +0800
Subject: [PATCH] Merge pull request #1048 from lunasaw/wvp-dev-0901
---
src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java | 998 ++++++++++++++++++++++++++++++++++++++--------------------
1 files changed, 646 insertions(+), 352 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 5ada1e4..e614b9f 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,353 +1,647 @@
-package com.genersoft.iot.vmp.gb28181.utils;
-
-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 org.dom4j.Attribute;
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.Element;
-import org.dom4j.io.SAXReader;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-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);
-
- /**
- * 瑙f瀽XML涓篋ocument瀵硅薄
- *
- * @param xml 琚В鏋愮殑XMl
- *
- * @return Document
- */
- public static Element parseXml(String xml) {
- Document document = null;
- //
- StringReader sr = new StringReader(xml);
- SAXReader saxReader = new SAXReader();
- try {
- document = saxReader.read(sr);
- } catch (DocumentException e) {
- LOG.error("瑙f瀽澶辫触", e);
- }
- return null == document ? null : document.getRootElement();
- }
-
- /**
- * 鑾峰彇element瀵硅薄鐨則ext鐨勫��
- *
- * @param em 鑺傜偣鐨勫璞�
- * @param tag 鑺傜偣鐨則ag
- * @return 鑺傜偣
- */
- public static String getText(Element em, String tag) {
- if (null == em) {
- return null;
- }
- Element e = em.element(tag);
- //
- return null == e ? null : e.getText().trim();
- }
-
- /**
- * 閫掑綊瑙f瀽xml鑺傜偣锛岄�傜敤浜� 澶氳妭鐐规暟鎹�
- *
- * @param node node
- * @param nodeName nodeName
- * @return List<Map<String, Object>>
- */
- public static List<Map<String, Object>> listNodes(Element node, String nodeName) {
- if (null == node) {
- return null;
- }
- // 鍒濆鍖栬繑鍥�
- List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
- // 棣栧厛鑾峰彇褰撳墠鑺傜偣鐨勬墍鏈夊睘鎬ц妭鐐�
- List<Attribute> list = node.attributes();
-
- Map<String, Object> map = null;
- // 閬嶅巻灞炴�ц妭鐐�
- for (Attribute attribute : list) {
- if (nodeName.equals(node.getName())) {
- if (null == map) {
- map = new HashMap<String, Object>();
- listMap.add(map);
- }
- // 鍙栧埌鐨勮妭鐐瑰睘鎬ф斁鍒癿ap涓�
- map.put(attribute.getName(), attribute.getValue());
- }
-
- }
- // 閬嶅巻褰撳墠鑺傜偣涓嬬殑鎵�鏈夎妭鐐� 锛宯odeName 瑕佽В鏋愮殑鑺傜偣鍚嶇О
- // 浣跨敤閫掑綊
- Iterator<Element> iterator = node.elementIterator();
- while (iterator.hasNext()) {
- Element e = iterator.next();
- listMap.addAll(listNodes(e, nodeName));
- }
- return listMap;
- }
-
- /**
- * xml杞琷son
- *
- * @param element
- * @param json
- */
- public static void node2Json(Element element, JSONObject json) {
- // 濡傛灉鏄睘鎬�
- for (Object o : element.attributes()) {
- Attribute attr = (Attribute) o;
- if (!StringUtils.isEmpty(attr.getValue())) {
- json.put("@" + attr.getName(), attr.getValue());
- }
- }
- List<Element> chdEl = element.elements();
- if (chdEl.isEmpty() && !StringUtils.isEmpty(element.getText())) {// 濡傛灉娌℃湁瀛愬厓绱�,鍙湁涓�涓��
- json.put(element.getName(), element.getText());
- }
-
- for (Element e : chdEl) { // 鏈夊瓙鍏冪礌
- if (!e.elements().isEmpty()) { // 瀛愬厓绱犱篃鏈夊瓙鍏冪礌
- JSONObject chdjson = new JSONObject();
- node2Json(e, chdjson);
- Object o = json.get(e.getName());
- if (o != null) {
- JSONArray jsona = null;
- if (o instanceof JSONObject) { // 濡傛灉姝ゅ厓绱犲凡瀛樺湪,鍒欒浆涓簀sonArray
- JSONObject jsono = (JSONObject) o;
- json.remove(e.getName());
- jsona = new JSONArray();
- jsona.add(jsono);
- jsona.add(chdjson);
- }
- if (o instanceof JSONArray) {
- jsona = (JSONArray) o;
- jsona.add(chdjson);
- }
- json.put(e.getName(), jsona);
- } else {
- if (!chdjson.isEmpty()) {
- json.put(e.getName(), chdjson);
- }
- }
- } else { // 瀛愬厓绱犳病鏈夊瓙鍏冪礌
- for (Object o : element.attributes()) {
- Attribute attr = (Attribute) o;
- if (!StringUtils.isEmpty(attr.getValue())) {
- json.put("@" + attr.getName(), attr.getValue());
- }
- }
- if (!e.getText().isEmpty()) {
- json.put(e.getName(), e.getText());
- }
- }
- }
- }
- 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();
- }
-
- public static DeviceChannel channelContentHander(Element itemDevice, Device device){
- Element channdelNameElement = itemDevice.element("Name");
- String channelName = channdelNameElement != null ? channdelNameElement.getTextTrim().toString() : "";
- Element statusElement = itemDevice.element("Status");
- String status = statusElement != null ? statusElement.getTextTrim().toString() : "ON";
- DeviceChannel deviceChannel = new DeviceChannel();
- deviceChannel.setName(channelName);
- Element channdelIdElement = itemDevice.element("DeviceID");
- String channelId = channdelIdElement != null ? channdelIdElement.getTextTrim().toString() : "";
- deviceChannel.setChannelId(channelId);
- // 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);
- }
-
- deviceChannel.setManufacture(XmlUtil.getText(itemDevice, "Manufacturer"));
- deviceChannel.setModel(XmlUtil.getText(itemDevice, "Model"));
- deviceChannel.setOwner(XmlUtil.getText(itemDevice, "Owner"));
- deviceChannel.setCivilCode(XmlUtil.getText(itemDevice, "CivilCode"));
- deviceChannel.setBlock(XmlUtil.getText(itemDevice, "Block"));
- deviceChannel.setAddress(XmlUtil.getText(itemDevice, "Address"));
- String businessGroupID = XmlUtil.getText(itemDevice, "BusinessGroupID");
- if (XmlUtil.getText(itemDevice, "Parental") == null
- || XmlUtil.getText(itemDevice, "Parental").equals("")) {
- if (deviceChannel.getChannelId().length() <= 10
- || (deviceChannel.getChannelId().length() == 20 && (
- Integer.parseInt(deviceChannel.getChannelId().substring(10, 13)) == 215
- || Integer.parseInt(deviceChannel.getChannelId().substring(10, 13)) == 216
- )
- )
- ) {
- deviceChannel.setParental(1);
- }else {
- deviceChannel.setParental(0);
- }
- } else {
- // 鐢变簬娴峰悍浼氶敊璇殑鍙戦��65535浣滀负杩欓噷鐨勫彇鍊�,鎵�浠ヨ繖閲岄櫎闈炴槸0鍚﹀垯璁や负鏄�1
- deviceChannel.setParental(Integer.parseInt(XmlUtil.getText(itemDevice, "Parental")) == 1?1:0);
- }
- /**
- * 琛屾斂鍖哄垝灞曠ず璁惧鏍戜笌涓氬姟鍒嗙粍灞曠ず璁惧鏍戞槸涓ょ涓嶅悓鐨勬ā寮�
- * 琛屾斂鍖哄垝灞曠ず璁惧鏍� 鍚勪釜鐩綍涔嬮棿涓昏闈燿eviceId鍋氬叧鑱�,鎽勫儚澶撮�氳繃CivilCode鎸囧畾鍏跺睘浜庨偅涓鏀垮尯鍒�;閮芥槸涓嶈秴杩囧崄浣嶇殑缂栧彿; 缁撴瀯濡備笅:
- * 娌冲寳鐪�
- * --> 鐭冲搴勫競
- * --> 鎽勫儚澶�
- * --> 姝e畾鍘�
- * --> 鎽勫儚澶�
- * --> 鎽勫儚澶�
- *
- * 涓氬姟鍒嗙粍灞曠ず璁惧鏍戞槸椤剁骇鏄笟鍔″垎缁�,鍏朵笅鐨勮櫄鎷熺粍缁囬潬BusinessGroupID鎸囧畾鍏舵墍灞炵殑涓氬姟鍒嗙粍;鎽勫儚澶撮�氳繃ParentId鏉ユ寚瀹氬叾鎵�灞炰簬鐨勮櫄鎷熺粍缁�:
- * 涓氬姟鍒嗙粍
- * --> 铏氭嫙缁勭粐
- * --> 鎽勫儚澶�
- * --> 铏氭嫙缁勭粐
- * --> 鎽勫儚澶�
- * --> 鎽勫儚澶�
- */
- String parentId = XmlUtil.getText(itemDevice, "ParentID");
- if (parentId != null) {
- if (parentId.contains("/")) {
- String lastParentId = parentId.substring(parentId.lastIndexOf("/") + 1);
- deviceChannel.setParentId(lastParentId);
- }else {
- deviceChannel.setParentId(parentId);
- }
- }else {
- if (deviceChannel.getChannelId().length() <= 10) { // 姝ゆ椂涓鸿鏀垮尯鍒�, 涓婁笅绾ц鏀垮尯鍒掍娇鐢―eviceId鍏宠仈
- deviceChannel.setParentId(deviceChannel.getChannelId().substring(0, deviceChannel.getChannelId().length() - 2));
- }else if (deviceChannel.getChannelId().length() == 20) {
- if (Integer.parseInt(deviceChannel.getChannelId().substring(10, 13)) == 216) { // 铏氭嫙缁勭粐
- deviceChannel.setParentId(businessGroupID);
- }else if (Integer.parseInt(device.getDeviceId().substring(10, 13) )== 118) {//NVR 濡傛灉涓婄骇璁惧缂栧彿鏄疦VR鍒欑洿鎺ュ皢NVR鐨勭紪鍙疯缃粰閫氶亾鐨勪笂绾х紪鍙�
- deviceChannel.setParentId(device.getDeviceId());
- }else if (deviceChannel.getCivilCode() != null) {
- // 璁惧锛� 鏃爌arentId鐨�20浣嶆槸浣跨敤CivilCode琛ㄧず涓婄骇鐨勮澶囷紝
- // 娉細215 涓氬姟鍒嗙粍鏄渶瑕佹湁parentId鐨�
- deviceChannel.setParentId(deviceChannel.getCivilCode());
- }
- }else {
- deviceChannel.setParentId(deviceChannel.getDeviceId());
- }
- }
-
- if (XmlUtil.getText(itemDevice, "SafetyWay") == null
- || XmlUtil.getText(itemDevice, "SafetyWay") == "") {
- deviceChannel.setSafetyWay(0);
- } else {
- deviceChannel.setSafetyWay(Integer.parseInt(XmlUtil.getText(itemDevice, "SafetyWay")));
- }
- if (XmlUtil.getText(itemDevice, "RegisterWay") == null
- || XmlUtil.getText(itemDevice, "RegisterWay") == "") {
- deviceChannel.setRegisterWay(1);
- } else {
- deviceChannel.setRegisterWay(Integer.parseInt(XmlUtil.getText(itemDevice, "RegisterWay")));
- }
- deviceChannel.setCertNum(XmlUtil.getText(itemDevice, "CertNum"));
- 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")));
- }
- deviceChannel.setPassword(XmlUtil.getText(itemDevice, "Password"));
- if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Longitude"))) {
- deviceChannel.setLongitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Longitude")));
- } else {
- deviceChannel.setLongitude(0.00);
- }
- if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Latitude"))) {
- deviceChannel.setLatitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Latitude")));
- } else {
- deviceChannel.setLatitude(0.00);
- }
- if (deviceChannel.getLongitude()*deviceChannel.getLatitude() > 0) {
- if ("WGS84".equals(device.getGeoCoordSys())) {
- deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
- deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
- Double[] position = Coordtransform.WGS84ToGCJ02(deviceChannel.getLongitude(), deviceChannel.getLatitude());
- deviceChannel.setLongitudeGcj02(position[0]);
- deviceChannel.setLatitudeGcj02(position[1]);
- }else if ("GCJ02".equals(device.getGeoCoordSys())) {
- deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
- deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
- Double[] position = Coordtransform.GCJ02ToWGS84(deviceChannel.getLongitude(), deviceChannel.getLatitude());
- deviceChannel.setLongitudeWgs84(position[0]);
- deviceChannel.setLatitudeWgs84(position[1]);
- }else {
- deviceChannel.setLongitudeGcj02(0.00);
- deviceChannel.setLatitudeGcj02(0.00);
- deviceChannel.setLongitudeWgs84(0.00);
- deviceChannel.setLatitudeWgs84(0.00);
- }
- }else {
- deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
- deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
- deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
- deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
- }
- 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")));
- }
- deviceChannel.setHasAudio(true); // 榛樿鍚湁闊抽锛屾挱鏀炬椂鍐嶆鏌ユ槸鍚︽湁闊抽鍙婃槸鍚AC
- return deviceChannel;
- }
+package com.genersoft.iot.vmp.gb28181.utils;
+
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
+import com.genersoft.iot.vmp.common.CivilCodePo;
+import com.genersoft.iot.vmp.conf.CivilCodeFileConf;
+import com.genersoft.iot.vmp.gb28181.bean.Device;
+import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
+import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
+import com.genersoft.iot.vmp.utils.DateUtil;
+import org.apache.commons.lang3.math.NumberUtils;
+import org.dom4j.Attribute;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.ObjectUtils;
+import org.springframework.util.ReflectionUtils;
+
+import javax.sip.RequestEvent;
+import javax.sip.message.Request;
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.*;
+
+/**
+ * 鍩轰簬dom4j鐨勫伐鍏峰寘
+ *
+ *
+ */
+public class XmlUtil {
+ /**
+ * 鏃ュ織鏈嶅姟
+ */
+ private static Logger logger = LoggerFactory.getLogger(XmlUtil.class);
+
+ /**
+ * 瑙f瀽XML涓篋ocument瀵硅薄
+ *
+ * @param xml 琚В鏋愮殑XMl
+ *
+ * @return Document
+ */
+ public static Element parseXml(String xml) {
+ Document document = null;
+ //
+ StringReader sr = new StringReader(xml);
+ SAXReader saxReader = new SAXReader();
+ try {
+ document = saxReader.read(sr);
+ } catch (DocumentException e) {
+ logger.error("瑙f瀽澶辫触", e);
+ }
+ return null == document ? null : document.getRootElement();
+ }
+
+ /**
+ * 鑾峰彇element瀵硅薄鐨則ext鐨勫��
+ *
+ * @param em 鑺傜偣鐨勫璞�
+ * @param tag 鑺傜偣鐨則ag
+ * @return 鑺傜偣
+ */
+ public static String getText(Element em, String tag) {
+ if (null == em) {
+ return null;
+ }
+ Element e = em.element(tag);
+ //
+ return null == e ? null : e.getText().trim();
+ }
+
+ /**
+ * 閫掑綊瑙f瀽xml鑺傜偣锛岄�傜敤浜� 澶氳妭鐐规暟鎹�
+ *
+ * @param node node
+ * @param nodeName nodeName
+ * @return List<Map<String, Object>>
+ */
+ public static List<Map<String, Object>> listNodes(Element node, String nodeName) {
+ if (null == node) {
+ return null;
+ }
+ // 鍒濆鍖栬繑鍥�
+ List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
+ // 棣栧厛鑾峰彇褰撳墠鑺傜偣鐨勬墍鏈夊睘鎬ц妭鐐�
+ List<Attribute> list = node.attributes();
+
+ Map<String, Object> map = null;
+ // 閬嶅巻灞炴�ц妭鐐�
+ for (Attribute attribute : list) {
+ if (nodeName.equals(node.getName())) {
+ if (null == map) {
+ map = new HashMap<String, Object>();
+ listMap.add(map);
+ }
+ // 鍙栧埌鐨勮妭鐐瑰睘鎬ф斁鍒癿ap涓�
+ map.put(attribute.getName(), attribute.getValue());
+ }
+
+ }
+ // 閬嶅巻褰撳墠鑺傜偣涓嬬殑鎵�鏈夎妭鐐� 锛宯odeName 瑕佽В鏋愮殑鑺傜偣鍚嶇О
+ // 浣跨敤閫掑綊
+ Iterator<Element> iterator = node.elementIterator();
+ while (iterator.hasNext()) {
+ Element e = iterator.next();
+ listMap.addAll(listNodes(e, nodeName));
+ }
+ return listMap;
+ }
+
+ /**
+ * xml杞琷son
+ *
+ * @param element
+ * @param json
+ */
+ public static void node2Json(Element element, JSONObject json) {
+ // 濡傛灉鏄睘鎬�
+ for (Object o : element.attributes()) {
+ Attribute attr = (Attribute) o;
+ if (!ObjectUtils.isEmpty(attr.getValue())) {
+ json.put("@" + attr.getName(), attr.getValue());
+ }
+ }
+ List<Element> chdEl = element.elements();
+ if (chdEl.isEmpty() && !ObjectUtils.isEmpty(element.getText())) {// 濡傛灉娌℃湁瀛愬厓绱�,鍙湁涓�涓��
+ json.put(element.getName(), element.getText());
+ }
+
+ for (Element e : chdEl) { // 鏈夊瓙鍏冪礌
+ if (!e.elements().isEmpty()) { // 瀛愬厓绱犱篃鏈夊瓙鍏冪礌
+ JSONObject chdjson = new JSONObject();
+ node2Json(e, chdjson);
+ Object o = json.get(e.getName());
+ if (o != null) {
+ JSONArray jsona = null;
+ if (o instanceof JSONObject) { // 濡傛灉姝ゅ厓绱犲凡瀛樺湪,鍒欒浆涓簀sonArray
+ JSONObject jsono = (JSONObject) o;
+ json.remove(e.getName());
+ jsona = new JSONArray();
+ jsona.add(jsono);
+ jsona.add(chdjson);
+ }
+ if (o instanceof JSONArray) {
+ jsona = (JSONArray) o;
+ jsona.add(chdjson);
+ }
+ json.put(e.getName(), jsona);
+ } else {
+ if (!chdjson.isEmpty()) {
+ json.put(e.getName(), chdjson);
+ }
+ }
+ } else { // 瀛愬厓绱犳病鏈夊瓙鍏冪礌
+ for (Object o : element.attributes()) {
+ Attribute attr = (Attribute) o;
+ if (!ObjectUtils.isEmpty(attr.getValue())) {
+ json.put("@" + attr.getName(), attr.getValue());
+ }
+ }
+ if (!e.getText().isEmpty()) {
+ json.put(e.getName(), e.getText());
+ }
+ }
+ }
+ }
+ 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 channelContentHandler(Element itemDevice, Device device, String event, CivilCodeFileConf civilCodeFileConf){
+ 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;
+ }
+ Element nameElement = itemDevice.element("Name");
+ if (nameElement != null) {
+ deviceChannel.setName(nameElement.getText());
+ }
+ if(channelId.length() <= 8) {
+ deviceChannel.setHasAudio(false);
+ CivilCodePo parentCode = civilCodeFileConf.getParentCode(channelId);
+ if (parentCode != null) {
+ deviceChannel.setParentId(parentCode.getCode());
+ deviceChannel.setCivilCode(parentCode.getCode());
+ }else {
+ logger.warn("[xml瑙f瀽] 鏃犳硶纭畾琛屾斂鍖哄垝{}鐨勪笂绾ц鏀垮尯鍒�", channelId);
+ }
+ deviceChannel.setStatus(true);
+ return deviceChannel;
+ }else {
+ if(channelId.length() != 20) {
+ logger.warn("[xml瑙f瀽] 澶辫触锛岀紪鍙蜂笉绗﹀悎鍥芥爣28181瀹氫箟锛� {}", channelId);
+ return null;
+ }
+
+ int code = Integer.parseInt(channelId.substring(10, 13));
+ if (code == 136 || code == 137 || code == 138) {
+ deviceChannel.setHasAudio(true);
+ }else {
+ deviceChannel.setHasAudio(false);
+ }
+ // 璁惧鍘傚晢
+ String manufacturer = getText(itemDevice, "Manufacturer");
+ // 璁惧鍨嬪彿
+ String model = getText(itemDevice, "Model");
+ // 璁惧褰掑睘
+ String owner = getText(itemDevice, "Owner");
+ // 琛屾斂鍖哄煙
+ String civilCode = getText(itemDevice, "CivilCode");
+ // 铏氭嫙缁勭粐鎵�灞炵殑涓氬姟鍒嗙粍ID,涓氬姟鍒嗙粍鏍规嵁鐗瑰畾鐨勪笟鍔¢渶姹傚埗瀹�,涓�涓笟鍔″垎缁勫寘鍚竴缁勭壒瀹氱殑铏氭嫙缁勭粐
+ String businessGroupID = getText(itemDevice, "BusinessGroupID");
+ // 鐖惰澶�/鍖哄煙/绯荤粺ID
+ String parentID = getText(itemDevice, "ParentID");
+ if (parentID != null && parentID.equalsIgnoreCase("null")) {
+ parentID = null;
+ }
+ // 娉ㄥ唽鏂瑰紡(蹇呴��)缂虹渷涓�1;1:绗﹀悎IETFRFC3261鏍囧噯鐨勮璇佹敞鍐屾ā寮�;2:鍩轰簬鍙d护鐨勫弻鍚戣璇佹敞鍐屾ā寮�;3:鍩轰簬鏁板瓧璇佷功鐨勫弻鍚戣璇佹敞鍐屾ā寮�
+ String registerWay = getText(itemDevice, "RegisterWay");
+ // 淇濆瘑灞炴��(蹇呴��)缂虹渷涓�0;0:涓嶆秹瀵�,1:娑夊瘑
+ String secrecy = getText(itemDevice, "Secrecy");
+ // 瀹夎鍦板潃
+ String address = getText(itemDevice, "Address");
+
+ switch (code){
+ case 200:
+ // 绯荤粺鐩綍
+ if (!ObjectUtils.isEmpty(manufacturer)) {
+ deviceChannel.setManufacture(manufacturer);
+ }
+ if (!ObjectUtils.isEmpty(model)) {
+ deviceChannel.setModel(model);
+ }
+ if (!ObjectUtils.isEmpty(owner)) {
+ deviceChannel.setOwner(owner);
+ }
+ if (!ObjectUtils.isEmpty(civilCode)) {
+ deviceChannel.setCivilCode(civilCode);
+ deviceChannel.setParentId(civilCode);
+ }else {
+ if (!ObjectUtils.isEmpty(parentID)) {
+ deviceChannel.setParentId(parentID);
+ }
+ }
+ if (!ObjectUtils.isEmpty(address)) {
+ deviceChannel.setAddress(address);
+ }
+ deviceChannel.setStatus(true);
+ if (!ObjectUtils.isEmpty(registerWay)) {
+ try {
+ deviceChannel.setRegisterWay(Integer.parseInt(registerWay));
+ }catch (NumberFormatException exception) {
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁鑾峰彇registerWay澶辫触锛� {}", registerWay);
+ }
+ }
+ if (!ObjectUtils.isEmpty(secrecy)) {
+ deviceChannel.setSecrecy(secrecy);
+ }
+ return deviceChannel;
+ case 215:
+ // 涓氬姟鍒嗙粍
+ deviceChannel.setStatus(true);
+ if (!ObjectUtils.isEmpty(parentID)) {
+ if (!parentID.trim().equalsIgnoreCase(device.getDeviceId())) {
+ deviceChannel.setParentId(parentID);
+ }
+ }else {
+ logger.warn("[xml瑙f瀽] 涓氬姟鍒嗙粍鏁版嵁涓己灏戝叧閿俊鎭�->ParentId");
+ if (!ObjectUtils.isEmpty(civilCode)) {
+ deviceChannel.setCivilCode(civilCode);
+ }
+ }
+ break;
+ case 216:
+ // 铏氭嫙缁勭粐
+ deviceChannel.setStatus(true);
+ if (!ObjectUtils.isEmpty(businessGroupID)) {
+ deviceChannel.setBusinessGroupId(businessGroupID);
+ }
+
+ if (!ObjectUtils.isEmpty(parentID)) {
+ if (parentID.contains("/")) {
+ String[] parentIdArray = parentID.split("/");
+ parentID = parentIdArray[parentIdArray.length - 1];
+ }
+ deviceChannel.setParentId(parentID);
+ }else {
+ if (!ObjectUtils.isEmpty(businessGroupID)) {
+ deviceChannel.setParentId(businessGroupID);
+ }
+ }
+ break;
+ default:
+ // 璁惧鐩綍
+ if (!ObjectUtils.isEmpty(manufacturer)) {
+ deviceChannel.setManufacture(manufacturer);
+ }
+ if (!ObjectUtils.isEmpty(model)) {
+ deviceChannel.setModel(model);
+ }
+ if (!ObjectUtils.isEmpty(owner)) {
+ deviceChannel.setOwner(owner);
+ }
+ if (!ObjectUtils.isEmpty(civilCode)
+ && civilCode.length() <= 8
+ && NumberUtils.isParsable(civilCode)
+ && civilCode.length()%2 == 0
+ ) {
+ deviceChannel.setCivilCode(civilCode);
+ }
+ if (!ObjectUtils.isEmpty(businessGroupID)) {
+ deviceChannel.setBusinessGroupId(businessGroupID);
+ }
+
+ // 璀﹀尯
+ String block = getText(itemDevice, "Block");
+ if (!ObjectUtils.isEmpty(block)) {
+ deviceChannel.setBlock(block);
+ }
+ if (!ObjectUtils.isEmpty(address)) {
+ deviceChannel.setAddress(address);
+ }
+
+ if (!ObjectUtils.isEmpty(secrecy)) {
+ deviceChannel.setSecrecy(secrecy);
+ }
+
+ // 褰撲负璁惧鏃�,鏄惁鏈夊瓙璁惧(蹇呴��)1鏈�,0娌℃湁
+ String parental = getText(itemDevice, "Parental");
+ if (!ObjectUtils.isEmpty(parental)) {
+ try {
+ // 鐢变簬娴峰悍浼氶敊璇殑鍙戦��65535浣滀负杩欓噷鐨勫彇鍊�,鎵�浠ヨ繖閲岄櫎闈炴槸0鍚﹀垯璁や负鏄�1
+ if (!ObjectUtils.isEmpty(parental) && parental.length() == 1 && Integer.parseInt(parental) == 0) {
+ deviceChannel.setParental(0);
+ }else {
+ deviceChannel.setParental(1);
+ }
+ }catch (NumberFormatException e) {
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁鑾峰彇 parental澶辫触锛� {}", parental);
+ }
+ }
+ // 鐖惰澶�/鍖哄煙/绯荤粺ID
+
+ if (!ObjectUtils.isEmpty(parentID) ) {
+ if (parentID.contains("/")) {
+ String[] parentIdArray = parentID.split("/");
+ deviceChannel.setParentId(parentIdArray[parentIdArray.length - 1]);
+ }else {
+ if (parentID.length()%2 == 0) {
+ deviceChannel.setParentId(parentID);
+ }else {
+ logger.warn("[xml瑙f瀽] 涓嶈鑼冪殑parentID锛歿}, 宸茶垗寮�", parentID);
+ }
+ }
+ }else {
+ if (!ObjectUtils.isEmpty(businessGroupID)) {
+ deviceChannel.setParentId(businessGroupID);
+ }else {
+ if (!ObjectUtils.isEmpty(deviceChannel.getCivilCode())) {
+ deviceChannel.setParentId(deviceChannel.getCivilCode());
+ }
+ }
+ }
+ // 娉ㄥ唽鏂瑰紡
+ if (!ObjectUtils.isEmpty(registerWay)) {
+ try {
+ int registerWayInt = Integer.parseInt(registerWay);
+ deviceChannel.setRegisterWay(registerWayInt);
+ }catch (NumberFormatException exception) {
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁鑾峰彇registerWay澶辫触锛� {}", registerWay);
+ deviceChannel.setRegisterWay(1);
+ }
+ }else {
+ deviceChannel.setRegisterWay(1);
+ }
+
+ // 淇′护瀹夊叏妯″紡(鍙��)缂虹渷涓�0; 0:涓嶉噰鐢�;2:S/MIME 绛惧悕鏂瑰紡;3:S/MIME鍔犲瘑绛惧悕鍚屾椂閲囩敤鏂瑰紡;4:鏁板瓧鎽樿鏂瑰紡
+ String safetyWay = getText(itemDevice, "SafetyWay");
+ if (!ObjectUtils.isEmpty(safetyWay)) {
+ try {
+ deviceChannel.setSafetyWay(Integer.parseInt(safetyWay));
+ }catch (NumberFormatException e) {
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁鑾峰彇 safetyWay澶辫触锛� {}", safetyWay);
+ }
+ }
+
+ // 璇佷功搴忓垪鍙�(鏈夎瘉涔︾殑璁惧蹇呴��)
+ String certNum = getText(itemDevice, "CertNum");
+ if (!ObjectUtils.isEmpty(certNum)) {
+ deviceChannel.setCertNum(certNum);
+ }
+
+ // 璇佷功鏈夋晥鏍囪瘑(鏈夎瘉涔︾殑璁惧蹇呴��)缂虹渷涓�0;璇佷功鏈夋晥鏍囪瘑:0:鏃犳晥 1:鏈夋晥
+ String certifiable = getText(itemDevice, "Certifiable");
+ if (!ObjectUtils.isEmpty(certifiable)) {
+ try {
+ deviceChannel.setCertifiable(Integer.parseInt(certifiable));
+ }catch (NumberFormatException e) {
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁鑾峰彇 Certifiable澶辫触锛� {}", certifiable);
+ }
+ }
+
+ // 鏃犳晥鍘熷洜鐮�(鏈夎瘉涔︿笖璇佷功鏃犳晥鐨勮澶囧繀閫�)
+ String errCode = getText(itemDevice, "ErrCode");
+ if (!ObjectUtils.isEmpty(errCode)) {
+ try {
+ deviceChannel.setErrCode(Integer.parseInt(errCode));
+ }catch (NumberFormatException e) {
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁鑾峰彇 ErrCode澶辫触锛� {}", errCode);
+ }
+ }
+
+ // 璇佷功缁堟鏈夋晥鏈�(鏈夎瘉涔︾殑璁惧蹇呴��)
+ String endTime = getText(itemDevice, "EndTime");
+ if (!ObjectUtils.isEmpty(endTime)) {
+ deviceChannel.setEndTime(endTime);
+ }
+
+
+ // 璁惧/鍖哄煙/绯荤粺IP鍦板潃
+ String ipAddress = getText(itemDevice, "IPAddress");
+ if (!ObjectUtils.isEmpty(ipAddress)) {
+ deviceChannel.setIpAddress(ipAddress);
+ }
+
+ // 璁惧/鍖哄煙/绯荤粺绔彛
+ String port = getText(itemDevice, "Port");
+ if (!ObjectUtils.isEmpty(port)) {
+ try {
+ deviceChannel.setPort(Integer.parseInt(port));
+ }catch (NumberFormatException e) {
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁鑾峰彇 Port澶辫触锛� {}", port);
+ }
+ }
+
+ // 璁惧鍙d护
+ String password = getText(itemDevice, "Password");
+ if (!ObjectUtils.isEmpty(password)) {
+ deviceChannel.setPassword(password);
+ }
+
+
+ // 璁惧鐘舵��
+ String status = getText(itemDevice, "Status");
+ if (status != null) {
+ // ONLINE OFFLINE HIKVISION DS-7716N-E4 NVR鐨勫吋瀹规�у鐞�
+ if (status.equals("ON") || status.equals("On") || status.equals("ONLINE") || status.equals("OK")) {
+ deviceChannel.setStatus(true);
+ }
+ if (status.equals("OFF") || status.equals("Off") || status.equals("OFFLINE")) {
+ deviceChannel.setStatus(false);
+ }
+ }else {
+ deviceChannel.setStatus(true);
+ }
+
+ // 缁忓害
+ String longitude = getText(itemDevice, "Longitude");
+ if (NumericUtil.isDouble(longitude)) {
+ deviceChannel.setLongitude(Double.parseDouble(longitude));
+ } else {
+ deviceChannel.setLongitude(0.00);
+ }
+
+ // 绾害
+ String latitude = getText(itemDevice, "Latitude");
+ if (NumericUtil.isDouble(latitude)) {
+ deviceChannel.setLatitude(Double.parseDouble(latitude));
+ } else {
+ deviceChannel.setLatitude(0.00);
+ }
+
+ deviceChannel.setGpsTime(DateUtil.getNow());
+
+ // -鎽勫儚鏈虹被鍨嬫墿灞�,鏍囪瘑鎽勫儚鏈虹被鍨�:1-鐞冩満;2-鍗婄悆;3-鍥哄畾鏋満;4-閬ユ帶鏋満銆傚綋鐩綍椤逛负鎽勫儚鏈烘椂鍙��
+ String ptzType = getText(itemDevice, "PTZType");
+ if (ObjectUtils.isEmpty(ptzType)) {
+ //鍏煎INFO涓殑淇℃伅
+ Element info = itemDevice.element("Info");
+ String ptzTypeFromInfo = XmlUtil.getText(info, "PTZType");
+ if(!ObjectUtils.isEmpty(ptzTypeFromInfo)){
+ try {
+ deviceChannel.setPTZType(Integer.parseInt(ptzTypeFromInfo));
+ }catch (NumberFormatException e){
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁info涓幏鍙朠TZType澶辫触锛� {}", ptzTypeFromInfo);
+ }
+ }
+ } else {
+ try {
+ deviceChannel.setPTZType(Integer.parseInt(ptzType));
+ }catch (NumberFormatException e){
+ logger.warn("[xml瑙f瀽] 浠庨�氶亾鏁版嵁涓幏鍙朠TZType澶辫触锛� {}", ptzType);
+ }
+ }
+
+ // TODO 鎽勫儚鏈轰綅缃被鍨嬫墿灞曘��
+ // 1-鐪侀檯妫�鏌ョ珯銆�
+ // 2-鍏氭斂鏈哄叧銆�
+ // 3-杞︾珯鐮佸ご銆�
+ // 4-涓績骞垮満銆�
+ // 5-浣撹偛鍦洪銆�
+ // 6-鍟嗕笟涓績銆�
+ // 7-瀹楁暀鍦烘墍銆�
+ // 8-鏍″洯鍛ㄨ竟銆�
+ // 9-娌诲畨澶嶆潅鍖哄煙銆�
+ // 10-浜ら�氬共绾裤��
+ // String positionType = getText(itemDevice, "PositionType");
+
+ // TODO 鎽勫儚鏈哄畨瑁呬綅缃澶栥�佸鍐呭睘鎬с��1-瀹ゅ銆�2-瀹ゅ唴銆�
+ // String roomType = getText(itemDevice, "RoomType");
+ // TODO 鎽勫儚鏈虹敤閫斿睘鎬�
+ // String useType = getText(itemDevice, "UseType");
+ // TODO 鎽勫儚鏈鸿ˉ鍏夊睘鎬с��1-鏃犺ˉ鍏夈��2-绾㈠琛ュ厜銆�3-鐧藉厜琛ュ厜
+ // String supplyLightType = getText(itemDevice, "SupplyLightType");
+ // TODO 鎽勫儚鏈虹洃瑙嗘柟浣嶅睘鎬с��1-涓溿��2-瑗裤��3-鍗椼��4-鍖椼��5-涓滃崡銆�6-涓滃寳銆�7-瑗垮崡銆�8-瑗垮寳銆�
+ // String directionType = getText(itemDevice, "DirectionType");
+ // TODO 鎽勫儚鏈烘敮鎸佺殑鍒嗚鲸鐜�,鍙湁澶氫釜鍒嗚鲸鐜囧��,鍚勪釜鍙栧�奸棿浠モ��/鈥濆垎闅斻�傚垎杈ㄧ巼鍙栧�煎弬瑙侀檮褰� F涓璖DPf瀛楁瑙勫畾
+ // String resolution = getText(itemDevice, "Resolution");
+
+ // TODO 涓嬭浇鍊嶉�熻寖鍥�(鍙��),鍚勫彲閫夊弬鏁颁互鈥�/鈥濆垎闅�,濡傝澶囨敮鎸�1,2,4鍊嶉�熶笅杞藉垯搴斿啓涓衡��1/2/4
+ // String downloadSpeed = getText(itemDevice, "DownloadSpeed");
+ // TODO 绌哄煙缂栫爜鑳藉姏,鍙栧��0:涓嶆敮鎸�;1:1绾у寮�(1涓寮哄眰);2:2绾у寮�(2涓寮哄眰);3:3绾у寮�(3涓寮哄眰)
+ // String svcSpaceSupportMode = getText(itemDevice, "SVCSpaceSupportMode");
+ // TODO 鏃跺煙缂栫爜鑳藉姏,鍙栧��0:涓嶆敮鎸�;1:1绾у寮�;2:2绾у寮�;3:3绾у寮�
+ // String svcTimeSupportMode = getText(itemDevice, "SVCTimeSupportMode");
+
+
+ deviceChannel.setSecrecy(secrecy);
+ break;
+ }
+ }
+
+ return deviceChannel;
+ }
+
+ /**
+ * 鏂板鏂规硶鏀寔鍐呴儴宓屽
+ *
+ * @param element xmlElement
+ * @param clazz 缁撴灉绫�
+ * @param <T> 娉涘瀷
+ * @return 缁撴灉瀵硅薄
+ * @throws NoSuchMethodException
+ * @throws InvocationTargetException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ */
+ public static <T> T loadElement(Element element, Class<T> clazz) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
+ Field[] fields = clazz.getDeclaredFields();
+ T t = clazz.getDeclaredConstructor().newInstance();
+ for (Field field : fields) {
+ ReflectionUtils.makeAccessible(field);
+ MessageElement annotation = field.getAnnotation(MessageElement.class);
+ if (annotation == null) {
+ continue;
+ }
+ String value = annotation.value();
+ String subVal = annotation.subVal();
+ Element element1 = element.element(value);
+ if (element1 == null) {
+ continue;
+ }
+ if ("".equals(subVal)) {
+ // 鏃犱笅绾ф暟鎹�
+ Object fieldVal = element1.isTextOnly() ? element1.getText() : loadElement(element1, field.getType());
+ Object o = simpleTypeDeal(field.getType(), fieldVal);
+ ReflectionUtils.setField(field, t, o);
+ } else {
+ // 瀛樺湪涓嬬骇鏁版嵁
+ ArrayList<Object> list = new ArrayList<>();
+ Type genericType = field.getGenericType();
+ if (!(genericType instanceof ParameterizedType)) {
+ continue;
+ }
+ Class<?> aClass = (Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0];
+ for (Element element2 : element1.elements(subVal)) {
+ list.add(loadElement(element2, aClass));
+ }
+ ReflectionUtils.setField(field, t, list);
+ }
+ }
+ return t;
+ }
+
+ /**
+ * 绠�鍗曠被鍨嬪鐞�
+ *
+ * @param tClass
+ * @param val
+ * @return
+ */
+ private static Object simpleTypeDeal(Class<?> tClass, Object val) {
+ if (tClass.equals(String.class)) {
+ return val.toString();
+ }
+ if (tClass.equals(Integer.class)) {
+ return Integer.valueOf(val.toString());
+ }
+ if (tClass.equals(Double.class)) {
+ return Double.valueOf(val.toString());
+ }
+ if (tClass.equals(Long.class)) {
+ return Long.valueOf(val.toString());
+ }
+ return val;
+ }
}
\ No newline at end of file
--
Gitblit v1.8.0