refactor: 重命名 connection -> session
This commit is contained in:
parent
a6cc98a32b
commit
82d3f99404
@ -12,7 +12,7 @@ func ONVIFDeviceDiscovery(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
arr := make([]ZBXDevice, 0)
|
arr := make([]ZBXDevice, 0)
|
||||||
for xaddr := range onvif.Conns {
|
for xaddr := range onvif.Sessions {
|
||||||
arr = append(arr, ZBXDevice{
|
arr = append(arr, ZBXDevice{
|
||||||
Xaddr: xaddr,
|
Xaddr: xaddr,
|
||||||
})
|
})
|
||||||
|
@ -7,44 +7,43 @@ import (
|
|||||||
"onvif-agent/service/onvif"
|
"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"`
|
Xaddr string `json:"xaddr"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateConnection(c *gin.Context) {
|
func CreateSession(c *gin.Context) {
|
||||||
var req CreateConnectionRequest
|
var req CreateSessionRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
response.NewResponse().Error(err).Send(c)
|
response.NewResponse().Error(err).Send(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := onvif.New(req.Xaddr, req.Username, req.Password)
|
session, err := onvif.NewSession(req.Xaddr, req.Username, req.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.NewResponse().Error(err).Send(c)
|
response.NewResponse().Error(err).Send(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := conn.GetDeviceInfo()
|
info, err := session.GetDeviceInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.NewResponse().Error(err).Send(c)
|
response.NewResponse().Error(err).Send(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// store connection
|
Sessions[session.Device.GetDeviceParams().Xaddr] = session
|
||||||
Conns[conn.Device.GetDeviceParams().Xaddr] = conn
|
|
||||||
|
|
||||||
response.NewResponse().WithData(info).Send(c)
|
response.NewResponse().WithData(info).Send(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConnections(c *gin.Context) {
|
func GetSessions(c *gin.Context) {
|
||||||
devices := make(map[string]any)
|
devices := make(map[string]any)
|
||||||
|
|
||||||
for xaddr, conn := range Conns {
|
for xaddr, session := range Sessions {
|
||||||
info, err := conn.GetDeviceInfo()
|
info, err := session.GetDeviceInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.NewResponse().Error(err).Send(c)
|
response.NewResponse().Error(err).Send(c)
|
||||||
return
|
return
|
||||||
@ -56,16 +55,16 @@ func GetConnections(c *gin.Context) {
|
|||||||
response.NewResponse().WithData(devices).Send(c)
|
response.NewResponse().WithData(devices).Send(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConnectionByXaddr(c *gin.Context) {
|
func GetSessionByXaddr(c *gin.Context) {
|
||||||
xaddr := c.Param("xaddr")
|
xaddr := c.Param("xaddr")
|
||||||
|
|
||||||
conn := Conns[xaddr]
|
session := Sessions[xaddr]
|
||||||
if conn == nil {
|
if session == nil {
|
||||||
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
response.NewResponse().Fail("Session not found").WithCode(http.StatusNotFound).Send(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := conn.GetDeviceInfo()
|
info, err := session.GetDeviceInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.NewResponse().Error(err).Send(c)
|
response.NewResponse().Error(err).Send(c)
|
||||||
return
|
return
|
||||||
@ -74,9 +73,9 @@ func GetConnectionByXaddr(c *gin.Context) {
|
|||||||
response.NewResponse().WithData(info).Send(c)
|
response.NewResponse().WithData(info).Send(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteConnection(c *gin.Context) {
|
func DeleteSession(c *gin.Context) {
|
||||||
xaddr := c.Param("xaddr")
|
xaddr := c.Param("xaddr")
|
||||||
delete(Conns, xaddr)
|
delete(Sessions, xaddr)
|
||||||
|
|
||||||
response.NewResponse().Success().Send(c)
|
response.NewResponse().Success().Send(c)
|
||||||
}
|
}
|
||||||
|
@ -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 := Conns[xaddr]
|
conn := 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
|
||||||
@ -32,8 +32,7 @@ func CreateSubscription(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NotifyCallback(c *gin.Context) {
|
func NotifyCallback(c *gin.Context) {
|
||||||
xaddr := c.Param("xaddr")
|
//xaddr := c.Param("xaddr")
|
||||||
log.Printf("NotifyCallback from: %s", xaddr)
|
|
||||||
|
|
||||||
var notify event.Notify
|
var notify event.Notify
|
||||||
envelope := gosoap.NewSOAPEnvelope(¬ify)
|
envelope := gosoap.NewSOAPEnvelope(¬ify)
|
||||||
|
@ -12,12 +12,12 @@ func SetupRoutes(r *gin.Engine) {
|
|||||||
|
|
||||||
onvifGroup := r.Group("/onvif")
|
onvifGroup := r.Group("/onvif")
|
||||||
{
|
{
|
||||||
connectionGroup := onvifGroup.Group("/connections")
|
connectionGroup := onvifGroup.Group("/sessions")
|
||||||
{
|
{
|
||||||
connectionGroup.POST("/", onvif.CreateConnection)
|
connectionGroup.POST("/", onvif.CreateSession)
|
||||||
connectionGroup.GET("/", onvif.GetConnections)
|
connectionGroup.GET("/", onvif.GetSessions)
|
||||||
connectionGroup.GET("/:xaddr", onvif.GetConnectionByXaddr)
|
connectionGroup.GET("/:xaddr", onvif.GetSessionByXaddr)
|
||||||
connectionGroup.DELETE("/:xaddr", onvif.DeleteConnection)
|
connectionGroup.DELETE("/:xaddr", onvif.DeleteSession)
|
||||||
}
|
}
|
||||||
|
|
||||||
subscriptionGroup := onvifGroup.Group("/subscriptions")
|
subscriptionGroup := onvifGroup.Group("/subscriptions")
|
||||||
|
@ -5,16 +5,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Connection struct {
|
type Session struct {
|
||||||
Device *onvif.Device `json:"device"`
|
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, ":") {
|
if !strings.Contains(xaddr, ":") {
|
||||||
xaddr += ":80"
|
xaddr += ":80"
|
||||||
}
|
}
|
||||||
|
|
||||||
dev, err := onvif.NewDevice(onvif.DeviceParams{
|
dev, err := onvif.NewDevice(onvif.DeviceParams{
|
||||||
Xaddr: xaddr,
|
Xaddr: xaddr,
|
||||||
Username: username,
|
Username: username,
|
||||||
@ -25,5 +25,5 @@ func New(xaddr string, username string, password string) (*Connection, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Connection{Device: dev}, nil
|
return &Session{Device: dev}, nil
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package onvif
|
|||||||
|
|
||||||
import "github.com/IOTechSystems/onvif/device"
|
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{})
|
resp, err := c.Device.CallMethod(device.GetDeviceInformation{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"github.com/IOTechSystems/onvif/xsd"
|
"github.com/IOTechSystems/onvif/xsd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Connection) SubscribeEvents(
|
func (c *Session) SubscribeEvents(
|
||||||
consumerAddress event.AttributedURIType,
|
consumerAddress event.AttributedURIType,
|
||||||
terminationTime xsd.String, // PT60S
|
terminationTime xsd.String, // PT60S
|
||||||
) (*event.SubscribeResponse, error) {
|
) (*event.SubscribeResponse, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user