feat(router): 参数化 callback URL
This commit is contained in:
parent
49fb4b1f91
commit
66254725df
@ -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(),
|
||||
|
@ -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"})
|
||||
|
11
router/handler/ping.go
Normal file
11
router/handler/ping.go
Normal 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",
|
||||
})
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user