完成设备更新、添加、删除等功能
This commit is contained in:
parent
fc810dd289
commit
14a0863bdf
|
|
@ -16,6 +16,7 @@ func Init() {
|
||||||
}
|
}
|
||||||
db.AutoMigrate(&User{}) // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表
|
db.AutoMigrate(&User{}) // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表
|
||||||
db.AutoMigrate(&Video{})
|
db.AutoMigrate(&Video{})
|
||||||
|
db.AutoMigrate(&Device{})
|
||||||
DB = db
|
DB = db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
package dao
|
package dao
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
ID int `gorm:"primaryKey column:device_id"`
|
gorm.Model
|
||||||
AuthID int `gorm:"column:auth_id"`
|
AuthID int `gorm:"column:auth_id"`
|
||||||
DeviceName string `gorm:"column:device_name"`
|
DeviceName string `gorm:"column:device_name"`
|
||||||
DeviceType string `gorm:"column:device_type"`
|
DeviceType string `gorm:"column:device_type"`
|
||||||
|
|
@ -9,22 +14,30 @@ type Device struct {
|
||||||
DeviceLocation string `gorm:"column:device_location"`
|
DeviceLocation string `gorm:"column:device_location"`
|
||||||
DeviceIP string `gorm:"column:device_ip"`
|
DeviceIP string `gorm:"column:device_ip"`
|
||||||
DeviceInfo string `gorm:"column:device_info"`
|
DeviceInfo string `gorm:"column:device_info"`
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDevice(authID int, deviceName, deviceType string, deviceStatus, deviceLocation, deviceIP, deviceInfo string) int {
|
func CreateDevice(authID int, deviceName, deviceType string, deviceStatus, deviceLocation, deviceIP, deviceInfo string) uint {
|
||||||
device := Device{AuthID: authID, DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo}
|
device := Device{AuthID: authID, DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo}
|
||||||
DB.Create(&device)
|
result := DB.Debug().Create(&device)
|
||||||
|
if result.Error != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return device.ID
|
return device.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteDeviceByID(id, user int) int {
|
func DeleteDeviceByID(id, user int) bool {
|
||||||
DB.Delete(&Device{}, id)
|
res := DB.Debug().Model(&Device{}).Where("id = ? and auth_id = ?", id, user).Delete(&Device{})
|
||||||
return id
|
if res.Error != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindDeviceByID(id, auth_id int) Device {
|
func FindDeviceByID(id, auth_id int) Device {
|
||||||
var device Device
|
var device Device
|
||||||
DB.Debug().Where("device_id = ? and auth_id = ?", id, auth_id).First(&device)
|
DB.Debug().Where("id = ? and auth_id = ?", id, auth_id).First(&device)
|
||||||
return device
|
return device
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,14 +48,40 @@ func FindDeviceByAuthID(auth_id int) []Device {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetDeviceStatus(status string, id, auth_id int) bool {
|
func SetDeviceStatus(status string, id, auth_id int) bool {
|
||||||
DB.Model(&Device{}).Where("device_id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceStatus: status})
|
result := DB.Debug().Model(&Device{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceStatus: status})
|
||||||
|
if result.Error != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateDeviceByID(id, auth_id int, deviceName, deviceType string, deviceStatus, deviceLocation, deviceIP, deviceInfo string) {
|
func UpdateDeviceByID(id, auth_id int, deviceName, deviceType, deviceStatus, deviceLocation, deviceIP, deviceInfo string) bool {
|
||||||
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})
|
pd := FindDeviceByID(id, auth_id)
|
||||||
}
|
if pd.ID == 0 {
|
||||||
|
return false
|
||||||
func UpdateDeviceStatusByID(id, auth_id int, status string) {
|
} else {
|
||||||
DB.Model(&Device{}).Where("device_id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceStatus: status})
|
if deviceName == "" {
|
||||||
|
deviceName = pd.DeviceName
|
||||||
|
}
|
||||||
|
if deviceType == "" {
|
||||||
|
deviceType = pd.DeviceType
|
||||||
|
}
|
||||||
|
if deviceStatus == "" {
|
||||||
|
deviceStatus = pd.DeviceStatus
|
||||||
|
}
|
||||||
|
if deviceLocation == "" {
|
||||||
|
deviceLocation = pd.DeviceLocation
|
||||||
|
}
|
||||||
|
if deviceIP == "" {
|
||||||
|
deviceIP = pd.DeviceIP
|
||||||
|
}
|
||||||
|
if deviceInfo == "" {
|
||||||
|
deviceInfo = pd.DeviceInfo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res := DB.Debug().Model(&Device{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(Device{DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo})
|
||||||
|
if res.Error != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
package dao
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"videoplayer/service"
|
"videoplayer/service"
|
||||||
|
|
@ -16,38 +17,99 @@ type DeviceAddReq struct {
|
||||||
DeviceLocation string `json:"device_location"`
|
DeviceLocation string `json:"device_location"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DeviceUpdateReq struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
DeviceName string `json:"device_name"`
|
||||||
|
DeviceIP string `json:"device_ip"`
|
||||||
|
DeviceStatus string `json:"device_status"`
|
||||||
|
AuthID int `json:"auth_id"`
|
||||||
|
DeviceInfo string `json:"device_info"`
|
||||||
|
DeviceType string `json:"device_type"`
|
||||||
|
DeviceLocation string `json:"device_location"`
|
||||||
|
}
|
||||||
|
|
||||||
type DeviceStatus struct {
|
type DeviceStatus struct {
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DeviceDelReq struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
func SetUpDeviceGroup(router *gin.Engine) {
|
func SetUpDeviceGroup(router *gin.Engine) {
|
||||||
deviceGroup := router.Group("/device")
|
deviceGroup := router.Group("/device")
|
||||||
deviceGroup.POST("/get_device_list", GetDeviceList)
|
deviceGroup.POST("/get_device_list", GetDeviceList)
|
||||||
deviceGroup.POST("/restart", RestartDevice)
|
deviceGroup.POST("/restart", RestartDevice)
|
||||||
deviceGroup.POST("/add_device", AddDevice)
|
deviceGroup.POST("/add_device", AddDevice)
|
||||||
deviceGroup.POST("/set_device_status", SetDeviceStatus)
|
deviceGroup.POST("/set_device_status", SetDeviceStatus)
|
||||||
|
deviceGroup.POST("/update_device", UpdateDevice)
|
||||||
|
deviceGroup.POST("/delete_device", DeleteDevice)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteDevice(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
//获取post参数
|
||||||
|
var req DeviceDelReq
|
||||||
|
if err := c.ShouldBindJSON(&req); err == nil {
|
||||||
|
if service.DeleteDevice(req.ID, int(id.(float64))) {
|
||||||
|
c.JSON(200, gin.H{"code": 0, "message": "success"})
|
||||||
|
} else {
|
||||||
|
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateDevice(c *gin.Context) {
|
||||||
|
var req DeviceUpdateReq
|
||||||
|
user_id, _ := c.Get("id")
|
||||||
|
if err := c.ShouldBindJSON(&req); err == nil {
|
||||||
|
fmt.Println("req:", req)
|
||||||
|
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(400, gin.H{
|
||||||
|
"code": 1,
|
||||||
|
"message": "failed",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(400, gin.H{
|
||||||
|
"code": 1,
|
||||||
|
"message": "failed",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
func SetDeviceStatus(c *gin.Context) {
|
func SetDeviceStatus(c *gin.Context) {
|
||||||
var req DeviceStatus
|
var req DeviceStatus
|
||||||
|
id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindJSON(&req); err == nil {
|
if err := c.ShouldBindJSON(&req); err == nil {
|
||||||
if req.IP != "" {
|
if req.IP != "" {
|
||||||
if service.SetDeviceStatus(req.Status, req.ID, c.GetInt("id")) {
|
if service.SetDeviceStatus(req.Status, req.ID, int(id.(float64))) {
|
||||||
c.JSON(200, gin.H{"code": 0, "message": "success"})
|
c.JSON(200, gin.H{"code": 0, "message": "success"})
|
||||||
} else {
|
} else {
|
||||||
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddDevice(c *gin.Context) {
|
func AddDevice(c *gin.Context) {
|
||||||
var req DeviceAddReq
|
var req DeviceAddReq
|
||||||
|
id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindJSON(&req); err == nil {
|
if err := c.ShouldBindJSON(&req); err == nil {
|
||||||
user_id := c.GetInt("id")
|
user_id := int(id.(float64))
|
||||||
|
fmt.Println(req)
|
||||||
device_id := service.AddDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, user_id)
|
device_id := service.AddDevice(req.DeviceName, req.DeviceIP, req.DeviceStatus, req.DeviceInfo, req.DeviceType, req.DeviceLocation, user_id)
|
||||||
if device_id != 0 {
|
if device_id != 0 {
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
|
|
@ -70,7 +132,8 @@ func AddDevice(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDeviceList(c *gin.Context) {
|
func GetDeviceList(c *gin.Context) {
|
||||||
devices := service.GetDeviceList(c.GetInt("id"))
|
id, _ := c.Get("id")
|
||||||
|
devices := service.GetDeviceList(int(id.(float64)))
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"message": "success",
|
"message": "success",
|
||||||
|
|
@ -79,9 +142,9 @@ func GetDeviceList(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RestartDevice(c *gin.Context) {
|
func RestartDevice(c *gin.Context) {
|
||||||
user_id := c.GetInt("id")
|
user_id, _ := c.Get("id")
|
||||||
device_id := c.GetInt("device_id")
|
device_id := c.GetInt("device_id")
|
||||||
device := service.GetDevice(device_id, user_id)
|
device := service.GetDevice(device_id, int(user_id.(float64)))
|
||||||
if device.ID != 0 {
|
if device.ID != 0 {
|
||||||
if device.DeviceIP != "" {
|
if device.DeviceIP != "" {
|
||||||
if Restart(device.DeviceIP) {
|
if Restart(device.DeviceIP) {
|
||||||
|
|
@ -90,8 +153,9 @@ func RestartDevice(c *gin.Context) {
|
||||||
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
c.JSON(400, gin.H{"code": 1, "message": "failed"})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Restart(ip string) bool {
|
func Restart(ip string) bool {
|
||||||
|
|
|
||||||
|
|
@ -34,14 +34,14 @@ func loginHandler(c *gin.Context) {
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
"username": user.Name,
|
"username": user.Name,
|
||||||
"id": user.ID,
|
"id": user.ID,
|
||||||
"exp": time.Now().Add(time.Minute * 10).Unix(), // 令牌过期时间, 1分钟后过期
|
"exp": time.Now().Add(time.Hour * 10).Unix(), // 令牌过期时间, 1分钟后过期
|
||||||
})
|
})
|
||||||
tokenString, err = token.SignedString(signingKey)
|
tokenString, err = token.SignedString(signingKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error(), "code": 1, "message": "error"})
|
c.JSON(400, gin.H{"error": err.Error(), "code": 1, "message": "error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
worker.SetRedisWithExpire(tokenString, tokenString, time.Second*100) // 设置过期时间为10分钟
|
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
||||||
// 返回令牌
|
// 返回令牌
|
||||||
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": 0, "message": "success"})
|
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": 0, "message": "success"})
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -62,7 +62,7 @@ func registerHandler(c *gin.Context) {
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
"username": req_data.User,
|
"username": req_data.User,
|
||||||
"id": id,
|
"id": id,
|
||||||
"exp": time.Now().Add(time.Minute * 10).Unix(), // 令牌过期时间, 1分钟后过期
|
"exp": time.Now().Add(time.Hour * 100).Unix(), // 令牌过期时间, 1分钟后过期
|
||||||
})
|
})
|
||||||
tokenString, err = token.SignedString(signingKey)
|
tokenString, err = token.SignedString(signingKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -95,8 +95,9 @@ func CreateVideo(c *gin.Context) {
|
||||||
|
|
||||||
func GetVideoList(c *gin.Context) {
|
func GetVideoList(c *gin.Context) {
|
||||||
var gvl_req gvlReq
|
var gvl_req gvlReq
|
||||||
|
id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindJSON(&gvl_req); err == nil {
|
if err := c.ShouldBindJSON(&gvl_req); err == nil {
|
||||||
videos := service.GetVideoList(c.GetInt("id"), gvl_req.StartTime, gvl_req.EndTime)
|
videos := service.GetVideoList(int(id.(float64)), 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"})
|
||||||
|
|
@ -105,8 +106,9 @@ func GetVideoList(c *gin.Context) {
|
||||||
|
|
||||||
func DelayVideo(c *gin.Context) {
|
func DelayVideo(c *gin.Context) {
|
||||||
var delay_req delayReq
|
var delay_req delayReq
|
||||||
|
id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindJSON(&delay_req); err == nil {
|
if err := c.ShouldBindJSON(&delay_req); err == nil {
|
||||||
service.DelayVideo(delay_req.ID, delay_req.AuthId, delay_req.Day)
|
service.DelayVideo(delay_req.ID, int(id.(float64)), delay_req.Day)
|
||||||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
|
c.JSON(http.StatusOK, gin.H{"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"})
|
||||||
|
|
@ -115,8 +117,9 @@ func DelayVideo(c *gin.Context) {
|
||||||
|
|
||||||
func DeleteVideo(c *gin.Context) {
|
func DeleteVideo(c *gin.Context) {
|
||||||
var video_req videoPReq
|
var video_req videoPReq
|
||||||
|
id, _ := c.Get("id")
|
||||||
if err := c.ShouldBindJSON(&video_req); err == nil {
|
if err := c.ShouldBindJSON(&video_req); err == nil {
|
||||||
service.DeleteVideo(video_req.ID, c.GetInt("id"))
|
service.DeleteVideo(video_req.ID, int(id.(float64)))
|
||||||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
|
c.JSON(http.StatusOK, gin.H{"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"})
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,18 @@ func GetDevice(id, auth_id int) dao.Device {
|
||||||
return dao.FindDeviceByID(id, auth_id)
|
return dao.FindDeviceByID(id, auth_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddDevice(deviceName, deviceIP, deviceStatus, deviceInfo, deviceType, deviceLocation string, authID int) int {
|
func AddDevice(deviceName, deviceIP, deviceStatus, deviceInfo, deviceType, deviceLocation string, authID int) uint {
|
||||||
return dao.CreateDevice(authID, deviceName, deviceType, deviceStatus, deviceLocation, deviceIP, deviceInfo)
|
return dao.CreateDevice(authID, deviceName, deviceType, deviceStatus, deviceLocation, deviceIP, deviceInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetDeviceStatus(status string, id, auth_id int) bool {
|
func SetDeviceStatus(status string, id, auth_id int) bool {
|
||||||
return dao.SetDeviceStatus(status, id, auth_id)
|
return dao.SetDeviceStatus(status, id, auth_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpdateDevice(deviceName, deviceIP, deviceStatus, deviceInfo, deviceType, deviceLocation string, id, auth_id int) bool {
|
||||||
|
return dao.UpdateDeviceByID(id, auth_id, deviceName, deviceType, deviceStatus, deviceLocation, deviceIP, deviceInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteDevice(id, auth_id int) bool {
|
||||||
|
return dao.DeleteDeviceByID(id, auth_id)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue