From 381c3bdc2079ece5147cf4cee004e9071edadf7a Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期四, 04 五月 2023 16:04:44 +0800 Subject: [PATCH] 修复国标点播下级平台,ssrc更新的时单端口错误更新rtpserver的问题 --- src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java | 330 +++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 235 insertions(+), 95 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 c44001a..d51ab9f 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,9 +1,11 @@ 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.event.subscribe.catalog.CatalogEvent; +import com.genersoft.iot.vmp.utils.DateUtil; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; @@ -11,12 +13,17 @@ import org.dom4j.io.SAXReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.util.StringUtils; +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.*; /** @@ -28,7 +35,7 @@ /** * 鏃ュ織鏈嶅姟 */ - private static Logger LOG = LoggerFactory.getLogger(XmlUtil.class); + private static Logger logger = LoggerFactory.getLogger(XmlUtil.class); /** * 瑙f瀽XML涓篋ocument瀵硅薄 @@ -45,7 +52,7 @@ try { document = saxReader.read(sr); } catch (DocumentException e) { - LOG.error("瑙f瀽澶辫触", e); + logger.error("瑙f瀽澶辫触", e); } return null == document ? null : document.getRootElement(); } @@ -115,12 +122,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 +158,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()); } } @@ -181,47 +188,78 @@ 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"; + private enum ChannelType{ + CivilCode, BusinessGroup,VirtualOrganization,Other + } + + public static DeviceChannel channelContentHander(Element itemDevice, Device device, String event){ DeviceChannel deviceChannel = new DeviceChannel(); - deviceChannel.setName(channelName); + deviceChannel.setDeviceId(device.getDeviceId()); 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 (channdelIdElement == null) { + logger.warn("瑙f瀽Catalog娑堟伅鏃跺彂鐜扮己灏� DeviceID"); + return null; } - if (status.equals("OFF") || status.equals("Off") || status.equals("OFFLINE")) { - deviceChannel.setStatus(0); + 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; } - 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); + 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; + + } } - } else { - // 鐢变簬娴峰悍浼氶敊璇殑鍙戦��65535浣滀负杩欓噷鐨勫彇鍊�,鎵�浠ヨ繖閲岄櫎闈炴槸0鍚﹀垯璁や负鏄�1 - deviceChannel.setParental(Integer.parseInt(XmlUtil.getText(itemDevice, "Parental")) == 1?1:0); + } + + 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; } /** * 琛屾斂鍖哄垝灞曠ず璁惧鏍戜笌涓氬姟鍒嗙粍灞曠ず璁惧鏍戞槸涓ょ涓嶅悓鐨勬ā寮� @@ -229,7 +267,17 @@ * 娌冲寳鐪� * --> 鐭冲搴勫競 * --> 鎽勫儚澶� - * --> 姝e畾鍘� + *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畾鍘� * --> 鎽勫儚澶� * --> 鎽勫儚澶� * @@ -242,54 +290,92 @@ * --> 鎽勫儚澶� */ 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); } - }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 (deviceChannel.getCivilCode() != null) { - // 璁惧锛� 鏃爌arentId鐨�20浣嶆槸浣跨敤CivilCode琛ㄧず涓婄骇鐨勮澶囷紝 - // 娉細215 涓氬姟鍒嗙粍鏄渶瑕佹湁parentId鐨� - deviceChannel.setParentId(deviceChannel.getCivilCode()); - } - }else { - deviceChannel.setParentId(deviceChannel.getDeviceId()); + // 鍏煎璁惧閫氶亾淇℃伅涓嚜宸变负鑷繁鐖惰妭鐐圭殑鎯呭喌 + if (deviceChannel.getParentId().equals(deviceChannel.getChannelId())) { + deviceChannel.setParentId(null); } } + 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; + } - if (XmlUtil.getText(itemDevice, "SafetyWay") == null - || XmlUtil.getText(itemDevice, "SafetyWay") == "") { + 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(XmlUtil.getText(itemDevice, "SafetyWay"))); + deviceChannel.setSafetyWay(Integer.parseInt(safetyWay)); } - if (XmlUtil.getText(itemDevice, "RegisterWay") == null - || XmlUtil.getText(itemDevice, "RegisterWay") == "") { + + String registerWay = XmlUtil.getText(itemDevice, "RegisterWay"); + if (ObjectUtils.isEmpty(registerWay)) { deviceChannel.setRegisterWay(1); } else { - deviceChannel.setRegisterWay(Integer.parseInt(XmlUtil.getText(itemDevice, "RegisterWay"))); + deviceChannel.setRegisterWay(Integer.parseInt(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")); @@ -298,42 +384,24 @@ } 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"))); + + + String longitude = XmlUtil.getText(itemDevice, "Longitude"); + if (NumericUtil.isDouble(longitude)) { + deviceChannel.setLongitude(Double.parseDouble(longitude)); } else { deviceChannel.setLongitude(0.00); } - if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Latitude"))) { - deviceChannel.setLatitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Latitude"))); + String latitude = XmlUtil.getText(itemDevice, "Latitude"); + if (NumericUtil.isDouble(latitude)) { + deviceChannel.setLatitude(Double.parseDouble(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()); - } + + deviceChannel.setGpsTime(DateUtil.getNow()); + + if (XmlUtil.getText(itemDevice, "PTZType") == null || "".equals(XmlUtil.getText(itemDevice, "PTZType"))) { //鍏煎INFO涓殑淇℃伅 Element info = itemDevice.element("Info"); @@ -345,7 +413,79 @@ } else { deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(itemDevice, "PTZType"))); } - deviceChannel.setHasAudio(true); // 榛樿鍚湁闊抽锛屾挱鏀炬椂鍐嶆鏌ユ槸鍚︽湁闊抽鍙婃槸鍚AC + 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