refactor: 重命名 connection -> session
This commit is contained in:
@ -7,44 +7,43 @@ import (
|
||||
"onvif-agent/service/onvif"
|
||||
)
|
||||
|
||||
var Conns = make(map[string]*onvif.Connection)
|
||||
var Sessions = make(map[string]*onvif.Session)
|
||||
|
||||
type CreateConnectionRequest struct {
|
||||
type CreateSessionRequest struct {
|
||||
Xaddr string `json:"xaddr"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func CreateConnection(c *gin.Context) {
|
||||
var req CreateConnectionRequest
|
||||
func CreateSession(c *gin.Context) {
|
||||
var req CreateSessionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.NewResponse().Error(err).Send(c)
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := onvif.New(req.Xaddr, req.Username, req.Password)
|
||||
session, err := onvif.NewSession(req.Xaddr, req.Username, req.Password)
|
||||
if err != nil {
|
||||
response.NewResponse().Error(err).Send(c)
|
||||
return
|
||||
}
|
||||
|
||||
info, err := conn.GetDeviceInfo()
|
||||
info, err := session.GetDeviceInfo()
|
||||
if err != nil {
|
||||
response.NewResponse().Error(err).Send(c)
|
||||
return
|
||||
}
|
||||
|
||||
// store connection
|
||||
Conns[conn.Device.GetDeviceParams().Xaddr] = conn
|
||||
Sessions[session.Device.GetDeviceParams().Xaddr] = session
|
||||
|
||||
response.NewResponse().WithData(info).Send(c)
|
||||
}
|
||||
|
||||
func GetConnections(c *gin.Context) {
|
||||
func GetSessions(c *gin.Context) {
|
||||
devices := make(map[string]any)
|
||||
|
||||
for xaddr, conn := range Conns {
|
||||
info, err := conn.GetDeviceInfo()
|
||||
for xaddr, session := range Sessions {
|
||||
info, err := session.GetDeviceInfo()
|
||||
if err != nil {
|
||||
response.NewResponse().Error(err).Send(c)
|
||||
return
|
||||
@ -56,16 +55,16 @@ func GetConnections(c *gin.Context) {
|
||||
response.NewResponse().WithData(devices).Send(c)
|
||||
}
|
||||
|
||||
func GetConnectionByXaddr(c *gin.Context) {
|
||||
func GetSessionByXaddr(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
|
||||
conn := Conns[xaddr]
|
||||
if conn == nil {
|
||||
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
||||
session := Sessions[xaddr]
|
||||
if session == nil {
|
||||
response.NewResponse().Fail("Session not found").WithCode(http.StatusNotFound).Send(c)
|
||||
return
|
||||
}
|
||||
|
||||
info, err := conn.GetDeviceInfo()
|
||||
info, err := session.GetDeviceInfo()
|
||||
if err != nil {
|
||||
response.NewResponse().Error(err).Send(c)
|
||||
return
|
||||
@ -74,9 +73,9 @@ func GetConnectionByXaddr(c *gin.Context) {
|
||||
response.NewResponse().WithData(info).Send(c)
|
||||
}
|
||||
|
||||
func DeleteConnection(c *gin.Context) {
|
||||
func DeleteSession(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
delete(Conns, xaddr)
|
||||
delete(Sessions, xaddr)
|
||||
|
||||
response.NewResponse().Success().Send(c)
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ func CreateSubscription(c *gin.Context) {
|
||||
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Config.App.URL, xaddr))
|
||||
log.Printf("CreateSubscription callback URL: %s", callbackURL)
|
||||
|
||||
conn := Conns[xaddr]
|
||||
conn := Sessions[xaddr]
|
||||
if conn == nil {
|
||||
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
||||
return
|
||||
@ -32,8 +32,7 @@ func CreateSubscription(c *gin.Context) {
|
||||
}
|
||||
|
||||
func NotifyCallback(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
log.Printf("NotifyCallback from: %s", xaddr)
|
||||
//xaddr := c.Param("xaddr")
|
||||
|
||||
var notify event.Notify
|
||||
envelope := gosoap.NewSOAPEnvelope(¬ify)
|
||||
|
@ -12,12 +12,12 @@ func SetupRoutes(r *gin.Engine) {
|
||||
|
||||
onvifGroup := r.Group("/onvif")
|
||||
{
|
||||
connectionGroup := onvifGroup.Group("/connections")
|
||||
connectionGroup := onvifGroup.Group("/sessions")
|
||||
{
|
||||
connectionGroup.POST("/", onvif.CreateConnection)
|
||||
connectionGroup.GET("/", onvif.GetConnections)
|
||||
connectionGroup.GET("/:xaddr", onvif.GetConnectionByXaddr)
|
||||
connectionGroup.DELETE("/:xaddr", onvif.DeleteConnection)
|
||||
connectionGroup.POST("/", onvif.CreateSession)
|
||||
connectionGroup.GET("/", onvif.GetSessions)
|
||||
connectionGroup.GET("/:xaddr", onvif.GetSessionByXaddr)
|
||||
connectionGroup.DELETE("/:xaddr", onvif.DeleteSession)
|
||||
}
|
||||
|
||||
subscriptionGroup := onvifGroup.Group("/subscriptions")
|
||||
|
Reference in New Issue
Block a user