|
|
package device
|
|
|
|
|
|
import (
|
|
|
"autogo/controllers"
|
|
|
"autogo/dbsql"
|
|
|
"autogo/models"
|
|
|
"errors"
|
|
|
"net/http"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
)
|
|
|
|
|
|
// @Tags 设备相关 /api/device/v2/
|
|
|
// @Summary 新建设备
|
|
|
// @Description 新建设备,UDID唯一
|
|
|
// @accept application/json
|
|
|
// @Param body body models.DeviceV2 true "设备信息"
|
|
|
// @Success 200 {object} models.Response "返回结果"
|
|
|
// @Router /api/device/v2/create [post]
|
|
|
func CreateDeviceV2(c *gin.Context) {
|
|
|
rsp := controllers.NewResponse()
|
|
|
|
|
|
var dv models.DeviceV2
|
|
|
c.ShouldBind(&dv)
|
|
|
|
|
|
if dv.Udid == "" {
|
|
|
c.JSON(http.StatusOK, rsp.Error("udid不能为空"))
|
|
|
return
|
|
|
}
|
|
|
if dv.Platform == "" {
|
|
|
c.JSON(http.StatusOK, rsp.Error("platform不能为空"))
|
|
|
return
|
|
|
}
|
|
|
if dv.Platform != "adr" && dv.Platform != "ios" {
|
|
|
c.JSON(http.StatusOK, rsp.Error("参数platform异常, 只允许值=adr/ios: "+dv.Platform))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
db, err := dbsql.GetConn(dbsql.DSN_qaoms())
|
|
|
if err != nil {
|
|
|
c.JSON(http.StatusOK, rsp.Error(err.Error()))
|
|
|
return
|
|
|
}
|
|
|
defer dbsql.Close(db)
|
|
|
|
|
|
db.Model(models.DeviceV2{}).Create(&dv)
|
|
|
if db.Error != nil {
|
|
|
c.JSON(http.StatusOK, rsp.Error(db.Error.Error()))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if dv.ID < 1 {
|
|
|
c.JSON(http.StatusOK, rsp.Error("新建失败"))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
rsp.Success()
|
|
|
rsp.Data = dv
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
}
|
|
|
|
|
|
// @Tags 设备相关 /api/device/v2/
|
|
|
// @Summary 更新设备状态
|
|
|
// @Description 更新设备状态,错误的状态会被拒绝
|
|
|
// @accept x-www-form-urlencoded
|
|
|
// @Param udid formData string true "设备udid"
|
|
|
// @Param status formData string true "要更新的设备状态,online-在线空闲,busy-占用中,offline-离线"
|
|
|
// @Param status_details formData string false "状态详细,接收文本"
|
|
|
// @Success 200 {object} models.Response "返回更新的状态"
|
|
|
// @Router /api/device/v2/update_status [post]
|
|
|
func UpdateDeviceStatusV2(c *gin.Context) {
|
|
|
rsp := controllers.NewResponse()
|
|
|
|
|
|
udid := c.PostForm("udid")
|
|
|
status := c.PostForm("status")
|
|
|
status_details := c.PostForm("status_details")
|
|
|
|
|
|
if udid == "" {
|
|
|
c.JSON(http.StatusOK, rsp.Error("参数udid错误:"+udid))
|
|
|
return
|
|
|
}
|
|
|
if c.PostForm("status") != "offline" && c.PostForm("status") != "online" && c.PostForm("status") != "busy" {
|
|
|
c.JSON(http.StatusOK, rsp.Error("参数status错误: "+status))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// db, err := dbsql.GetConn(dbsql.DSN_qaoms())
|
|
|
// if err != nil {
|
|
|
// rsp.Error(err.Error())
|
|
|
// c.JSON(http.StatusOK, rsp)
|
|
|
// return
|
|
|
// }
|
|
|
// defer dbsql.Close(db)
|
|
|
|
|
|
// var dv models.DeviceV2
|
|
|
// db.Model(models.DeviceV2{}).Where("udid = ?", udid).Last(&dv)
|
|
|
// if dv.ID < 1 {
|
|
|
// c.JSON(http.StatusOK, rsp.Error("设备不存在: "+udid))
|
|
|
// return
|
|
|
// }
|
|
|
// dv.Status = status
|
|
|
// dv.StatusDetails = status_details
|
|
|
|
|
|
// db.Save(&dv)
|
|
|
|
|
|
dv, err := UpdateStatus(udid, status, status_details)
|
|
|
if err != nil {
|
|
|
c.JSON(http.StatusOK, rsp.Error(err.Error()))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
rsp.Success()
|
|
|
rsp.Data = dv
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
}
|
|
|
|
|
|
func UpdateStatus(udid, status string, status_details ...string) (models.DeviceV2, error) {
|
|
|
var dv models.DeviceV2
|
|
|
db, err := dbsql.GetConn(dbsql.DSN_qaoms())
|
|
|
if err != nil {
|
|
|
return dv, err
|
|
|
}
|
|
|
defer dbsql.Close(db)
|
|
|
|
|
|
db.Model(models.DeviceV2{}).Where("udid = ?", udid).Last(&dv)
|
|
|
if dv.ID < 1 {
|
|
|
return dv, errors.New("设备不存在-" + udid)
|
|
|
}
|
|
|
dv.Status = status
|
|
|
if len(status_details) == 1 {
|
|
|
dv.StatusDetails = status_details[0]
|
|
|
}
|
|
|
|
|
|
db.Save(&dv)
|
|
|
return dv, nil
|
|
|
}
|
|
|
|
|
|
// @Tags 设备相关 /api/device/v2/
|
|
|
// @Summary 更新设备IP
|
|
|
// @Description 更新设备IP
|
|
|
// @accept x-www-form-urlencoded
|
|
|
// @Param udid formData string true "设备udid"
|
|
|
// @Param ip formData string true "设备ip"
|
|
|
// @Success 200 {object} models.Response "返回更新的状态"
|
|
|
// @Router /api/device/v2/update_ip [post]
|
|
|
func UpdateDeviceIpV2(c *gin.Context) {
|
|
|
rsp := controllers.NewResponse()
|
|
|
|
|
|
udid := c.PostForm("udid")
|
|
|
ip := c.PostForm("ip")
|
|
|
|
|
|
if udid == "" {
|
|
|
c.JSON(http.StatusOK, rsp.Error("参数udid错误:"+udid))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
db, err := dbsql.GetConn(dbsql.DSN_qaoms())
|
|
|
if err != nil {
|
|
|
rsp.Error(err.Error())
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
return
|
|
|
}
|
|
|
defer dbsql.Close(db)
|
|
|
|
|
|
db.Model(models.DeviceV2{}).Where("udid = ?", udid).Update("lan_ip", ip)
|
|
|
|
|
|
rsp.Success()
|
|
|
rsp.Data = ip
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
}
|
|
|
|
|
|
// @Tags 设备相关 /api/device/v2/
|
|
|
// @Summary 查询设备
|
|
|
// @Description 根据udid查询设备
|
|
|
// @accept x-www-form-urlencoded
|
|
|
// @Param udid query string true "设备udid"
|
|
|
// @Success 200 {object} models.Response "返回设备信息"
|
|
|
// @Router /api/device/v2/get_device [get]
|
|
|
func GetDeviceByUdidV2(c *gin.Context) {
|
|
|
rsp := controllers.NewResponse()
|
|
|
|
|
|
udid := c.DefaultQuery("udid", "")
|
|
|
if udid == "" {
|
|
|
c.JSON(http.StatusOK, rsp.Error("udid error"))
|
|
|
return
|
|
|
}
|
|
|
db, err := dbsql.GetConn(dbsql.DSN_qaoms())
|
|
|
if err != nil {
|
|
|
c.JSON(http.StatusOK, rsp.Error(err.Error()))
|
|
|
return
|
|
|
}
|
|
|
defer dbsql.Close(db)
|
|
|
|
|
|
var dv models.DeviceV2
|
|
|
|
|
|
db.Model(models.DeviceV2{}).Where("udid = ?", udid).Last(&dv)
|
|
|
|
|
|
if dv.ID < 1 {
|
|
|
c.JSON(http.StatusOK, rsp.Error("没有找到该设备,udid-"+udid))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
rsp.Data = dv
|
|
|
c.JSON(http.StatusOK, rsp.Success())
|
|
|
}
|