add ptz support

This commit is contained in:
程广 2024-11-01 16:53:22 +08:00
parent 952e34bcb4
commit 3967a6c9ac
4 changed files with 115 additions and 9 deletions

View File

@ -11,8 +11,10 @@ import (
"git.pyer.club/kingecg/goonvif/onvif" "git.pyer.club/kingecg/goonvif/onvif"
device "git.pyer.club/kingecg/goonvif/onvif/device" device "git.pyer.club/kingecg/goonvif/onvif/device"
"git.pyer.club/kingecg/goonvif/onvif/media" "git.pyer.club/kingecg/goonvif/onvif/media"
ptz "git.pyer.club/kingecg/goonvif/onvif/ptz"
sdk "git.pyer.club/kingecg/goonvif/onvif/sdk/device" sdk "git.pyer.club/kingecg/goonvif/onvif/sdk/device"
media_sdk "git.pyer.club/kingecg/goonvif/onvif/sdk/media" media_sdk "git.pyer.club/kingecg/goonvif/onvif/sdk/media"
ptz_sdk "git.pyer.club/kingecg/goonvif/onvif/sdk/ptz"
wsdiscovery "git.pyer.club/kingecg/goonvif/onvif/ws-discovery" wsdiscovery "git.pyer.club/kingecg/goonvif/onvif/ws-discovery"
"git.pyer.club/kingecg/goonvif/onvif/xsd" "git.pyer.club/kingecg/goonvif/onvif/xsd"
onvifmodel "git.pyer.club/kingecg/goonvif/onvif/xsd/onvif" onvifmodel "git.pyer.club/kingecg/goonvif/onvif/xsd/onvif"
@ -30,6 +32,11 @@ type Device struct {
mediaCapablities *media.Capabilities mediaCapablities *media.Capabilities
} }
const (
panTiltSpace string = "http://www.onvif.org/ver10/tptz/PanTiltSpaces/VelocityGenericSpace"
zoomSpace string = "http://www.onvif.org/ver10/tptz/ZoomSpaces/VelocityGenericSpace"
)
var ErrAuthRequired = errors.New("auth required") var ErrAuthRequired = errors.New("auth required")
func NewDevice(params onvif.DeviceParams) *Device { func NewDevice(params onvif.DeviceParams) *Device {
@ -219,6 +226,80 @@ func (d *Device) GetProfiles() ([]onvifmodel.Profile, error) {
} }
return resp.Profiles, nil return resp.Profiles, nil
} }
func (d *Device) GetDeviceInformation() (interface{}, error) {
err := d.check()
if err != nil {
return nil, err
}
resp, err := sdk.Call_GetDeviceInformation(d.ctx, d.device, device.GetDeviceInformation{})
if err != nil {
return nil, err
}
return resp, nil
}
func (d *Device) PTZNodes() ([]onvifmodel.PTZNode, error) {
err := d.check()
if err != nil {
return nil, err
}
resp, err := ptz_sdk.Call_GetNodes(d.ctx, d.device, ptz.GetNodes{})
if err != nil {
return nil, err
}
return resp.PTZNode, nil
}
func (d *Device) continueMove(panTilt onvifmodel.Vector2D, zoom float64) error {
err := d.check()
if err != nil {
return err
}
v := onvifmodel.PTZSpeed{}
panTilt.Space = xsd.AnyURI(panTiltSpace)
v.PanTilt = panTilt
v.Zoom = onvifmodel.Vector1D{
Space: xsd.AnyURI(zoomSpace),
X: zoom,
}
_, perr := ptz_sdk.Call_ContinuousMove(d.ctx, d.device, ptz.ContinuousMove{
ProfileToken: "Profile_1",
Velocity: v,
})
return perr
}
func (d *Device) StopMove() error {
err := d.check()
if err != nil {
return err
}
_, perr := ptz_sdk.Call_Stop(d.ctx, d.device, ptz.Stop{})
return perr
}
// Pan 执行设备的水平和垂直移动。
// 此函数通过指定的x和y坐标来控制设备的移动方向和距离。
// 参数:
//
// x - 水平移动的坐标值。range: -1 to 1
// y - 垂直移动的坐标值。range: -1 to 1
//
// 返回值:
//
// 如果移动操作成功返回nil否则返回错误。
func (d *Device) Pan(x, y float64) error {
// 创建一个二维向量来表示移动的方向和距离。
v := onvifmodel.Vector2D{X: x, Y: y}
// 调用continueMove函数来执行实际的移动操作移动速度设置为0。
return d.continueMove(v, 0)
}
func (d *Device) Zoom(zoom float64) error {
return d.continueMove(onvifmodel.Vector2D{}, zoom)
}
func GetAvailableDevicesAtSpecificEthernetInterface(interfaceName string) ([]Device, error) { func GetAvailableDevicesAtSpecificEthernetInterface(interfaceName string) ([]Device, error) {
// Call a ws-discovery Probe Message to Discover NVT type Devices // Call a ws-discovery Probe Message to Discover NVT type Devices
devices, err := wsdiscovery.SendProbe(interfaceName, nil, []string{"dn:" + onvif.NVT.String()}, map[string]string{"dn": "http://www.onvif.org/ver10/network/wsdl"}) devices, err := wsdiscovery.SendProbe(interfaceName, nil, []string{"dn:" + onvif.NVT.String()}, map[string]string{"dn": "http://www.onvif.org/ver10/network/wsdl"})

View File

@ -121,3 +121,28 @@ func TestGetStreamUri(t *testing.T) {
} }
fmt.Println(uri) fmt.Println(uri)
} }
func TestGetPtzNodes(t *testing.T) {
d := NewDevice(onvif.DeviceParams{
Xaddr: "192.168.12.52",
Username: "dctdev",
Password: "dacenT2017",
})
nodes, err := d.PTZNodes()
if err != nil {
t.Fatal(err)
}
t.Log(nodes)
}
func TestZoom(t *testing.T) {
d := NewDevice(onvif.DeviceParams{
Xaddr: "192.168.12.52",
Username: "dctdev",
Password: "dacenT2017",
})
err := d.Zoom(-0.5)
if err != nil {
t.Fatal(err)
}
}

View File

@ -28,7 +28,7 @@ type GetNodes struct {
} }
type GetNodesResponse struct { type GetNodesResponse struct {
PTZNode onvif.PTZNode PTZNode []onvif.PTZNode
} }
type GetNode struct { type GetNode struct {

View File

@ -933,14 +933,14 @@ type PTZNode struct {
} }
type PTZSpaces struct { type PTZSpaces struct {
AbsolutePanTiltPositionSpace Space2DDescription AbsolutePanTiltPositionSpace []Space2DDescription
AbsoluteZoomPositionSpace Space1DDescription AbsoluteZoomPositionSpace []Space1DDescription
RelativePanTiltTranslationSpace Space2DDescription RelativePanTiltTranslationSpace []Space2DDescription
RelativeZoomTranslationSpace Space1DDescription RelativeZoomTranslationSpace []Space1DDescription
ContinuousPanTiltVelocitySpace Space2DDescription ContinuousPanTiltVelocitySpace []Space2DDescription
ContinuousZoomVelocitySpace Space1DDescription ContinuousZoomVelocitySpace []Space1DDescription
PanTiltSpeedSpace Space1DDescription PanTiltSpeedSpace []Space1DDescription
ZoomSpeedSpace Space1DDescription ZoomSpeedSpace []Space1DDescription
Extension PTZSpacesExtension Extension PTZSpacesExtension
} }