gateway.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. "io/ioutil"
  16. "strings"
  17. )
  18. // ScanDevice
  19. // PingExample confrontation-training
  20. // @Summary 扫描设备
  21. // @Schemes
  22. // @Description 扫描设备
  23. // @Tags 设备管理
  24. // @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 表示已录入的设备"
  25. // @Accept json
  26. // @Produce json
  27. // @Success 200 {string} string "ok"
  28. // @Router /v1/device/scan [get]
  29. func ScanDevice(c *gin.Context) {
  30. var param gateway.DeviceScanParam
  31. err := c.ShouldBindJSON(&param)
  32. if err != nil {
  33. fmt.Printf("参数格式化异常:%s", err.Error())
  34. response.Failed(errors.ParamInvalid, c)
  35. return
  36. }
  37. paramMap := make(map[string]string)
  38. if param.Chip != "" {
  39. paramMap["chip"] = param.Chip
  40. }
  41. if param.FilterName != "" {
  42. //查询设备mac过滤信息
  43. filterMac := ""
  44. deviceInfos, count := GetDeviceService().DeviceService.FindDeviceByType(param.FilterName)
  45. if count > 0 {
  46. for i := range deviceInfos {
  47. filterMac += deviceInfos[i].Mac + ","
  48. }
  49. }
  50. if len(filterMac) > 0 {
  51. if strings.HasSuffix(filterMac, ",") {
  52. filterMac = filterMac[0 : len(filterMac)-1]
  53. }
  54. paramMap["filter_mac"] = filterMac
  55. }
  56. if param.FilterName == "0" {
  57. paramMap["filter_name"] = constant.FilterNameEEG
  58. } else if param.FilterName == "1" {
  59. paramMap["filter_name"] = constant.FilterNameECG
  60. } else {
  61. response.Failed(errors.ParamInvalid+":过滤类型-"+param.FilterName+"无效", c)
  62. return
  63. }
  64. } else {
  65. paramMap["filter_name"] = constant.FilterNameALL
  66. }
  67. if param.FilterRssi != "" {
  68. paramMap["filter_rssi"] = param.FilterRssi
  69. }
  70. if param.FilterMac != "" {
  71. paramMap["filter_mac"] = param.FilterMac
  72. }
  73. paramMap["active"] = "1"
  74. paramMap["event"] = "1"
  75. SseScanDevice(paramMap, param.FilterType)
  76. //http.GetReq(global.Config.Gateway.BaseUrl + global.Config.Gateway.OpenNotify)
  77. response.Success("扫描完成", "", c)
  78. return
  79. }
  80. // ConnectDevice
  81. // PingExample confrontation-training
  82. // @Summary 连接设备
  83. // @Schemes
  84. // @Description 连接设备
  85. // @Tags 设备管理
  86. // @Param device body string true "chip:芯片编号,0或1;mac:Mac地址;addrType:地址类型 public/random "
  87. // @Accept json
  88. // @Produce json
  89. // @Success 200 {string} string "ok"
  90. // @Router /v1/device/connection [get]
  91. func ConnectDevice(c *gin.Context) {
  92. var param gateway.DeviceConnParam
  93. err := c.ShouldBindJSON(&param)
  94. if err != nil {
  95. fmt.Printf("参数格式化异常:%s", err.Error())
  96. response.Failed(errors.ParamInvalid, c)
  97. return
  98. }
  99. jsonParam, err := json.Marshal(param)
  100. if err != nil {
  101. fmt.Printf("参数转换异常:%s", err.Error())
  102. response.Failed("参数转换异常:%s"+err.Error(), c)
  103. return
  104. }
  105. url := global.Config.Gateway.BaseUrl + global.Config.Gateway.ConnUrl
  106. url = strings.Replace(url, "MAC", param.Mac, -1)
  107. url = strings.Replace(url, "chip", "chip="+param.Chip, -1)
  108. httpResponse := http.PostReqJson(url, jsonParam)
  109. status := httpResponse.StatusCode
  110. fmt.Println("status", status)
  111. fmt.Println("response:", httpResponse.Header)
  112. body, _ := ioutil.ReadAll(httpResponse.Body)
  113. fmt.Println("response Body:", string(body))
  114. if status != 200 {
  115. response.Failed(errors.DeviceConnectError+string(body), c)
  116. return
  117. }
  118. if param.FilterName == "1" {
  119. openChannelUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.OpenChannel
  120. openChannelUrl = strings.Replace(openChannelUrl, "MAC", param.Mac, -1)
  121. fmt.Println("openChannelUrl===" + openChannelUrl)
  122. httpResponse = http.GetReq(openChannelUrl)
  123. if httpResponse.StatusCode != 200 {
  124. fmt.Printf("%s:%s", errors.OpenChannelError, httpResponse.Body)
  125. response.Failed(errors.OpenChannelError, c)
  126. return
  127. }
  128. }
  129. response.Success(errors.DeviceConnectSuccess, param.Mac, c)
  130. return
  131. }
  132. // WriteData
  133. // PingExample confrontation-training
  134. // @Summary 写入数据——发送指令
  135. // @Schemes
  136. // @Description 写入数据——发送指令 ,ECG设备开启测试功能
  137. // @Tags 设备管理
  138. // @Param mac body string true "mac:设备MAC地址 userName:用户姓名 gender:性别 age:年龄 height:身高 weight:体重"
  139. // @Accept json
  140. // @Produce json
  141. // @Success 200 {string} string "ok"
  142. // @Router /v1/device/write/data/ [post]
  143. func WriteData(c *gin.Context) {
  144. var param models.WriteData
  145. err := c.ShouldBindJSON(&param)
  146. if err != nil {
  147. fmt.Printf("参数格式化异常:%s", err.Error())
  148. response.Failed(errors.ParamInvalid, c)
  149. return
  150. }
  151. userNameHex := hex.EncodeToString([]byte(param.UserName))
  152. //1 绑定用户指令
  153. //指令头(E841) + user信息前缀(AA)+姓名(姓名字节长度+姓名+姓名不足12字节占位符)+性别+年龄+身高+体重
  154. var stringBuild strings.Builder
  155. stringBuild.WriteString(constant.BindUserInfoCmdPrefix)
  156. length := len(userNameHex) / 2
  157. bytes := common.Int2Bytes(length)
  158. result := common.Bytes2HexStr(bytes)
  159. result = common.Int2Byte(int64(length))
  160. if result == "" {
  161. response.Failed("用户名处理异常", c)
  162. return
  163. }
  164. result = result[len(result)-2:]
  165. stringBuild.WriteString(result)
  166. stringBuild.WriteString(userNameHex)
  167. placeHolder := common.GenerateHexStringCmdPlaceHolder(constant.MaxNameByteLength - length)
  168. stringBuild.WriteString(placeHolder)
  169. hexGender := common.Int2Byte(param.Gender)
  170. stringBuild.WriteString(hexGender[len(hexGender)-2:])
  171. if param.Age == 0 {
  172. param.Age = constant.EcgDefaultAge
  173. }
  174. hexAge := common.Int2Byte(param.Age)
  175. stringBuild.WriteString(hexAge[len(hexAge)-2:])
  176. if param.Height == 0 {
  177. param.Height = constant.EcgDefaultHeight
  178. }
  179. hexHeight := common.Int2Byte(param.Height)
  180. stringBuild.WriteString(hexHeight[len(hexHeight)-2:])
  181. if param.Weight == 0 {
  182. param.Weight = constant.EcgDefaultWeight
  183. }
  184. hexWeight := common.Int2Byte(param.Weight)
  185. stringBuild.WriteString(hexWeight[len(hexWeight)-2:])
  186. bindUserCmdByte := []byte(stringBuild.String())
  187. bindUserCmd := string(bindUserCmdByte)
  188. fmt.Println(bindUserCmd)
  189. //2.授时指令
  190. //timeCmd := common.GetTimeCmd()
  191. timeCmd := common.GetHexTimeStr()
  192. //timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
  193. setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
  194. fmt.Println(setTimeCmd)
  195. //3.开始收集指令
  196. startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
  197. fmt.Println(startCollectCmd)
  198. //发送指令
  199. //绑定用户
  200. bindUserUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  201. bindUserUrl = strings.Replace(bindUserUrl, "MAC", param.Mac, -1)
  202. bindUserUrl = strings.Replace(bindUserUrl, "DATA", bindUserCmd, -1)
  203. fmt.Println("bindUserUrl====" + bindUserUrl)
  204. httpResponse := http.GetReq(bindUserUrl)
  205. if httpResponse.StatusCode != 200 {
  206. fmt.Printf("%s:%s", errors.SetDeviceTimeFailed, httpResponse.Body)
  207. response.Failed(errors.SetDeviceTimeFailed, c)
  208. return
  209. }
  210. //授时
  211. setTimeUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  212. setTimeUrl = strings.Replace(setTimeUrl, "MAC", param.Mac, -1)
  213. setTimeUrl = strings.Replace(setTimeUrl, "DATA", setTimeCmd, -1)
  214. httpResponse = http.GetReq(setTimeUrl)
  215. if httpResponse.StatusCode != 200 {
  216. fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
  217. response.Failed(errors.BindUserFailed, c)
  218. return
  219. }
  220. fmt.Println("setTimeUrl=====" + setTimeUrl)
  221. //开始收集
  222. startCollectUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  223. startCollectUrl = strings.Replace(startCollectUrl, "MAC", param.Mac, -1)
  224. startCollectUrl = strings.Replace(startCollectUrl, "DATA", startCollectCmd, -1)
  225. fmt.Println("startCollectUrl=====" + startCollectUrl)
  226. httpResponse = http.GetReq(startCollectUrl)
  227. if httpResponse.StatusCode != 200 {
  228. fmt.Printf("%s:%s", errors.StartCollocateFailed, httpResponse.Body)
  229. response.Failed(errors.StartCollocateFailed, c)
  230. return
  231. }
  232. //开始传输指令
  233. startTransUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
  234. startTransUrl = strings.Replace(startTransUrl, "MAC", param.Mac, -1)
  235. startTransUrl = strings.Replace(startTransUrl, "DATA", constant.StartTransCmd, -1)
  236. fmt.Println("startTransUrl====" + startTransUrl)
  237. httpResponse = http.GetReq(startTransUrl)
  238. if httpResponse.StatusCode != 200 {
  239. fmt.Printf("%s:%s", errors.StartTransFailed, httpResponse.Body)
  240. response.Failed(errors.StartTransFailed, c)
  241. return
  242. }
  243. response.Success(errors.WriteDataSuccess, nil, c)
  244. return
  245. }
  246. // OpenNotify
  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/open/notify/ [get]
  256. func OpenNotify(c *gin.Context) {
  257. SseOpenNotify()
  258. response.Success("开启通知", "", c)
  259. return
  260. }
  261. // StopTrans
  262. // PingExample confrontation-training
  263. // @Summary 停止传输
  264. // @Schemes
  265. // @Description 停止传输
  266. // @Tags 设备管理
  267. // @Accept json
  268. // @Produce json
  269. // @Success 200 {string} string "ok"
  270. // @Router /v1/device/:mac/stop/trans [get]
  271. func StopTrans(c *gin.Context) {
  272. mac := c.Param("mac")
  273. stopTransUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.WriteDataUrl
  274. stopTransUrl = strings.Replace(stopTransUrl, "MAC", mac, -1)
  275. stopTransUrl = strings.Replace(stopTransUrl, "DATA", constant.StopTransCmd, -1)
  276. response.Success("停止传输", "", c)
  277. return
  278. }
  279. // StopCollect
  280. // PingExample confrontation-training
  281. // @Summary 停止采集
  282. // @Schemes
  283. // @Description 停止采集
  284. // @Tags 设备管理
  285. // @Accept json
  286. // @Produce json
  287. // @Success 200 {string} string "ok"
  288. // @Router /v1/device/:mac/stop/collect [get]
  289. func StopCollect(c *gin.Context) {
  290. mac := c.Param("mac")
  291. stopTransUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.WriteDataUrl
  292. stopTransUrl = strings.Replace(stopTransUrl, "MAC", mac, -1)
  293. stopTransUrl = strings.Replace(stopTransUrl, "DATA", constant.StopCollectCmd, -1)
  294. response.Success("停止采集", "", c)
  295. return
  296. }
  297. // Disconnect
  298. // PingExample confrontation-training
  299. // @Summary 断开连接
  300. // @Schemes
  301. // @Description 断开连接
  302. // @Tags 设备管理
  303. // @Accept json
  304. // @Produce json
  305. // @Success 200 {string} string "ok"
  306. // @Router /v1/device/:mac/disconnect [get]
  307. func Disconnect(c *gin.Context) {
  308. mac := c.Param("mac")
  309. disconnectUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.DisconnectUrl
  310. disconnectUrl = strings.Replace(disconnectUrl, "MAC", mac, -1)
  311. response.Success("断开连接", mac, c)
  312. return
  313. }