修复视频获取列表及延迟功能,添加设备的信息获取与重启

This commit is contained in:
lijun 2024-05-20 11:02:55 +08:00
parent 6ebdf684b8
commit cde4ba685c
6 changed files with 175 additions and 8 deletions

43
dao/device.go Normal file
View File

@ -0,0 +1,43 @@
package dao
type Device struct {
ID int `gorm:"primaryKey column:device_id"`
AuthID int `gorm:"column:auth_id"`
DeviceName string `gorm:"column:device_name"`
DeviceType string `gorm:"column:device_type"`
DeviceStatus int `gorm:"column:device_status"`
DeviceLocation string `gorm:"column:device_location"`
DeviceIP string `gorm:"column:device_ip"`
DeviceInfo string `gorm:"column:device_info"`
}
func CreateDevice(authID int, deviceName, deviceType string, deviceStatus int, deviceLocation, deviceIP, deviceInfo string) int {
device := Device{AuthID: authID, DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo}
DB.Create(&device)
return device.ID
}
func DeleteDeviceByID(id, user int) int {
DB.Delete(&Device{}, id)
return id
}
func FindDeviceByID(id, auth_id int) Device {
var device Device
DB.Debug().Where("device_id = ? and auth_id = ?", id, auth_id).First(&device)
return device
}
func FindDeviceByAuthID(auth_id int) []Device {
var devices []Device
DB.Debug().Where("auth_id = ?", auth_id).Find(&devices)
return devices
}
func UpdateDeviceByID(id, auth_id int, deviceName, deviceType string, deviceStatus int, deviceLocation, deviceIP, deviceInfo string) {
DB.Model(&Device{}).Where("device_id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo})
}
func UpdateDeviceStatusByID(id, auth_id, status int) {
DB.Model(&Device{}).Where("device_id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceStatus: status})
}

View File

@ -22,9 +22,9 @@ func CreateVideo(videoPath, videoName string, authID, human, isDelete int, creat
return video.ID return video.ID
} }
func DeleteVideoByID(id int) int { func DeleteVideoByID(id, user int) int {
delete_time := time.Now().Format("2006-01-02 15:04:05") delete_time := time.Now().Format("2006-01-02 15:04:05")
DB.Updates(&Video{IsDelete: 1, DeleteTime: delete_time}).Where("id = ?", id) DB.Updates(&Video{IsDelete: 1, DeleteTime: delete_time}).Where("id = ? and auth_id = ?", id, user)
return id return id
} }
@ -45,3 +45,7 @@ func FindVideoListByTime(auth_id int, startTime, endTime string) []Video {
DB.Debug().Where("auth_id = ?", auth_id).Where("isdelete=0").Where("create_time > ? and create_time < ?", startTime, endTime).Find(&videos) DB.Debug().Where("auth_id = ?", auth_id).Where("isdelete=0").Where("create_time > ? and create_time < ?", startTime, endTime).Find(&videos)
return videos return videos
} }
func DelayVideo(id, auth_id, day int) {
DB.Exec("update video set end_time = date_add(end_time, interval ? day) where id = ? and auth_id = ?", day, id, auth_id)
}

View File

@ -1 +1,51 @@
package handler package handler
import (
"github.com/gin-gonic/gin"
"net/http"
"videoplayer/service"
)
func SetUpDeviceGroup(router *gin.Engine) {
videoGroup := router.Group("/device")
videoGroup.POST("/get_device_list", GetDeviceList)
videoGroup.POST("/restart", RestartDevice)
}
func GetDeviceList(c *gin.Context) {
devices := service.GetDeviceList(c.GetInt("id"))
c.JSON(200, gin.H{
"code": 0,
"message": "success",
"devices": devices,
})
}
func RestartDevice(c *gin.Context) {
user_id := c.GetInt("id")
device_id := c.GetInt("device_id")
device := service.GetDevice(device_id, user_id)
if device.ID != 0 {
if device.DeviceIP != "" {
if Restart(device.DeviceIP) {
c.JSON(200, gin.H{"code": 0, "message": "success"})
} else {
c.JSON(400, gin.H{"code": 1, "message": "failed"})
}
}
}
c.JSON(400, gin.H{"code": 1, "message": "failed"})
}
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
}

View File

@ -8,12 +8,15 @@ import (
"videoplayer/service" "videoplayer/service"
) )
// video获取视频列表请求
type gvlReq struct { type gvlReq struct {
StartTime string `json:"begin"` StartTime string `json:"begin"`
EndTime string `json:"end"` EndTime string `json:"end"`
IP string `json:"ip"` IP string `json:"ip"`
Token string `json:"token"` Token string `json:"token"`
} }
// video添加视频记录请求
type videoReq struct { type videoReq struct {
CameraID int `json:"camera_id"` CameraID int `json:"camera_id"`
VideoPath string `json:"video_path"` VideoPath string `json:"video_path"`
@ -27,6 +30,7 @@ type videoReq struct {
FileSize int `json:"file_size"` FileSize int `json:"file_size"`
} }
// video播放视频请求
type videoPReq struct { type videoPReq struct {
ID int `json:"id"` ID int `json:"id"`
Token string `json:"token"` Token string `json:"token"`
@ -34,11 +38,20 @@ type videoPReq struct {
IP string `json:"ip"` IP string `json:"ip"`
} }
// video延迟视频请求
type delayReq struct {
ID int `json:"id"`
AuthId int `json:"userId"`
Day int `json:"day"`
}
func SetUpVideoGroup(router *gin.Engine) { func SetUpVideoGroup(router *gin.Engine) {
videoGroup := router.Group("/video") videoGroup := router.Group("/video")
videoGroup.POST("/get_video_list", GetVideoList) videoGroup.POST("/get_video_list", GetVideoList)
videoGroup.POST("/create", CreateVideo) videoGroup.POST("/create", CreateVideo)
videoGroup.GET("/mp4", GetVideo) videoGroup.GET("/mp4", GetVideo)
videoGroup.POST("/delay", DelayVideo)
videoGroup.POST("/delete", DeleteVideo)
} }
func GetVideo(c *gin.Context) { func GetVideo(c *gin.Context) {
@ -73,18 +86,39 @@ func GetVideo(c *gin.Context) {
func CreateVideo(c *gin.Context) { func CreateVideo(c *gin.Context) {
var video_req videoReq var video_req videoReq
if err := c.ShouldBindJSON(&video_req); err == nil { if err := c.ShouldBindJSON(&video_req); err == nil {
id := service.CreateVideo(video_req.VideoPath, video_req.VideoName, video_req.AuthId, video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.FileSize)
c.JSON(http.StatusOK, gin.H{"id": id, "code": 0, "message": "success"})
} else { } else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
} }
} }
func GetVideoList(c *gin.Context) { func GetVideoList(c *gin.Context) {
var gvl_req gvlReq var gvl_req gvlReq
if err := c.ShouldBindJSON(&gvl_req); err == nil { if err := c.ShouldBindJSON(&gvl_req); err == nil {
videos := service.GetVideoList(1) videos := service.GetVideoList(c.GetInt("id"), gvl_req.StartTime, gvl_req.EndTime)
c.JSON(http.StatusOK, gin.H{"videos": videos, "code": 0, "message": "success"}) c.JSON(http.StatusOK, gin.H{"videos": videos, "code": 0, "message": "success"})
} else { } else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": 1, "message": "failed"}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
} }
} }
func DelayVideo(c *gin.Context) {
var delay_req delayReq
if err := c.ShouldBindJSON(&delay_req); err == nil {
service.DelayVideo(delay_req.ID, delay_req.AuthId, delay_req.Day)
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
}
}
func DeleteVideo(c *gin.Context) {
var video_req videoPReq
if err := c.ShouldBindJSON(&video_req); err == nil {
service.DeleteVideo(video_req.ID, c.GetInt("id"))
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": 1, "message": "failed"})
}
}

11
service/deviceService.go Normal file
View File

@ -0,0 +1,11 @@
package service
import "videoplayer/dao"
func GetDeviceList(auth_id int) []dao.Device {
return dao.FindDeviceByAuthID(auth_id)
}
func GetDevice(id, auth_id int) dao.Device {
return dao.FindDeviceByID(id, auth_id)
}

View File

@ -1,11 +1,36 @@
package service package service
import "videoplayer/dao" import (
"time"
"videoplayer/dao"
)
func GetVideo(id, auth_id int) dao.Video { func GetVideo(id, auth_id int) dao.Video {
return dao.FindVideoByID(id, auth_id) return dao.FindVideoByID(id, auth_id)
} }
func GetVideoList(auth_id int) []dao.Video { func GetVideoList(auth_id int, start, end string) []dao.Video {
return dao.FindVideoListsByAuthID(auth_id) if start == "" {
return dao.FindVideoListsByAuthID(auth_id)
} else {
s, _ := time.Parse("2006-01-02 15:04:05", start)
e, _ := time.Parse("2006-01-02 15:04:05", end)
if s.After(e) {
return []dao.Video{}
}
return dao.FindVideoListByTime(auth_id, start, end)
}
}
func DelayVideo(id, auth_id, day int) {
dao.DelayVideo(id, auth_id, day)
}
func CreateVideo(videoPath, videoName string, authID, human, isDelete int, createTime, endTime string, fileSize int) int {
return dao.CreateVideo(videoPath, videoName, authID, human, isDelete, createTime, endTime, fileSize)
}
func DeleteVideo(id, user int) int {
return dao.DeleteVideoByID(id, user)
} }