refactor(router): 改为结构化 web 项目
This commit is contained in:
41
router/handler/onvif/connection.go
Normal file
41
router/handler/onvif/connection.go
Normal file
@ -0,0 +1,41 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"onvif-agent/service/onvif"
|
||||
)
|
||||
|
||||
var conns = make(map[string]*onvif.Connection)
|
||||
|
||||
func CreateConnection(c *gin.Context) {
|
||||
conn, err := onvif.NewConnection("172.16.19.239", "admin", "admin123")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
conns[conn.Device.GetDeviceParams().Xaddr] = conn
|
||||
|
||||
info, err := conn.GetDeviceInformation()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"connectionParams": conn.Params,
|
||||
"deviceInfo": info,
|
||||
})
|
||||
}
|
||||
|
||||
func GetConnections(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, conns)
|
||||
}
|
||||
|
||||
func GetConnectionByXaddr(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
c.JSON(http.StatusOK, conns[xaddr])
|
||||
}
|
||||
|
||||
func DeleteConnection(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
delete(conns, xaddr)
|
||||
}
|
38
router/handler/onvif/subscription.go
Normal file
38
router/handler/onvif/subscription.go
Normal file
@ -0,0 +1,38 @@
|
||||
package onvif
|
||||
|
||||
import (
|
||||
"github.com/IOTechSystems/onvif/event"
|
||||
"github.com/IOTechSystems/onvif/gosoap"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CreateEventSubscription(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
// FIXME: 把参数变成传入的参数
|
||||
result, err := conns[xaddr].SubscribeEvents("http://172.16.19.230:8080/events/callback", "PT60S")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
func EventNotifyCallback(c *gin.Context) {
|
||||
var notify event.Notify
|
||||
envelope := gosoap.NewSOAPEnvelope(¬ify)
|
||||
if err := c.ShouldBindXML(&envelope); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
for _, msg := range envelope.Body.Content.(*event.Notify).NotificationMessage {
|
||||
log.Println(msg.Topic.TopicKinds, msg.Message.Message)
|
||||
}
|
||||
|
||||
c.String(http.StatusOK, "OK")
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetConnections(c *gin.Context) {
|
||||
// 这里可以添加获取产品的逻辑
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Get Connections"})
|
||||
}
|
||||
|
||||
func CreateConnection(c *gin.Context) {
|
||||
// 这里可以添加创建产品的逻辑
|
||||
c.JSON(http.StatusCreated, gin.H{"message": "Connection Created"})
|
||||
}
|
Reference in New Issue
Block a user