From 3967a6c9ac00a5ec0cd613eef9eb76c37971eecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Fri, 1 Nov 2024 16:53:22 +0800 Subject: [PATCH] add ptz support --- device.go | 81 ++++++++++++++++++++++++++++++++++++++++ device_test.go | 25 +++++++++++++ onvif/ptz/types.go | 2 +- onvif/xsd/onvif/onvif.go | 16 ++++---- 4 files changed, 115 insertions(+), 9 deletions(-) diff --git a/device.go b/device.go index c6f519c..c481246 100644 --- a/device.go +++ b/device.go @@ -11,8 +11,10 @@ import ( "git.pyer.club/kingecg/goonvif/onvif" device "git.pyer.club/kingecg/goonvif/onvif/device" "git.pyer.club/kingecg/goonvif/onvif/media" + ptz "git.pyer.club/kingecg/goonvif/onvif/ptz" sdk "git.pyer.club/kingecg/goonvif/onvif/sdk/device" 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" "git.pyer.club/kingecg/goonvif/onvif/xsd" onvifmodel "git.pyer.club/kingecg/goonvif/onvif/xsd/onvif" @@ -30,6 +32,11 @@ type Device struct { 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") func NewDevice(params onvif.DeviceParams) *Device { @@ -219,6 +226,80 @@ func (d *Device) GetProfiles() ([]onvifmodel.Profile, error) { } 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) { // 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"}) diff --git a/device_test.go b/device_test.go index 6f0ef0a..156ba17 100644 --- a/device_test.go +++ b/device_test.go @@ -121,3 +121,28 @@ func TestGetStreamUri(t *testing.T) { } 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) + } +} diff --git a/onvif/ptz/types.go b/onvif/ptz/types.go index 058398f..e4d0f3c 100644 --- a/onvif/ptz/types.go +++ b/onvif/ptz/types.go @@ -28,7 +28,7 @@ type GetNodes struct { } type GetNodesResponse struct { - PTZNode onvif.PTZNode + PTZNode []onvif.PTZNode } type GetNode struct { diff --git a/onvif/xsd/onvif/onvif.go b/onvif/xsd/onvif/onvif.go index 7059694..521ea84 100644 --- a/onvif/xsd/onvif/onvif.go +++ b/onvif/xsd/onvif/onvif.go @@ -933,14 +933,14 @@ type PTZNode struct { } type PTZSpaces struct { - AbsolutePanTiltPositionSpace Space2DDescription - AbsoluteZoomPositionSpace Space1DDescription - RelativePanTiltTranslationSpace Space2DDescription - RelativeZoomTranslationSpace Space1DDescription - ContinuousPanTiltVelocitySpace Space2DDescription - ContinuousZoomVelocitySpace Space1DDescription - PanTiltSpeedSpace Space1DDescription - ZoomSpeedSpace Space1DDescription + AbsolutePanTiltPositionSpace []Space2DDescription + AbsoluteZoomPositionSpace []Space1DDescription + RelativePanTiltTranslationSpace []Space2DDescription + RelativeZoomTranslationSpace []Space1DDescription + ContinuousPanTiltVelocitySpace []Space2DDescription + ContinuousZoomVelocitySpace []Space1DDescription + PanTiltSpeedSpace []Space1DDescription + ZoomSpeedSpace []Space1DDescription Extension PTZSpacesExtension }