refactor: 参数化 ONVIF 连接
This commit is contained in:
parent
66254725df
commit
c7327246a6
@ -8,10 +8,22 @@ import (
|
||||
|
||||
var conns = make(map[string]*onvif.Connection)
|
||||
|
||||
type CreateConnectionRequest struct {
|
||||
Xaddr string `json:"xaddr"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func CreateConnection(c *gin.Context) {
|
||||
conn, err := onvif.NewConnection("172.16.19.239:80", "admin", "admin123")
|
||||
var req CreateConnectionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := onvif.NewConnection(req.Xaddr, req.Username, req.Password)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
@ -20,22 +32,67 @@ func CreateConnection(c *gin.Context) {
|
||||
conns[conn.Device.GetDeviceParams().Xaddr] = conn
|
||||
|
||||
info, err := conn.GetDeviceInformation()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"connectionParams": conn.Params,
|
||||
"deviceInfo": info,
|
||||
"device": info,
|
||||
})
|
||||
}
|
||||
|
||||
func GetConnections(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, conns)
|
||||
devices := make(map[string]interface{})
|
||||
|
||||
for xaddr, conn := range conns {
|
||||
info, err := conn.GetDeviceInformation()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
devices[xaddr] = info
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"connections": devices,
|
||||
})
|
||||
}
|
||||
|
||||
func GetConnectionByXaddr(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
c.JSON(http.StatusOK, conns[xaddr])
|
||||
|
||||
conn := conns[xaddr]
|
||||
if conn == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"message": "Connection not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
info, err := conn.GetDeviceInformation()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"device": info,
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteConnection(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
delete(conns, xaddr)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "OK",
|
||||
})
|
||||
}
|
||||
|
@ -5,21 +5,12 @@ import (
|
||||
)
|
||||
|
||||
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) {
|
||||
func NewConnection(xaddr string, username string, password string) (*Connection, error) {
|
||||
dev, err := onvif.NewDevice(onvif.DeviceParams{
|
||||
Xaddr: addr,
|
||||
Xaddr: xaddr,
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
@ -27,14 +18,5 @@ func NewConnection(addr string, username string, password string) (*Connection,
|
||||
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
|
||||
return &Connection{Device: dev}, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user