device.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package gateway
  2. import (
  3. errors "confrontation-training/err"
  4. "confrontation-training/global"
  5. deviceModel "confrontation-training/models/gateway"
  6. "confrontation-training/response"
  7. deviceService "confrontation-training/service/device"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "strings"
  11. )
  12. type DeviceService struct {
  13. deviceService.DeviceService
  14. }
  15. func GetDeviceService() *DeviceService {
  16. return &DeviceService{}
  17. }
  18. // DeviceAdd
  19. // PingExample confrontation-training
  20. // @Summary 新增设备
  21. // @Schemes
  22. // @Description 新增设备
  23. // @Tags 设备管理
  24. // @Param device body string true "type:类型 0脑电1心电;mac:设备MAC地址"
  25. // @Accept json
  26. // @Produce json
  27. // @Success 200 {string} string "ok"
  28. // @Router /v1/device/add [post]
  29. func (d *DeviceService) DeviceAdd(c *gin.Context) {
  30. var param deviceModel.DeviceAddParam
  31. err := c.ShouldBindJSON(&param)
  32. if err != nil {
  33. fmt.Printf("参数格式化异常:%s", err.Error())
  34. response.Failed(errors.ParamInvalid, c)
  35. return
  36. }
  37. if _, count := d.FindDeviceByMac(param.Mac); count > 0 {
  38. response.Failed(errors.DeviceAddFailed, c)
  39. return
  40. }
  41. if result := d.CreateDevice(param); result.Error == nil {
  42. response.Success(errors.DeviceAddSuccess, result.RowsAffected, c)
  43. return
  44. } else {
  45. response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
  46. return
  47. }
  48. }
  49. // DeviceRemove
  50. // PingExample confrontation-training
  51. // @Summary 移除设备
  52. // @Schemes
  53. // @Description 移除设备
  54. // @Tags 设备管理
  55. // @Accept json
  56. // @Produce json
  57. // @Success 200 {string} string "ok"
  58. // @Router /v1/device/:mac/remove [delete]
  59. func (d *DeviceService) DeviceRemove(c *gin.Context) {
  60. mac := c.Param("mac")
  61. count := d.RemoveDevice(mac)
  62. if count == 0 {
  63. response.Failed(errors.DeviceRemoveFailed, c)
  64. } else {
  65. response.Success(errors.DeviceRemoveSuccess, count, c)
  66. }
  67. return
  68. }
  69. // DeviceList
  70. // PingExample confrontation-training
  71. // @Summary 设备列表
  72. // @Schemes
  73. // @Description 设备列表
  74. // @Tags 设备管理
  75. // @Accept json
  76. // @Produce json
  77. // @Success 200 {string} string "ok"
  78. // @Router /v1/device/list/:type [get]
  79. func (d *DeviceService) DeviceList(c *gin.Context) {
  80. deviceInfos, _ := d.FindDeviceByType(c.Param("type"))
  81. response.Success(errors.FindSuccess, deviceInfos, c)
  82. }
  83. // DeviceConnected 连接列表
  84. // PingExample confrontation-training
  85. // @Summary 连接列表
  86. // @Schemes
  87. // @Description 连接列表
  88. // @Tags 设备管理
  89. // @Accept json
  90. // @Produce json
  91. // @Success 200 {string} string "ok"
  92. // @Router /v1/device/connected [get]
  93. func (d *DeviceService) DeviceConnected(c *gin.Context) {
  94. url := global.Config.Gateway.BaseUrl + global.Config.Gateway.ConnectedUrl
  95. list, err := d.DeviceConnectedList(url)
  96. if err != nil {
  97. response.Failed("查询失败", c)
  98. }
  99. response.Success(errors.FindSuccess, list, c)
  100. return
  101. }
  102. // Disconnect
  103. // PingExample confrontation-training
  104. // @Summary 断开连接
  105. // @Schemes
  106. // @Description 断开连接
  107. // @Tags 设备管理
  108. // @Accept json
  109. // @Produce json
  110. // @Success 200 {string} string "ok"
  111. // @Router /v1/device/:mac/disconnect [get]
  112. func (d *DeviceService) Disconnect(c *gin.Context) {
  113. mac := c.Param("mac")
  114. disconnectUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.DisconnectUrl
  115. disconnectUrl = strings.Replace(disconnectUrl, "MAC", mac, -1)
  116. resp := d.DisconnectDevice(disconnectUrl)
  117. if resp.StatusCode != 200 {
  118. fmt.Println("断开连接失败")
  119. response.Failed("断开连接失败", c)
  120. } else {
  121. response.Success("断开连接", mac, c)
  122. }
  123. return
  124. }