forked from use-go/onvif
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
132 lines (113 loc) · 3.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"github.com/IOTechSystems/onvif"
"github.com/IOTechSystems/onvif/gosoap"
"github.com/IOTechSystems/onvif/networking"
"github.com/gin-gonic/gin"
)
func main() {
RunApi()
}
func RunApi() {
router := gin.Default()
router.POST("/:service/:function", func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
//c.Header("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers")
serviceName := c.Param("service")
functionName := c.Param("function")
username := c.GetHeader("username")
pass := c.GetHeader("password")
xaddr := c.GetHeader("xaddr")
acceptedData, err := c.GetRawData()
if err != nil {
fmt.Println(err)
}
dev, err := onvif.NewDevice(onvif.DeviceParams{
Xaddr: xaddr,
Username: username,
Password: pass,
})
message, err := CallOnvifFunction(dev, serviceName, functionName, string(acceptedData))
if err != nil {
c.XML(http.StatusBadRequest, err.Error())
} else {
c.String(http.StatusOK, message)
}
})
router.Run()
}
func CallOnvifFunction(dev *onvif.Device, serviceName, functionName, acceptedData string) (string, error) {
function, err := functionByServiceAndFunctionName(serviceName, functionName)
if err != nil {
return "", err
}
request := function.Request()
if len(acceptedData) > 0 {
err = json.Unmarshal([]byte(acceptedData), request)
if err != nil {
return "", err
}
}
requestBody, err := xml.Marshal(request)
if err != nil {
return "", err
}
endpoint, err := dev.GetEndpointByRequestStruct(request)
if err != nil {
return "", err
}
soap := gosoap.NewEmptySOAP()
soap.AddStringBodyContent(string(requestBody))
soap.AddRootNamespaces(onvif.Xlmns)
soap.AddWSSecurity(dev.GetDeviceParams().Username, dev.GetDeviceParams().Password)
servResp, err := networking.SendSoap(new(http.Client), endpoint, soap.String())
if err != nil {
return "", err
}
rsp, err := ioutil.ReadAll(servResp.Body)
if err != nil {
return "", err
}
responseEnvelope := gosoap.NewSOAPEnvelope(function.Response())
err = xml.Unmarshal(rsp, responseEnvelope)
if err != nil {
return "", err
}
if responseEnvelope.Body.Fault != nil {
jsonData, err := json.Marshal(responseEnvelope.Body.Fault)
if err != nil {
return "", err
}
return string(jsonData), nil
} else {
jsonData, err := json.Marshal(responseEnvelope.Body.Content)
if err != nil {
return "", err
}
return string(jsonData), nil
}
}
func functionByServiceAndFunctionName(serviceName, functionName string) (onvif.Function, error) {
var function onvif.Function
var exist bool
switch serviceName {
case onvif.DeviceWebService:
function, exist = onvif.DeviceFunctionMap[functionName]
if !exist {
return nil, fmt.Errorf("the web service '%s'not support the function '%s'", serviceName, functionName)
}
case onvif.MediaWebService:
function, exist = onvif.MediaFunctionMap[functionName]
if !exist {
return nil, fmt.Errorf("the web service '%s' not support the function '%s'", serviceName, functionName)
}
default:
return nil, fmt.Errorf("not support the web service '%s'", serviceName)
}
return function, nil
}