videoplayer/handler/device.go

173 lines
4.1 KiB
Go

package handler
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"videoplayer/service"
)
type DeviceAddReq struct {
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 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 {
IP string `json:"ip"`
Status string `json:"status"`
ID int `json:"id"`
}
type DeviceDelReq struct {
ID int `json:"id"`
}
func SetUpDeviceGroup(router *gin.Engine) {
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)
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(200, gin.H{"code": 1, "message": "failed"})
}
} else {
c.JSON(200, 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(200, gin.H{
"code": 1,
"message": "failed",
})
}
} else {
c.JSON(200, gin.H{
"code": 1,
"message": "failed",
})
}
}
func SetDeviceStatus(c *gin.Context) {
var req DeviceStatus
id, _ := c.Get("id")
if err := c.ShouldBindJSON(&req); err == nil {
if req.IP != "" {
if service.SetDeviceStatus(req.Status, req.ID, int(id.(float64))) {
c.JSON(200, gin.H{"code": 0, "message": "success"})
} else {
c.JSON(200, gin.H{"code": 1, "message": "failed"})
}
}
} else {
c.JSON(200, gin.H{"code": 1, "message": "failed"})
}
}
func AddDevice(c *gin.Context) {
var req DeviceAddReq
id, _ := c.Get("id")
if err := c.ShouldBindJSON(&req); err == nil {
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)
if device_id != 0 {
c.JSON(200, gin.H{
"code": 0,
"message": "success",
"device_id": device_id,
})
} else {
c.JSON(200, gin.H{
"code": 1,
"message": "failed",
})
}
} else {
c.JSON(200, gin.H{
"code": 1,
"message": "failed",
})
}
}
func GetDeviceList(c *gin.Context) {
id, _ := c.Get("id")
devices := service.GetDeviceList(int(id.(float64)))
c.JSON(200, gin.H{
"code": 0,
"message": "success",
"data": devices,
})
}
func RestartDevice(c *gin.Context) {
user_id, _ := c.Get("id")
device_id := c.GetInt("device_id")
device := service.GetDevice(device_id, int(user_id.(float64)))
if device.ID != 0 {
if device.DeviceIP != "" {
if Restart(device.DeviceIP) {
c.JSON(200, gin.H{"code": 0, "message": "success"})
} else {
c.JSON(200, gin.H{"code": 1, "message": "failed"})
}
}
} else {
c.JSON(200, 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
}