gateway.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package api
  2. import (
  3. "confrontation-training/common"
  4. "confrontation-training/constant"
  5. errors "confrontation-training/err"
  6. "confrontation-training/global"
  7. "confrontation-training/http"
  8. "confrontation-training/models"
  9. "confrontation-training/response"
  10. "encoding/hex"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. "io/ioutil"
  15. "strings"
  16. )
  17. // ScanDevice
  18. // PingExample confrontation-training
  19. // @Summary 扫描设备
  20. // @Schemes
  21. // @Description 扫描设备
  22. // @Tags 设备管理
  23. // @Param device body string true "chip:芯片编号,1或1;filterName:0 脑电 1 心电;filterRssi:信号强度,小于0的整数,字符串格式传输;filterMac:过滤Mac地址,以","分割,如 61-Dg-89-22-39-3b,80-kD-0E-40-57-8A"
  24. // @Accept json
  25. // @Produce json
  26. // @Success 200 {string} string "ok"
  27. // @Router /v1/device/scan [get]
  28. func ScanDevice(c *gin.Context) {
  29. var param models.DeviceScanParam
  30. err := c.ShouldBindJSON(&param)
  31. if err != nil {
  32. fmt.Printf("参数格式化异常:%s", err.Error())
  33. response.Failed(errors.ParamInvalid, c)
  34. return
  35. }
  36. paramMap := make(map[string]string)
  37. if param.Chip != "" {
  38. paramMap["chip"] = param.Chip
  39. }
  40. if param.FilterName != "" {
  41. if param.FilterName == "0" {
  42. paramMap["filter_name"] = constant.FilterNameEEG
  43. } else if param.FilterName == "1" {
  44. paramMap["filter_name"] = constant.FilterNameECG
  45. } else {
  46. response.Failed(errors.ParamInvalid+":过滤类型-"+param.FilterName+"无效", c)
  47. return
  48. }
  49. } else {
  50. paramMap["filter_name"] = constant.FilterNameALL
  51. }
  52. if param.FilterRssi != "" {
  53. paramMap["filter_rssi"] = param.FilterRssi
  54. }
  55. if param.FilterMac != "" {
  56. paramMap["filter_mac"] = param.FilterMac
  57. }
  58. paramMap["active"] = "1"
  59. paramMap["event"] = "1"
  60. SseScanDevice(paramMap)
  61. //http.GetReq(global.Config.Gateway.BaseUrl + global.Config.Gateway.OpenNotify)
  62. response.Success("扫描完成", "", c)
  63. return
  64. }
  65. // ConnectDevice
  66. // PingExample confrontation-training
  67. // @Summary 连接设备
  68. // @Schemes
  69. // @Description 连接设备
  70. // @Tags 设备管理
  71. // @Param device body string true "chip:芯片编号,0或1;mac:Mac地址;addrType:地址类型 public/random "
  72. // @Accept json
  73. // @Produce json
  74. // @Success 200 {string} string "ok"
  75. // @Router /v1/device/connection [get]
  76. func ConnectDevice(c *gin.Context) {
  77. var param models.DeviceConnParam
  78. err := c.ShouldBindJSON(&param)
  79. if err != nil {
  80. fmt.Printf("参数格式化异常:%s", err.Error())
  81. response.Failed(errors.ParamInvalid, c)
  82. return
  83. }
  84. jsonParam, err := json.Marshal(param)
  85. if err != nil {
  86. fmt.Printf("参数转换异常:%s", err.Error())
  87. response.Failed("参数转换异常:%s"+err.Error(), c)
  88. return
  89. }
  90. url := global.Config.Gateway.BaseUrl + global.Config.Gateway.ConnUrl
  91. url = strings.Replace(url, "MAC", param.Mac, -1)
  92. url = strings.Replace(url, "chip", "chip="+param.Chip, -1)
  93. httpResponse := http.PostReqJson(url, jsonParam)
  94. status := httpResponse.StatusCode
  95. fmt.Println("status", status)
  96. fmt.Println("response:", httpResponse.Header)
  97. body, _ := ioutil.ReadAll(httpResponse.Body)
  98. fmt.Println("response Body:", string(body))
  99. if status != 200 {
  100. response.Failed(errors.DeviceConnectError+string(body), c)
  101. return
  102. }
  103. httpResponse = http.GetReq(global.Config.Gateway.BaseUrl + global.Config.Gateway.OpenChannel)
  104. if httpResponse.StatusCode != 200 {
  105. fmt.Printf("%s:%s", errors.OpenChannelError, httpResponse.Body)
  106. response.Failed(errors.OpenChannelError, c)
  107. return
  108. }
  109. response.Success(errors.DeviceConnectSuccess, param.Mac, c)
  110. return
  111. }
  112. // WriteData
  113. // PingExample confrontation-training
  114. // @Summary 写入数据——发送指令
  115. // @Schemes
  116. // @Description 写入数据——发送指令 ,ECG设备开启测试功能
  117. // @Tags 设备管理
  118. // @Param mac body string true "mac:设备MAC地址 userName:用户姓名 gender:性别 age:年龄 height:身高 weight:体重"
  119. // @Accept json
  120. // @Produce json
  121. // @Success 200 {string} string "ok"
  122. // @Router /v1/device/write/data/ [post]
  123. func WriteData(c *gin.Context) {
  124. var param models.WriteData
  125. err := c.ShouldBindJSON(&param)
  126. if err != nil {
  127. fmt.Printf("参数格式化异常:%s", err.Error())
  128. response.Failed(errors.ParamInvalid, c)
  129. return
  130. }
  131. userNameHex := hex.EncodeToString([]byte(param.UserName))
  132. //1 绑定用户指令
  133. //指令头(E841) + user信息前缀(AA)+姓名(姓名字节长度+姓名+姓名不足12字节占位符)+性别+年龄+身高+体重
  134. var stringBuild strings.Builder
  135. stringBuild.WriteString(constant.BindUserInfoCmdPrefix)
  136. length := len(userNameHex) / 2
  137. bytes := common.Int2Bytes(length)
  138. result := common.Bytes2HexStr(bytes)
  139. result = common.Int2Byte(int64(length))
  140. if result == "" {
  141. response.Failed("用户名处理异常", c)
  142. return
  143. }
  144. result = result[len(result)-2:]
  145. stringBuild.WriteString(result)
  146. stringBuild.WriteString(userNameHex)
  147. placeHolder := common.GenerateHexStringCmdPlaceHolder(constant.MaxNameByteLength - length)
  148. stringBuild.WriteString(placeHolder)
  149. hexGender := common.Int2Byte(param.Gender)
  150. stringBuild.WriteString(hexGender[len(hexGender)-2:])
  151. if param.Age == 0 {
  152. param.Age = constant.EcgDefaultAge
  153. }
  154. hexAge := common.Int2Byte(param.Age)
  155. stringBuild.WriteString(hexAge[len(hexAge)-2:])
  156. if param.Height == 0 {
  157. param.Height = constant.EcgDefaultHeight
  158. }
  159. hexHeight := common.Int2Byte(param.Height)
  160. stringBuild.WriteString(hexHeight[len(hexHeight)-2:])
  161. if param.Weight == 0 {
  162. param.Weight = constant.EcgDefaultWeight
  163. }
  164. hexWeight := common.Int2Byte(param.Weight)
  165. stringBuild.WriteString(hexWeight[len(hexWeight)-2:])
  166. bindUserCmdByte := []byte(stringBuild.String())
  167. bindUserCmd := string(bindUserCmdByte)
  168. fmt.Println(bindUserCmd)
  169. //2.授时指令
  170. //timeCmd := common.GetTimeCmd()
  171. timeCmd := common.GetHexTimeStr()
  172. //timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
  173. setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
  174. fmt.Println(setTimeCmd)
  175. //3.开始收集指令
  176. startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
  177. fmt.Println(startCollectCmd)
  178. //发送指令
  179. //绑定用户
  180. bindUserUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  181. bindUserUrl = strings.Replace(bindUserUrl, "MAC", param.Mac, -1)
  182. bindUserUrl = strings.Replace(bindUserUrl, "DATA", bindUserCmd, -1)
  183. fmt.Println("bindUserUrl====" + bindUserUrl)
  184. httpResponse := http.GetReq(bindUserUrl)
  185. if httpResponse.StatusCode != 200 {
  186. fmt.Printf("%s:%s", errors.SetDeviceTimeFailed, httpResponse.Body)
  187. response.Failed(errors.SetDeviceTimeFailed, c)
  188. return
  189. }
  190. //授时
  191. setTimeUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  192. setTimeUrl = strings.Replace(setTimeUrl, "MAC", param.Mac, -1)
  193. setTimeUrl = strings.Replace(setTimeUrl, "DATA", setTimeCmd, -1)
  194. httpResponse = http.GetReq(setTimeUrl)
  195. if httpResponse.StatusCode != 200 {
  196. fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
  197. response.Failed(errors.BindUserFailed, c)
  198. return
  199. }
  200. fmt.Println("setTimeUrl=====" + setTimeUrl)
  201. //开始收集
  202. startCollectUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  203. startCollectUrl = strings.Replace(startCollectUrl, "MAC", param.Mac, -1)
  204. startCollectUrl = strings.Replace(startCollectUrl, "DATA", startCollectCmd, -1)
  205. fmt.Println("startCollectUrl=====" + startCollectUrl)
  206. httpResponse = http.GetReq(startCollectUrl)
  207. if httpResponse.StatusCode != 200 {
  208. fmt.Printf("%s:%s", errors.StartCollocateFailed, httpResponse.Body)
  209. response.Failed(errors.StartCollocateFailed, c)
  210. return
  211. }
  212. //开始传输指令
  213. startTransUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  214. startTransUrl = strings.Replace(startTransUrl, "MAC", param.Mac, -1)
  215. startTransUrl = strings.Replace(startTransUrl, "DATA", constant.StartTransCmd, -1)
  216. fmt.Println("startTransUrl====" + startTransUrl)
  217. httpResponse = http.GetReq(startTransUrl)
  218. if httpResponse.StatusCode != 200 {
  219. fmt.Printf("%s:%s", errors.StartTransFailed, httpResponse.Body)
  220. response.Failed(errors.StartTransFailed, c)
  221. return
  222. }
  223. response.Success(errors.WriteDataSuccess, nil, c)
  224. return
  225. }
  226. // OpenNotify
  227. // PingExample confrontation-training
  228. // @Summary 开启数据通知
  229. // @Schemes
  230. // @Description 开启数据通知
  231. // @Tags 设备管理
  232. // @Accept json
  233. // @Produce json
  234. // @Success 200 {string} string "ok"
  235. // @Router /v1/device/open/notify/ [get]
  236. func OpenNotify(c *gin.Context) {
  237. SseOpenNotify()
  238. response.Success("开启通知", "", c)
  239. return
  240. }