device.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package gateway
  2. import (
  3. errmsg "confrontation-training/err"
  4. deviceModel "confrontation-training/models/gateway"
  5. "confrontation-training/response"
  6. deviceService "confrontation-training/service/device"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type DeviceService struct {
  11. deviceService.DeviceService
  12. }
  13. func GetDeviceService() *DeviceService {
  14. return &DeviceService{}
  15. }
  16. // DeviceAdd
  17. // PingExample confrontation-training
  18. // @Summary 新增设备
  19. // @Schemes
  20. // @Description 新增设备
  21. // @Tags 设备管理
  22. // @Param device body string true "type:类型 0脑电1心电;mac:设备MAC地址"
  23. // @Accept json
  24. // @Produce json
  25. // @Success 200 {string} string "ok"
  26. // @Router /v1/device/add [post]
  27. func (d *DeviceService) DeviceAdd(c *gin.Context) {
  28. var param deviceModel.DeviceAddParam
  29. err := c.ShouldBindJSON(&param)
  30. if err != nil {
  31. fmt.Printf("参数格式化异常:%s", err.Error())
  32. response.Failed(errmsg.ParamInvalid, c)
  33. return
  34. }
  35. if _, count := d.FindDeviceByMac(param.Mac); count > 0 {
  36. response.Failed(errmsg.DeviceAddFailed, c)
  37. return
  38. }
  39. if result := d.CreateDevice(param); result.Error == nil {
  40. response.Success(errmsg.DeviceAddSuccess, result.RowsAffected, c)
  41. return
  42. } else {
  43. response.Failed(errmsg.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
  44. return
  45. }
  46. }