| | |
| | | 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.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; |
| | | |
| | |
| | | |
| | | @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(); |
| | | CloseableHttpClient httpClient = HttpClients.createDefault(); |
| | | String hostUrl = "http://" + ip; |
| | | try { |
| | | //获取OSD |
| | | HttpResponse OSDResponse = getHttpResponse(httpClient, hostUrl + ApiConstants.HK_OSD_PATH, userName, password); |
| | | String OSDString = EntityUtils.toString(OSDResponse.getEntity(), "utf-8"); |
| | | /** |
| | | * 通过国标倒数第七位判断通道 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); |
| | | parseXMl(OSDString, "TextOverlay", "displayText", osdResult, null, ip); |
| | | //获取Time |
| | | HttpResponse TimeResponse = getHttpResponse(httpClient, hostUrl + ApiConstants.HK_OSD_TIME, userName, password); |
| | | String timeString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_TIME, userName, password); |
| | | if (timeString == null) return null; |
| | | |
| | | Date date = new Date(); |
| | | String timeString = EntityUtils.toString(TimeResponse.getEntity(), "utf-8"); |
| | | //解析xml |
| | | parseXMl(timeString, "Time", "localTime", osdResult, date); |
| | | 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) throws ParserConfigurationException, SAXException, IOException { |
| | | 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); |
| | | 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())); |
| | | } |
| | | } 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 HttpResponse getHttpResponse(CloseableHttpClient httpClient, String url, String userName, String password) throws URISyntaxException, IOException { |
| | | 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) |
| | | .setConnectTimeout(3000) |
| | | .setSocketTimeout(3000) |
| | | .build(); |
| | | httpGet.setConfig(requestConfig); |
| | | |
| | | // 绑定凭证提供者到HttpClient |
| | | httpClient = HttpClientBuilder.create() |
| | | .setDefaultCredentialsProvider(credsProvider) |
| | | .build(); |
| | | try { |
| | | return httpClient.execute(httpGet); |
| | | } catch (Exception e){ |
| | | throw new RuntimeException("海康OSD执行失败"); |
| | | 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); |
| | | } |
| | | |
| | | } |
| | | } |