refactor: 重命名 connection -> session

This commit is contained in:
Liam Chan 2024-08-26 14:41:57 +08:00
parent a6cc98a32b
commit 82d3f99404
7 changed files with 31 additions and 33 deletions

View File

@ -12,7 +12,7 @@ func ONVIFDeviceDiscovery(c *gin.Context) {
}
arr := make([]ZBXDevice, 0)
for xaddr := range onvif.Conns {
for xaddr := range onvif.Sessions {
arr = append(arr, ZBXDevice{
Xaddr: xaddr,
})

View File

@ -7,44 +7,43 @@ import (
"onvif-agent/service/onvif"
)
var Conns = make(map[string]*onvif.Connection)
var Sessions = make(map[string]*onvif.Session)
type CreateConnectionRequest struct {
type CreateSessionRequest struct {
Xaddr string `json:"xaddr"`
Username string `json:"username"`
Password string `json:"password"`
}
func CreateConnection(c *gin.Context) {
var req CreateConnectionRequest
func CreateSession(c *gin.Context) {
var req CreateSessionRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.NewResponse().Error(err).Send(c)
return
}
conn, err := onvif.New(req.Xaddr, req.Username, req.Password)
session, err := onvif.NewSession(req.Xaddr, req.Username, req.Password)
if err != nil {
response.NewResponse().Error(err).Send(c)
return
}
info, err := conn.GetDeviceInfo()
info, err := session.GetDeviceInfo()
if err != nil {
response.NewResponse().Error(err).Send(c)
return
}
// store connection
Conns[conn.Device.GetDeviceParams().Xaddr] = conn
Sessions[session.Device.GetDeviceParams().Xaddr] = session
response.NewResponse().WithData(info).Send(c)
}
func GetConnections(c *gin.Context) {
func GetSessions(c *gin.Context) {
devices := make(map[string]any)
for xaddr, conn := range Conns {
info, err := conn.GetDeviceInfo()
for xaddr, session := range Sessions {
info, err := session.GetDeviceInfo()
if err != nil {
response.NewResponse().Error(err).Send(c)
return
@ -56,16 +55,16 @@ func GetConnections(c *gin.Context) {
response.NewResponse().WithData(devices).Send(c)
}
func GetConnectionByXaddr(c *gin.Context) {
func GetSessionByXaddr(c *gin.Context) {
xaddr := c.Param("xaddr")
conn := Conns[xaddr]
if conn == nil {
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
session := Sessions[xaddr]
if session == nil {
response.NewResponse().Fail("Session not found").WithCode(http.StatusNotFound).Send(c)
return
}
info, err := conn.GetDeviceInfo()
info, err := session.GetDeviceInfo()
if err != nil {
response.NewResponse().Error(err).Send(c)
return
@ -74,9 +73,9 @@ func GetConnectionByXaddr(c *gin.Context) {
response.NewResponse().WithData(info).Send(c)
}
func DeleteConnection(c *gin.Context) {
func DeleteSession(c *gin.Context) {
xaddr := c.Param("xaddr")
delete(Conns, xaddr)
delete(Sessions, xaddr)
response.NewResponse().Success().Send(c)
}

View File

@ -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 := Conns[xaddr]
conn := Sessions[xaddr]
if conn == nil {
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
return
@ -32,8 +32,7 @@ func CreateSubscription(c *gin.Context) {
}
func NotifyCallback(c *gin.Context) {
xaddr := c.Param("xaddr")
log.Printf("NotifyCallback from: %s", xaddr)
//xaddr := c.Param("xaddr")
var notify event.Notify
envelope := gosoap.NewSOAPEnvelope(&notify)

View File

@ -12,12 +12,12 @@ func SetupRoutes(r *gin.Engine) {
onvifGroup := r.Group("/onvif")
{
connectionGroup := onvifGroup.Group("/connections")
connectionGroup := onvifGroup.Group("/sessions")
{
connectionGroup.POST("/", onvif.CreateConnection)
connectionGroup.GET("/", onvif.GetConnections)
connectionGroup.GET("/:xaddr", onvif.GetConnectionByXaddr)
connectionGroup.DELETE("/:xaddr", onvif.DeleteConnection)
connectionGroup.POST("/", onvif.CreateSession)
connectionGroup.GET("/", onvif.GetSessions)
connectionGroup.GET("/:xaddr", onvif.GetSessionByXaddr)
connectionGroup.DELETE("/:xaddr", onvif.DeleteSession)
}
subscriptionGroup := onvifGroup.Group("/subscriptions")

View File

@ -5,16 +5,16 @@ import (
"strings"
)
type Connection struct {
type Session struct {
Device *onvif.Device `json:"device"`
}
func New(xaddr string, username string, password string) (*Connection, error) {
func NewSession(xaddr string, username string, password string) (*Session, error) {
// 规范化连接地址
if !strings.Contains(xaddr, ":") {
xaddr += ":80"
}
dev, err := onvif.NewDevice(onvif.DeviceParams{
Xaddr: xaddr,
Username: username,
@ -25,5 +25,5 @@ func New(xaddr string, username string, password string) (*Connection, error) {
return nil, err
}
return &Connection{Device: dev}, nil
return &Session{Device: dev}, nil
}

View File

@ -2,7 +2,7 @@ package onvif
import "github.com/IOTechSystems/onvif/device"
func (c *Connection) GetDeviceInfo() (*device.GetDeviceInformationResponse, error) {
func (c *Session) GetDeviceInfo() (*device.GetDeviceInformationResponse, error) {
resp, err := c.Device.CallMethod(device.GetDeviceInformation{})
if err != nil {
return nil, err

View File

@ -5,7 +5,7 @@ import (
"github.com/IOTechSystems/onvif/xsd"
)
func (c *Connection) SubscribeEvents(
func (c *Session) SubscribeEvents(
consumerAddress event.AttributedURIType,
terminationTime xsd.String, // PT60S
) (*event.SubscribeResponse, error) {