gateway.go 19 KB

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