zxl
2025-03-11 5c82602d54928cc414177084ab6d81cef4c11d15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.OffsetDateTime;
import java.util.Date;
 
@Slf4j
public class HKApi {
    public static OSDResult getOsdByIP(String serialNumber, String ip, String userName, String password) {
        OSDResult osdResult = new OSDResult();
        String hostUrl = "http://" + ip;
        try {
            //获取OSD
            /**
             * 通过国标倒数第七位判断通道 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);
            //获取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);
            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 {
 
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new InputSource(new StringReader(OSDString)));
            document.getDocumentElement().normalize();
            NodeList nodeList = document.getElementsByTagName(tagName1);
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE && "TextOverlay".equals(tagName1)) {
                    Element element = (Element) node;
                    String textContent = element.getElementsByTagName(tagName2).item(0).getTextContent();
                    log.info("HKTEXT : " + textContent);
                    if (i == 0) {
                        osdResult.setOSD1(textContent);
                    } else if (i == 1) {
                        osdResult.setOSD2(textContent);
                    } else if (i == 2) {
                        osdResult.setOSD3(textContent);
                    } else if (i == 3) {
                        osdResult.setName(textContent);
                    } else if (i == 4) {
                        osdResult.setOSD4(textContent);
                    }
                } 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);
                    osdResult.setCheckTime(date);
                    OffsetDateTime dateTime = OffsetDateTime.parse(textContent);
                    // 将OffsetDateTime转换为Date对象
                    osdResult.setOsdTime(Date.from(dateTime.toInstant()));
                }
            }
        } catch (Exception e) {
            log.error("ip:{}出现异常,{}", ip, e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }
 
    private static String getHttpResponse(String url, String userName, String password) throws URISyntaxException, IOException {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(new URI(url).getHost(), new URI(url).getPort()),
                new UsernamePasswordCredentials(userName, password));
        // 绑定凭证提供者到HttpClient
        CloseableHttpClient httpClient = HttpClientBuilder.create()
                .setDefaultCredentialsProvider(credsProvider)
                .build();
 
        HttpGet httpGet = new HttpGet(url);
 
        // 设置请求配置
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(3000)
                .setSocketTimeout(3000)
                .build();
        httpGet.setConfig(requestConfig);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                return EntityUtils.toString(response.getEntity(), "utf-8");
            } else {
                return null;
            }
        } catch (Exception e) {
            throw new RuntimeException("海康OSD执行失败", e);
        }
 
    }
}