videoplayer/handler/device.go

52 lines
1.1 KiB
Go
Raw Normal View History

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
}