diff --git a/router/handler/onvif/connection.go b/router/handler/onvif/connection.go index e4fb256..381cbc3 100644 --- a/router/handler/onvif/connection.go +++ b/router/handler/onvif/connection.go @@ -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(), diff --git a/router/handler/onvif/subscription.go b/router/handler/onvif/subscription.go index b142e32..e30f229 100644 --- a/router/handler/onvif/subscription.go +++ b/router/handler/onvif/subscription.go @@ -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(¬ify) 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"}) diff --git a/router/handler/ping.go b/router/handler/ping.go new file mode 100644 index 0000000..5ec69b5 --- /dev/null +++ b/router/handler/ping.go @@ -0,0 +1,11 @@ +package handler + +import ( + "github.com/gin-gonic/gin" +) + +func Ping(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "pong", + }) +} diff --git a/router/router.go b/router/router.go index d2bc1f7..984171a 100644 --- a/router/router.go +++ b/router/router.go @@ -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) } } }