device.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package gateway
  2. import (
  3. errors "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(errors.ParamInvalid, c)
  33. return
  34. }
  35. if _, count := d.FindDeviceByMac(param.Mac); count > 0 {
  36. response.Failed(errors.DeviceAddFailed, c)
  37. return
  38. }
  39. if result := d.CreateDevice(param); result.Error == nil {
  40. response.Success(errors.DeviceAddSuccess, result.RowsAffected, c)
  41. return
  42. } else {
  43. response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
  44. return
  45. }
  46. }
  47. // DeviceRemove
  48. // PingExample confrontation-training
  49. // @Summary 移除设备
  50. // @Schemes
  51. // @Description 移除设备
  52. // @Tags 设备管理
  53. // @Accept json
  54. // @Produce json
  55. // @Success 200 {string} string "ok"
  56. // @Router /v1/device/:mac/remove [delete]
  57. func (d *DeviceService) DeviceRemove(c *gin.Context) {
  58. mac := c.Param("mac")
  59. count := d.RemoveDevice(mac)
  60. if count == 0 {
  61. response.Failed(errors.DeviceRemoveFailed, c)
  62. } else {
  63. response.Success(errors.DeviceRemoveSuccess, count, c)
  64. }
  65. return
  66. }
  67. // DeviceList
  68. // PingExample confrontation-training
  69. // @Summary 设备列表
  70. // @Schemes
  71. // @Description 设备列表
  72. // @Tags 设备管理
  73. // @Accept json
  74. // @Produce json
  75. // @Success 200 {string} string "ok"
  76. // @Router /v1/device/list/:type [get]
  77. func (d *DeviceService) DeviceList(c *gin.Context) {
  78. deviceInfos, _ := d.FindDeviceByType(c.Param("type"))
  79. response.Success(errors.FindSuccess, deviceInfos, c)
  80. }