mirror of
https://github.com/kerberos-io/onvif.git
synced 2026-08-23 15:08:33 +00:00
Bugs fixed
This commit is contained in:
34
Device.go
34
Device.go
@@ -12,6 +12,8 @@ import (
|
||||
"strings"
|
||||
"github.com/yakovlevdmv/goonvif/Device"
|
||||
"errors"
|
||||
"net/http"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
var xlmns = map[string]string {
|
||||
@@ -98,13 +100,16 @@ func GetAvailableDevicesAtSpecificEthernetInterface(interfaceName string) []devi
|
||||
}
|
||||
|
||||
func (dev *device) getSupportedServices() {
|
||||
resp, err := dev.CallMethod(Device.GetCapabilities{})
|
||||
resp, err := dev.CallMethod(Device.GetCapabilities{Category:"All"})
|
||||
if err != nil {
|
||||
//log.Println(err.Error())
|
||||
return
|
||||
} else {
|
||||
doc := etree.NewDocument()
|
||||
if err := doc.ReadFromString(resp); err != nil {
|
||||
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err := doc.ReadFromBytes(data); err != nil {
|
||||
//log.Println(err.Error())
|
||||
return
|
||||
}
|
||||
@@ -124,9 +129,10 @@ func NewDevice(xaddr string) (*device, error) {
|
||||
dev.endpoints = make(map[string]string)
|
||||
dev.addEndpoint("Device", "http://"+xaddr+"/onvif/device_service")
|
||||
|
||||
systemDateTime := Device.GetDeviceInformation{}
|
||||
_, err := dev.CallMethod(systemDateTime)
|
||||
if err != nil {
|
||||
systemDateTime := Device.GetSystemDateAndTime{}
|
||||
resp, err := dev.CallMethod(systemDateTime)
|
||||
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
//panic(errors.New("camera is not available at " + xaddr + " or it does not support ONVIF services"))
|
||||
return nil, errors.New("camera is not available at " + xaddr + " or it does not support ONVIF services")
|
||||
}
|
||||
@@ -169,7 +175,7 @@ func buildMethodSOAP(msg string) (gosoap.SoapMessage, error) {
|
||||
|
||||
//CallMethod functions call an method, defined <method> struct.
|
||||
//You should use Authenticate method to call authorized requests.
|
||||
func (dev device) CallMethod(method interface{}) (string, error) {
|
||||
func (dev device) CallMethod(method interface{}) (*http.Response, error) {
|
||||
pkgPath := strings.Split(reflect.TypeOf(method).PkgPath(),"/")
|
||||
pkg := pkgPath[len(pkgPath)-1]
|
||||
|
||||
@@ -182,6 +188,8 @@ func (dev device) CallMethod(method interface{}) (string, error) {
|
||||
case "PTZ": endpoint = dev.endpoints["PTZ"]
|
||||
}
|
||||
|
||||
//fmt.Println("endpoint", endpoint)
|
||||
|
||||
//TODO: Get endpoint automatically
|
||||
if dev.login != "" && dev.password != "" {
|
||||
/*resp, err := dev.сallAuthorizedMethod(endpoint, method)
|
||||
@@ -205,7 +213,7 @@ func (dev device) CallMethod(method interface{}) (string, error) {
|
||||
}
|
||||
|
||||
//CallNonAuthorizedMethod functions call an method, defined <method> struct without authentication data
|
||||
func (dev device) callNonAuthorizedMethod(endpoint string, method interface{}) (string, error) {
|
||||
func (dev device) callNonAuthorizedMethod(endpoint string, method interface{}) (*http.Response, error) {
|
||||
//TODO: Get endpoint automatically
|
||||
/*
|
||||
Converting <method> struct to xml string representation
|
||||
@@ -213,7 +221,7 @@ func (dev device) callNonAuthorizedMethod(endpoint string, method interface{}) (
|
||||
output, err := xml.MarshalIndent(method, " ", " ")
|
||||
if err != nil {
|
||||
//log.Printf("error: %v\n", err.Error())
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -222,7 +230,7 @@ func (dev device) callNonAuthorizedMethod(endpoint string, method interface{}) (
|
||||
soap, err := buildMethodSOAP(string(output))
|
||||
if err != nil {
|
||||
//log.Printf("error: %v\n", err)
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
soap.AddRootNamespaces(xlmns)
|
||||
@@ -234,14 +242,14 @@ func (dev device) callNonAuthorizedMethod(endpoint string, method interface{}) (
|
||||
}
|
||||
|
||||
//CallMethod functions call an method, defined <method> struct with authentication data
|
||||
func (dev device) callAuthorizedMethod(endpoint string, method interface{}) (string, error) {
|
||||
func (dev device) callAuthorizedMethod(endpoint string, method interface{}) (*http.Response, error) {
|
||||
/*
|
||||
Converting <method> struct to xml string representation
|
||||
*/
|
||||
output, err := xml.MarshalIndent(method, " ", " ")
|
||||
if err != nil {
|
||||
//log.Printf("error: %v\n", err.Error())
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -250,7 +258,7 @@ func (dev device) callAuthorizedMethod(endpoint string, method interface{}) (str
|
||||
soap, err := buildMethodSOAP(string(output))
|
||||
if err != nil {
|
||||
//log.Printf("error: %v\n", err.Error())
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -269,7 +277,7 @@ func (dev device) callAuthorizedMethod(endpoint string, method interface{}) (str
|
||||
soapReq, err := xml.MarshalIndent(auth, "", " ")
|
||||
if err != nil {
|
||||
//log.Printf("error: %v\n", err.Error())
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
13
README.md
13
README.md
@@ -87,7 +87,7 @@ if err != nil {
|
||||
2. Создание пользователя методом CreateUsers сервиса DeviceManagement
|
||||
Все необходимые типы данных определены в пакете [Device](Device/types.go).
|
||||
В файле (https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl) можно увидеть структуру запроса:
|
||||

|
||||

|
||||
|
||||
Создадим объект `Device.CreateUsers`:
|
||||
```
|
||||
@@ -103,5 +103,14 @@ if err != nil {
|
||||
В данном примере можно наблюдать использования пакета onvif, в котором определено большинство типов, используемых в различных сервисах.
|
||||
Поэтому при создании структур запросов необходимо это учитывать.
|
||||
|
||||
#####**ВАЖНО**
|
||||
##### ВАЖНО
|
||||
Некоторые камеры работают специфично. Это означает, что в зависимости от модели камеры можно не получить ошибки при неправильном запросе. Поэтому советую проверять точно ли выполнилась операция. Например, для метода CreateUsers вывести список всех пользователей и сравнить.
|
||||
|
||||
3. Метод ContinuousMove сервиса PTZ
|
||||
Все необходимые типы данных определены в пакете [PTZ](PTZ/types.go).
|
||||
В файле (https://www.onvif.org/ver20/ptz/wsdl/ptz.wsdl) можно увидеть структуру запроса:
|
||||

|
||||
|
||||
Так как данная команда определяется сервисом PTZ, необходимый тип находится в пакете [PTZ](PTZ/types.go).
|
||||
|
||||
Создадим объект `PTZ.ContinuousMove`:
|
||||
|
||||
BIN
img/exmp_ContinuousMove.png
Normal file
BIN
img/exmp_ContinuousMove.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 29 KiB |
BIN
img/exmp_CreateUsers.png
Normal file
BIN
img/exmp_CreateUsers.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
@@ -3,27 +3,26 @@ package networking
|
||||
import (
|
||||
"net/http"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"errors"
|
||||
)
|
||||
|
||||
func SendSoap(endpoint, message string) (string, error) {
|
||||
func SendSoap(endpoint, message string) (*http.Response, error) {
|
||||
httpClient := new(http.Client)
|
||||
|
||||
resp, err := httpClient.Post(endpoint, "application/soap+xml; charset=utf-8", bytes.NewBufferString(message))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", errors.New("error: got HTTP response status " + strconv.Itoa(resp.StatusCode))
|
||||
}
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return resp, err
|
||||
}
|
||||
|
||||
/*if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusBadRequest {
|
||||
return "", errors.New("error: got HTTP response status " + strconv.Itoa(resp.StatusCode))
|
||||
}*/
|
||||
//b, err := ioutil.ReadAll(resp.Body)
|
||||
//if err != nil {
|
||||
// return resp, err
|
||||
//}
|
||||
//fmt.Println(endpoint)
|
||||
//fmt.Println(string(b))
|
||||
//log.Println(resp.StatusCode)
|
||||
|
||||
return string(b),nil
|
||||
return resp,nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user