fuliqi
2024-11-13 6da8bd9bf08a75e3d026505f23c008f3b8188c22
ycl-server/src/main/java/com/ycl/api/HK/HKApi.java
@@ -1,7 +1,9 @@
package com.ycl.api.HK;
import com.ycl.platform.domain.result.OSDResult;
import com.ycl.utils.StringUtils;
import constant.ApiConstants;
import enumeration.DeviceType;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
@@ -31,30 +33,69 @@
@Slf4j
public class HKApi {
    public static OSDResult getOsdByIP(String ip, String userName, String password) {
    public static OSDResult getOsdByIP(String serialNumber, String ip, String userName, String password) {
        OSDResult osdResult = new OSDResult();
        String hostUrl = "http://" + ip;
        try {
            //获取OSD
            String OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH, userName, password);
            /**
             * 通过国标倒数第七位判断通道 0/全景 1/细节
             * 对于海康来说: 国标倒7 0/全景/通道2 1/细节/通道1
             * 对于大华来说: 国标倒7 0/全景/通道1 1/细节/通道2
             * 宇视单通道不考虑
             */
            String OSDString = null;
            if(StringUtils.isEmpty(serialNumber)) return null;
            if(serialNumber.charAt(ApiConstants.SerialNumber_Channel) == ApiConstants.SerialNumber_All ) {
                OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_2, userName, password);
            }else if(serialNumber.charAt(ApiConstants.SerialNumber_Channel) == ApiConstants.SerialNumber_Detail){
                OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_1, userName, password);
            }
            //通过设备类型判断(弃用)
//            if(serialNumber.charAt(ApiConstants.SerialNumber_Category) == 1 ){
//                //枪机
//                if(serialNumber.charAt(ApiConstants.SerialNumber_Channel) == 1){
//                    OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_1, userName, password);
//                }else if(serialNumber.charAt(ApiConstants.SerialNumber_Channel) == 0){
//                    OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_2, userName, password);
//                }
//            }else if(serialNumber.charAt(ApiConstants.SerialNumber_Category) == 2){
//                //球机
//                if(serialNumber.charAt(ApiConstants.SerialNumber_Channel) == 0){
//                    OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_1, userName, password);
//                }else if(serialNumber.charAt(ApiConstants.SerialNumber_Channel) == 1){
//                    OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_2, userName, password);
//                }
//            }else if(serialNumber.charAt(ApiConstants.SerialNumber_Category) == 3){
//                //卡口 单通道
//                OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_1, userName, password);
//            }else if(serialNumber.charAt(ApiConstants.SerialNumber_Category) == 4){
//                //高空
//                if(serialNumber.charAt(ApiConstants.SerialNumber_Channel) == 0){
//                    OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_1, userName, password);
//                }else if(serialNumber.charAt(ApiConstants.SerialNumber_Channel) == 1){
//                    OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH_2, userName, password);
//                }
//            }
            if (OSDString == null) return null;
            //解析xml
            parseXMl(OSDString, "TextOverlay", "displayText", osdResult, null,ip);
            parseXMl(OSDString, "TextOverlay", "displayText", osdResult, null, ip);
            //获取Time
            String timeString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_TIME, userName, password);
            if (timeString == null) return null;
            Date date = new Date();
            //解析xml
            parseXMl(timeString, "Time", "localTime", osdResult, date,ip);
            parseXMl(timeString, "Time", "localTime", osdResult, date, ip);
            osdResult.setDeviceBrand(DeviceType.HK.getType());
        } catch (Exception e) {
            return null;
        }
        return osdResult;
    }
    private static void parseXMl(String OSDString, String tagName1, String tagName2, OSDResult osdResult, Date date,String ip) throws ParserConfigurationException, SAXException, IOException {
    private static void parseXMl(String OSDString, String tagName1, String tagName2, OSDResult osdResult, Date date, String ip) throws ParserConfigurationException, SAXException, IOException {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -67,7 +108,7 @@
                if (node.getNodeType() == Node.ELEMENT_NODE && "TextOverlay".equals(tagName1)) {
                    Element element = (Element) node;
                    String textContent = element.getElementsByTagName(tagName2).item(0).getTextContent();
    //                log.info("TEXT : " + textContent);
                    //                log.info("TEXT : " + textContent);
                    if (i == 0) {
                        osdResult.setOSD1(textContent);
                    } else if (i == 1) {
@@ -82,7 +123,7 @@
                } else if (node.getNodeType() == Node.ELEMENT_NODE && "Time".equals(tagName1)) {
                    Element element = (Element) node;
                    String textContent = element.getElementsByTagName(tagName2).item(0).getTextContent();
    //                log.info("TEXT : " + textContent);
                    //                log.info("TEXT : " + textContent);
                    osdResult.setCheckTime(date);
                    OffsetDateTime dateTime = OffsetDateTime.parse(textContent);
                    // 将OffsetDateTime转换为Date对象
@@ -90,8 +131,8 @@
                }
            }
        } catch (Exception e) {
           log.error("ip:{}出现异常,{}",ip,e.getMessage());
           throw new RuntimeException(e.getMessage());
            log.error("ip:{}出现异常,{}", ip, e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }
@@ -109,8 +150,8 @@
        // 设置请求配置
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(1000)
                .setSocketTimeout(1000)
                .setConnectTimeout(3000)
                .setSocketTimeout(3000)
                .build();
        httpGet.setConfig(requestConfig);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {