fuliqi
2024-10-10 d268f82d25c25ebdc7a1ea30614f6cda6d3a9ae3
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
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 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);
            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("TEXT : " + 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);
        }
 
    }
}