device.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "reflect"
  11. "strings"
  12. )
  13. type DeviceService struct {
  14. deviceService.DeviceService
  15. }
  16. func GetDeviceService() *DeviceService {
  17. return &DeviceService{}
  18. }
  19. // DeviceAdd
  20. // PingExample confrontation-training
  21. // @Summary 新增设备
  22. // @Schemes
  23. // @Description 新增设备
  24. // @Tags 设备管理
  25. // @Param device body string true "type:类型 0脑电1心电;mac:设备MAC地址"
  26. // @Accept json
  27. // @Produce json
  28. // @Success 200 {string} string "ok"
  29. // @Router /v1/device/add [post]
  30. func (d *DeviceService) DeviceAdd(c *gin.Context) {
  31. var param deviceModel.DeviceAddParam
  32. err := c.ShouldBindJSON(&param)
  33. if err != nil {
  34. fmt.Printf("参数格式化异常:%s", err.Error())
  35. response.Failed(errors.ParamInvalid, c)
  36. return
  37. }
  38. if _, count := d.FindDeviceByMac(param.Mac); count > 0 {
  39. response.Failed(errors.DeviceAddFailed, c)
  40. return
  41. }
  42. if result := d.CreateDevice(param); result.Error == nil {
  43. deviceInfo := deviceModel.DeviceInfo{
  44. Type: param.Type,
  45. Mac: param.Mac,
  46. }
  47. if param.Type == "0" {
  48. deviceInfo.Name = "MIND" + strings.ReplaceAll(param.Mac[len(param.Mac)-5:], ":", "")
  49. } else {
  50. deviceInfo.Name = "BW-ECG-01"
  51. }
  52. global.DeviceMap[param.Mac] = deviceInfo
  53. response.Success(errors.DeviceAddSuccess, result.RowsAffected, c)
  54. return
  55. } else {
  56. response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
  57. return
  58. }
  59. }
  60. // DeviceRemove
  61. // PingExample confrontation-training
  62. // @Summary 移除设备
  63. // @Schemes
  64. // @Description 移除设备
  65. // @Tags 设备管理
  66. // @Accept json
  67. // @Produce json
  68. // @Success 200 {string} string "ok"
  69. // @Router /v1/device/:mac/remove [delete]
  70. func (d *DeviceService) DeviceRemove(c *gin.Context) {
  71. mac := c.Param("mac")
  72. count := d.RemoveDevice(mac)
  73. if count == 0 {
  74. response.Failed(errors.DeviceRemoveFailed, c)
  75. } else {
  76. response.Success(errors.DeviceRemoveSuccess, count, c)
  77. }
  78. delete(global.DeviceMap, mac)
  79. return
  80. }
  81. // DeviceList
  82. // PingExample confrontation-training
  83. // @Summary 设备列表
  84. // @Schemes
  85. // @Description 设备列表
  86. // @Tags 设备管理
  87. // @Accept json
  88. // @Produce json
  89. // @Success 200 {string} string "ok"
  90. // @Router /v1/device/list/:type [get]
  91. func (d *DeviceService) DeviceList(c *gin.Context) {
  92. deviceInfos, _ := d.FindDeviceByType(c.Query("type"))
  93. response.Success(errors.FindSuccess, deviceInfos, c)
  94. }
  95. // DeviceConnected 连接列表
  96. // PingExample confrontation-training
  97. // @Summary 连接列表
  98. // @Schemes
  99. // @Description 连接列表
  100. // @Tags 设备管理
  101. // @Accept json
  102. // @Produce json
  103. // @Success 200 {string} string "ok"
  104. // @Router /v1/device/connected [get]
  105. func (d *DeviceService) DeviceConnected(c *gin.Context) {
  106. url := global.Config.Gateway.BaseUrl + global.Config.Gateway.ConnectedUrl
  107. listResult, err := d.DeviceConnectedList(url)
  108. if err != nil {
  109. response.Failed("查询失败", c)
  110. }
  111. a := make(map[string]interface{})
  112. a = listResult
  113. b := a["nodes"]
  114. var list []map[string]interface{}
  115. var connectedList []deviceModel.ConnectedDevice
  116. if reflect.ValueOf(b).Kind() == reflect.Slice {
  117. s := reflect.ValueOf(b)
  118. for i := 0; i < s.Len(); i++ {
  119. ele := s.Index(i)
  120. list = append(list, ele.Interface().(map[string]interface{}))
  121. }
  122. for _, m := range list {
  123. mm := make(map[string]interface{})
  124. mm = m
  125. mac := mm["id"]
  126. if device, count := d.FindDeviceByMac(mac.(string)); count > 0 {
  127. var connDevice = deviceModel.ConnectedDevice{
  128. MAC: mac.(string),
  129. DeviceType: device.Type,
  130. Name: device.Name,
  131. }
  132. if connDevice.Name == "" {
  133. if device.Type == "1" {
  134. connDevice.Name = "BW-EGC-01"
  135. } else if device.Type == "0" {
  136. connDevice.Name = "MIND" + strings.ReplaceAll(connDevice.MAC[len(connDevice.MAC)-6:], ":", "")
  137. }
  138. }
  139. connectedList = append(connectedList, connDevice)
  140. }
  141. }
  142. }
  143. response.Success(errors.FindSuccess, connectedList, c)
  144. return
  145. }
  146. // Disconnect
  147. // PingExample confrontation-training
  148. // @Summary 断开连接
  149. // @Schemes
  150. // @Description 断开连接
  151. // @Tags 设备管理
  152. // @Accept json
  153. // @Produce json
  154. // @Success 200 {string} string "ok"
  155. // @Router /v1/device/:mac/disconnect [get]
  156. func (d *DeviceService) Disconnect(c *gin.Context) {
  157. mac := c.Param("mac")
  158. disconnectUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.DisconnectUrl
  159. disconnectUrl = strings.Replace(disconnectUrl, "MAC", mac, -1)
  160. resp := d.DisconnectDevice(disconnectUrl)
  161. if resp.StatusCode != 200 {
  162. fmt.Println("断开连接失败")
  163. response.Failed("断开连接失败", c)
  164. } else {
  165. response.Success("断开连接", mac, c)
  166. }
  167. return
  168. }