52 lines
1.4 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"
"onvif-agent/response"
)
func CreateEventSubscription(c *gin.Context) {
xaddr := c.Param("xaddr")
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Config.App.URL, xaddr))
log.Printf("CreateEventSubscription callback URL: %s", callbackURL)
conn := Conns[xaddr]
if conn == nil {
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
return
}
result, err := conn.SubscribeEvents(callbackURL, "PT60S")
if err != nil {
response.NewResponse().Error(err).Send(c)
return
}
response.NewResponse().Success().WithData(result).Send(c)
}
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 {
response.NewResponse().Error(err).WithCode(http.StatusBadRequest).Send(c)
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)
}
response.NewResponse().Success().Send(c)
}