device.go 4.8 KB

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