From 82d3f9940483d30576f99833a8f3a7b0f56fd0c4 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Mon, 26 Aug 2024 14:41:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E5=91=BD=E5=90=8D=20conne?= =?UTF-8?q?ction=20->=20session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- integration/zabbixagent/gin_handler.go | 2 +- router/handler/onvif/connection.go | 35 +++++++++++++------------- router/handler/onvif/subscription.go | 5 ++-- router/router.go | 10 ++++---- service/onvif/connection.go | 8 +++--- service/onvif/device.go | 2 +- service/onvif/event.go | 2 +- 7 files changed, 31 insertions(+), 33 deletions(-) diff --git a/integration/zabbixagent/gin_handler.go b/integration/zabbixagent/gin_handler.go index 8209744..70c508f 100644 --- a/integration/zabbixagent/gin_handler.go +++ b/integration/zabbixagent/gin_handler.go @@ -12,7 +12,7 @@ func ONVIFDeviceDiscovery(c *gin.Context) { } arr := make([]ZBXDevice, 0) - for xaddr := range onvif.Conns { + for xaddr := range onvif.Sessions { arr = append(arr, ZBXDevice{ Xaddr: xaddr, }) diff --git a/router/handler/onvif/connection.go b/router/handler/onvif/connection.go index 1c55e4a..ab337de 100644 --- a/router/handler/onvif/connection.go +++ b/router/handler/onvif/connection.go @@ -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) } diff --git a/router/handler/onvif/subscription.go b/router/handler/onvif/subscription.go index f268b9f..46d8b0d 100644 --- a/router/handler/onvif/subscription.go +++ b/router/handler/onvif/subscription.go @@ -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) diff --git a/router/router.go b/router/router.go index 8e827b3..e8ee2c4 100644 --- a/router/router.go +++ b/router/router.go @@ -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") diff --git a/service/onvif/connection.go b/service/onvif/connection.go index 0932624..47a15fc 100644 --- a/service/onvif/connection.go +++ b/service/onvif/connection.go @@ -5,16 +5,16 @@ import ( "strings" ) -type Connection struct { +type Session struct { Device *onvif.Device `json:"device"` } -func New(xaddr string, username string, password string) (*Connection, error) { +func NewSession(xaddr string, username string, password string) (*Session, error) { // 规范化连接地址 if !strings.Contains(xaddr, ":") { xaddr += ":80" } - + dev, err := onvif.NewDevice(onvif.DeviceParams{ Xaddr: xaddr, Username: username, @@ -25,5 +25,5 @@ func New(xaddr string, username string, password string) (*Connection, error) { return nil, err } - return &Connection{Device: dev}, nil + return &Session{Device: dev}, nil } diff --git a/service/onvif/device.go b/service/onvif/device.go index af423e9..2dbbf09 100644 --- a/service/onvif/device.go +++ b/service/onvif/device.go @@ -2,7 +2,7 @@ package onvif import "github.com/IOTechSystems/onvif/device" -func (c *Connection) GetDeviceInfo() (*device.GetDeviceInformationResponse, error) { +func (c *Session) GetDeviceInfo() (*device.GetDeviceInformationResponse, error) { resp, err := c.Device.CallMethod(device.GetDeviceInformation{}) if err != nil { return nil, err diff --git a/service/onvif/event.go b/service/onvif/event.go index c15c438..f4a5872 100644 --- a/service/onvif/event.go +++ b/service/onvif/event.go @@ -5,7 +5,7 @@ import ( "github.com/IOTechSystems/onvif/xsd" ) -func (c *Connection) SubscribeEvents( +func (c *Session) SubscribeEvents( consumerAddress event.AttributedURIType, terminationTime xsd.String, // PT60S ) (*event.SubscribeResponse, error) {