mirror of
https://github.com/kerberos-io/onvif.git
synced 2026-08-24 15:18:34 +00:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37d8a71395 | ||
|
|
af24622015 | ||
|
|
2fb044d81c | ||
|
|
51ae55e8a0 | ||
|
|
6a2c796805 | ||
|
|
48daf5acb5 | ||
|
|
d85a0742b9 | ||
|
|
24a85afc84 | ||
|
|
c8ac58ebd7 | ||
|
|
4ec21b20e0 | ||
|
|
4e696ec65a | ||
|
|
e1345d5e6b | ||
|
|
918ce541d2 | ||
|
|
15e268c1b7 | ||
|
|
dce0d0faad | ||
|
|
be84cab1fe | ||
|
|
c10b8ca105 | ||
|
|
e6593ad04f | ||
|
|
702fded39b | ||
|
|
298c2dabb9 | ||
|
|
bedd8886e4 | ||
|
|
ddb7588318 |
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/onvif.iml" filepath="$PROJECT_DIR$/.idea/onvif.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
9
.idea/onvif.iml
generated
Normal file
9
.idea/onvif.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
127
Device.go
127
Device.go
@@ -6,15 +6,16 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
"github.com/use-go/onvif/device"
|
||||
"github.com/use-go/onvif/gosoap"
|
||||
"github.com/use-go/onvif/networking"
|
||||
wsdiscovery "github.com/use-go/onvif/ws-discovery"
|
||||
"github.com/kerberos-io/onvif/device"
|
||||
"github.com/kerberos-io/onvif/gosoap"
|
||||
"github.com/kerberos-io/onvif/networking"
|
||||
wsdiscovery "github.com/kerberos-io/onvif/ws-discovery"
|
||||
)
|
||||
|
||||
//Xlmns XML Scheam
|
||||
@@ -63,8 +64,8 @@ func (devType DeviceType) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
//deviceInfo struct contains general information about ONVIF device
|
||||
type deviceInfo struct {
|
||||
//DeviceInfo struct contains general information about ONVIF device
|
||||
type DeviceInfo struct {
|
||||
Manufacturer string
|
||||
Model string
|
||||
FirmwareVersion string
|
||||
@@ -72,15 +73,20 @@ type deviceInfo struct {
|
||||
HardwareId string
|
||||
}
|
||||
|
||||
//Device for a new device of onvif and deviceInfo
|
||||
//Device for a new device of onvif and DeviceInfo
|
||||
//struct represents an abstract ONVIF device.
|
||||
//It contains methods, which helps to communicate with ONVIF device
|
||||
type Device struct {
|
||||
xaddr string
|
||||
login string
|
||||
password string
|
||||
params DeviceParams
|
||||
endpoints map[string]string
|
||||
info deviceInfo
|
||||
info DeviceInfo
|
||||
}
|
||||
|
||||
type DeviceParams struct {
|
||||
Xaddr string
|
||||
Username string
|
||||
Password string
|
||||
HttpClient *http.Client
|
||||
}
|
||||
|
||||
//GetServices return available endpoints
|
||||
@@ -88,6 +94,11 @@ func (dev *Device) GetServices() map[string]string {
|
||||
return dev.endpoints
|
||||
}
|
||||
|
||||
//GetServices return available endpoints
|
||||
func (dev *Device) GetDeviceInfo() DeviceInfo {
|
||||
return dev.info
|
||||
}
|
||||
|
||||
func readResponse(resp *http.Response) string {
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
@@ -103,52 +114,47 @@ func GetAvailableDevicesAtSpecificEthernetInterface(interfaceName string) []Devi
|
||||
*/
|
||||
devices := wsdiscovery.SendProbe(interfaceName, nil, []string{"dn:" + NVT.String()}, map[string]string{"dn": "http://www.onvif.org/ver10/network/wsdl"})
|
||||
nvtDevices := make([]Device, 0)
|
||||
////fmt.Println(devices)
|
||||
|
||||
for _, j := range devices {
|
||||
doc := etree.NewDocument()
|
||||
if err := doc.ReadFromString(j); err != nil {
|
||||
fmt.Errorf("%s", err.Error())
|
||||
return nil
|
||||
}
|
||||
////fmt.Println(j)
|
||||
|
||||
endpoints := doc.Root().FindElements("./Body/ProbeMatches/ProbeMatch/XAddrs")
|
||||
for _, xaddr := range endpoints {
|
||||
//fmt.Println(xaddr.Tag,strings.Split(strings.Split(xaddr.Text(), " ")[0], "/")[2] )
|
||||
xaddr := strings.Split(strings.Split(xaddr.Text(), " ")[0], "/")[2]
|
||||
fmt.Println(xaddr)
|
||||
c := 0
|
||||
|
||||
for c = 0; c < len(nvtDevices); c++ {
|
||||
if nvtDevices[c].xaddr == xaddr {
|
||||
fmt.Println(nvtDevices[c].xaddr, "==", xaddr)
|
||||
if nvtDevices[c].params.Xaddr == xaddr {
|
||||
fmt.Println(nvtDevices[c].params.Xaddr, "==", xaddr)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if c < len(nvtDevices) {
|
||||
continue
|
||||
}
|
||||
dev, err := NewDevice(strings.Split(xaddr, " ")[0])
|
||||
//fmt.Println(dev)
|
||||
|
||||
dev, err := NewDevice(DeviceParams{Xaddr: strings.Split(xaddr, " ")[0]})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error", xaddr)
|
||||
fmt.Println(err)
|
||||
continue
|
||||
} else {
|
||||
////fmt.Println(dev)
|
||||
nvtDevices = append(nvtDevices, *dev)
|
||||
}
|
||||
}
|
||||
////fmt.Println(j)
|
||||
//nvtDevices[i] = NewDevice()
|
||||
}
|
||||
|
||||
return nvtDevices
|
||||
}
|
||||
|
||||
func (dev *Device) getSupportedServices(resp *http.Response) {
|
||||
//resp, err := dev.CallMethod(device.GetCapabilities{Category:"All"})
|
||||
//if err != nil {
|
||||
// log.Println(err.Error())
|
||||
//return
|
||||
//} else {
|
||||
doc := etree.NewDocument()
|
||||
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
@@ -159,28 +165,27 @@ func (dev *Device) getSupportedServices(resp *http.Response) {
|
||||
}
|
||||
services := doc.FindElements("./Envelope/Body/GetCapabilitiesResponse/Capabilities/*/XAddr")
|
||||
for _, j := range services {
|
||||
////fmt.Println(j.Text())
|
||||
////fmt.Println(j.Parent().Tag)
|
||||
dev.addEndpoint(j.Parent().Tag, j.Text())
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
//NewDevice function construct a ONVIF Device entity
|
||||
func NewDevice(xaddr string) (*Device, error) {
|
||||
func NewDevice(params DeviceParams) (*Device, error) {
|
||||
dev := new(Device)
|
||||
dev.xaddr = xaddr
|
||||
dev.params = params
|
||||
dev.endpoints = make(map[string]string)
|
||||
dev.addEndpoint("Device", "http://"+xaddr+"/onvif/device_service")
|
||||
dev.addEndpoint("Device", "http://"+dev.params.Xaddr+"/onvif/device_service")
|
||||
|
||||
if dev.params.HttpClient == nil {
|
||||
dev.params.HttpClient = new(http.Client)
|
||||
}
|
||||
|
||||
getCapabilities := device.GetCapabilities{Category: "All"}
|
||||
|
||||
resp, err := dev.CallMethod(getCapabilities)
|
||||
//fmt.Println(resp.Request.Host)
|
||||
//fmt.Println(readResponse(resp))
|
||||
|
||||
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")
|
||||
return nil, errors.New("camera is not available at " + dev.params.Xaddr + " or it does not support ONVIF services")
|
||||
}
|
||||
|
||||
dev.getSupportedServices(resp)
|
||||
@@ -188,20 +193,17 @@ func NewDevice(xaddr string) (*Device, error) {
|
||||
}
|
||||
|
||||
func (dev *Device) addEndpoint(Key, Value string) {
|
||||
|
||||
//use lowCaseKey
|
||||
//make key having ability to handle Mixed Case for Different vendor devcie (e.g. Events EVENTS, events)
|
||||
lowCaseKey := strings.ToLower(Key)
|
||||
dev.endpoints[lowCaseKey] = Value
|
||||
}
|
||||
|
||||
//Authenticate function authenticate client in the ONVIF device.
|
||||
//Function takes <username> and <password> params.
|
||||
//You should use this function to allow authorized requests to the ONVIF Device
|
||||
//To change auth data call this function again.
|
||||
func (dev *Device) Authenticate(username, password string) {
|
||||
dev.login = username
|
||||
dev.password = password
|
||||
// Replace host with host from device params.
|
||||
if u, err := url.Parse(Value); err == nil {
|
||||
u.Host = dev.params.Xaddr
|
||||
Value = u.String()
|
||||
}
|
||||
|
||||
dev.endpoints[lowCaseKey] = Value
|
||||
}
|
||||
|
||||
//GetEndpoint returns specific ONVIF service endpoint address
|
||||
@@ -209,7 +211,7 @@ func (dev *Device) GetEndpoint(name string) string {
|
||||
return dev.endpoints[name]
|
||||
}
|
||||
|
||||
func buildMethodSOAP(msg string) (gosoap.SoapMessage, error) {
|
||||
func (dev Device) buildMethodSOAP(msg string) (gosoap.SoapMessage, error) {
|
||||
doc := etree.NewDocument()
|
||||
if err := doc.ReadFromString(msg); err != nil {
|
||||
//log.Println("Got error")
|
||||
@@ -220,7 +222,6 @@ func buildMethodSOAP(msg string) (gosoap.SoapMessage, error) {
|
||||
|
||||
soap := gosoap.NewEmptySOAP()
|
||||
soap.AddBodyContent(element)
|
||||
//soap.AddRootNamespace("onvif", "http://www.onvif.org/ver10/device/wsdl")
|
||||
|
||||
return soap, nil
|
||||
}
|
||||
@@ -261,41 +262,23 @@ func (dev Device) CallMethod(method interface{}) (*http.Response, error) {
|
||||
|
||||
//CallMethod functions call an method, defined <method> struct with authentication data
|
||||
func (dev Device) callMethodDo(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 nil, err
|
||||
}
|
||||
//fmt.Println(gosoap.SoapMessage(string(output)).StringIndent())
|
||||
/*
|
||||
Build an SOAP request with <method>
|
||||
*/
|
||||
soap, err := buildMethodSOAP(string(output))
|
||||
|
||||
soap, err := dev.buildMethodSOAP(string(output))
|
||||
if err != nil {
|
||||
//log.Printf("error: %v\n", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//fmt.Println(soap.StringIndent())
|
||||
/*
|
||||
Adding namespaces and WS-Security headers
|
||||
*/
|
||||
soap.AddRootNamespaces(Xlmns)
|
||||
|
||||
//fmt.Println(soap.StringIndent())
|
||||
//Header handling
|
||||
soap.AddAction()
|
||||
|
||||
//Auth Handling
|
||||
if dev.login != "" && dev.password != "" {
|
||||
soap.AddWSSecurity(dev.login, dev.password)
|
||||
if dev.params.Username != "" && dev.params.Password != "" {
|
||||
soap.AddWSSecurity(dev.params.Username, dev.params.Password)
|
||||
}
|
||||
//fmt.Println(soap.StringIndent())
|
||||
/*
|
||||
Sending request and returns the response
|
||||
*/
|
||||
return networking.SendSoap(endpoint, soap.String())
|
||||
|
||||
return networking.SendSoap(dev.params.HttpClient, endpoint, soap.String())
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"github.com/use-go/onvif/xsd"
|
||||
"github.com/use-go/onvif/xsd/onvif"
|
||||
"github.com/kerberos-io/onvif/xsd"
|
||||
"github.com/kerberos-io/onvif/xsd/onvif"
|
||||
)
|
||||
|
||||
type GetServiceCapabilities struct {
|
||||
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Yakovlev Dmitry, Zhorzh Palanjyan,Crazybber
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
18
README.md
18
README.md
@@ -1,4 +1,4 @@
|
||||
# onvif[golang]
|
||||
# onvif protocol
|
||||
|
||||
Simple management of onvif IP-devices cameras. onvif is an implementation of ONVIF protocol for managing onvif IP devices. The purpose of this library is convenient and easy management of IP cameras and other devices that support ONVIF standard.
|
||||
|
||||
@@ -7,19 +7,22 @@ Simple management of onvif IP-devices cameras. onvif is an implementation of ON
|
||||
To install the library, use **go get**:
|
||||
|
||||
```go
|
||||
go get github.com/use-go/onvif
|
||||
go get github.com/kerberos-io/onvif
|
||||
|
||||
```
|
||||
|
||||
## Supported services
|
||||
|
||||
The following services are fully implemented:
|
||||
The following services are implemented:
|
||||
|
||||
- Device
|
||||
- Media
|
||||
- PTZ
|
||||
- Imaging
|
||||
- Event
|
||||
- Discovery
|
||||
- Auth(More Options)
|
||||
- Soap
|
||||
|
||||
## Using
|
||||
|
||||
@@ -35,7 +38,7 @@ The following services are fully implemented:
|
||||
If there is a device on the network at the address *192.168.13.42*, and its ONVIF services use the *1234* port, then you can connect to the device in the following way:
|
||||
|
||||
```go
|
||||
dev, err := onvif.NewDevice("192.168.13.42:1234")
|
||||
dev, err := onvif.NewDevice(onvif.DeviceParams{Xaddr: "192.168.13.42:1234"})
|
||||
```
|
||||
|
||||
*The ONVIF port may differ depending on the device , to find out which port to use, you can go to the web interface of the device. **Usually this is 80 port.***
|
||||
@@ -45,8 +48,7 @@ dev, err := onvif.NewDevice("192.168.13.42:1234")
|
||||
If any function of the ONVIF services requires authentication, you must use the `Authenticate` method.
|
||||
|
||||
```go
|
||||
device := onvif.NewDevice("192.168.13.42:1234")
|
||||
device.Authenticate("username", "password")
|
||||
device := onvif.NewDevice(onvif.DeviceParams{Xaddr: "192.168.13.42:1234", Username: "username", Password: password})
|
||||
```
|
||||
|
||||
#### Defining Data Types
|
||||
@@ -91,11 +93,11 @@ To perform any function of one of the ONVIF services whose structure has been de
|
||||
|
||||
```go
|
||||
createUsers := device.CreateUsers{User: onvif.User{Username:"admin", Password:"qwerty", UserLevel:"User"}}
|
||||
device := onvif.NewDevice("192.168.13.42:1234")
|
||||
device := onvif.NewDevice(onvif.DeviceParams{Xaddr: "192.168.13.42:1234", Username: "username", Password: password})
|
||||
device.Authenticate("username", "password")
|
||||
resp, err := dev.CallMethod(createUsers)
|
||||
```
|
||||
|
||||
## Great Thanks
|
||||
|
||||
Modified from: [goonvif](https://github.com/yakovlevdmv/goonvif)
|
||||
Enhanced and Improved from: [goonvif](https://github.com/yakovlevdmv/goonvif)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package analytics
|
||||
|
||||
import (
|
||||
"github.com/use-go/onvif/xsd"
|
||||
"github.com/use-go/onvif/xsd/onvif"
|
||||
"github.com/kerberos-io/onvif/xsd"
|
||||
"github.com/kerberos-io/onvif/xsd/onvif"
|
||||
)
|
||||
|
||||
type GetSupportedRules struct {
|
||||
|
||||
12
api/api.go
12
api/api.go
@@ -12,10 +12,10 @@ import (
|
||||
|
||||
"github.com/beevik/etree"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/use-go/onvif"
|
||||
"github.com/use-go/onvif/gosoap"
|
||||
"github.com/use-go/onvif/networking"
|
||||
wsdiscovery "github.com/use-go/onvif/ws-discovery"
|
||||
"github.com/kerberos-io/onvif"
|
||||
"github.com/kerberos-io/onvif/gosoap"
|
||||
"github.com/kerberos-io/onvif/networking"
|
||||
wsdiscovery "github.com/kerberos-io/onvif/ws-discovery"
|
||||
)
|
||||
|
||||
func RunApi() {
|
||||
@@ -147,7 +147,7 @@ func callNecessaryMethod(serviceName, methodName, acceptedData, username, passwo
|
||||
soap.AddRootNamespaces(onvif.Xlmns)
|
||||
soap.AddWSSecurity(username, password)
|
||||
|
||||
servResp, err := networking.SendSoap(endpoint, soap.String())
|
||||
servResp, err := networking.SendSoap(new(http.Client), endpoint, soap.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -161,7 +161,7 @@ func callNecessaryMethod(serviceName, methodName, acceptedData, username, passwo
|
||||
}
|
||||
|
||||
func getEndpoint(service, xaddr string) (string, error) {
|
||||
dev, err := onvif.NewDevice(xaddr)
|
||||
dev, err := onvif.NewDevice(onvif.DeviceParams{Xaddr: xaddr})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ package api
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/use-go/onvif/device"
|
||||
"github.com/use-go/onvif/media"
|
||||
"github.com/use-go/onvif/ptz"
|
||||
"github.com/kerberos-io/onvif/device"
|
||||
"github.com/kerberos-io/onvif/media"
|
||||
"github.com/kerberos-io/onvif/ptz"
|
||||
)
|
||||
|
||||
func getPTZStructByName(name string) (interface{}, error) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"github.com/use-go/onvif/xsd"
|
||||
"github.com/use-go/onvif/xsd/onvif"
|
||||
"github.com/kerberos-io/onvif/xsd"
|
||||
"github.com/kerberos-io/onvif/xsd/onvif"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"github.com/use-go/onvif/xsd"
|
||||
"github.com/kerberos-io/onvif/xsd"
|
||||
)
|
||||
|
||||
//GetServiceCapabilities action
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"github.com/use-go/onvif/xsd"
|
||||
"github.com/kerberos-io/onvif/xsd"
|
||||
)
|
||||
|
||||
//Address Alias
|
||||
|
||||
@@ -6,10 +6,10 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
goonvif "github.com/use-go/onvif"
|
||||
"github.com/use-go/onvif/device"
|
||||
"github.com/use-go/onvif/gosoap"
|
||||
"github.com/use-go/onvif/xsd/onvif"
|
||||
goonvif "github.com/kerberos-io/onvif"
|
||||
"github.com/kerberos-io/onvif/device"
|
||||
"github.com/kerberos-io/onvif/gosoap"
|
||||
"github.com/kerberos-io/onvif/xsd/onvif"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -27,12 +27,15 @@ func readResponse(resp *http.Response) string {
|
||||
|
||||
func main() {
|
||||
//Getting an camera instance
|
||||
dev, err := goonvif.NewDevice("192.168.13.14:80")
|
||||
dev, err := goonvif.NewDevice(goonvif.DeviceParams{
|
||||
Xaddr: "192.168.13.14:80",
|
||||
Username: login,
|
||||
Password: password,
|
||||
HttpClient: new(http.Client),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//Authorization
|
||||
dev.Authenticate(login, password)
|
||||
|
||||
//Preparing commands
|
||||
systemDateAndTyme := device.GetSystemDateAndTime{}
|
||||
@@ -62,7 +65,7 @@ func main() {
|
||||
log.Println(err)
|
||||
} else {
|
||||
/*
|
||||
You could use https://github.com/use-go/onvif/gosoap for pretty printing response
|
||||
You could use https://github.com/kerberos-io/onvif/gosoap for pretty printing response
|
||||
*/
|
||||
fmt.Println(gosoap.SoapMessage(readResponse(createUserResponse)).StringIndent())
|
||||
}
|
||||
|
||||
83
examples/discovery_test.go
Normal file
83
examples/discovery_test.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
"github.com/kerberos-io/onvif"
|
||||
"github.com/kerberos-io/onvif/device"
|
||||
discover "github.com/kerberos-io/onvif/ws-discovery"
|
||||
)
|
||||
|
||||
func TestGetAvailableDevicesAtSpecificEthernetInterface(t *testing.T) {
|
||||
|
||||
// client()
|
||||
// runDiscovery("en0")
|
||||
s := onvif.GetAvailableDevicesAtSpecificEthernetInterface("en0")
|
||||
|
||||
log.Printf("%s", s)
|
||||
}
|
||||
|
||||
func client() {
|
||||
dev, err := onvif.NewDevice(onvif.DeviceParams{Xaddr: "192.168.3.10", Username: "admin", Password: "zsyy12345"})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Printf("output %+v", dev.GetServices())
|
||||
|
||||
res, err := dev.CallMethod(device.GetUsers{})
|
||||
bs, _ := ioutil.ReadAll(res.Body)
|
||||
log.Printf("output %+v %s", res.StatusCode, bs)
|
||||
}
|
||||
|
||||
// Host host
|
||||
type Host struct {
|
||||
URL string `json:"url"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func runDiscovery(interfaceName string) {
|
||||
var hosts []*Host
|
||||
devices := discover.SendProbe(interfaceName, nil, []string{"dn:NetworkVideoTransmitter"}, map[string]string{"dn": "http://www.onvif.org/ver10/network/wsdl"})
|
||||
for _, j := range devices {
|
||||
doc := etree.NewDocument()
|
||||
if err := doc.ReadFromString(j); err != nil {
|
||||
log.Printf("error %s", err)
|
||||
} else {
|
||||
|
||||
endpoints := doc.Root().FindElements("./Body/ProbeMatches/ProbeMatch/XAddrs")
|
||||
scopes := doc.Root().FindElements("./Body/ProbeMatches/ProbeMatch/Scopes")
|
||||
|
||||
flag := false
|
||||
|
||||
host := &Host{}
|
||||
|
||||
for _, xaddr := range endpoints {
|
||||
xaddr := strings.Split(strings.Split(xaddr.Text(), " ")[0], "/")[2]
|
||||
host.URL = xaddr
|
||||
}
|
||||
if flag {
|
||||
break
|
||||
}
|
||||
for _, scope := range scopes {
|
||||
re := regexp.MustCompile(`onvif:\/\/www\.onvif\.org\/name\/[A-Za-z0-9-]+`)
|
||||
match := re.FindStringSubmatch(scope.Text())
|
||||
host.Name = path.Base(match[0])
|
||||
}
|
||||
|
||||
hosts = append(hosts, host)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bys, _ := json.Marshal(hosts)
|
||||
log.Printf("done %s", bys)
|
||||
}
|
||||
5
go.mod
5
go.mod
@@ -1,12 +1,11 @@
|
||||
module github.com/use-go/onvif
|
||||
module github.com/kerberos-io/onvif
|
||||
|
||||
go 1.14
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/beevik/etree v1.1.0
|
||||
github.com/elgs/gostrgen v0.0.0-20161222160715-9d61ae07eeae
|
||||
github.com/gin-gonic/gin v1.6.2
|
||||
github.com/gofrs/uuid v3.2.0+incompatible
|
||||
github.com/yakovlevdmv/Golang-iso8601-duration v0.0.0-20180403125811-e5db0413b903
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0
|
||||
)
|
||||
|
||||
2
go.sum
2
go.sum
@@ -42,8 +42,6 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
|
||||
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
|
||||
github.com/yakovlevdmv/Golang-iso8601-duration v0.0.0-20180403125811-e5db0413b903 h1:W1Y09qPuFslSd+3famnSJpy4c3My7Gp1cgk5x4ZlbfU=
|
||||
github.com/yakovlevdmv/Golang-iso8601-duration v0.0.0-20180403125811-e5db0413b903/go.mod h1:9o96byDMk+osDZqiIS2a7E7y0cWmg4rRTjQRWVHpFWE=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
|
||||
@@ -62,18 +62,19 @@ func NewSecurity(username, passwd string) Security {
|
||||
charSet := gostrgen.Lower | gostrgen.Digit
|
||||
|
||||
nonceSeq, _ := gostrgen.RandGen(charsToGenerate, charSet, "", "")
|
||||
created := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
auth := Security{
|
||||
Auth: wsAuth{
|
||||
Username: username,
|
||||
Password: password{
|
||||
Type: passwordType,
|
||||
Password: generateToken(username, nonceSeq, time.Now().UTC(), passwd),
|
||||
Password: generateToken(username, nonceSeq, created, passwd),
|
||||
},
|
||||
Nonce: nonce{
|
||||
Type: encodingType,
|
||||
Nonce: nonceSeq,
|
||||
},
|
||||
Created: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
Created: created,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -81,13 +82,11 @@ func NewSecurity(username, passwd string) Security {
|
||||
}
|
||||
|
||||
//Digest = B64ENCODE( SHA1( B64DECODE( Nonce ) + Date + Password ) )
|
||||
func generateToken(Username string, Nonce string, Created time.Time, Password string) string {
|
||||
|
||||
func generateToken(Username string, Nonce string, Created string, Password string) string {
|
||||
sDec, _ := base64.StdEncoding.DecodeString(Nonce)
|
||||
|
||||
hasher := sha1.New()
|
||||
//hasher.Write([]byte((base64.StdEncoding.EncodeToString([]byte(Nonce)) + Created.Format(time.RFC3339) + Password)))
|
||||
hasher.Write([]byte(string(sDec) + Created.Format(time.RFC3339Nano) + Password))
|
||||
hasher.Write([]byte(string(sDec) + Created + Password))
|
||||
|
||||
return base64.StdEncoding.EncodeToString(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"github.com/use-go/onvif/xsd"
|
||||
"github.com/use-go/onvif/xsd/onvif"
|
||||
"github.com/kerberos-io/onvif/xsd"
|
||||
"github.com/kerberos-io/onvif/xsd/onvif"
|
||||
)
|
||||
|
||||
type Capabilities struct {
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
package networking
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"bytes"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SendSoap(endpoint, message string) (*http.Response, error) {
|
||||
httpClient := new(http.Client)
|
||||
|
||||
// SendSoap send soap message
|
||||
func SendSoap(httpClient *http.Client, endpoint, message string) (*http.Response, error) {
|
||||
resp, err := httpClient.Post(endpoint, "application/soap+xml; charset=utf-8", bytes.NewBufferString(message))
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return resp,nil
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ptz
|
||||
|
||||
import (
|
||||
"github.com/use-go/onvif/xsd"
|
||||
"github.com/use-go/onvif/xsd/onvif"
|
||||
"github.com/kerberos-io/onvif/xsd"
|
||||
"github.com/kerberos-io/onvif/xsd/onvif"
|
||||
)
|
||||
|
||||
type Capabilities struct {
|
||||
|
||||
@@ -10,9 +10,11 @@ package wsdiscovery
|
||||
*******************************************************/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
@@ -85,7 +87,9 @@ func sendUDPMulticast(msg string, interfaceName string) []string {
|
||||
b := make([]byte, bufSize)
|
||||
n, _, _, err := p.ReadFrom(b)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
result = append(result, string(b[0:n]))
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
"github.com/use-go/onvif/gosoap"
|
||||
"github.com/kerberos-io/onvif/gosoap"
|
||||
)
|
||||
|
||||
func buildProbeMessage(uuidV4 string, scopes, types []string, nmsp map[string]string) gosoap.SoapMessage {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
iso8601 "github.com/yakovlevdmv/Golang-iso8601-duration"
|
||||
"github.com/kerberos-io/onvif/xsd/iso8601"
|
||||
)
|
||||
|
||||
/*
|
||||
|
||||
104
xsd/iso8601/iso8601_duration.go
Normal file
104
xsd/iso8601/iso8601_duration.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package iso8601
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
//Duration of iso8601
|
||||
type Duration struct {
|
||||
years string //= number of years
|
||||
months string //= number of months
|
||||
days string //= number of days
|
||||
// Time section
|
||||
hours string //= the number of hours
|
||||
minutes string //= the number of minutes
|
||||
seconds string //= the number of seconds
|
||||
}
|
||||
|
||||
//NewDuration return duration
|
||||
func NewDuration(years, months, days, hours, minutes, seconds string) (*Duration, error) {
|
||||
// Pattern for Years, Months, Days, Hours and Minutes components
|
||||
pattern1 := "^$|[0-9]+"
|
||||
// Pattern for Seconds component
|
||||
pattern2 := "^$|[0-9]+(\\.[0-9]+)?"
|
||||
|
||||
matched, err := regexp.MatchString(pattern1, years)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("years value = " + years + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern1, months)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("months value = " + months + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern1, days)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("months value = " + days + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern1, hours)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("months value = " + hours + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern1, minutes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("months value = " + minutes + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern2, seconds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("years value = " + seconds + " does not match pattern " + pattern2)
|
||||
}
|
||||
|
||||
return &Duration{years: years, months: months, hours: hours, days: days, minutes: minutes, seconds: seconds}, nil
|
||||
}
|
||||
|
||||
//ISO8601Duration to string
|
||||
func (duration Duration) ISO8601Duration() string {
|
||||
var result string
|
||||
result += "P" // time duration designator
|
||||
//years
|
||||
if duration.years != "" {
|
||||
result += duration.years + "Y"
|
||||
}
|
||||
if duration.months != "" {
|
||||
result += duration.months + "M"
|
||||
}
|
||||
if duration.days != "" {
|
||||
result += duration.days + "D"
|
||||
}
|
||||
|
||||
if duration.hours != "" && duration.minutes != "" && duration.seconds != "" {
|
||||
result += "T"
|
||||
if duration.hours != "" {
|
||||
result += duration.hours + "H"
|
||||
}
|
||||
if duration.minutes != "" {
|
||||
result += duration.minutes + "M"
|
||||
}
|
||||
if duration.seconds != "" {
|
||||
result += duration.seconds + "S"
|
||||
}
|
||||
}
|
||||
|
||||
if len(result) == 1 {
|
||||
result += "T0S"
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"github.com/use-go/onvif/xsd"
|
||||
"github.com/kerberos-io/onvif/xsd"
|
||||
)
|
||||
|
||||
// BUG(r): Enum types implemented as simple string
|
||||
|
||||
Reference in New Issue
Block a user