2024-05-18 17:12:24 +08:00
|
|
|
package handler
|
2024-05-20 11:02:55 +08:00
|
|
|
|
|
|
|
|
import (
|
2024-05-20 16:01:04 +08:00
|
|
|
"fmt"
|
2024-05-20 11:02:55 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"net/http"
|
2024-06-25 09:38:21 +08:00
|
|
|
"videoplayer/proto"
|
2024-05-20 11:02:55 +08:00
|
|
|
"videoplayer/service"
|
|
|
|
|
)
|
|
|
|
|
|
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-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
|
|
|
|
|
}
|