From 219116c961606f535a5a70361c7d7254f012d87e Mon Sep 17 00:00:00 2001 From: imbytecat Date: Mon, 26 Aug 2024 14:50:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=BF=81=E7=A7=BB=20service=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- integration/zabbixagent/gin_handler.go | 2 +- router/handler/onvif/notification.go | 28 +++++++++++++++++++ .../onvif/{connection.go => session.go} | 10 +++---- router/handler/onvif/subscription.go | 22 ++------------- service/onvif/{connection.go => session.go} | 2 ++ 5 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 router/handler/onvif/notification.go rename router/handler/onvif/{connection.go => session.go} (87%) rename service/onvif/{connection.go => session.go} (92%) diff --git a/integration/zabbixagent/gin_handler.go b/integration/zabbixagent/gin_handler.go index 70c508f..62fceee 100644 --- a/integration/zabbixagent/gin_handler.go +++ b/integration/zabbixagent/gin_handler.go @@ -3,7 +3,7 @@ package zabbixagent import ( "github.com/gin-gonic/gin" "net/http" - "onvif-agent/router/handler/onvif" + "onvif-agent/service/onvif" ) func ONVIFDeviceDiscovery(c *gin.Context) { diff --git a/router/handler/onvif/notification.go b/router/handler/onvif/notification.go new file mode 100644 index 0000000..d16472d --- /dev/null +++ b/router/handler/onvif/notification.go @@ -0,0 +1,28 @@ +package onvif + +import ( + "github.com/IOTechSystems/onvif/event" + "github.com/IOTechSystems/onvif/gosoap" + "github.com/gin-gonic/gin" + "log" + "net/http" + "onvif-agent/response" +) + +func NotifyCallback(c *gin.Context) { + //xaddr := c.Param("xaddr") + + var notify event.Notify + envelope := gosoap.NewSOAPEnvelope(¬ify) + if err := c.ShouldBindXML(&envelope); err != nil { + response.NewResponse().Error(err).WithCode(http.StatusBadRequest).Send(c) + return + } + + // TODO: handle notifications + for _, msg := range envelope.Body.Content.(*event.Notify).NotificationMessage { + log.Printf("Topic: %s, Message: %s", msg.Topic.TopicKinds, msg.Message.Message) + } + + response.NewResponse().Success().Send(c) +} diff --git a/router/handler/onvif/connection.go b/router/handler/onvif/session.go similarity index 87% rename from router/handler/onvif/connection.go rename to router/handler/onvif/session.go index ab337de..ee1b095 100644 --- a/router/handler/onvif/connection.go +++ b/router/handler/onvif/session.go @@ -7,8 +7,6 @@ import ( "onvif-agent/service/onvif" ) -var Sessions = make(map[string]*onvif.Session) - type CreateSessionRequest struct { Xaddr string `json:"xaddr"` Username string `json:"username"` @@ -34,7 +32,7 @@ func CreateSession(c *gin.Context) { return } - Sessions[session.Device.GetDeviceParams().Xaddr] = session + onvif.Sessions[session.Device.GetDeviceParams().Xaddr] = session response.NewResponse().WithData(info).Send(c) } @@ -42,7 +40,7 @@ func CreateSession(c *gin.Context) { func GetSessions(c *gin.Context) { devices := make(map[string]any) - for xaddr, session := range Sessions { + for xaddr, session := range onvif.Sessions { info, err := session.GetDeviceInfo() if err != nil { response.NewResponse().Error(err).Send(c) @@ -58,7 +56,7 @@ func GetSessions(c *gin.Context) { func GetSessionByXaddr(c *gin.Context) { xaddr := c.Param("xaddr") - session := Sessions[xaddr] + session := onvif.Sessions[xaddr] if session == nil { response.NewResponse().Fail("Session not found").WithCode(http.StatusNotFound).Send(c) return @@ -75,7 +73,7 @@ func GetSessionByXaddr(c *gin.Context) { func DeleteSession(c *gin.Context) { xaddr := c.Param("xaddr") - delete(Sessions, xaddr) + delete(onvif.Sessions, xaddr) response.NewResponse().Success().Send(c) } diff --git a/router/handler/onvif/subscription.go b/router/handler/onvif/subscription.go index 067c963..e36d6d7 100644 --- a/router/handler/onvif/subscription.go +++ b/router/handler/onvif/subscription.go @@ -3,12 +3,12 @@ package onvif import ( "fmt" "github.com/IOTechSystems/onvif/event" - "github.com/IOTechSystems/onvif/gosoap" "github.com/gin-gonic/gin" "log" "net/http" "onvif-agent/config" "onvif-agent/response" + "onvif-agent/service/onvif" ) func CreateSubscription(c *gin.Context) { @@ -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 := Sessions[xaddr] + conn := onvif.Sessions[xaddr] if conn == nil { response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c) return @@ -30,21 +30,3 @@ func CreateSubscription(c *gin.Context) { response.NewResponse().Success().WithData(result).Send(c) } - -func NotifyCallback(c *gin.Context) { - //xaddr := c.Param("xaddr") - - var notify event.Notify - envelope := gosoap.NewSOAPEnvelope(¬ify) - if err := c.ShouldBindXML(&envelope); err != nil { - response.NewResponse().Error(err).WithCode(http.StatusBadRequest).Send(c) - return - } - - // TODO: handle notifications - for _, msg := range envelope.Body.Content.(*event.Notify).NotificationMessage { - log.Printf("Topic: %s, Message: %s", msg.Topic.TopicKinds, msg.Message.Message) - } - - response.NewResponse().Success().Send(c) -} diff --git a/service/onvif/connection.go b/service/onvif/session.go similarity index 92% rename from service/onvif/connection.go rename to service/onvif/session.go index 47a15fc..3321dd0 100644 --- a/service/onvif/connection.go +++ b/service/onvif/session.go @@ -9,6 +9,8 @@ type Session struct { Device *onvif.Device `json:"device"` } +var Sessions = make(map[string]*Session) + func NewSession(xaddr string, username string, password string) (*Session, error) { // 规范化连接地址 if !strings.Contains(xaddr, ":") {