package gateway import ( errors "confrontation-training/err" deviceModel "confrontation-training/models/gateway" "confrontation-training/response" deviceService "confrontation-training/service/device" "fmt" "github.com/gin-gonic/gin" ) type DeviceService struct { deviceService.DeviceService } func GetDeviceService() *DeviceService { return &DeviceService{} } // DeviceAdd // PingExample confrontation-training // @Summary 新增设备 // @Schemes // @Description 新增设备 // @Tags 设备管理 // @Param device body string true "type:类型 0脑电1心电;mac:设备MAC地址" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/device/add [post] func (d *DeviceService) DeviceAdd(c *gin.Context) { var param deviceModel.DeviceAddParam err := c.ShouldBindJSON(¶m) if err != nil { fmt.Printf("参数格式化异常:%s", err.Error()) response.Failed(errors.ParamInvalid, c) return } if _, count := d.FindDeviceByMac(param.Mac); count > 0 { response.Failed(errors.DeviceAddFailed, c) return } if result := d.CreateDevice(param); result.Error == nil { response.Success(errors.DeviceAddSuccess, result.RowsAffected, c) return } else { response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c) return } } // DeviceRemove // PingExample confrontation-training // @Summary 移除设备 // @Schemes // @Description 移除设备 // @Tags 设备管理 // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/device/:mac/remove [post] func (d *DeviceService) DeviceRemove(c *gin.Context) { mac := c.Param("mac") count := d.RemoveDevice(mac) if count == 0 { response.Failed(errors.DeviceRemoveFailed, c) } else { response.Success(errors.DeviceRemoveSuccess, count, c) } return } // DeviceList // PingExample confrontation-training // @Summary 设备列表 // @Schemes // @Description 设备列表 // @Tags 设备管理 // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/device/list/:type [get] func (d *DeviceService) DeviceList(c *gin.Context) { deviceInfos, _ := d.FindDeviceByType(c.Param("type")) response.Success(errors.FindSuccess, deviceInfos, c) }