80 lines
1.6 KiB
Go

package onvif
import (
"github.com/gin-gonic/gin"
"net/http"
"onvif-agent/response"
"onvif-agent/service/onvif"
)
type CreateSessionRequest struct {
Xaddr string `json:"xaddr"`
Username string `json:"username"`
Password string `json:"password"`
}
func CreateSession(c *gin.Context) {
var req CreateSessionRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.NewResponse().Error(err).Send(c)
return
}
session, err := onvif.NewSession(req.Xaddr, req.Username, req.Password)
if err != nil {
response.NewResponse().Error(err).Send(c)
return
}
info, err := session.GetDeviceInfo()
if err != nil {
response.NewResponse().Error(err).Send(c)
return
}
onvif.Sessions[session.Device.GetDeviceParams().Xaddr] = session
response.NewResponse().WithData(info).Send(c)
}
func GetSessions(c *gin.Context) {
devices := make(map[string]any)
for xaddr, session := range onvif.Sessions {
info, err := session.GetDeviceInfo()
if err != nil {
response.NewResponse().Error(err).Send(c)
return
}
devices[xaddr] = info
}
response.NewResponse().WithData(devices).Send(c)
}
func GetSessionByXaddr(c *gin.Context) {
xaddr := c.Param("xaddr")
session := onvif.Sessions[xaddr]
if session == nil {
response.NewResponse().Fail("Session not found").WithCode(http.StatusNotFound).Send(c)
return
}
info, err := session.GetDeviceInfo()
if err != nil {
response.NewResponse().Error(err).Send(c)
return
}
response.NewResponse().WithData(info).Send(c)
}
func DeleteSession(c *gin.Context) {
xaddr := c.Param("xaddr")
delete(onvif.Sessions, xaddr)
response.NewResponse().Success().Send(c)
}