Golang Onvif 使用 use-go/onvif

6 阅读1分钟

使用github.com/use-go/onvif包,实现go语言对接onvif协议

1. Installation use-go/onvif

go get github.com/use-go/onvif

2. 新建连接并获取摄像头基础信息

package main

import (
	"context"
	"fmt"
	goonvif "github.com/use-go/onvif"
	"github.com/use-go/onvif/device"
	sdkdevice "github.com/use-go/onvif/sdk/device"
	"net/http"
)

var (
	xaddr    = "192.168.1.100:80"
	username = "onvif"
	password = "onvifpassword"
)

func main() {
	ctx := context.Background()
	dev, err := goonvif.NewDevice(goonvif.DeviceParams{
		Xaddr:      xaddr,
		Username:   username,
		Password:   password,
		HttpClient: new(http.Client),
	})
	if err != nil {
		panic(err)
	}
	// 读取设备基础信息
	GetDeviceInformation(ctx, dev)
}

// GetDeviceInformation 读取设备基础信息
// @author: Sen
// @date  : 2023-03-09 16:36:16
func GetDeviceInformation(ctx context.Context, dev *goonvif.Device) {
	// 读取设备基础信息
	getDeviceInformation := device.GetDeviceInformation{}
	getDeviceInformationResponse, err := sdkdevice.Call_GetDeviceInformation(ctx, dev, getDeviceInformation)
	if err != nil {
		panic(err)
	}
	//HardwareId      string //固件ID/设备编号
	//SerialNumber    string //设备序列号
	//FirmwareVersion string //固件版本
	//Model           string //设备类型
	//Manufacturer    string //厂家信息
	fmt.Println("getDeviceInformationResponse:")
	fmt.Println(getDeviceInformationResponse)
}

3. 备注

需要特别注意: 1、import use-go/onvif 相关包的时候,一定要加上别名

2、摄像头的地址一定要可以ping通,并且username和password不是登陆摄像头管理后台的用户,而是协议用户:

海康摄像头的配置路径:管理后台 -> 配置 -> 网络 -> 高级配置 -> 集成协议