refactor: 重构 zabbix 功能到集成模块中

This commit is contained in:
Liam Chan 2024-08-26 14:09:45 +08:00
parent 00d2b30658
commit 7eee4cfc26
4 changed files with 36 additions and 25 deletions

View File

@ -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,
})
}

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}
}