From 9c8a1d8b1974fb57cc626e4e96f6226cb0f7b925 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Tue, 2 May 2023 11:07:12 +0300 Subject: [PATCH] Add path to ONVIF requests --- pkg/onvif/client.go | 46 +++++++++++++++++++++++++++++--------------- pkg/onvif/helpers.go | 5 +++++ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/pkg/onvif/client.go b/pkg/onvif/client.go index 77157d0b..a4b6413f 100644 --- a/pkg/onvif/client.go +++ b/pkg/onvif/client.go @@ -104,6 +104,7 @@ func (c *Client) HasSnapshots() bool { func (c *Client) GetCapabilities() ([]byte, error) { return c.Request( + PathDevice, ` All `, @@ -111,64 +112,79 @@ func (c *Client) GetCapabilities() ([]byte, error) { } func (c *Client) GetNetworkInterfaces() ([]byte, error) { - return c.Request(``) + return c.Request( + PathDevice, ``, + ) } func (c *Client) GetDeviceInformation() ([]byte, error) { - return c.Request(``) + return c.Request( + PathDevice, ``, + ) } func (c *Client) GetProfiles() ([]byte, error) { - return c.Request(``) + return c.Request( + PathMedia, ``, + ) } func (c *Client) GetStreamUri(token string) ([]byte, error) { return c.Request( + PathMedia, ` RTP-Unicast RTSP - ` + token + ` -`) + `+token+` +`, + ) } func (c *Client) GetSnapshotUri(token string) ([]byte, error) { return c.Request( + PathMedia, ` - ` + token + ` -`) + `+token+` +`, + ) } func (c *Client) GetSystemDateAndTime() ([]byte, error) { return c.Request( - ``, + PathDevice, ``, ) } func (c *Client) GetServiceCapabilities() ([]byte, error) { + // some cameras answer GetServiceCapabilities for media only for path = "/onvif/media" return c.Request( - ``, + PathMedia, ``, ) } func (c *Client) SystemReboot() ([]byte, error) { return c.Request( - ``, + PathDevice, ``, ) } func (c *Client) GetServices() ([]byte, error) { - return c.Request(` + return c.Request( + PathDevice, ` true -`) +`, + ) } func (c *Client) GetScopes() ([]byte, error) { - return c.Request(``) + return c.Request( + PathDevice, ``, + ) } -func (c *Client) Request(body string) ([]byte, error) { +func (c *Client) Request(path, body string) ([]byte, error) { buf := bytes.NewBuffer(nil) buf.WriteString( ``, @@ -198,7 +214,7 @@ func (c *Client) Request(body string) ([]byte, error) { client := &http.Client{Timeout: time.Second * 5000} res, err := client.Post( - "http://"+c.url.Host+"/onvif/", + "http://"+c.url.Host+path, `application/soap+xml;charset=utf-8`, buf, ) diff --git a/pkg/onvif/helpers.go b/pkg/onvif/helpers.go index c772ee02..ba9e61bf 100644 --- a/pkg/onvif/helpers.go +++ b/pkg/onvif/helpers.go @@ -10,6 +10,11 @@ import ( "time" ) +const ( + PathDevice = "/onvif/device_service" + PathMedia = "/onvif/media_service" +) + func FindTagValue(b []byte, tag string) string { re := regexp.MustCompile(tag + `[^>]*>([^<]+)`) m := re.FindSubmatch(b)