refactor(router): 改为结构化 web 项目
This commit is contained in:
40
service/onvif/connection.go
Normal file
40
service/onvif/connection.go
Normal file
@ -0,0 +1,40 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"github.com/IOTechSystems/onvif"
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
Params DeviceParams `json:"params"`
|
||||
Device *onvif.Device `json:"device"`
|
||||
}
|
||||
|
||||
type DeviceParams struct {
|
||||
Xaddr string `json:"xaddr"`
|
||||
EndpointRefAddress string `json:"endpointRefAddress"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
AuthMode string `json:"authMode"`
|
||||
}
|
||||
|
||||
func NewConnection(addr string, username string, password string) (*Connection, error) {
|
||||
dev, err := onvif.NewDevice(onvif.DeviceParams{
|
||||
Xaddr: addr,
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Connection{
|
||||
Params: DeviceParams{
|
||||
Xaddr: dev.GetDeviceParams().Xaddr,
|
||||
EndpointRefAddress: dev.GetDeviceParams().EndpointRefAddress,
|
||||
Username: dev.GetDeviceParams().Username,
|
||||
Password: dev.GetDeviceParams().Password,
|
||||
AuthMode: dev.GetDeviceParams().AuthMode,
|
||||
},
|
||||
Device: dev,
|
||||
}, nil
|
||||
}
|
18
service/onvif/device.go
Normal file
18
service/onvif/device.go
Normal file
@ -0,0 +1,18 @@
|
||||
package onvif
|
||||
|
||||
import "github.com/IOTechSystems/onvif/device"
|
||||
|
||||
func (c *Connection) GetDeviceInformation() (*device.GetDeviceInformationResponse, error) {
|
||||
resp, err := c.Device.CallMethod(device.GetDeviceInformation{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &device.GetDeviceInformationResponse{}
|
||||
err = unmarshalResponse(resp, result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
34
service/onvif/event.go
Normal file
34
service/onvif/event.go
Normal file
@ -0,0 +1,34 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"github.com/IOTechSystems/onvif/event"
|
||||
"github.com/IOTechSystems/onvif/xsd"
|
||||
)
|
||||
|
||||
func (c *Connection) SubscribeEvents(
|
||||
consumerAddress event.AttributedURIType,
|
||||
terminationTime xsd.String, // PT60S
|
||||
) (*event.SubscribeResponse, error) {
|
||||
resp, err := c.Device.CallMethod(event.Subscribe{
|
||||
ConsumerReference: &event.EndpointReferenceType{
|
||||
Address: consumerAddress,
|
||||
},
|
||||
Filter: &event.FilterType{
|
||||
TopicExpression: &event.TopicExpressionType{
|
||||
TopicKinds: "tns1:VideoSource//.",
|
||||
},
|
||||
},
|
||||
TerminationTime: &terminationTime,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &event.SubscribeResponse{}
|
||||
err = unmarshalResponse(resp, result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
30
service/onvif/util.go
Normal file
30
service/onvif/util.go
Normal file
@ -0,0 +1,30 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"github.com/IOTechSystems/onvif/gosoap"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func readResponse(resp *http.Response) (string, error) {
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
func unmarshalResponse(resp *http.Response, target any) error {
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = xml.Unmarshal(b, gosoap.NewSOAPEnvelope(target)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user