feat(response): 统一结构返回

This commit is contained in:
2024-08-26 11:19:18 +08:00
parent 85c90ec1e1
commit 74c955a3f7
4 changed files with 92 additions and 40 deletions

View File

@ -3,6 +3,7 @@ package onvif
import (
"github.com/gin-gonic/gin"
"net/http"
"onvif-agent/response"
"onvif-agent/service/onvif"
)
@ -17,32 +18,26 @@ type CreateConnectionRequest struct {
func CreateConnection(c *gin.Context) {
var req CreateConnectionRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
response.NewResponse().Error(err).Send(c)
return
}
conn, err := onvif.New(req.Xaddr, req.Username, req.Password)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
info, err := conn.GetDeviceInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
// store connection
conns[conn.Device.GetDeviceParams().Xaddr] = conn
c.JSON(http.StatusOK, gin.H{
"device": info,
})
response.NewResponse().WithData(info).Send(c)
}
func GetConnections(c *gin.Context) {
@ -51,18 +46,14 @@ func GetConnections(c *gin.Context) {
for xaddr, conn := range conns {
info, err := conn.GetDeviceInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
devices[xaddr] = info
}
c.JSON(http.StatusOK, gin.H{
"connections": devices,
})
response.NewResponse().WithData(devices).Send(c)
}
func GetConnectionByXaddr(c *gin.Context) {
@ -70,30 +61,22 @@ func GetConnectionByXaddr(c *gin.Context) {
conn := conns[xaddr]
if conn == nil {
c.JSON(http.StatusNotFound, gin.H{
"message": "Connection not found",
})
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
return
}
info, err := conn.GetDeviceInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
c.JSON(http.StatusOK, gin.H{
"device": info,
})
response.NewResponse().WithData(info).Send(c)
}
func DeleteConnection(c *gin.Context) {
xaddr := c.Param("xaddr")
delete(conns, xaddr)
c.JSON(http.StatusOK, gin.H{
"message": "OK",
})
response.NewResponse().Success().Send(c)
}

View File

@ -8,6 +8,7 @@ import (
"log"
"net/http"
"onvif-agent/config"
"onvif-agent/response"
)
func CreateEventSubscription(c *gin.Context) {
@ -17,21 +18,17 @@ func CreateEventSubscription(c *gin.Context) {
conn := conns[xaddr]
if conn == nil {
c.JSON(http.StatusNotFound, gin.H{
"message": "Connection not found",
})
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
return
}
result, err := conn.SubscribeEvents(callbackURL, "PT60S")
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
c.JSON(http.StatusOK, result)
response.NewResponse().Success().WithData(result).Send(c)
}
func EventNotifyCallback(c *gin.Context) {
@ -41,7 +38,7 @@ func EventNotifyCallback(c *gin.Context) {
var notify event.Notify
envelope := gosoap.NewSOAPEnvelope(&notify)
if err := c.ShouldBindXML(&envelope); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
response.NewResponse().Error(err).WithCode(http.StatusBadRequest).Send(c)
return
}
@ -50,5 +47,5 @@ func EventNotifyCallback(c *gin.Context) {
log.Printf("Topic: %s, Message: %s", msg.Topic.TopicKinds, msg.Message.Message)
}
c.JSON(http.StatusOK, gin.H{"message": "OK"})
response.NewResponse().Success().Send(c)
}