refactor: 重构 zabbix 功能到集成模块中
This commit is contained in:
parent
00d2b30658
commit
7eee4cfc26
24
integration/zabbixagent/gin_handler.go
Normal file
24
integration/zabbixagent/gin_handler.go
Normal 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,
|
||||||
|
})
|
||||||
|
}
|
@ -7,7 +7,7 @@ import (
|
|||||||
"onvif-agent/service/onvif"
|
"onvif-agent/service/onvif"
|
||||||
)
|
)
|
||||||
|
|
||||||
var conns = make(map[string]*onvif.Connection)
|
var Conns = make(map[string]*onvif.Connection)
|
||||||
|
|
||||||
type CreateConnectionRequest struct {
|
type CreateConnectionRequest struct {
|
||||||
Xaddr string `json:"xaddr"`
|
Xaddr string `json:"xaddr"`
|
||||||
@ -35,7 +35,7 @@ func CreateConnection(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// store connection
|
// store connection
|
||||||
conns[conn.Device.GetDeviceParams().Xaddr] = conn
|
Conns[conn.Device.GetDeviceParams().Xaddr] = conn
|
||||||
|
|
||||||
response.NewResponse().WithData(info).Send(c)
|
response.NewResponse().WithData(info).Send(c)
|
||||||
}
|
}
|
||||||
@ -43,7 +43,7 @@ func CreateConnection(c *gin.Context) {
|
|||||||
func GetConnections(c *gin.Context) {
|
func GetConnections(c *gin.Context) {
|
||||||
devices := make(map[string]any)
|
devices := make(map[string]any)
|
||||||
|
|
||||||
for xaddr, conn := range conns {
|
for xaddr, conn := range Conns {
|
||||||
info, err := conn.GetDeviceInfo()
|
info, err := conn.GetDeviceInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.NewResponse().Error(err).Send(c)
|
response.NewResponse().Error(err).Send(c)
|
||||||
@ -56,27 +56,10 @@ func GetConnections(c *gin.Context) {
|
|||||||
response.NewResponse().WithData(devices).Send(c)
|
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) {
|
func GetConnectionByXaddr(c *gin.Context) {
|
||||||
xaddr := c.Param("xaddr")
|
xaddr := c.Param("xaddr")
|
||||||
|
|
||||||
conn := conns[xaddr]
|
conn := Conns[xaddr]
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
||||||
return
|
return
|
||||||
@ -93,7 +76,7 @@ func GetConnectionByXaddr(c *gin.Context) {
|
|||||||
|
|
||||||
func DeleteConnection(c *gin.Context) {
|
func DeleteConnection(c *gin.Context) {
|
||||||
xaddr := c.Param("xaddr")
|
xaddr := c.Param("xaddr")
|
||||||
delete(conns, xaddr)
|
delete(Conns, xaddr)
|
||||||
|
|
||||||
response.NewResponse().Success().Send(c)
|
response.NewResponse().Success().Send(c)
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ func CreateEventSubscription(c *gin.Context) {
|
|||||||
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Config.App.URL, xaddr))
|
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Config.App.URL, xaddr))
|
||||||
log.Printf("CreateEventSubscription callback URL: %s", callbackURL)
|
log.Printf("CreateEventSubscription callback URL: %s", callbackURL)
|
||||||
|
|
||||||
conn := conns[xaddr]
|
conn := Conns[xaddr]
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
||||||
return
|
return
|
||||||
|
@ -2,6 +2,7 @@ package router
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"onvif-agent/integration/zabbixagent"
|
||||||
"onvif-agent/router/handler"
|
"onvif-agent/router/handler"
|
||||||
"onvif-agent/router/handler/onvif"
|
"onvif-agent/router/handler/onvif"
|
||||||
)
|
)
|
||||||
@ -17,8 +18,6 @@ func SetupRoutes(r *gin.Engine) {
|
|||||||
connectionGroup.GET("/", onvif.GetConnections)
|
connectionGroup.GET("/", onvif.GetConnections)
|
||||||
connectionGroup.GET("/:xaddr", onvif.GetConnectionByXaddr)
|
connectionGroup.GET("/:xaddr", onvif.GetConnectionByXaddr)
|
||||||
connectionGroup.DELETE("/:xaddr", onvif.DeleteConnection)
|
connectionGroup.DELETE("/:xaddr", onvif.DeleteConnection)
|
||||||
|
|
||||||
connectionGroup.POST("/zbxDiscovery", onvif.ZBXConnectionDiscovery)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subscriptionGroup := onvifGroup.Group("/subscriptions")
|
subscriptionGroup := onvifGroup.Group("/subscriptions")
|
||||||
@ -27,4 +26,9 @@ func SetupRoutes(r *gin.Engine) {
|
|||||||
subscriptionGroup.POST("/:xaddr/callback", onvif.EventNotifyCallback)
|
subscriptionGroup.POST("/:xaddr/callback", onvif.EventNotifyCallback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zabbixGroup := r.Group("/zabbix")
|
||||||
|
{
|
||||||
|
zabbixGroup.POST("/onvifDeviceDiscovery", zabbixagent.ONVIFDeviceDiscovery)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user