refactor: 参数化 ONVIF 连接
This commit is contained in:
@ -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",
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user