jiang
2022-07-13 fc89b7b517e191fb6c9b66fa86dd05ebab0f54c5
src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
@@ -2,7 +2,9 @@
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.utils.DateUtil;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
@@ -180,7 +182,7 @@
        return xml.getRootElement();
    }
    public static DeviceChannel channelContentHander(Element itemDevice){
    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");
@@ -206,12 +208,40 @@
        deviceChannel.setAddress(XmlUtil.getText(itemDevice, "Address"));
        String businessGroupID = XmlUtil.getText(itemDevice, "BusinessGroupID");
        if (XmlUtil.getText(itemDevice, "Parental") == null
                || XmlUtil.getText(itemDevice, "Parental") == "") {
            deviceChannel.setParental(0);
                || 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 {
            deviceChannel.setParental(Integer.parseInt(XmlUtil.getText(itemDevice, "Parental")));
            // 由于海康会错误的发送65535作为这里的取值,所以这里除非是0否则认为是1
            deviceChannel.setParental(Integer.parseInt(XmlUtil.getText(itemDevice, "Parental")) == 1?1:0);
        }
        deviceChannel.setParentId(XmlUtil.getText(itemDevice, "ParentID"));
        /**
         * 行政区划展示设备树与业务分组展示设备树是两种不同的模式
         * 行政区划展示设备树 各个目录之间主要靠deviceId做关联,摄像头通过CivilCode指定其属于那个行政区划;都是不超过十位的编号; 结构如下:
         * 河北省
         *    --> 石家庄市
         *          --> 摄像头
         *          --> 正定县
         *                  --> 摄像头
         *                  --> 摄像头
         *
         * 业务分组展示设备树是顶级是业务分组,其下的虚拟组织靠BusinessGroupID指定其所属的业务分组;摄像头通过ParentId来指定其所属于的虚拟组织:
         * 业务分组
         *    --> 虚拟组织
         *         --> 摄像头
         *         --> 虚拟组织
         *             --> 摄像头
         *             --> 摄像头
         */
        String parentId = XmlUtil.getText(itemDevice, "ParentID");
        if (parentId != null) {
            if (parentId.contains("/")) {
@@ -220,21 +250,26 @@
            }else {
                deviceChannel.setParentId(parentId);
            }
        }else {
            if (deviceChannel.getChannelId().length() <= 10) { // 此时为行政区划, 上下级行政区划使用DeviceId关联
                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 (deviceChannel.getCivilCode() != null) {
                    // 设备, 无parentId的20位是使用CivilCode表示上级的设备,
                    // 注:215 业务分组是需要有parentId的
                    deviceChannel.setParentId(deviceChannel.getCivilCode());
                }
            }else {
                deviceChannel.setParentId(deviceChannel.getDeviceId());
            }
        }
        deviceChannel.setBusinessGroupId(businessGroupID);
//        else {
//            if (deviceChannel.getChannelId().length() <= 10) { // 此时为行政区划, 上下级行政区划使用DeviceId关联
//                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.setBusinessGroupId(businessGroupID);
//                }else if (Integer.parseInt(device.getDeviceId().substring(10, 13) )== 118) {//NVR 如果上级设备编号是NVR则直接将NVR的编号设置给通道的上级编号
//                    deviceChannel.setParentId(device.getDeviceId());
//                }else if (deviceChannel.getCivilCode() != null) {
//                    // 设备, 无parentId的20位是使用CivilCode表示上级的设备,
//                    // 注:215 业务分组是需要有parentId的
//                    deviceChannel.setParentId(deviceChannel.getCivilCode());
//                }
//            }else {
//                deviceChannel.setParentId(deviceChannel.getDeviceId());
//            }
//        }
        if (XmlUtil.getText(itemDevice, "SafetyWay") == null
                || XmlUtil.getText(itemDevice, "SafetyWay") == "") {
@@ -280,6 +315,32 @@
        } else {
            deviceChannel.setLatitude(0.00);
        }
        deviceChannel.setGpsTime(DateUtil.getNow());
        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");