123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- package gateway
- import (
- "confrontation-training/common"
- "confrontation-training/constant"
- errors "confrontation-training/err"
- "confrontation-training/global"
- "confrontation-training/http"
- "confrontation-training/models"
- "confrontation-training/models/gateway"
- "confrontation-training/response"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "strings"
- "time"
- )
- // ScanDevice
- // PingExample confrontation-training
- // @Summary 扫描设备
- // @Schemes
- // @Description 扫描设备
- // @Tags 设备管理
- // @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 表示已录入的设备"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/scan [post]
- func ScanDevice(c *gin.Context) {
- var param gateway.DeviceScanParam
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errors.ParamInvalid, c)
- return
- }
- paramMap := make(map[string]string)
- if param.Chip != "" {
- paramMap["chip"] = param.Chip
- }
- if param.FilterName != "" {
- //查询设备mac过滤信息
- filterMac := ""
- deviceInfos, count := GetDeviceService().DeviceService.FindDeviceByType(param.FilterName)
- if count > 0 {
- for i := range deviceInfos {
- filterMac += deviceInfos[i].Mac + ","
- }
- }
- if len(filterMac) > 0 {
- if strings.HasSuffix(filterMac, ",") {
- filterMac = filterMac[0 : len(filterMac)-1]
- }
- paramMap["filter_mac"] = filterMac
- }
- if param.FilterName == "0" {
- paramMap["filter_name"] = constant.FilterNameEEG
- } else if param.FilterName == "1" {
- paramMap["filter_name"] = constant.FilterNameECG
- } else {
- response.Failed(errors.ParamInvalid+":过滤类型-"+param.FilterName+"无效", c)
- return
- }
- } else {
- paramMap["filter_name"] = constant.FilterNameALL
- }
- if param.FilterRssi != "" {
- paramMap["filter_rssi"] = param.FilterRssi
- }
- if param.FilterMac != "" {
- paramMap["filter_mac"] = param.FilterMac
- }
- paramMap["active"] = "1"
- paramMap["event"] = "1"
- param.FilterType = "1"
- go SseScanDevice(paramMap, param.FilterType)
- response.Success("扫描完成", "", c)
- return
- }
- // ConnectDevice
- // PingExample confrontation-training
- // @Summary 连接设备
- // @Schemes
- // @Description 连接设备
- // @Tags 设备管理
- // @Param device body string true "chip:芯片编号,0或1;mac:Mac地址;addrType:地址类型 public/random "
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/connection [post]
- func ConnectDevice(c *gin.Context) {
- var param gateway.DeviceConnParam
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errors.ParamInvalid, c)
- return
- }
- jsonParam, err := json.Marshal(param)
- if err != nil {
- fmt.Printf("参数转换异常:%s", err.Error())
- response.Failed("参数转换异常:%s"+err.Error(), c)
- return
- }
- url := global.Config.Gateway.BaseUrl + global.Config.Gateway.ConnUrl
- url = strings.Replace(url, "MAC", param.Mac, -1)
- if param.FilterName == "0" {
- url = strings.Replace(url, "chip", "chip=0", -1)
- } else if param.FilterName == "1" {
- url = strings.Replace(url, "chip", "chip=1", -1)
- }
- httpResponse := http.PostReqJson(url, jsonParam)
- status := httpResponse.StatusCode
- body, _ := ioutil.ReadAll(httpResponse.Body)
- if status != 200 {
- response.Failed(errors.DeviceConnectError+string(body), c)
- return
- }
- if param.FilterName == "1" {
- openChannelUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.OpenChannel
- openChannelUrl = strings.Replace(openChannelUrl, "MAC", param.Mac, -1)
- httpResponse = http.GetReq(openChannelUrl)
- if httpResponse.StatusCode != 200 {
- fmt.Printf("%s:%s", errors.OpenChannelError, httpResponse.Body)
- response.Failed(errors.OpenChannelError, c)
- return
- }
- }
- go StopScan(c.Writer, c.Request)
- response.Success(errors.DeviceConnectSuccess, param.Mac, c)
- return
- }
- // WriteData
- // PingExample confrontation-training
- // @Summary 写入数据——发送指令
- // @Schemes
- // @Description 写入数据——发送指令 ,ECG设备开启测试功能
- // @Tags 设备管理
- // @Param mac body string true "mac:设备MAC地址 userName:用户姓名 gender:性别 age:年龄 height:身高 weight:体重"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/write/data/ [post]
- func WriteData(c *gin.Context) {
- var param models.WriteData
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errors.ParamInvalid, c)
- return
- }
- userNameHex := hex.EncodeToString([]byte(param.UserName))
- //1 绑定用户指令
- //指令头(E841) + user信息前缀(AA)+姓名(姓名字节长度+姓名+姓名不足12字节占位符)+性别+年龄+身高+体重
- var stringBuild strings.Builder
- stringBuild.WriteString(constant.BindUserInfoCmdPrefix)
- length := len(userNameHex) / 2
- bytes := common.Int2Bytes(length)
- result := common.Bytes2HexStr(bytes)
- result = common.Int2Byte(int64(length))
- if result == "" {
- response.Failed("用户名处理异常", c)
- return
- }
- result = result[len(result)-2:]
- stringBuild.WriteString(result)
- stringBuild.WriteString(userNameHex)
- placeHolder := common.GenerateHexStringCmdPlaceHolder(constant.MaxNameByteLength - length)
- stringBuild.WriteString(placeHolder)
- hexGender := common.Int2Byte(param.Gender)
- stringBuild.WriteString(hexGender[len(hexGender)-2:])
- if param.Age == 0 {
- param.Age = constant.EcgDefaultAge
- }
- hexAge := common.Int2Byte(param.Age)
- stringBuild.WriteString(hexAge[len(hexAge)-2:])
- if param.Height == 0 {
- param.Height = constant.EcgDefaultHeight
- }
- hexHeight := common.Int2Byte(param.Height)
- stringBuild.WriteString(hexHeight[len(hexHeight)-2:])
- if param.Weight == 0 {
- param.Weight = constant.EcgDefaultWeight
- }
- hexWeight := common.Int2Byte(param.Weight)
- stringBuild.WriteString(hexWeight[len(hexWeight)-2:])
- bindUserCmdByte := []byte(stringBuild.String())
- bindUserCmd := string(bindUserCmdByte)
- fmt.Println(bindUserCmd)
- //2.授时指令
- //timeCmd := common.GetTimeCmd()
- timeCmd := common.GetHexTimeStr()
- //timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
- setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
- fmt.Println(setTimeCmd)
- //3.开始收集指令
- startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
- fmt.Println(startCollectCmd)
- //发送指令
- //绑定用户
- bindUserUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
- bindUserUrl = strings.Replace(bindUserUrl, "MAC", param.Mac, -1)
- bindUserUrl = strings.Replace(bindUserUrl, "DATA", bindUserCmd, -1)
- fmt.Println("bindUserUrl====" + bindUserUrl)
- httpResponse := http.GetReq(bindUserUrl)
- if httpResponse.StatusCode != 200 {
- fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
- response.Failed(errors.BindUserFailed, c)
- return
- }
- //授时
- setTimeUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
- setTimeUrl = strings.Replace(setTimeUrl, "MAC", param.Mac, -1)
- setTimeUrl = strings.Replace(setTimeUrl, "DATA", setTimeCmd, -1)
- httpResponse = http.GetReq(setTimeUrl)
- if httpResponse.StatusCode != 200 {
- fmt.Printf("%s:%s", errors.BindUserFailed, httpResponse.Body)
- response.Failed(errors.BindUserFailed, c)
- return
- }
- fmt.Println("setTimeUrl=====" + setTimeUrl)
- //开始收集
- startCollectUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
- startCollectUrl = strings.Replace(startCollectUrl, "MAC", param.Mac, -1)
- startCollectUrl = strings.Replace(startCollectUrl, "DATA", startCollectCmd, -1)
- fmt.Println("startCollectUrl=====" + startCollectUrl)
- httpResponse = http.GetReq(startCollectUrl)
- if httpResponse.StatusCode != 200 {
- fmt.Printf("%s:%s", errors.StartCollocateFailed, httpResponse.Body)
- response.Failed(errors.StartCollocateFailed, c)
- return
- }
- //开始传输指令
- startTransUrl := fmt.Sprintf("%s%s", global.Config.Gateway.BaseUrl, global.Config.Gateway.WriteDataUrl)
- startTransUrl = strings.Replace(startTransUrl, "MAC", param.Mac, -1)
- startTransUrl = strings.Replace(startTransUrl, "DATA", constant.StartTransCmd, -1)
- fmt.Println("startTransUrl====" + startTransUrl)
- httpResponse = http.GetReq(startTransUrl)
- if httpResponse.StatusCode != 200 {
- fmt.Printf("%s:%s", errors.StartTransFailed, httpResponse.Body)
- response.Failed(errors.StartTransFailed, c)
- return
- }
- response.Success(errors.WriteDataSuccess, nil, c)
- return
- }
- // OpenNotify
- // PingExample confrontation-training
- // @Summary 开启数据通知
- // @Schemes
- // @Description 开启数据通知
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/open/notify/ [get]
- func OpenNotify(c *gin.Context) {
- SseOpenNotify()
- response.Success("开启通知", "", c)
- return
- }
- // StopTrans
- // PingExample confrontation-training
- // @Summary 停止传输
- // @Schemes
- // @Description 停止传输
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/:mac/stop/trans [get]
- func StopTrans(c *gin.Context) {
- mac := c.Param("mac")
- stopTransUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.WriteDataUrl
- stopTransUrl = strings.Replace(stopTransUrl, "MAC", mac, -1)
- stopTransUrl = strings.Replace(stopTransUrl, "DATA", constant.StopTransCmd, -1)
- response.Success("停止传输", "", c)
- return
- }
- // StopCollect
- // PingExample confrontation-training
- // @Summary 停止采集
- // @Schemes
- // @Description 停止采集
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/:mac/stop/collect [get]
- func StopCollect(c *gin.Context) {
- mac := c.Param("mac")
- stopTransUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.WriteDataUrl
- stopTransUrl = strings.Replace(stopTransUrl, "MAC", mac, -1)
- stopTransUrl = strings.Replace(stopTransUrl, "DATA", constant.StopCollectCmd, -1)
- response.Success("停止采集", "", c)
- return
- }
- // ScanDeviceEmq
- // PingExample confrontation-training
- // @Summary 扫描设备
- // @Schemes
- // @Description 扫描设备
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v2/device/scan [get]
- func ScanDeviceEmq(c *gin.Context) {
- topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
- client := global.EmqClient
- //停止扫描
- fmt.Println("停止扫描")
- fmt.Println(constant.CmdStopScan)
- Publish(client, topic, constant.CmdStopScan)
- //开启扫描
- //Publish(client, "/EE3870DA24C4/connect_packet/connect1_subscribe", "[{\"cmd\":\"AT+SCAN=\",\"s\":\"1\"}]")
- //停止扫描
- //Publish(client, "/EE3870DA24C4/connect_packet/connect1_subscribe", "[{\"cmd\":\"AT+SCAN=\",\"s\":\"0\"}]")
- //设置设备服务UUID
- //fmt.Println("设置设备服务UUID")
- if global.EmqConfig.FirstOpen == "0" {
- SendUUID(client, topic)
- }
- time.Sleep(time.Millisecond * 100)
- Publish(global.EmqClient, topic, constant.CmdStartScan)
- response.Success("开启扫描完成", "", c)
- return
- }
- // StopScanDeviceEmq
- // PingExample confrontation-training
- // @Summary 停止扫描设备
- // @Schemes
- // @Description 停止扫描设备
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v2/device/stop/scan [get]
- func StopScanDeviceEmq(c *gin.Context) {
- topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
- Publish(global.EmqClient, topic, constant.CmdStopScan)
- response.Success("关闭扫描完成", "", c)
- return
- }
- // ConnectDevice2
- // PingExample confrontation-training
- // @Summary 连接设备
- // @Schemes
- // @Description 连接设备
- // @Tags 设备管理
- // @Param device body string true "mac:设备MAC地址 ai:终端BLE设备的地址ID at:终端BLE设备的地址类型 "
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v2/device/conn [post]
- func ConnectDevice2(c *gin.Context) {
- var param gateway.ConnectDevice
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errors.ParamInvalid, c)
- return
- }
- topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
- ConnectDeviceEmq(global.EmqClient, topic, param.Mac, "0", param.Ai, param.At)
- response.Success("连接设备完成", "", c)
- return
- }
- // DisConnect
- // PingExample confrontation-training
- // @Summary 断开连接设备
- // @Schemes
- // @Description 断开连接设备
- // @Tags 设备管理
- // @Param device body string true "mac:设备MAC地址 "
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v2/device/dis/conn [post]
- func DisConnect(c *gin.Context) {
- var param gateway.ConnectDevice
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errors.ParamInvalid, c)
- return
- }
- topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
- DisConnectDeviceEmq(global.EmqClient, topic, param.Mac)
- response.Success("断开设备连接成功", "", c)
- return
- }
- // ConnectList
- // PingExample confrontation-training
- // @Summary 已连接列表 已连接列表中不再做任何处理,在连接或断开连接 成功或失败时系统自动调用
- // @Schemes
- // @Description 已连接列表
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v2/device/connected/list [get]
- func ConnectList(c *gin.Context) {
- //topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
- ////ConnectedListEmq(global.EmqClient, topic)
- ////ConnectedNumber(global.EmqClient, topic)
- //for i := 0; i < 6; i++ {
- // ConnectedListEmq(global.EmqClient, topic, strconv.Itoa(i))
- //}
- response.Success("查询连接列表完成", "", c)
- return
- }
- // WriteDataEmq
- // PingExample confrontation-training
- // @Summary 写入数据-脑电写入指令
- // @Schemes
- // @Description 写入数据-脑电写入指令
- // @Tags 设备管理
- // @Param mac body string true "mac:设备MAC地址 userName:用户姓名 gender:性别 age:年龄 height:身高 weight:体重"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v2/device/write/data [post]
- func WriteDataEmq(c *gin.Context) {
- var param models.WriteData
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errors.ParamInvalid, c)
- return
- }
- userNameHex := hex.EncodeToString([]byte(param.UserName))
- topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
- //写入
- //1 绑定用户指令
- //指令头(E841) + user信息前缀(AA)+姓名(姓名字节长度+姓名+姓名不足12字节占位符)+性别+年龄+身高+体重
- var stringBuild strings.Builder
- stringBuild.WriteString(constant.BindUserInfoCmdPrefix)
- length := len(userNameHex) / 2
- bytes := common.Int2Bytes(length)
- result := common.Bytes2HexStr(bytes)
- result = common.Int2Byte(int64(length))
- if result == "" {
- response.Failed("用户名处理异常", c)
- return
- }
- result = result[len(result)-2:]
- stringBuild.WriteString(result)
- stringBuild.WriteString(userNameHex)
- placeHolder := common.GenerateHexStringCmdPlaceHolder(constant.MaxNameByteLength - length)
- stringBuild.WriteString(placeHolder)
- hexGender := common.Int2Byte(param.Gender)
- stringBuild.WriteString(hexGender[len(hexGender)-2:])
- if param.Age == 0 {
- param.Age = constant.EcgDefaultAge
- }
- hexAge := common.Int2Byte(param.Age)
- stringBuild.WriteString(hexAge[len(hexAge)-2:])
- if param.Height == 0 {
- param.Height = constant.EcgDefaultHeight
- }
- hexHeight := common.Int2Byte(param.Height)
- stringBuild.WriteString(hexHeight[len(hexHeight)-2:])
- if param.Weight == 0 {
- param.Weight = constant.EcgDefaultWeight
- }
- hexWeight := common.Int2Byte(param.Weight)
- stringBuild.WriteString(hexWeight[len(hexWeight)-2:])
- bindUserCmdByte := []byte(stringBuild.String())
- bindUserCmd := string(bindUserCmdByte)
- fmt.Println(bindUserCmd)
- //2.授时指令
- //timeCmd := common.GetTimeCmd()
- timeCmd := common.GetHexTimeStr()
- //timeCmd = fmt.Sprintf("%s%s", "0x", timeCmd)
- setTimeCmd := fmt.Sprintf("%s%s", constant.SetTimeCmdPrefix, timeCmd)
- fmt.Println(setTimeCmd)
- //3.开始收集指令
- startCollectCmd := fmt.Sprintf("%s%s", constant.StartCollectCmdPrefix, timeCmd)
- fmt.Println(startCollectCmd)
- //发送指令
- //绑定用户
- WNP(global.EmqClient, topic, param.Mac, "FFF1", bindUserCmd)
- //授时
- WNP(global.EmqClient, topic, param.Mac, "FFF1", setTimeCmd)
- //开始收集
- WNP(global.EmqClient, topic, param.Mac, "FFF1", startCollectCmd)
- //开始传输指令
- WNP(global.EmqClient, topic, param.Mac, "FFF1", constant.StartTransCmd)
- response.Success(errors.WriteDataSuccess, nil, c)
- return
- }
|