From 7eee4cfc2647e8d198b1d9964c21c87fa54bc985 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Mon, 26 Aug 2024 14:09:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20zabbix=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=88=B0=E9=9B=86=E6=88=90=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- integration/zabbixagent/gin_handler.go | 24 +++++++++++++++++++++++ router/handler/onvif/connection.go | 27 +++++--------------------- router/handler/onvif/subscription.go | 2 +- router/router.go | 8 ++++++-- 4 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 integration/zabbixagent/gin_handler.go diff --git a/integration/zabbixagent/gin_handler.go b/integration/zabbixagent/gin_handler.go new file mode 100644 index 0000000..8209744 --- /dev/null +++ b/integration/zabbixagent/gin_handler.go @@ -0,0 +1,24 @@ +package zabbixagent + +import ( + "github.com/gin-gonic/gin" + "net/http" + "onvif-agent/router/handler/onvif" +) + +func ONVIFDeviceDiscovery(c *gin.Context) { + type ZBXDevice struct { + Xaddr string `json:"{#XADDR}"` + } + + arr := make([]ZBXDevice, 0) + for xaddr := range onvif.Conns { + arr = append(arr, ZBXDevice{ + Xaddr: xaddr, + }) + } + + c.JSON(http.StatusOK, gin.H{ + "data": arr, + }) +} diff --git a/router/handler/onvif/connection.go b/router/handler/onvif/connection.go index 0976aad..1c55e4a 100644 --- a/router/handler/onvif/connection.go +++ b/router/handler/onvif/connection.go @@ -7,7 +7,7 @@ import ( "onvif-agent/service/onvif" ) -var conns = make(map[string]*onvif.Connection) +var Conns = make(map[string]*onvif.Connection) type CreateConnectionRequest struct { Xaddr string `json:"xaddr"` @@ -35,7 +35,7 @@ func CreateConnection(c *gin.Context) { } // store connection - conns[conn.Device.GetDeviceParams().Xaddr] = conn + Conns[conn.Device.GetDeviceParams().Xaddr] = conn response.NewResponse().WithData(info).Send(c) } @@ -43,7 +43,7 @@ func CreateConnection(c *gin.Context) { func GetConnections(c *gin.Context) { devices := make(map[string]any) - for xaddr, conn := range conns { + for xaddr, conn := range Conns { info, err := conn.GetDeviceInfo() if err != nil { response.NewResponse().Error(err).Send(c) @@ -56,27 +56,10 @@ func GetConnections(c *gin.Context) { response.NewResponse().WithData(devices).Send(c) } -func ZBXConnectionDiscovery(c *gin.Context) { - type ZBXDiscovery struct { - Xaddr string `json:"{#XADDR}"` - } - - arr := make([]ZBXDiscovery, 0) - for xaddr := range conns { - arr = append(arr, ZBXDiscovery{ - Xaddr: xaddr, - }) - } - - c.JSON(http.StatusOK, gin.H{ - "data": arr, - }) -} - func GetConnectionByXaddr(c *gin.Context) { xaddr := c.Param("xaddr") - conn := conns[xaddr] + conn := Conns[xaddr] if conn == nil { response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c) return @@ -93,7 +76,7 @@ func GetConnectionByXaddr(c *gin.Context) { func DeleteConnection(c *gin.Context) { xaddr := c.Param("xaddr") - delete(conns, xaddr) + delete(Conns, xaddr) response.NewResponse().Success().Send(c) } diff --git a/router/handler/onvif/subscription.go b/router/handler/onvif/subscription.go index e573edf..66f654f 100644 --- a/router/handler/onvif/subscription.go +++ b/router/handler/onvif/subscription.go @@ -16,7 +16,7 @@ func CreateEventSubscription(c *gin.Context) { 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] + conn := Conns[xaddr] if conn == nil { response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c) return diff --git a/router/router.go b/router/router.go index 54329fd..d78a9ba 100644 --- a/router/router.go +++ b/router/router.go @@ -2,6 +2,7 @@ package router import ( "github.com/gin-gonic/gin" + "onvif-agent/integration/zabbixagent" "onvif-agent/router/handler" "onvif-agent/router/handler/onvif" ) @@ -17,8 +18,6 @@ func SetupRoutes(r *gin.Engine) { connectionGroup.GET("/", onvif.GetConnections) connectionGroup.GET("/:xaddr", onvif.GetConnectionByXaddr) connectionGroup.DELETE("/:xaddr", onvif.DeleteConnection) - - connectionGroup.POST("/zbxDiscovery", onvif.ZBXConnectionDiscovery) } subscriptionGroup := onvifGroup.Group("/subscriptions") @@ -27,4 +26,9 @@ func SetupRoutes(r *gin.Engine) { subscriptionGroup.POST("/:xaddr/callback", onvif.EventNotifyCallback) } } + + zabbixGroup := r.Group("/zabbix") + { + zabbixGroup.POST("/onvifDeviceDiscovery", zabbixagent.ONVIFDeviceDiscovery) + } }