记录⼀次Java对接监控(⼤华、海康、onvif)
1. 需求
1. 获取视频流并在内嵌video标签进⾏播放
2. 操控监控进⾏转向、调焦操作
2. 解决流程
1. 官⽅SDK⼀个赛⼀个的不靠谱,尤其是mac开发⽆法加载dll⽂件,就算可以加载,服务器也加载不了,果断pass
2. 从onvif⼊⼿,写⼀个全通⽤的微服务
3. 不需要依赖,但需要如下⼏个包
org.oasis_open.docs
org.w3._p.include
org.w3._2005
4. 需要写⼀些实体类ImagingDevice,InitialDevice,MediaDevice,OnvifDevice,PtzDevice,Soap,这⾥就只列⼀下主要的
import java.io.IOException;
import java.ConnectException;
import java.InetSocketAddress;
import java.Socket;
import java.SocketAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
SimpleDateFormat;
import java.util.*;
l.soap.SOAPException;
import ption.TourBizException;
import lombok.Cleanup;
import lombok.Data;
import vif.ver10.schema.Capabilities;
/**
* @author Mr. Cui
*/
@Data
public class OnvifDevice {
private final String HOST_IP;
private String originalIp;
private boolean isProxy;
private final String username;
private final String password;
private String nonce;
private String utcTime;
private final String serverDeviceUri;
private String serverPtzUri;
private String serverMediaUri;
private String serverImagingUri;
private String serverEventsUri;
private final SOAP soap;
private final InitialDevices initialDevices;
private final PtzDevices ptzDevices;
private final MediaDevices mediaDevices;
private final ImagingDevices imagingDevices;
public OnvifDevice(String hostIp, String user, String password)throws ConnectException, SOAPException {
public OnvifDevice(String hostIp, String user, String password)throws ConnectException, SOAPException { this.HOST_IP = hostIp;
if(!isOnline()){
throw new ConnectException("Host not available.");
}
this.serverDeviceUri =""+ HOST_IP +"/onvif/device_service";
this.username = user;
this.password = password;
this.soap =new SOAP(this);
this.initialDevices =new InitialDevices(this);
this.ptzDevices =new PtzDevices(this);
this.imagingDevices =new ImagingDevices(this);
init();
}
public OnvifDevice(String hostIp)throws ConnectException, SOAPException {
this(hostIp, null, null);
}
private boolean isOnline(){
String port = ains(":")? HOST_IP.substring(HOST_IP.indexOf(':')+1):"80";
String ip = ains(":")? HOST_IP.substring(0, HOST_IP.indexOf(':')): HOST_IP;
try{
SocketAddress sockaddr =new InetSocketAddress(ip,new Integer(port));
@Cleanup Socket socket =new Socket();
}
catch(NumberFormatException | IOException e){
return false;
}
return true;
}
protected void init()throws ConnectException, SOAPException {
Capabilities capabilities =getInitialDevices().getCapabilities();
if(capabilities == null){
throw new TourBizException("Capabilities not reachable.");
}
String localDeviceUri = Device().getXAddr();
if(localDeviceUri.startsWith("")){
originalIp = place("","");
originalIp = originalIp.substring(0, originalIp.indexOf('/'));
}
else{
throw new TourBizException("Unknown/Not implemented local procotol!");
}
if(!originalIp.equals(HOST_IP)){
isProxy =true;
}
Media()!= null && Media().getXAddr()!= null){
serverMediaUri =Media().getXAddr());
}
PTZ()!= null && PTZ().getXAddr()!= null){
serverPtzUri =PTZ().getXAddr());
}
Imaging()!= null && Imaging().getXAddr()!= null){
serverImagingUri =Imaging().getXAddr());
}
Media()!= null && Events().getXAddr()!= null){
serverEventsUri =Events().getXAddr());
}
}
public String replaceLocalIpWithProxyIp(String original){
if(original.startsWith("/")){
}
if(isProxy){
place(originalIp, HOST_IP);
}
return original;
}
public String getUsername(){
return username;
}
public String getEncryptedPassword(){
return encryptPassword();
}
public String encryptPassword(){
String nonce =getNonce();
String timestamp =getUTCTime();
String beforeEncryption = nonce + timestamp + password;
byte[] encryptedRaw;
try{
encryptedRaw =sha1(beforeEncryption);
}
catch(NoSuchAlgorithmException e){
e.printStackTrace();
return null;
}
Encoder().encodeToString(encryptedRaw);
}
private static byte[]sha1(String s)throws NoSuchAlgorithmException {
MessageDigest SHA1;
SHA1 = Instance("SHA1");
SHA1.Bytes());
return SHA1.digest();
}
private String getNonce(){
if(nonce == null){
createNonce();
}
return nonce;
}
public String getEncryptedNonce(){
if(nonce == null){
createNonce();
}
Encoder().Bytes());
}
public void createNonce(){
Random generator =new Random();
nonce =""+ Int();
}
public String getUTCTime(){
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-d'T'HH:mm:ss'Z'"); sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME,"UTC")); Calendar cal =new TimeZone("UTC"));
Calendar cal =new TimeZone("UTC"));
String utcTime = sdf.Time());
this.utcTime = utcTime;
return utcTime;
}
public Date getDate(){
Date();
}
public String getName(){
DeviceInformation().getModel();
}
}
import java.ConnectException;
l.bind.JAXBContext;
l.bind.JAXBException;
l.bind.Marshaller;
l.bind.UnmarshalException;
l.bind.Unmarshaller;
l.parsers.DocumentBuilderFactory;
l.parsers.ParserConfigurationException;
l.soap.MessageFactory;
l.soap.SOAPConnection;
l.soap.SOAPConnectionFactory;
l.soap.SOAPConstants;
l.soap.SOAPElement;
l.soap.SOAPEnvelope;
l.soap.SOAPException;
l.soap.SOAPHeader;
l.soap.SOAPMessage;
l.soap.SOAPPart;
import ption.TourBizException;
import lombok.Cleanup;
import lombok.Data;
import org.w3c.dom.Document;
@Data
public class SOAP {
private OnvifDevice onvifDevice;
public SOAP(OnvifDevice onvifDevice){
super();
}
public Object createSOAPDeviceRequest(Object soapRequestElem, Object soapResponseElem)throws SOAPException,
ConnectException {
return createSOAPRequest(soapRequestElem, soapResponseElem, ServerDeviceUri());
}
public Object createSOAPPtzRequest(Object soapRequestElem, Object soapResponseElem)throws SOAPException, ConnectException { return createSOAPRequest(soapRequestElem, soapResponseElem, ServerPtzUri());
}
public Object createSOAPMediaRequest(Object soapRequestElem, Object soapResponseElem)throws SOAPException, ConnectException { return createSOAPRequest(soapRequestElem, soapResponseElem, ServerMediaUri());
}
public Object createSOAPImagingRequest(Object soapRequestElem, Object soapResponseElem)throws SOAPException,
ConnectException {
return createSOAPRequest(soapRequestElem, soapResponseElem, ServerImagingUri());
}
public Object createSOAPRequest(Object soapRequestElem, Object soapResponseElem, String soapUri){
try{
SOAPConnectionFactory soapConnectionFactory = wInstance();
@Cleanup SOAPConnection soapConnection = ateConnection();
SOAPMessage soapMessage =createSoapMessage(soapRequestElem);
SOAPMessage soapResponse = soapConnection.call(soapMessage, soapUri);
SOAPMessage soapResponse = soapConnection.call(soapMessage, soapUri);
if(soapResponseElem == null){
throw new NullPointerException("Improper SOAP Response Element given (is null).");
}
Unmarshaller unmarshaller = Class()).createUnmarshaller();
try{
try{
soapResponseElem = unmarshaller.SOAPBody().extractContentAsDocument());
}
catch(SOAPException e){
soapResponseElem = unmarshaller.SOAPBody().extractContentAsDocument());
}
}
catch(UnmarshalException e){
throw new TourBizException("Could not unmarshal, ended in SOAP fault.");
}
return soapResponseElem;
}catch(SOAPException e){
javaparser野外throw new TourBizException("Unexpected response. Response should be from class "+ Class());
}
catch(ParserConfigurationException | JAXBException e){
throw new TourBizException("Unhandled exception: "+ e.getMessage());
}
}
protected SOAPMessage createSoapMessage(Object soapRequestElem)throws SOAPException, ParserConfigurationException,
JAXBException {
MessageFactory messageFactory = wInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage soapMessage = ateMessage();
Document document = wInstance().newDocumentBuilder().newDocume
nt();
Marshaller marshaller = Class()).createMarshaller();
marshaller.marshal(soapRequestElem, document);
// if (needAuthentification)
createSoapHeader(soapMessage);
soapMessage.saveChanges();
return soapMessage;
}
protected void createSoapHeader(SOAPMessage soapMessage)throws SOAPException {
String encrypedPassword = EncryptedPassword();
if(encrypedPassword != null && Username()!= null){
SOAPPart sp = SOAPPart();
SOAPEnvelope se = sp.getEnvelope();
SOAPHeader header = SOAPHeader();
se.addNamespaceDeclaration("wsse","/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
se.addNamespaceDeclaration("wsu","/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
SOAPElement securityElem = header.addChildElement("Security","wsse");
// securityElem.setAttribute("SOAP-ENV:mustUnderstand", "1");
SOAPElement usernameTokenElem = securityElem.addChildElement("UsernameToken","wsse");
SOAPElement usernameElem = usernameTokenElem.addChildElement("Username","wsse");
usernameElem.Username());
SOAPElement passwordElem = usernameTokenElem.addChildElement("Password","wsse");
passwordElem.setAttribute("Type","/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"); passwordElem.setTextContent(encrypedPassword);
SOAPElement nonceElem = usernameTokenElem.addChildElement("Nonce","wsse");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论