device.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package gateway
  2. import (
  3. errors "confrontation-training/err"
  4. "confrontation-training/global"
  5. deviceModel "confrontation-training/models/gateway"
  6. gatewaym "confrontation-training/models/gateway"
  7. "confrontation-training/response"
  8. deviceService "confrontation-training/service/device"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "reflect"
  12. "strings"
  13. )
  14. type DeviceService struct {
  15. deviceService.DeviceService
  16. }
  17. func GetDeviceService() *DeviceService {
  18. return &DeviceService{}
  19. }
  20. // DeviceAdd
  21. // PingExample confrontation-training
  22. // @Summary 新增设备
  23. // @Schemes
  24. // @Description 新增设备
  25. // @Tags 设备管理
  26. // @Param device body string true "type:类型 0脑电1心电;mac:设备MAC地址"
  27. // @Accept json
  28. // @Produce json
  29. // @Success 200 {string} string "ok"
  30. // @Router /v1/device/add [post]
  31. func (d *DeviceService) DeviceAdd(c *gin.Context) {
  32. var param deviceModel.DeviceAddParam
  33. err := c.ShouldBindJSON(&param)
  34. if err != nil {
  35. fmt.Printf("参数格式化异常:%s", err.Error())
  36. global.Log4J.Info("参数格式化异常:" + err.Error())
  37. response.Failed(errors.ParamInvalid, c)
  38. return
  39. }
  40. if _, count := d.FindDeviceByMac(param.Mac); count > 0 {
  41. response.Failed(errors.DeviceAddFailed, c)
  42. return
  43. }
  44. if result := d.CreateDevice(param); result.Error == nil {
  45. service := GetDeviceService()
  46. d, i := service.FindDeviceByType("")
  47. global.DeviceMap = make(map[string]gatewaym.DeviceInfo)
  48. if i > 0 {
  49. for j := 0; j < len(d); j++ {
  50. global.DeviceMap[d[j].Mac] = d[j]
  51. }
  52. }
  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. service := GetDeviceService()
  79. device, i := service.FindDeviceByType("")
  80. global.DeviceMap = make(map[string]gatewaym.DeviceInfo)
  81. if i > 0 {
  82. for j := 0; j < len(device); j++ {
  83. global.DeviceMap[device[j].Mac] = device[j]
  84. }
  85. }
  86. return
  87. }
  88. // DeviceList
  89. // PingExample confrontation-training
  90. // @Summary 设备列表
  91. // @Schemes
  92. // @Description 设备列表
  93. // @Tags 设备管理
  94. // @Accept json
  95. // @Produce json
  96. // @Success 200 {string} string "ok"
  97. // @Router /v1/device/list/:type [get]
  98. func (d *DeviceService) DeviceList(c *gin.Context) {
  99. deviceInfos, _ := d.FindDeviceByType(c.Query("type"))
  100. response.Success(errors.FindSuccess, deviceInfos, c)
  101. }
  102. // DeviceConnected 连接列表
  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/connected [get]
  112. func (d *DeviceService) DeviceConnected(c *gin.Context) {
  113. url := global.Config.Gateway.BaseUrl + global.Config.Gateway.ConnectedUrl
  114. listResult, err := d.DeviceConnectedList(url)
  115. if err != nil {
  116. response.Failed("查询失败", c)
  117. return
  118. }
  119. a := make(map[string]interface{})
  120. a = listResult
  121. b := a["nodes"]
  122. var list []map[string]interface{}
  123. var connectedList []deviceModel.ConnectedDevice
  124. if reflect.ValueOf(b).Kind() == reflect.Slice {
  125. s := reflect.ValueOf(b)
  126. for i := 0; i < s.Len(); i++ {
  127. ele := s.Index(i)
  128. list = append(list, ele.Interface().(map[string]interface{}))
  129. }
  130. for _, m := range list {
  131. mm := make(map[string]interface{})
  132. mm = m
  133. mac := mm["id"]
  134. if device, count := d.FindDeviceByMac(mac.(string)); count > 0 {
  135. var connDevice = deviceModel.ConnectedDevice{
  136. MAC: mac.(string),
  137. DeviceType: device.Type,
  138. Name: device.Name,
  139. }
  140. if connDevice.Name == "" {
  141. if device.Type == "1" {
  142. connDevice.Name = "BW-EGC-01"
  143. } else if device.Type == "0" {
  144. connDevice.Name = "MIND" + strings.ReplaceAll(connDevice.MAC[len(connDevice.MAC)-6:], ":", "")
  145. }
  146. }
  147. connectedList = append(connectedList, connDevice)
  148. }
  149. }
  150. }
  151. response.Success(errors.FindSuccess, connectedList, c)
  152. return
  153. }
  154. // Disconnect
  155. // PingExample confrontation-training
  156. // @Summary 断开连接
  157. // @Schemes
  158. // @Description 断开连接
  159. // @Tags 设备管理
  160. // @Accept json
  161. // @Produce json
  162. // @Success 200 {string} string "ok"
  163. // @Router /v1/device/:mac/disconnect [get]
  164. func (d *DeviceService) Disconnect(c *gin.Context) {
  165. mac := c.Param("mac")
  166. disconnectUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.DisconnectUrl
  167. disconnectUrl = strings.Replace(disconnectUrl, "MAC", mac, -1)
  168. resp := d.DisconnectDevice(disconnectUrl)
  169. if resp.StatusCode != 200 {
  170. fmt.Println("断开连接失败")
  171. global.Log4J.Info("断开连接失败")
  172. response.Failed("断开连接失败", c)
  173. } else {
  174. response.Success("断开连接", mac, c)
  175. }
  176. return
  177. }