gateway.go 10 KB

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