From ccc0a99d6894844d83d751b924cfebe74da7826c Mon Sep 17 00:00:00 2001
From: ‘sxh’ <1632740646@qq.com>
Date: 星期四, 15 六月 2023 11:20:43 +0800
Subject: [PATCH] 同步主线的代码,保持一致性

---
 src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java |   96 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 87 insertions(+), 9 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 0892243..750cd8b 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,10 +1,9 @@
 package com.genersoft.iot.vmp.gb28181.utils;
 
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.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;
@@ -15,12 +14,16 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.util.ObjectUtils;
-import org.springframework.util.StringUtils;
+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.*;
 
 /**
@@ -252,7 +255,7 @@
         }
         if (channelType.equals(ChannelType.CivilCode)) {
             // 琛屾斂鍖哄垝鍏朵粬瀛楁娌″繀瑕佽瘑鍒簡锛岄粯璁ゅ湪绾垮嵆鍙�
-            deviceChannel.setStatus(1);
+            deviceChannel.setStatus(true);
             deviceChannel.setParental(1);
             deviceChannel.setCreateTime(DateUtil.getNow());
             deviceChannel.setUpdateTime(DateUtil.getNow());
@@ -298,6 +301,7 @@
             }else {
                 deviceChannel.setParentId(parentId);
             }
+            // 鍏煎璁惧閫氶亾淇℃伅涓嚜宸变负鑷繁鐖惰妭鐐圭殑鎯呭喌
             if (deviceChannel.getParentId().equals(deviceChannel.getChannelId())) {
                 deviceChannel.setParentId(null);
             }
@@ -305,7 +309,7 @@
         deviceChannel.setBusinessGroupId(businessGroupID);
         if (channelType.equals(ChannelType.BusinessGroup) || channelType.equals(ChannelType.VirtualOrganization)) {
             // 涓氬姟鍒嗙粍鍜岃櫄鎷熺粍缁� 鍏朵粬瀛楁娌″繀瑕佽瘑鍒簡锛岄粯璁ゅ湪绾垮嵆鍙�
-            deviceChannel.setStatus(1);
+            deviceChannel.setStatus(true);
             deviceChannel.setParental(1);
             deviceChannel.setCreateTime(DateUtil.getNow());
             deviceChannel.setUpdateTime(DateUtil.getNow());
@@ -318,13 +322,13 @@
             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);
+                deviceChannel.setStatus(true);
             }
             if (status.equals("OFF") || status.equals("Off") || status.equals("OFFLINE")) {
-                deviceChannel.setStatus(0);
+                deviceChannel.setStatus(false);
             }
         }else {
-            deviceChannel.setStatus(1);
+            deviceChannel.setStatus(true);
         }
         // 璇嗗埆鑷甫鐨勭洰褰曟爣璇�
         String parental = XmlUtil.getText(itemDevice, "Parental");
@@ -394,6 +398,7 @@
         } else {
             deviceChannel.setLatitude(0.00);
         }
+
         deviceChannel.setGpsTime(DateUtil.getNow());
 
 
@@ -408,6 +413,79 @@
         } else {
             deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(itemDevice, "PTZType")));
         }
+
         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