55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package onvif
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/IOTechSystems/onvif/event"
|
|
"github.com/IOTechSystems/onvif/gosoap"
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"net/http"
|
|
"onvif-agent/config"
|
|
)
|
|
|
|
func CreateEventSubscription(c *gin.Context) {
|
|
xaddr := c.Param("xaddr")
|
|
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Conf.App.URL, xaddr))
|
|
log.Printf("CreateEventSubscription callback URL: %s", callbackURL)
|
|
|
|
conn := conns[xaddr]
|
|
if conn == nil {
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"message": "Connection not found",
|
|
})
|
|
return
|
|
}
|
|
|
|
result, err := conn.SubscribeEvents(callbackURL, "PT60S")
|
|
if err != nil {
|
|
c.JSON(http.StatusServiceUnavailable, gin.H{
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
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 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// TODO: handle notifications
|
|
for _, msg := range envelope.Body.Content.(*event.Notify).NotificationMessage {
|
|
log.Printf("Topic: %s, Message: %s", msg.Topic.TopicKinds, msg.Message.Message)
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "OK"})
|
|
}
|