refactor: 迁移 service 代码逻辑

This commit is contained in:
Liam Chan 2024-08-26 14:50:46 +08:00
parent de3667f957
commit 219116c961
5 changed files with 37 additions and 27 deletions

View File

@ -3,7 +3,7 @@ package zabbixagent
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"onvif-agent/router/handler/onvif" "onvif-agent/service/onvif"
) )
func ONVIFDeviceDiscovery(c *gin.Context) { func ONVIFDeviceDiscovery(c *gin.Context) {

View File

@ -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(&notify)
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)
}

View File

@ -7,8 +7,6 @@ import (
"onvif-agent/service/onvif" "onvif-agent/service/onvif"
) )
var Sessions = make(map[string]*onvif.Session)
type CreateSessionRequest struct { type CreateSessionRequest struct {
Xaddr string `json:"xaddr"` Xaddr string `json:"xaddr"`
Username string `json:"username"` Username string `json:"username"`
@ -34,7 +32,7 @@ func CreateSession(c *gin.Context) {
return return
} }
Sessions[session.Device.GetDeviceParams().Xaddr] = session onvif.Sessions[session.Device.GetDeviceParams().Xaddr] = session
response.NewResponse().WithData(info).Send(c) response.NewResponse().WithData(info).Send(c)
} }
@ -42,7 +40,7 @@ func CreateSession(c *gin.Context) {
func GetSessions(c *gin.Context) { func GetSessions(c *gin.Context) {
devices := make(map[string]any) devices := make(map[string]any)
for xaddr, session := range Sessions { for xaddr, session := range onvif.Sessions {
info, err := session.GetDeviceInfo() info, err := session.GetDeviceInfo()
if err != nil { if err != nil {
response.NewResponse().Error(err).Send(c) response.NewResponse().Error(err).Send(c)
@ -58,7 +56,7 @@ func GetSessions(c *gin.Context) {
func GetSessionByXaddr(c *gin.Context) { func GetSessionByXaddr(c *gin.Context) {
xaddr := c.Param("xaddr") xaddr := c.Param("xaddr")
session := Sessions[xaddr] session := onvif.Sessions[xaddr]
if session == nil { if session == nil {
response.NewResponse().Fail("Session not found").WithCode(http.StatusNotFound).Send(c) response.NewResponse().Fail("Session not found").WithCode(http.StatusNotFound).Send(c)
return return
@ -75,7 +73,7 @@ func GetSessionByXaddr(c *gin.Context) {
func DeleteSession(c *gin.Context) { func DeleteSession(c *gin.Context) {
xaddr := c.Param("xaddr") xaddr := c.Param("xaddr")
delete(Sessions, xaddr) delete(onvif.Sessions, xaddr)
response.NewResponse().Success().Send(c) response.NewResponse().Success().Send(c)
} }

View File

@ -3,12 +3,12 @@ package onvif
import ( import (
"fmt" "fmt"
"github.com/IOTechSystems/onvif/event" "github.com/IOTechSystems/onvif/event"
"github.com/IOTechSystems/onvif/gosoap"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"log" "log"
"net/http" "net/http"
"onvif-agent/config" "onvif-agent/config"
"onvif-agent/response" "onvif-agent/response"
"onvif-agent/service/onvif"
) )
func CreateSubscription(c *gin.Context) { 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)) callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Config.App.URL, xaddr))
log.Printf("CreateSubscription callback URL: %s", callbackURL) log.Printf("CreateSubscription callback URL: %s", callbackURL)
conn := Sessions[xaddr] conn := onvif.Sessions[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
@ -30,21 +30,3 @@ func CreateSubscription(c *gin.Context) {
response.NewResponse().Success().WithData(result).Send(c) response.NewResponse().Success().WithData(result).Send(c)
} }
func NotifyCallback(c *gin.Context) {
//xaddr := c.Param("xaddr")
var notify event.Notify
envelope := gosoap.NewSOAPEnvelope(&notify)
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)
}

View File

@ -9,6 +9,8 @@ type Session struct {
Device *onvif.Device `json:"device"` Device *onvif.Device `json:"device"`
} }
var Sessions = make(map[string]*Session)
func NewSession(xaddr string, username string, password string) (*Session, error) { func NewSession(xaddr string, username string, password string) (*Session, error) {
// 规范化连接地址 // 规范化连接地址
if !strings.Contains(xaddr, ":") { if !strings.Contains(xaddr, ":") {