feat(router): 参数化 callback URL

This commit is contained in:
Liam Chan 2024-08-22 12:07:45 +08:00
parent 49fb4b1f91
commit 66254725df
4 changed files with 22 additions and 5 deletions

View File

@ -9,7 +9,7 @@ import (
var conns = make(map[string]*onvif.Connection)
func CreateConnection(c *gin.Context) {
conn, err := onvif.NewConnection("172.16.19.239", "admin", "admin123")
conn, err := onvif.NewConnection("172.16.19.239:80", "admin", "admin123")
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{
"message": err.Error(),

View File

@ -12,7 +12,8 @@ import (
func CreateEventSubscription(c *gin.Context) {
xaddr := c.Param("xaddr")
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/subscriptions/callback", config.Conf.App.URL))
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Conf.App.URL, xaddr))
log.Printf("CreateEventSubscription callback URL: %s", callbackURL)
result, err := conns[xaddr].SubscribeEvents(callbackURL, "PT60S")
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{
@ -25,6 +26,9 @@ func CreateEventSubscription(c *gin.Context) {
}
func EventNotifyCallback(c *gin.Context) {
xaddr := c.Param("xaddr")
log.Printf("EventNotifyCallback from: %s", xaddr)
var notify event.Notify
envelope := gosoap.NewSOAPEnvelope(&notify)
if err := c.ShouldBindXML(&envelope); err != nil {
@ -34,7 +38,7 @@ func EventNotifyCallback(c *gin.Context) {
// TODO: handle notifications
for _, msg := range envelope.Body.Content.(*event.Notify).NotificationMessage {
log.Println(msg.Topic.TopicKinds, msg.Message.Message)
log.Printf("Topic: %s, Message: %s", msg.Topic.TopicKinds, msg.Message.Message)
}
c.JSON(http.StatusOK, gin.H{"message": "OK"})

11
router/handler/ping.go Normal file
View File

@ -0,0 +1,11 @@
package handler
import (
"github.com/gin-gonic/gin"
)
func Ping(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}

View File

@ -2,11 +2,13 @@ package router
import (
"github.com/gin-gonic/gin"
"onvif-agent/router/handler"
"onvif-agent/router/handler/onvif"
)
func SetupRoutes(r *gin.Engine) {
// ONVIF 相关路由
r.GET("/ping", handler.Ping)
userGroup := r.Group("/onvif")
{
connectionGroup := userGroup.Group("/connections")
@ -20,7 +22,7 @@ func SetupRoutes(r *gin.Engine) {
subscriptionGroup := userGroup.Group("/subscriptions")
{
subscriptionGroup.POST("/:xaddr", onvif.CreateEventSubscription)
subscriptionGroup.POST("/callback", onvif.EventNotifyCallback)
subscriptionGroup.POST("/:xaddr/callback", onvif.EventNotifyCallback)
}
}
}