videoplayer/handler/device.go

304 lines
8.5 KiB
Go
Raw Normal View History

package handler
import (
"context"
2024-07-12 11:34:24 +08:00
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
2024-07-12 11:34:24 +08:00
"github.com/gorilla/websocket"
"net/http"
2024-07-12 11:34:24 +08:00
"strconv"
"time"
"videoplayer/proto"
"videoplayer/service"
2024-07-12 11:34:24 +08:00
"videoplayer/worker"
)
2024-05-20 11:16:53 +08:00
type DeviceAddReq struct {
2024-07-02 15:18:23 +08:00
DeviceName string `json:"device_name" form:"device_name"`
DeviceIP string `json:"device_ip" form:"device_ip"`
DeviceStatus string `json:"device_status" form:"device_status"`
AuthID int `json:"auth_id" form:"auth_id"`
DeviceInfo string `json:"device_info" form:"device_info"`
DeviceType string `json:"device_type" form:"device_type"`
DeviceLocation string `json:"device_location" form:"device_location"`
2024-05-20 11:16:53 +08:00
}
type DeviceUpdateReq struct {
ID int `json:"id" form:"id"`
2024-07-02 15:18:23 +08:00
DeviceName string `json:"device_name" form:"device_name" `
DeviceIP string `json:"device_ip" form:"device_ip" `
DeviceStatus string `json:"device_status" form:"device_status" `
AuthID int `json:"auth_id" form:"auth_id" `
DeviceInfo string `json:"device_info" form:"device_info" `
DeviceType string `json:"device_type" form:"device_type" `
DeviceLocation string `json:"device_location" form:"device_location" `
}
2024-05-20 11:16:53 +08:00
type DeviceStatus struct {
2024-07-02 15:18:23 +08:00
IP string `json:"ip" form:"ip" `
Status string `json:"status" form:"status" `
ID int `json:"id" form:"id" `
2024-05-20 11:16:53 +08:00
}
type DeviceDelReq struct {
2024-07-02 15:18:23 +08:00
ID int `json:"id" form:"id" `
}
type DeviceRestartReq struct {
2024-07-02 15:18:23 +08:00
ID int `json:"id" form:"id" `
Option string `json:"option" form:"option" `
}
func SetUpDeviceGroup(router *gin.Engine) {
2024-05-20 11:16:53 +08:00
deviceGroup := router.Group("/device")
deviceGroup.POST("/get_device_list", GetDeviceList)
deviceGroup.POST("/restart", RestartDevice)
deviceGroup.POST("/add_device", AddDevice)
deviceGroup.POST("/set_device_status", SetDeviceStatus)
deviceGroup.POST("/update_device", UpdateDevice)
deviceGroup.POST("/delete_device", DeleteDevice)
2024-07-18 10:14:38 +08:00
deviceGroup.GET("/get_real_time_image", GetRealTimeImage)
2024-05-20 11:16:53 +08:00
}
func DeleteDevice(c *gin.Context) {
id, _ := c.Get("id")
//获取post参数
var req DeviceDelReq
2024-07-02 15:18:23 +08:00
if err := c.ShouldBind(&req); err == nil {
if service.DeleteDevice(req.ID, int(id.(float64))) {
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
} else {
c.JSON(200, gin.H{"code": proto.OperationFailed, "message": "failed"})
}
} else {
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "failed"})
}
}
func UpdateDevice(c *gin.Context) {
var req DeviceUpdateReq
user_id, _ := c.Get("id")
2024-07-02 15:18:23 +08:00
if err := c.ShouldBind(&req); err == nil {
res := service.UpdateDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, req.ID, int(user_id.(float64)))
if res {
c.JSON(200, gin.H{
"code": 0,
"message": "success",
})
} else {
c.JSON(200, gin.H{
"code": proto.DeviceUpdateFailed,
"message": "failed",
})
}
} else {
c.JSON(200, gin.H{
"code": proto.ParameterError,
"message": "failed",
})
}
}
2024-05-20 11:16:53 +08:00
func SetDeviceStatus(c *gin.Context) {
var req DeviceStatus
id, _ := c.Get("id")
2024-07-02 15:18:23 +08:00
if err := c.ShouldBind(&req); err == nil {
2024-05-20 11:16:53 +08:00
if req.IP != "" {
if service.SetDeviceStatus(req.Status, req.ID, int(id.(float64))) {
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
2024-05-20 11:16:53 +08:00
} else {
c.JSON(200, gin.H{"code": 1, "message": "failed"})
2024-05-20 11:16:53 +08:00
}
}
} else {
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "failed", "data": err.Error()})
2024-05-20 11:16:53 +08:00
}
}
func AddDevice(c *gin.Context) {
var req DeviceAddReq
id, _ := c.Get("id")
2024-07-02 15:18:23 +08:00
if err := c.ShouldBind(&req); err == nil {
user_id := int(id.(float64))
fmt.Println(req)
2024-05-20 11:16:53 +08:00
device_id := service.AddDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, user_id)
if device_id != 0 {
c.JSON(200, gin.H{
"code": proto.SuccessCode,
"message": "success",
"data": device_id,
2024-05-20 11:16:53 +08:00
})
} else {
c.JSON(200, gin.H{
"code": proto.DeviceAddFailed,
2024-05-20 11:16:53 +08:00
"message": "failed",
"data": "device add failed",
2024-05-20 11:16:53 +08:00
})
}
} else {
c.JSON(200, gin.H{
"code": proto.ParameterError,
2024-05-20 11:16:53 +08:00
"message": "failed",
"data": err.Error(),
2024-05-20 11:16:53 +08:00
})
}
}
func GetDeviceList(c *gin.Context) {
id, _ := c.Get("id")
devices := service.GetDeviceList(int(id.(float64)))
c.JSON(200, gin.H{
"code": proto.SuccessCode,
"message": "success",
2024-06-11 15:27:33 +08:00
"data": devices,
})
}
func RestartDevice(c *gin.Context) {
user_id, _ := c.Get("id")
var req DeviceRestartReq
2024-06-11 15:27:33 +08:00
if err := c.ShouldBind(&req); err != nil {
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "failed", "data": err.Error()})
2024-06-11 15:27:33 +08:00
return
}
device_id := req.ID
if req.Option == "one" {
device := service.GetDevice(device_id, int(user_id.(float64)))
if device.ID != 0 {
if device.DeviceIP != "" {
if Restart(device.DeviceIP) {
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
} else {
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": "failed"})
}
}
} else {
c.JSON(200, gin.H{"code": proto.DataNotFound, "message": "failed", "data": string(device_id) + ": device not found"})
}
} else if req.Option == "all" {
devices := service.GetDeviceList(int(user_id.(float64)))
if len(devices) > 0 {
for _, device := range devices {
if device.DeviceIP != "" {
if !Restart(device.DeviceIP) {
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": "failed"})
return
}
}
}
} else {
c.JSON(200, gin.H{"code": proto.DataNotFound, "message": "failed", "data": "device not found"})
}
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
}
}
func Restart(ip string) bool {
url := "http://" + ip + "/restart"
resp, err := http.Get(url)
if err != nil {
return false
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
return true
}
return false
}
2024-07-12 11:34:24 +08:00
// 发送实时视频流
2024-07-12 11:34:24 +08:00
func GetRealTimeImage(c *gin.Context) {
id, _ := c.Get("id")
id1 := int(id.(float64))
device_id := c.Query("device_id")
device_id_int, _ := strconv.Atoi(device_id)
device := service.GetDevice(device_id_int, id1)
if device.ID == 0 {
c.JSON(http.StatusOK, gin.H{"code": proto.DataNotFound, "message": "device not found"})
return
}
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
clients[ws] = true
if err != nil {
2024-10-28 20:56:26 +08:00
fmt.Println("connect wss err:", err)
2024-07-12 11:34:24 +08:00
return
}
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5)
2024-10-28 20:56:26 +08:00
fmt.Println("device_id:", device_id_int, " has set is_play to 1")
go subscribeAndHandleMessages(ws, device_id_int)
}
2024-07-12 11:34:24 +08:00
func subscribeAndHandleMessages(ws *websocket.Conn, device_id int) {
ctx := context.Background()
2024-10-28 21:01:23 +08:00
pubsub := worker.RedisClient.Subscribe(ctx, strconv.Itoa(device_id)+"_frames_msgs")
// 生成唯一连接 uuid
con_id := uuid.New().String()
online_conn_key := "device_" + strconv.Itoa(device_id) + "_online_conn_ids"
// 加入设备在线连接集合
worker.SetRedisSetAddWithExpire(online_conn_key, con_id, time.Minute*5)
defer pubsub.Close()
2024-10-28 21:11:44 +08:00
defer ws.Close()
ch := pubsub.Channel()
2024-07-18 15:48:43 +08:00
var check_cnt int
var ticker *time.Ticker
for {
select {
case msg, _ := <-ch:
var res3 []byte
var msgObj proto.Message
if msg.Payload != "" {
msgObj.Type = "img"
msgObj.Msg = msg.Payload
msgObj.From_user_id = -1
res3, _ = json.Marshal(msgObj)
} else {
if check_cnt < 5 {
check_cnt++
time.Sleep(time.Millisecond * 200)
continue
}
check_cnt = 0
msgObj.Type = "check"
msgObj.Msg = "check"
msgObj.From_user_id = -1
res3, _ = json.Marshal(msgObj)
}
// fmt.Println("send message to client length:", len(res3))
err2 := ws.WriteMessage(websocket.TextMessage, res3)
if err2 != nil {
clientsMux.Lock()
clients[ws] = false
clientsMux.Unlock()
fmt.Println("send message to client err:", err2)
worker.SetRedisSetRemove(online_conn_key, con_id)
goto end
}
default:
if ticker == nil {
ticker = time.NewTicker(time.Second)
}
select {
case <-ticker.C:
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
if err != nil {
fmt.Println("Connection check failed:", err)
worker.SetRedisSetRemove(online_conn_key, con_id)
goto end
}
default:
continue
2024-07-12 11:34:24 +08:00
}
}
}
end:
// 查看是否还有其他连接,没有则设置 is_play 为 0
if worker.IsContainKey(online_conn_key) == false {
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "0", time.Minute*5)
fmt.Println("device_id:", device_id, " has set is_play to 0")
}
2024-07-12 11:34:24 +08:00
}