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