diff --git a/Device.go b/Device.go index 5418585..e9ed85f 100644 --- a/Device.go +++ b/Device.go @@ -12,6 +12,13 @@ import ( "crypto/sha1" "github.com/elgs/gostrgen" "github.com/yakovlevdmv/gosoap" + "reflect" + "strings" + "github.com/yakovlevdmv/goonvif/Device" + "github.com/yakovlevdmv/goonvif/Imaging" + "github.com/yakovlevdmv/goonvif/Media" + "github.com/yakovlevdmv/goonvif/PTZ" + "errors" ) type DeviceInfo struct { @@ -45,7 +52,7 @@ func (dev *device) AddAuthentification(username, password string) { dev.password = password } -func buildMethodSOAP(msg string) (gosoap.SoapMessage, error) { +func buildMethodSOAP(msg, space string) (gosoap.SoapMessage, error) { doc := etree.NewDocument() if err := doc.ReadFromString(msg); err != nil { log.Println("Got error") @@ -56,11 +63,30 @@ func buildMethodSOAP(msg string) (gosoap.SoapMessage, error) { soap := gosoap.NewEmptySOAP() soap.AddBodyContent(element) - soap.AddRootNamespace("wsdl", "http://www.onvif.org/ver10/device/wsdl") + soap.AddRootNamespace("onvif", "http://www.onvif.org/ver10/device/wsdl") + soap.AddRootNamespace("wsdl", space) return soap, nil } +func setXMLNamespaces(strct interface{}) (string, error) { + pkgName := reflect.TypeOf( strct ).PkgPath() + pkgSplit := strings.Split(pkgName, "/") + pkgName = pkgSplit[len(pkgSplit)-1] + switch pkgName { + case "Device": + return Device.WSDL, nil + case "Imaging": + return Imaging.WSDL, nil + case "Media": + return Media.WSDL, nil + case "PTZ": + return PTZ.WSDL, nil + default: + return "", errors.New("Can't find Service") + } +} + //TODO: Get endpoint automatically func (dev device) CallMethod(endpoint string, method interface{}) { @@ -73,10 +99,17 @@ func (dev device) CallMethod(endpoint string, method interface{}) { fmt.Println(string(output)) - soap, err := buildMethodSOAP(string(output)) + wsdlSpaces, err := setXMLNamespaces(method) + if err != nil { + fmt.Println(err) + } + + soap, err := buildMethodSOAP(string(output), wsdlSpaces) if err != nil { log.Fatal(err) } + + fmt.Println("Send soap\n") fmt.Println(soap.String()) @@ -135,18 +168,16 @@ func (dev device) CallAuthorizedMethod(endpoint string, method interface{}) { log.Println("Marshalled struct: ", string(output)) } - soap, err := buildMethodSOAP(string(output)) + wsdlSpaces, err := setXMLNamespaces(method) + if err != nil { + fmt.Println(err) + } + + soap, err := buildMethodSOAP(string(output), wsdlSpaces) if err != nil { log.Fatal(err) } - /**soap := gosoap.NewEmptySOAP() - - soap.AddStringBodyContent( - //`Test`, - ``, - )*/ - /** Generating Nonce sequence **/ charsToGenerate := 16 charSet := gostrgen.Lower | gostrgen.Digit diff --git a/Device/types.go b/Device/types.go index 66bd131..d37dd72 100644 --- a/Device/types.go +++ b/Device/types.go @@ -5,6 +5,8 @@ import ( "github.com/yakovlevdmv/goonvif/xsd/onvif" ) +const WSDL = "http://www.onvif.org/ver10/device/wsdl" + type Service struct { Namespace xsd.AnyURI XAddr xsd.AnyURI diff --git a/Imaging/types.go b/Imaging/types.go index 3c77375..4cc280b 100644 --- a/Imaging/types.go +++ b/Imaging/types.go @@ -5,6 +5,8 @@ import ( "github.com/yakovlevdmv/goonvif/xsd" ) +const WSDL = "http://www.onvif.org/ver20/imaging/wsdl" + type GetServiceCapabilities struct { } diff --git a/Media/types.go b/Media/types.go index 2ed1dea..0fe885b 100644 --- a/Media/types.go +++ b/Media/types.go @@ -2,6 +2,8 @@ package Media import "github.com/yakovlevdmv/goonvif/xsd/onvif" +const WSDL = "http://www.onvif.org/ver10/media/wsdl" + type Capabilities struct { SnapshotUri bool `xml:"SnapshotUri,attr"` Rotation bool `xml:"Rotation,attr"` diff --git a/PTZ/types.go b/PTZ/types.go index 3c1075a..b23e220 100644 --- a/PTZ/types.go +++ b/PTZ/types.go @@ -11,6 +11,8 @@ type Capabilities struct { StatusPosition xsd.Boolean `xml:"StatusPosition,attr"` } +const WSDL = "http://www.onvif.org/ver20/ptz/wsdl" + //PTZ main types diff --git a/xsd/onvif/onvif.go b/xsd/onvif/onvif.go index 508cafb..46c21df 100644 --- a/xsd/onvif/onvif.go +++ b/xsd/onvif/onvif.go @@ -4,6 +4,8 @@ import ( "github.com/yakovlevdmv/goonvif/xsd" ) +const ONVIF = "http://www.onvif.org/ver10/schema" + //TODO: rename package xsdTypes //TODO: enumerations //TODO: type struct {Any string} convert to type AnyType