99 lines
1.8 KiB
Go
99 lines
1.8 KiB
Go
package onvif
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"onvif-agent/service/onvif"
|
|
)
|
|
|
|
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) {
|
|
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.StatusInternalServerError, gin.H{
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
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{
|
|
"device": info,
|
|
})
|
|
}
|
|
|
|
func GetConnections(c *gin.Context) {
|
|
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")
|
|
|
|
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",
|
|
})
|
|
}
|