package handler import ( "fmt" "github.com/gin-gonic/gin" "net/http" "videoplayer/service" ) type DeviceAddReq struct { DeviceName string `json:"device_name" form:"device_name" binding:"required"` DeviceIP string `json:"device_ip" form:"device_ip" binding:"required"` DeviceStatus string `json:"device_status" form:"device_status" binding:"required"` AuthID int `json:"auth_id" form:"auth_id" binding:"required"` DeviceInfo string `json:"device_info" form:"device_info" binding:"required"` DeviceType string `json:"device_type" form:"device_type" binding:"required"` DeviceLocation string `json:"device_location" form:"device_location" binding:"required"` } type DeviceUpdateReq struct { ID int `json:"id" form:"id"` DeviceName string `json:"device_name" form:"device_name" binding:"required"` DeviceIP string `json:"device_ip" form:"device_ip" binding:"required"` DeviceStatus string `json:"device_status" form:"device_status" binding:"required"` AuthID int `json:"auth_id" form:"auth_id" binding:"required"` DeviceInfo string `json:"device_info" form:"device_info" binding:"required"` DeviceType string `json:"device_type" form:"device_type" binding:"required"` DeviceLocation string `json:"device_location" form:"device_location" binding:"required"` } type DeviceStatus struct { IP string `json:"ip" form:"ip" binding:"required"` Status string `json:"status" form:"status" binding:"required"` ID int `json:"id" form:"id" binding:"required"` } type DeviceDelReq struct { ID int `json:"id" form:"id" binding:"required"` } type DeviceRestartReq struct { ID int `json:"id" form:"id" binding:"required"` Option string `json:"option" form:"option" binding:"required"` } 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": 17, "message": "failed"}) } } else { c.JSON(200, gin.H{"code": 9, "message": "failed"}) } } func UpdateDevice(c *gin.Context) { var req DeviceUpdateReq user_id, _ := c.Get("id") if err := c.ShouldBindJSON(&req); err == nil { 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": 16, "message": "failed", }) } } else { c.JSON(200, gin.H{ "code": 9, "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": 9, "message": "failed", "data": err.Error()}) } } 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", "data": device_id, }) } else { c.JSON(200, gin.H{ "code": 15, "message": "failed", "data": "device add failed", }) } } else { c.JSON(200, gin.H{ "code": 9, "message": "failed", "data": err.Error(), }) } } 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") var req DeviceRestartReq if err := c.ShouldBind(&req); err != nil { c.JSON(200, gin.H{"code": 9, "message": "failed", "data": err.Error()}) return } device_id := req.ID if req.Option == "one" { 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": 13, "message": "failed"}) } } } else { c.JSON(200, gin.H{"code": 14, "message": "failed", "data": string(device_id) + ": device not found"}) } } 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) { c.JSON(200, gin.H{"code": 13, "message": "failed"}) return } } } } else { c.JSON(200, gin.H{"code": 14, "message": "failed", "data": "device not found"}) } c.JSON(200, gin.H{"code": 0, "message": "success"}) } } 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 }