2024-05-18 17:12:24 +08:00
|
|
|
package handler
|
2024-05-20 11:02:55 +08:00
|
|
|
|
|
|
|
|
import (
|
2024-10-28 17:17:23 +08:00
|
|
|
"context"
|
2024-07-12 11:34:24 +08:00
|
|
|
"encoding/json"
|
2024-05-20 16:01:04 +08:00
|
|
|
"fmt"
|
2024-05-20 11:02:55 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-11-06 11:59:53 +08:00
|
|
|
"github.com/google/uuid"
|
2024-07-12 11:34:24 +08:00
|
|
|
"github.com/gorilla/websocket"
|
2024-05-20 11:02:55 +08:00
|
|
|
"net/http"
|
2024-07-12 11:34:24 +08:00
|
|
|
"strconv"
|
|
|
|
|
"time"
|
2024-06-25 09:38:21 +08:00
|
|
|
"videoplayer/proto"
|
2024-05-20 11:02:55 +08:00
|
|
|
"videoplayer/service"
|
2024-07-12 11:34:24 +08:00
|
|
|
"videoplayer/worker"
|
2024-05-20 11:02:55 +08:00
|
|
|
)
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-05-20 16:01:04 +08:00
|
|
|
type DeviceUpdateReq struct {
|
2024-06-11 15:39:20 +08:00
|
|
|
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 16:01:04 +08:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-05-20 16:01:04 +08:00
|
|
|
type DeviceDelReq struct {
|
2024-07-02 15:18:23 +08:00
|
|
|
ID int `json:"id" form:"id" `
|
2024-06-11 15:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DeviceRestartReq struct {
|
2024-07-02 15:18:23 +08:00
|
|
|
ID int `json:"id" form:"id" `
|
|
|
|
|
Option string `json:"option" form:"option" `
|
2024-05-20 16:01:04 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 11:02:55 +08:00
|
|
|
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)
|
2024-05-20 16:01:04 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2024-05-20 16:01:04 +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 {
|
2024-05-20 16:01:04 +08:00
|
|
|
if service.DeleteDevice(req.ID, int(id.(float64))) {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
|
2024-05-20 16:01:04 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.OperationFailed, "message": "failed"})
|
2024-05-20 16:01:04 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "failed"})
|
2024-05-20 16:01:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-05-20 16:01:04 +08:00
|
|
|
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 {
|
2024-05-28 15:15:02 +08:00
|
|
|
c.JSON(200, gin.H{
|
2024-06-25 09:38:21 +08:00
|
|
|
"code": proto.DeviceUpdateFailed,
|
2024-05-20 16:01:04 +08:00
|
|
|
"message": "failed",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-05-28 15:15:02 +08:00
|
|
|
c.JSON(200, gin.H{
|
2024-06-25 09:38:21 +08:00
|
|
|
"code": proto.ParameterError,
|
2024-05-20 16:01:04 +08:00
|
|
|
"message": "failed",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-20 11:16:53 +08:00
|
|
|
func SetDeviceStatus(c *gin.Context) {
|
|
|
|
|
var req DeviceStatus
|
2024-05-20 16:01:04 +08:00
|
|
|
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 != "" {
|
2024-05-20 16:01:04 +08:00
|
|
|
if service.SetDeviceStatus(req.Status, req.ID, int(id.(float64))) {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
|
2024-05-20 11:16:53 +08:00
|
|
|
} else {
|
2024-05-28 15:15:02 +08:00
|
|
|
c.JSON(200, gin.H{"code": 1, "message": "failed"})
|
2024-05-20 11:16:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-20 16:01:04 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
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
|
2024-05-20 16:01:04 +08:00
|
|
|
id, _ := c.Get("id")
|
2024-07-02 15:18:23 +08:00
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
2024-05-20 16:01:04 +08:00
|
|
|
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{
|
2024-06-25 09:38:21 +08:00
|
|
|
"code": proto.SuccessCode,
|
2024-06-12 10:04:17 +08:00
|
|
|
"message": "success",
|
|
|
|
|
"data": device_id,
|
2024-05-20 11:16:53 +08:00
|
|
|
})
|
|
|
|
|
} else {
|
2024-05-28 15:15:02 +08:00
|
|
|
c.JSON(200, gin.H{
|
2024-06-25 09:38:21 +08:00
|
|
|
"code": proto.DeviceAddFailed,
|
2024-05-20 11:16:53 +08:00
|
|
|
"message": "failed",
|
2024-06-12 10:04:17 +08:00
|
|
|
"data": "device add failed",
|
2024-05-20 11:16:53 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-05-28 15:15:02 +08:00
|
|
|
c.JSON(200, gin.H{
|
2024-06-25 09:38:21 +08:00
|
|
|
"code": proto.ParameterError,
|
2024-05-20 11:16:53 +08:00
|
|
|
"message": "failed",
|
2024-06-12 10:04:17 +08:00
|
|
|
"data": err.Error(),
|
2024-05-20 11:16:53 +08:00
|
|
|
})
|
|
|
|
|
}
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetDeviceList(c *gin.Context) {
|
2024-05-20 16:01:04 +08:00
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
devices := service.GetDeviceList(int(id.(float64)))
|
2024-05-20 11:02:55 +08:00
|
|
|
c.JSON(200, gin.H{
|
2024-06-25 09:38:21 +08:00
|
|
|
"code": proto.SuccessCode,
|
2024-05-20 11:02:55 +08:00
|
|
|
"message": "success",
|
2024-06-11 15:27:33 +08:00
|
|
|
"data": devices,
|
2024-05-20 11:02:55 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RestartDevice(c *gin.Context) {
|
2024-05-20 16:01:04 +08:00
|
|
|
user_id, _ := c.Get("id")
|
2024-06-11 15:39:20 +08:00
|
|
|
var req DeviceRestartReq
|
2024-06-11 15:27:33 +08:00
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2024-06-25 09:38:21 +08:00
|
|
|
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
|
2024-06-11 15:39:20 +08:00
|
|
|
if req.Option == "one" {
|
|
|
|
|
device := service.GetDevice(device_id, int(user_id.(float64)))
|
|
|
|
|
if device.ID != 0 {
|
|
|
|
|
if device.DeviceIP != "" {
|
|
|
|
|
if Restart(device.DeviceIP) {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
|
2024-06-11 15:39:20 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": "failed"})
|
2024-06-11 15:39:20 +08:00
|
|
|
}
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|
2024-06-11 15:39:20 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DataNotFound, "message": "failed", "data": string(device_id) + ": device not found"})
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|
2024-06-11 15:39:20 +08:00
|
|
|
} 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) {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DeviceRestartFailed, "message": "failed"})
|
2024-06-11 15:39:20 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-12 10:04:17 +08:00
|
|
|
} else {
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.DataNotFound, "message": "failed", "data": "device not found"})
|
2024-06-11 15:39:20 +08:00
|
|
|
}
|
2024-06-25 09:38:21 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success"})
|
2024-05-20 11:02:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-10-28 17:17:23 +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
|
|
|
|
|
}
|
2024-10-28 17:17:23 +08:00
|
|
|
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")
|
2024-10-28 17:17:23 +08:00
|
|
|
go subscribeAndHandleMessages(ws, device_id_int)
|
|
|
|
|
}
|
2024-07-12 11:34:24 +08:00
|
|
|
|
2024-10-28 17:17:23 +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")
|
2024-11-06 21:41:02 +08:00
|
|
|
// 生成唯一连接 uuid
|
2024-11-06 11:59:53 +08:00
|
|
|
con_id := uuid.New().String()
|
|
|
|
|
online_conn_key := "device_" + strconv.Itoa(device_id) + "_online_conn_ids"
|
2024-11-06 21:41:02 +08:00
|
|
|
// 加入设备在线连接集合
|
|
|
|
|
worker.SetRedisSetAddWithExpire(online_conn_key, con_id, time.Minute*5)
|
2024-10-28 17:17:23 +08:00
|
|
|
defer pubsub.Close()
|
2024-10-28 21:11:44 +08:00
|
|
|
defer ws.Close()
|
2024-10-28 17:17:23 +08:00
|
|
|
ch := pubsub.Channel()
|
2024-07-18 15:48:43 +08:00
|
|
|
var check_cnt int
|
2024-11-06 21:52:57 +08:00
|
|
|
var ticker *time.Ticker
|
2024-11-06 21:41:02 +08:00
|
|
|
for {
|
|
|
|
|
select {
|
2024-11-06 21:52:57 +08:00
|
|
|
case msg, _ := <-ch:
|
2024-11-06 21:41:02 +08:00
|
|
|
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)
|
2024-11-06 21:52:57 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-06 21:41:02 +08:00
|
|
|
|
2024-11-06 21:52:57 +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
|
|
|
}
|