Initial additional method for device

This commit is contained in:
Radhi Fadlillah
2017-05-16 07:31:22 +07:00
parent 01f9b9eca3
commit d23a0ab8f9
2 changed files with 66 additions and 10 deletions

66
device.go Normal file
View File

@@ -0,0 +1,66 @@
package onvif
import (
"bytes"
"io/ioutil"
"net/http"
"regexp"
"time"
"github.com/clbanning/mxj"
)
var httpClient = &http.Client{
Timeout: time.Second * 5,
}
// Device contains data of ONVIF camera
type Device struct {
ID string
Name string
XAddr string
User string
Password string
}
// GetSystemDateAndTime fetch date and time from ONVIF camera
func (device Device) GetSystemDateAndTime() (string, error) {
// Create request
request := `<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetSystemDateAndTime xmlns="http://www.onvif.org/ver10/device/wsdl"/>
</s:Body>
</s:Envelope>`
request = regexp.MustCompile(`\>\s+\<`).ReplaceAllString(request, "><")
request = regexp.MustCompile(`\s+`).ReplaceAllString(request, " ")
// Create request
buffer := bytes.NewBuffer([]byte(request))
req, err := http.NewRequest("POST", device.XAddr, buffer)
req.Header.Set("Content-Type", "application/soap+xml")
req.Header.Set("Charset", "utf-8")
// Send request
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Read response body
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Parse XML to map
mapXML, err := mxj.NewMapXml(responseBody)
if err != nil {
return "", err
}
dateTime, _ := mapXML.ValueForPathString("Envelope.Body.GetSystemDateAndTimeResponse.SystemDateAndTime")
return dateTime, nil
}

View File

@@ -1,10 +0,0 @@
package onvif
// Device contains data of Onvif device
type Device struct {
ID string
Name string
XAddrs []string
User string
Password string
}