diff --git a/dao/device.go b/dao/device.go new file mode 100644 index 0000000..5b55e03 --- /dev/null +++ b/dao/device.go @@ -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}) +} diff --git a/dao/video.go b/dao/video.go index 92f689c..cb5ee3d 100644 --- a/dao/video.go +++ b/dao/video.go @@ -22,9 +22,9 @@ func CreateVideo(videoPath, videoName string, authID, human, isDelete int, creat 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") - 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 } @@ -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) 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) +} diff --git a/handler/device.go b/handler/device.go index abeebd1..d90b170 100644 --- a/handler/device.go +++ b/handler/device.go @@ -1 +1,51 @@ 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 +} diff --git a/handler/video.go b/handler/video.go index 56ab595..32da7da 100644 --- a/handler/video.go +++ b/handler/video.go @@ -8,12 +8,15 @@ import ( "videoplayer/service" ) +// video获取视频列表请求 type gvlReq struct { StartTime string `json:"begin"` EndTime string `json:"end"` IP string `json:"ip"` Token string `json:"token"` } + +// video添加视频记录请求 type videoReq struct { CameraID int `json:"camera_id"` VideoPath string `json:"video_path"` @@ -27,6 +30,7 @@ type videoReq struct { FileSize int `json:"file_size"` } +// video播放视频请求 type videoPReq struct { ID int `json:"id"` Token string `json:"token"` @@ -34,11 +38,20 @@ type videoPReq struct { 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) { videoGroup := router.Group("/video") videoGroup.POST("/get_video_list", GetVideoList) videoGroup.POST("/create", CreateVideo) videoGroup.GET("/mp4", GetVideo) + videoGroup.POST("/delay", DelayVideo) + videoGroup.POST("/delete", DeleteVideo) } func GetVideo(c *gin.Context) { @@ -73,18 +86,39 @@ func GetVideo(c *gin.Context) { func CreateVideo(c *gin.Context) { var video_req videoReq 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 { - + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": 1, "message": "failed"}) } } func GetVideoList(c *gin.Context) { var gvl_req gvlReq 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"}) } else { 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"}) + } +} diff --git a/service/deviceService.go b/service/deviceService.go new file mode 100644 index 0000000..ef4f066 --- /dev/null +++ b/service/deviceService.go @@ -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) +} diff --git a/service/videoService.go b/service/videoService.go index 52a3c0e..d7ff97b 100644 --- a/service/videoService.go +++ b/service/videoService.go @@ -1,11 +1,36 @@ package service -import "videoplayer/dao" +import ( + "time" + "videoplayer/dao" +) func GetVideo(id, auth_id int) dao.Video { return dao.FindVideoByID(id, auth_id) } -func GetVideoList(auth_id int) []dao.Video { - return dao.FindVideoListsByAuthID(auth_id) +func GetVideoList(auth_id int, start, end string) []dao.Video { + 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) }