| | |
| | | package com.ycl.api.HK; |
| | | |
| | | import com.ycl.platform.domain.result.OSDResult; |
| | | import constant.ApiConstants; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.auth.AuthScope; |
| | | import org.apache.http.auth.Credentials; |
| | | 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.DefaultHttpClient; |
| | | 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.Document; |
| | | import org.w3c.dom.Element; |
| | | import org.w3c.dom.Node; |
| | | import org.w3c.dom.NodeList; |
| | | 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 void getOsdByIP(String ip) { |
| | | DefaultHttpClient httpclient = new DefaultHttpClient(); |
| | | public static OSDResult getOsdByIP(String ip, String userName, String password) { |
| | | OSDResult osdResult = new OSDResult(); |
| | | String hostUrl = "http://" + ip; |
| | | // 获取焦距 |
| | | String url = hostUrl + "/ISAPI/System/Video/inputs/channels/1/overlays"; |
| | | URI serverURI = null; |
| | | try { |
| | | serverURI = new URI(url); |
| | | HttpGet httpGet = new HttpGet(url); |
| | | String username = "admin"; |
| | | String password = "zg@2024dx"; |
| | | Credentials creds = new UsernamePasswordCredentials(username, |
| | | password); |
| | | httpclient.getCredentialsProvider(). |
| | | //获取OSD |
| | | String OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH, userName, password); |
| | | if (OSDString == null) return null; |
| | | |
| | | setCredentials( |
| | | new AuthScope(serverURI.getHost(), serverURI. |
| | | |
| | | getPort()), (Credentials) creds); |
| | | |
| | | HttpResponse response = httpclient.execute(httpGet); |
| | | |
| | | String resultString = EntityUtils.toString(response.getEntity(), "utf-8"); |
| | | log.info(resultString); |
| | | //解析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); |
| | | } 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(resultString))); |
| | | Document document = builder.parse(new InputSource(new StringReader(OSDString))); |
| | | document.getDocumentElement().normalize(); |
| | | NodeList nodeList = document.getElementsByTagName("TextOverlay"); |
| | | NodeList nodeList = document.getElementsByTagName(tagName1); |
| | | for (int i = 0; i < nodeList.getLength(); i++) { |
| | | Node node = nodeList.item(i); |
| | | if (node.getNodeType() == Node.ELEMENT_NODE) { |
| | | if (node.getNodeType() == Node.ELEMENT_NODE && "TextOverlay".equals(tagName1)) { |
| | | Element element = (Element) node; |
| | | log.info("TEXT : " + element.getElementsByTagName("displayText").item(0).getTextContent()); |
| | | 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) { |
| | | e.printStackTrace(); |
| | | 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(1000) |
| | | .setSocketTimeout(1000) |
| | | .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); |
| | | } |
| | | |
| | | } |
| | | } |