gateway.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. openChannelUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.OpenChannel
  104. openChannelUrl = strings.Replace(openChannelUrl, "MAC", param.Mac, -1)
  105. fmt.Println("openChannelUrl===" + openChannelUrl)
  106. httpResponse = http.GetReq(openChannelUrl)
  107. if httpResponse.StatusCode != 200 {
  108. fmt.Printf("%s:%s", errors.OpenChannelError, httpResponse.Body)
  109. response.Failed(errors.OpenChannelError, c)
  110. return
  111. }
  112. response.Success(errors.DeviceConnectSuccess, param.Mac, c)
  113. return
  114. }
  115. // WriteData
  116. // PingExample confrontation-training
  117. // @Summary 写入数据——发送指令
  118. // @Schemes
  119. // @Description 写入数据——发送指令 ,ECG设备开启测试功能
  120. // @Tags 设备管理
  121. // @Param mac body string true "mac:设备MAC地址 userName:用户姓名 gender:性别 age:年龄 height:身高 weight:体重"
  122. // @Accept json
  123. // @Produce json
  124. // @Success 200 {string} string "ok"
  125. // @Router /v1/device/write/data/ [post]
  126. func WriteData(c *gin.Context) {
  127. var param models.WriteData
  128. err := c.ShouldBindJSON(&param)
  129. if err != nil {
  130. fmt.Printf("参数格式化异常:%s", err.Error())
  131. response.Failed(errors.ParamInvalid, c)
  132. return
  133. }
  134. userNameHex := hex.EncodeToString([]byte(param.UserName))
  135. //1 绑定用户指令
  136. //指令头(E841) + user信息前缀(AA)+姓名(姓名字节长度+姓名+姓名不足12字节占位符)+性别+年龄+身高+体重
  137. var stringBuild strings.Builder
  138. stringBuild.WriteString(constant.BindUserInfoCmdPrefix)
  139. length := len(userNameHex) / 2
  140. bytes := common.Int2Bytes(length)
  141. result := common.Bytes2HexStr(bytes)
  142. result = common.Int2Byte(int64(length))
  143. if result == "" {
  144. response.Failed("用户名处理异常", c)
  145. return
  146. }
  147. result = result[len(result)-2:]
  148. stringBuild.WriteString(result)
  149. stringBuild.WriteString(userNameHex)
  150. placeHolder := common.GenerateHexStringCmdPlaceHolder(constant.MaxNameByteLength - length)
  151. stringBuild.WriteString(placeHolder)
  152. hexGender := common.Int2Byte(param.Gender)
  153. stringBuild.WriteString(hexGender[len(hexGender)-2:])
  154. if param.Age == 0 {
  155. param.Age = constant.EcgDefaultAge
  156. }
  157. hexAge := common.Int2Byte(param.Age)
  158. stringBuild.WriteString(hexAge[len(hexAge)-2:])
  159. if param.Height == 0 {
  160. param.Height = constant.EcgDefaultHeight
  161. }
  162. hexHeight := common.Int2Byte(param.Height)
  163. stringBuild.WriteString(hexHeight[len(hexHeight)-2:])
  164. if param.Weight == 0 {
  165. param.Weight = constant.EcgDefaultWeight
  166. }
  167. hexWeight := common.Int2Byte(param.Weight)
  168. stringBuild.WriteString(hexWeight[len(hexWeight)-2:])
  169. bindUserCmdByte := []byte(stringBuild.String())
  170. bindUserCmd := string(bindUserCmdByte)
  171. fmt.Println(bindUserCmd)
  172. //2.授时指令
  173. //timeCmd := common.GetTimeCmd()
  174. timeCmd := common.GetHexTimeStr()
  175. //timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
  176. setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
  177. fmt.Println(setTimeCmd)
  178. //3.开始收集指令
  179. startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
  180. fmt.Println(startCollectCmd)
  181. //发送指令
  182. //绑定用户
  183. bindUserUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  184. bindUserUrl = strings.Replace(bindUserUrl, "MAC", param.Mac, -1)
  185. bindUserUrl = strings.Replace(bindUserUrl, "DATA", bindUserCmd, -1)
  186. fmt.Println("bindUserUrl====" + bindUserUrl)
  187. httpResponse := http.GetReq(bindUserUrl)
  188. if httpResponse.StatusCode != 200 {
  189. fmt.Printf("%s:%s", errors.SetDeviceTimeFailed, httpResponse.Body)
  190. response.Failed(errors.SetDeviceTimeFailed, c)
  191. return
  192. }
  193. //授时
  194. setTimeUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  195. setTimeUrl = strings.Replace(setTimeUrl, "MAC", param.Mac, -1)
  196. setTimeUrl = strings.Replace(setTimeUrl, "DATA", setTimeCmd, -1)
  197. httpResponse = http.GetReq(setTimeUrl)
  198. if httpResponse.StatusCode != 200 {
  199. fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
  200. response.Failed(errors.BindUserFailed, c)
  201. return
  202. }
  203. fmt.Println("setTimeUrl=====" + setTimeUrl)
  204. //开始收集
  205. startCollectUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  206. startCollectUrl = strings.Replace(startCollectUrl, "MAC", param.Mac, -1)
  207. startCollectUrl = strings.Replace(startCollectUrl, "DATA", startCollectCmd, -1)
  208. fmt.Println("startCollectUrl=====" + startCollectUrl)
  209. httpResponse = http.GetReq(startCollectUrl)
  210. if httpResponse.StatusCode != 200 {
  211. fmt.Printf("%s:%s", errors.StartCollocateFailed, httpResponse.Body)
  212. response.Failed(errors.StartCollocateFailed, c)
  213. return
  214. }
  215. //开始传输指令
  216. startTransUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  217. startTransUrl = strings.Replace(startTransUrl, "MAC", param.Mac, -1)
  218. startTransUrl = strings.Replace(startTransUrl, "DATA", constant.StartTransCmd, -1)
  219. fmt.Println("startTransUrl====" + startTransUrl)
  220. httpResponse = http.GetReq(startTransUrl)
  221. if httpResponse.StatusCode != 200 {
  222. fmt.Printf("%s:%s", errors.StartTransFailed, httpResponse.Body)
  223. response.Failed(errors.StartTransFailed, c)
  224. return
  225. }
  226. response.Success(errors.WriteDataSuccess, nil, c)
  227. return
  228. }
  229. // OpenNotify
  230. // PingExample confrontation-training
  231. // @Summary 开启数据通知
  232. // @Schemes
  233. // @Description 开启数据通知
  234. // @Tags 设备管理
  235. // @Accept json
  236. // @Produce json
  237. // @Success 200 {string} string "ok"
  238. // @Router /v1/device/open/notify/ [get]
  239. func OpenNotify(c *gin.Context) {
  240. SseOpenNotify()
  241. response.Success("开启通知", "", c)
  242. return
  243. }
  244. // StopTrans
  245. // PingExample confrontation-training
  246. // @Summary 停止传输
  247. // @Schemes
  248. // @Description 停止传输
  249. // @Tags 设备管理
  250. // @Accept json
  251. // @Produce json
  252. // @Success 200 {string} string "ok"
  253. // @Router /v1/device/:mac/stop/trans [get]
  254. func StopTrans(c *gin.Context) {
  255. mac := c.Param("mac")
  256. stopTransUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.WriteDataUrl
  257. stopTransUrl = strings.Replace(stopTransUrl, "MAC", mac, -1)
  258. stopTransUrl = strings.Replace(stopTransUrl, "DATA", constant.StopTransCmd, -1)
  259. response.Success("停止传输", "", c)
  260. return
  261. }
  262. // StopCollect
  263. // PingExample confrontation-training
  264. // @Summary 停止采集
  265. // @Schemes
  266. // @Description 停止采集
  267. // @Tags 设备管理
  268. // @Accept json
  269. // @Produce json
  270. // @Success 200 {string} string "ok"
  271. // @Router /v1/device/:mac/stop/collect [get]
  272. func StopCollect(c *gin.Context) {
  273. mac := c.Param("mac")
  274. stopTransUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.WriteDataUrl
  275. stopTransUrl = strings.Replace(stopTransUrl, "MAC", mac, -1)
  276. stopTransUrl = strings.Replace(stopTransUrl, "DATA", constant.StopCollectCmd, -1)
  277. response.Success("停止采集", "", c)
  278. return
  279. }
  280. // Disconnect
  281. // PingExample confrontation-training
  282. // @Summary 断开连接
  283. // @Schemes
  284. // @Description 断开连接
  285. // @Tags 设备管理
  286. // @Accept json
  287. // @Produce json
  288. // @Success 200 {string} string "ok"
  289. // @Router /v1/device/:mac/disconnect [delete]
  290. func Disconnect(c *gin.Context) {
  291. mac := c.Param("mac")
  292. disconnectUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.DisconnectUrl
  293. disconnectUrl = strings.Replace(disconnectUrl, "MAC", mac, -1)
  294. response.Success("断开连接", mac, c)
  295. return
  296. }