gateway.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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/models/gateway"
  10. "confrontation-training/response"
  11. "encoding/hex"
  12. "encoding/json"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. log "github.com/sirupsen/logrus"
  16. "io/ioutil"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. // ScanDevice
  23. // PingExample confrontation-training
  24. // @Summary 扫描设备
  25. // @Schemes
  26. // @Description 扫描设备
  27. // @Tags 设备管理
  28. // @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;filterType:1 表示已录入的设备"
  29. // @Accept json
  30. // @Produce json
  31. // @Success 200 {string} string "ok"
  32. // @Router /v1/device/scan [post]
  33. func ScanDevice(c *gin.Context) {
  34. var param gateway.DeviceScanParam
  35. err := c.ShouldBindJSON(&param)
  36. if err != nil {
  37. fmt.Printf("参数格式化异常:%s", err.Error())
  38. global.Log4J.Info("参数格式化异常:", err.Error())
  39. response.Failed(errors.ParamInvalid, c)
  40. return
  41. }
  42. paramMap := make(map[string]string)
  43. if param.Chip != "" {
  44. paramMap["chip"] = param.Chip
  45. }
  46. if param.FilterName != "" {
  47. //查询设备mac过滤信息
  48. filterMac := ""
  49. deviceInfos, count := GetDeviceService().DeviceService.FindDeviceByType(param.FilterName)
  50. if count > 0 {
  51. for i := range deviceInfos {
  52. filterMac += deviceInfos[i].Mac + ","
  53. }
  54. }
  55. if len(filterMac) > 0 {
  56. if strings.HasSuffix(filterMac, ",") {
  57. filterMac = filterMac[0 : len(filterMac)-1]
  58. }
  59. paramMap["filter_mac"] = filterMac
  60. }
  61. if param.FilterName == "0" {
  62. paramMap["filter_name"] = constant.FilterNameEEG
  63. } else if param.FilterName == "1" {
  64. paramMap["filter_name"] = constant.FilterNameECG
  65. } else {
  66. response.Failed(errors.ParamInvalid+":过滤类型-"+param.FilterName+"无效", c)
  67. return
  68. }
  69. } else {
  70. paramMap["filter_name"] = constant.FilterNameALL
  71. }
  72. if param.FilterRssi != "" {
  73. paramMap["filter_rssi"] = param.FilterRssi
  74. }
  75. if param.FilterMac != "" {
  76. paramMap["filter_mac"] = param.FilterMac
  77. }
  78. paramMap["active"] = "1"
  79. paramMap["event"] = "1"
  80. param.FilterType = "1"
  81. go SseScanDevice(paramMap, param.FilterType)
  82. response.Success("扫描完成", "", c)
  83. return
  84. }
  85. // ConnectDevice
  86. // PingExample confrontation-training
  87. // @Summary 连接设备
  88. // @Schemes
  89. // @Description 连接设备
  90. // @Tags 设备管理
  91. // @Param device body string true "chip:芯片编号,0或1;mac:Mac地址;addrType:地址类型 public/random "
  92. // @Accept json
  93. // @Produce json
  94. // @Success 200 {string} string "ok"
  95. // @Router /v1/device/connection [post]
  96. func ConnectDevice(c *gin.Context) {
  97. var param gateway.DeviceConnParam
  98. err := c.ShouldBindJSON(&param)
  99. if err != nil {
  100. fmt.Printf("参数格式化异常:%s", err.Error())
  101. log.Infof("参数格式化异常:%s", err.Error())
  102. response.Failed(errors.ParamInvalid, c)
  103. return
  104. }
  105. jsonParam, err := json.Marshal(param)
  106. if err != nil {
  107. fmt.Printf("参数转换异常:%s", err.Error())
  108. log.Infof("参数转换异常:%s", err.Error())
  109. response.Failed("参数转换异常:%s"+err.Error(), c)
  110. return
  111. }
  112. url := global.Config.Gateway.BaseUrl + global.Config.Gateway.ConnUrl
  113. url = strings.Replace(url, "MAC", param.Mac, -1)
  114. if param.FilterName == "0" {
  115. url = strings.Replace(url, "chip", "chip=0", -1)
  116. } else if param.FilterName == "1" {
  117. url = strings.Replace(url, "chip", "chip=1", -1)
  118. }
  119. httpResponse := http.PostReqJson(url, jsonParam)
  120. status := httpResponse.StatusCode
  121. body, _ := ioutil.ReadAll(httpResponse.Body)
  122. if status != 200 {
  123. response.Failed(errors.DeviceConnectError+string(body), c)
  124. return
  125. }
  126. if param.FilterName == "1" {
  127. openChannelUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.OpenChannel
  128. openChannelUrl = strings.Replace(openChannelUrl, "MAC", param.Mac, -1)
  129. httpResponse = http.GetReq(openChannelUrl)
  130. if httpResponse.StatusCode != 200 {
  131. fmt.Printf("%s:%s", errors.OpenChannelError, httpResponse.Body)
  132. log.Infof("%s:%s", errors.OpenChannelError, httpResponse.Body)
  133. response.Failed(errors.OpenChannelError, c)
  134. return
  135. }
  136. }
  137. go StopScan(c.Writer, c.Request)
  138. response.Success(errors.DeviceConnectSuccess, param.Mac, c)
  139. return
  140. }
  141. // WriteData
  142. // PingExample confrontation-training
  143. // @Summary 写入数据——发送指令
  144. // @Schemes
  145. // @Description 写入数据——发送指令 ,ECG设备开启测试功能
  146. // @Tags 设备管理
  147. // @Param mac body string true "mac:设备MAC地址 userName:用户姓名 gender:性别 age:年龄 height:身高 weight:体重"
  148. // @Accept json
  149. // @Produce json
  150. // @Success 200 {string} string "ok"
  151. // @Router /v1/device/write/data/ [post]
  152. func WriteData(c *gin.Context) {
  153. var param models.WriteData
  154. err := c.ShouldBindJSON(&param)
  155. if err != nil {
  156. fmt.Printf("参数格式化异常:%s", err.Error())
  157. log.Infof("参数格式化异常:%s", err.Error())
  158. response.Failed(errors.ParamInvalid, c)
  159. return
  160. }
  161. userNameHex := hex.EncodeToString([]byte(param.UserName))
  162. //1 绑定用户指令
  163. //指令头(E841) + user信息前缀(AA)+姓名(姓名字节长度+姓名+姓名不足12字节占位符)+性别+年龄+身高+体重
  164. var stringBuild strings.Builder
  165. stringBuild.WriteString(constant.BindUserInfoCmdPrefix)
  166. length := len(userNameHex) / 2
  167. bytes := common.Int2Bytes(length)
  168. result := common.Bytes2HexStr(bytes)
  169. result = common.Int2Byte(int64(length))
  170. if result == "" {
  171. response.Failed("用户名处理异常", c)
  172. return
  173. }
  174. result = result[len(result)-2:]
  175. stringBuild.WriteString(result)
  176. stringBuild.WriteString(userNameHex)
  177. placeHolder := common.GenerateHexStringCmdPlaceHolder(constant.MaxNameByteLength - length)
  178. stringBuild.WriteString(placeHolder)
  179. hexGender := common.Int2Byte(param.Gender)
  180. stringBuild.WriteString(hexGender[len(hexGender)-2:])
  181. if param.Age == 0 {
  182. param.Age = constant.EcgDefaultAge
  183. }
  184. hexAge := common.Int2Byte(param.Age)
  185. stringBuild.WriteString(hexAge[len(hexAge)-2:])
  186. if param.Height == 0 {
  187. param.Height = constant.EcgDefaultHeight
  188. }
  189. hexHeight := common.Int2Byte(param.Height)
  190. stringBuild.WriteString(hexHeight[len(hexHeight)-2:])
  191. if param.Weight == 0 {
  192. param.Weight = constant.EcgDefaultWeight
  193. }
  194. hexWeight := common.Int2Byte(param.Weight)
  195. stringBuild.WriteString(hexWeight[len(hexWeight)-2:])
  196. bindUserCmdByte := []byte(stringBuild.String())
  197. bindUserCmd := string(bindUserCmdByte)
  198. fmt.Println(bindUserCmd)
  199. log.Infoln(bindUserCmd)
  200. //2.授时指令
  201. //timeCmd := common.GetTimeCmd()
  202. timeCmd := common.GetHexTimeStr()
  203. //timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
  204. setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
  205. fmt.Println(setTimeCmd)
  206. log.Infoln(setTimeCmd)
  207. //3.开始收集指令
  208. startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
  209. fmt.Println(startCollectCmd)
  210. log.Infoln(startCollectCmd)
  211. //发送指令
  212. //绑定用户
  213. bindUserUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  214. bindUserUrl = strings.Replace(bindUserUrl, "MAC", param.Mac, -1)
  215. bindUserUrl = strings.Replace(bindUserUrl, "DATA", bindUserCmd, -1)
  216. fmt.Println("bindUserUrl====" + bindUserUrl)
  217. log.Infoln("bindUserUrl====" + bindUserUrl)
  218. httpResponse := http.GetReq(bindUserUrl)
  219. if httpResponse.StatusCode != 200 {
  220. fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
  221. log.Infof("%s:%s", errors.BindUserFailed, httpResponse.Body)
  222. response.Failed(errors.BindUserFailed, c)
  223. return
  224. }
  225. //授时
  226. setTimeUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  227. setTimeUrl = strings.Replace(setTimeUrl, "MAC", param.Mac, -1)
  228. setTimeUrl = strings.Replace(setTimeUrl, "DATA", setTimeCmd, -1)
  229. httpResponse = http.GetReq(setTimeUrl)
  230. if httpResponse.StatusCode != 200 {
  231. fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
  232. log.Infof("%s:%s", errors.BindUserFailed, httpResponse.Body)
  233. response.Failed(errors.BindUserFailed, c)
  234. return
  235. }
  236. fmt.Println("setTimeUrl=====" + setTimeUrl)
  237. log.Infoln("setTimeUrl=====" + setTimeUrl)
  238. //开始收集
  239. startCollectUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  240. startCollectUrl = strings.Replace(startCollectUrl, "MAC", param.Mac, -1)
  241. startCollectUrl = strings.Replace(startCollectUrl, "DATA", startCollectCmd, -1)
  242. fmt.Println("startCollectUrl=====" + startCollectUrl)
  243. log.Infoln("startCollectUrl=====" + startCollectUrl)
  244. httpResponse = http.GetReq(startCollectUrl)
  245. if httpResponse.StatusCode != 200 {
  246. fmt.Printf("%s:%s", errors.StartCollocateFailed, httpResponse.Body)
  247. log.Infof("%s:%s", errors.StartCollocateFailed, httpResponse.Body)
  248. response.Failed(errors.StartCollocateFailed, c)
  249. return
  250. }
  251. //开始传输指令
  252. startTransUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  253. startTransUrl = strings.Replace(startTransUrl, "MAC", param.Mac, -1)
  254. startTransUrl = strings.Replace(startTransUrl, "DATA", constant.StartTransCmd, -1)
  255. fmt.Println("startTransUrl====" + startTransUrl)
  256. log.Infoln("startTransUrl====" + startTransUrl)
  257. httpResponse = http.GetReq(startTransUrl)
  258. if httpResponse.StatusCode != 200 {
  259. fmt.Printf("%s:%s", errors.StartTransFailed, httpResponse.Body)
  260. log.Infof("%s:%s", errors.StartTransFailed, httpResponse.Body)
  261. response.Failed(errors.StartTransFailed, c)
  262. return
  263. }
  264. response.Success(errors.WriteDataSuccess, nil, c)
  265. return
  266. }
  267. // OpenNotify
  268. // PingExample confrontation-training
  269. // @Summary 开启数据通知
  270. // @Schemes
  271. // @Description 开启数据通知
  272. // @Tags 设备管理
  273. // @Accept json
  274. // @Produce json
  275. // @Success 200 {string} string "ok"
  276. // @Router /v1/device/open/notify/ [get]
  277. func OpenNotify(c *gin.Context) {
  278. SseOpenNotify()
  279. response.Success("开启通知", "", c)
  280. return
  281. }
  282. // StopTrans
  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/stop/trans [get]
  292. func StopTrans(c *gin.Context) {
  293. mac := c.Param("mac")
  294. stopTransUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.WriteDataUrl
  295. stopTransUrl = strings.Replace(stopTransUrl, "MAC", mac, -1)
  296. stopTransUrl = strings.Replace(stopTransUrl, "DATA", constant.StopTransCmd, -1)
  297. response.Success("停止传输", "", c)
  298. return
  299. }
  300. // StopCollect
  301. // PingExample confrontation-training
  302. // @Summary 停止采集
  303. // @Schemes
  304. // @Description 停止采集
  305. // @Tags 设备管理
  306. // @Accept json
  307. // @Produce json
  308. // @Success 200 {string} string "ok"
  309. // @Router /v1/device/:mac/stop/collect [get]
  310. func StopCollect(c *gin.Context) {
  311. mac := c.Param("mac")
  312. stopTransUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.WriteDataUrl
  313. stopTransUrl = strings.Replace(stopTransUrl, "MAC", mac, -1)
  314. stopTransUrl = strings.Replace(stopTransUrl, "DATA", constant.StopCollectCmd, -1)
  315. response.Success("停止采集", "", c)
  316. return
  317. }
  318. // ScanDeviceEmq
  319. // PingExample confrontation-training
  320. // @Summary 扫描设备
  321. // @Schemes
  322. // @Description 扫描设备
  323. // @Tags 设备管理
  324. // @Accept json
  325. // @Produce json
  326. // @Success 200 {string} string "ok"
  327. // @Router /v2/device/scan [get]
  328. func ScanDeviceEmq(c *gin.Context) {
  329. topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
  330. client := global.EmqClient
  331. //停止扫描
  332. fmt.Println("停止扫描")
  333. log.Infoln("停止扫描")
  334. global.Log4J.Info("停止扫描")
  335. fmt.Println(constant.CmdStopScan)
  336. Publish(client, topic, constant.CmdStopScan)
  337. //开启扫描
  338. //Publish(client, "/EE3870DA24C4/connect_packet/connect1_subscribe", "[{\"cmd\":\"AT+SCAN=\",\"s\":\"1\"}]")
  339. //停止扫描
  340. //Publish(client, "/EE3870DA24C4/connect_packet/connect1_subscribe", "[{\"cmd\":\"AT+SCAN=\",\"s\":\"0\"}]")
  341. //设置设备服务UUID
  342. //fmt.Println("设置设备服务UUID")
  343. if global.EmqConfig.FirstOpen == "0" {
  344. SendUUID(client, topic)
  345. }
  346. time.Sleep(time.Millisecond * 100)
  347. Publish(global.EmqClient, topic, constant.CmdStartScan)
  348. response.Success("开启扫描完成", "", c)
  349. return
  350. }
  351. // StopScanDeviceEmq
  352. // PingExample confrontation-training
  353. // @Summary 停止扫描设备
  354. // @Schemes
  355. // @Description 停止扫描设备
  356. // @Tags 设备管理
  357. // @Accept json
  358. // @Produce json
  359. // @Success 200 {string} string "ok"
  360. // @Router /v2/device/stop/scan [get]
  361. func StopScanDeviceEmq(c *gin.Context) {
  362. topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
  363. Publish(global.EmqClient, topic, constant.CmdStopScan)
  364. response.Success("关闭扫描完成", "", c)
  365. return
  366. }
  367. // ConnectDevice2
  368. // PingExample confrontation-training
  369. // @Summary 连接设备
  370. // @Schemes
  371. // @Description 连接设备
  372. // @Tags 设备管理
  373. // @Param device body string true "mac:设备MAC地址 ai:终端BLE设备的地址ID at:终端BLE设备的地址类型 "
  374. // @Accept json
  375. // @Produce json
  376. // @Success 200 {string} string "ok"
  377. // @Router /v2/device/conn [post]
  378. func ConnectDevice2(c *gin.Context) {
  379. var param gateway.ConnectDevice
  380. err := c.ShouldBindJSON(&param)
  381. if err != nil {
  382. fmt.Printf("参数格式化异常:%s", err.Error())
  383. global.Log4J.Info("参数格式化异常:", err.Error())
  384. response.Failed(errors.ParamInvalid, c)
  385. return
  386. }
  387. global.Log4J.Info("连接Mac:" + param.Mac)
  388. info := global.DeviceMap[param.Mac]
  389. for s, deviceInfo := range global.DeviceMap {
  390. global.Log4J.Info("mac"+s, "deviceInfo:", deviceInfo)
  391. }
  392. global.Log4J.Info("设备信息:", info)
  393. if info.Type == "" {
  394. response.Failed("设备未入库,请先添加设备", c)
  395. return
  396. }
  397. topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
  398. mac := strings.ReplaceAll(param.Mac, ":", "")
  399. ConnectDeviceEmq(global.EmqClient, topic, mac, "0", param.Ai, param.At)
  400. response.Success("连接设备完成", "", c)
  401. return
  402. }
  403. // DisConnect
  404. // PingExample confrontation-training
  405. // @Summary 断开连接设备
  406. // @Schemes
  407. // @Description 断开连接设备
  408. // @Tags 设备管理
  409. // @Param device body string true "mac:设备MAC地址 "
  410. // @Accept json
  411. // @Produce json
  412. // @Success 200 {string} string "ok"
  413. // @Router /v2/device/dis/conn [post]
  414. func DisConnect(c *gin.Context) {
  415. var param gateway.ConnectDevice
  416. err := c.ShouldBindJSON(&param)
  417. if err != nil {
  418. fmt.Printf("参数格式化异常:%s", err.Error())
  419. global.Log4J.Info("参数格式化异常:", err.Error())
  420. response.Failed(errors.ParamInvalid, c)
  421. return
  422. }
  423. topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
  424. mac := strings.ReplaceAll(param.Mac, ":", "")
  425. DisConnectDeviceEmq(global.EmqClient, topic, mac)
  426. response.Success("断开设备连接成功", "", c)
  427. return
  428. }
  429. // DisConnectAll
  430. // PingExample confrontation-training
  431. // @Summary 断开所有已连接设备
  432. // @Schemes
  433. // @Description 断开所有已连接设备
  434. // @Tags 设备管理
  435. // @Accept json
  436. // @Produce json
  437. // @Success 200 {string} string "ok"
  438. // @Router /v2/device/dis/connAll [get]
  439. func DisConnectAll(c *gin.Context) {
  440. topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
  441. //断开所有的连接
  442. for s := range global.DeviceMap {
  443. if len(s) == 17 {
  444. s = strings.ReplaceAll(s, ":", "")
  445. DisConnectDeviceEmq(global.EmqClient, topic, s)
  446. }
  447. }
  448. response.Success("断开所有已连接设备成功", "", c)
  449. return
  450. }
  451. // ConnectList
  452. // PingExample confrontation-training
  453. // @Summary 已连接列表 已连接列表中不再做任何处理,在连接或断开连接 成功或失败时系统自动调用
  454. // @Schemes
  455. // @Description 已连接列表
  456. // @Tags 设备管理
  457. // @Accept json
  458. // @Produce json
  459. // @Success 200 {string} string "ok"
  460. // @Router /v2/device/connected/list [get]
  461. func ConnectList(c *gin.Context) {
  462. topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
  463. //ConnectedListEmq(global.EmqClient, topic)
  464. //ConnectedNumber(global.EmqClient, topic)
  465. for i := 0; i < 6; i++ {
  466. ConnectedListEmq(global.EmqClient, topic, strconv.Itoa(i))
  467. time.Sleep(time.Millisecond * 500)
  468. }
  469. response.Success("查询连接列表完成", "", c)
  470. return
  471. }
  472. // WriteDataEmq
  473. // PingExample confrontation-training
  474. // @Summary 写入数据-脑电写入指令
  475. // @Schemes
  476. // @Description 写入数据-脑电写入指令
  477. // @Tags 设备管理
  478. // @Param mac body string true "mac:设备MAC地址 userName:用户姓名 gender:性别 age:年龄 height:身高 weight:体重"
  479. // @Accept json
  480. // @Produce json
  481. // @Success 200 {string} string "ok"
  482. // @Router /v2/device/write/data [post]
  483. func WriteDataEmq(c *gin.Context) {
  484. var param models.WriteData
  485. err := c.ShouldBindJSON(&param)
  486. if err != nil {
  487. fmt.Printf("参数格式化异常:%s", err.Error())
  488. global.Log4J.Info("参数格式化异常:", err.Error())
  489. response.Failed(errors.ParamInvalid, c)
  490. return
  491. }
  492. userNameHex := hex.EncodeToString([]byte(param.UserName))
  493. topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
  494. //写入
  495. //1 绑定用户指令
  496. //指令头(E841) + user信息前缀(AA)+姓名(姓名字节长度+姓名+姓名不足12字节占位符)+性别+年龄+身高+体重
  497. var stringBuild strings.Builder
  498. stringBuild.WriteString(constant.BindUserInfoCmdPrefix)
  499. length := len(userNameHex) / 2
  500. bytes := common.Int2Bytes(length)
  501. result := common.Bytes2HexStr(bytes)
  502. result = common.Int2Byte(int64(length))
  503. if result == "" {
  504. response.Failed("用户名处理异常", c)
  505. return
  506. }
  507. result = result[len(result)-2:]
  508. stringBuild.WriteString(result)
  509. stringBuild.WriteString(userNameHex)
  510. placeHolder := common.GenerateHexStringCmdPlaceHolder(constant.MaxNameByteLength - length)
  511. stringBuild.WriteString(placeHolder)
  512. hexGender := common.Int2Byte(param.Gender)
  513. stringBuild.WriteString(hexGender[len(hexGender)-2:])
  514. if param.Age == 0 {
  515. param.Age = constant.EcgDefaultAge
  516. }
  517. hexAge := common.Int2Byte(param.Age)
  518. stringBuild.WriteString(hexAge[len(hexAge)-2:])
  519. if param.Height == 0 {
  520. param.Height = constant.EcgDefaultHeight
  521. }
  522. hexHeight := common.Int2Byte(param.Height)
  523. stringBuild.WriteString(hexHeight[len(hexHeight)-2:])
  524. if param.Weight == 0 {
  525. param.Weight = constant.EcgDefaultWeight
  526. }
  527. hexWeight := common.Int2Byte(param.Weight)
  528. stringBuild.WriteString(hexWeight[len(hexWeight)-2:])
  529. bindUserCmdByte := []byte(stringBuild.String())
  530. bindUserCmd := string(bindUserCmdByte)
  531. fmt.Println(bindUserCmd)
  532. global.Log4J.Info(bindUserCmd)
  533. //2.授时指令
  534. //timeCmd := common.GetTimeCmd()
  535. timeCmd := common.GetHexTimeStr()
  536. //timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
  537. setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
  538. fmt.Println(setTimeCmd)
  539. global.Log4J.Info(setTimeCmd)
  540. //3.开始收集指令
  541. startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
  542. fmt.Println(startCollectCmd)
  543. global.Log4J.Info(startCollectCmd)
  544. //发送指令
  545. //绑定用户
  546. WNP(global.EmqClient, topic, param.Mac, "FFF1", bindUserCmd)
  547. //授时
  548. WNP(global.EmqClient, topic, param.Mac, "FFF1", setTimeCmd)
  549. //开始收集
  550. WNP(global.EmqClient, topic, param.Mac, "FFF1", startCollectCmd)
  551. //开始传输指令
  552. WNP(global.EmqClient, topic, param.Mac, "FFF1", constant.StartTransCmd)
  553. response.Success(errors.WriteDataSuccess, nil, c)
  554. return
  555. }
  556. // FindGateway
  557. // PingExample confrontation-training
  558. // @Summary 查询可能是蓝牙网关的设备
  559. // @Schemes
  560. // @Description 查询可能是蓝牙网关的设备
  561. // @Tags 设备管理
  562. // @Accept json
  563. // @Produce json
  564. // @Success 200 {string} string "ok"
  565. // @Router /v2/gateway/find [get]
  566. func FindGateway(c *gin.Context) {
  567. marshal, err := json.Marshal(global.GatewayList)
  568. if err != nil {
  569. return
  570. }
  571. response.Success("查询网关设备完成", marshal, c)
  572. return
  573. }
  574. // SetGatewayMac
  575. // PingExample confrontation-training
  576. // @Summary 保存网关MAC
  577. // @Schemes
  578. // @Description 保存网关MAC-脑电写入指令
  579. // @Tags 设备管理
  580. // @Param mac body string true "mac:网关MAC地址 "
  581. // @Accept json
  582. // @Produce json
  583. // @Success 200 {string} string "ok"
  584. // @Router /v2/gateway/set [post]
  585. func SetGatewayMac(c *gin.Context) {
  586. var param gateway.GatewayInfo
  587. c.ShouldBindJSON(&param)
  588. file, err := os.OpenFile("./config/application.yaml", os.O_APPEND, 0666)
  589. if err != nil {
  590. response.Failed("读取配置文件失败", c)
  591. return
  592. }
  593. _, err = file.Write([]byte(param.Mac))
  594. if err != nil {
  595. response.Failed("写入配置文件失败", c)
  596. return
  597. }
  598. response.Success("网关信息保存成功,请重启软件!", "", c)
  599. return
  600. }