|
@@ -7,15 +7,15 @@ import (
|
|
|
"confrontation-training/models/gateway"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
"github.com/gorilla/websocket"
|
|
|
"github.com/r3labs/sse/v2"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
// SseScanDevice 扫描设备
|
|
|
-func SseScanDevice(paramMap map[string]string, filterType string, c *gin.Context) {
|
|
|
+func SseScanDevice(paramMap map[string]string, filterType string) {
|
|
|
var scanUrl = global.Config.Gateway.BaseUrl + global.Config.Gateway.ScanUrl
|
|
|
|
|
|
deviceMap := make(map[string]gateway.DeviceScanned)
|
|
@@ -118,42 +118,59 @@ func SseOpenNotify() {
|
|
|
client.URL = notifyUrl
|
|
|
err = client.SubscribeRaw(func(msg *sse.Event) {
|
|
|
bytes := msg.Data
|
|
|
- s := string(bytes)
|
|
|
+ //s := string(bytes)
|
|
|
//fmt.Println("notify receive data :" + s)
|
|
|
-
|
|
|
- flag := strings.HasPrefix(s, "E840") || strings.HasPrefix(s, "E841") || strings.HasPrefix(s, "E823")
|
|
|
- //非心电数据
|
|
|
- if !flag {
|
|
|
- //websocket 通知数据
|
|
|
- //msgMap := make(map[string]string)
|
|
|
- //msgMap[]
|
|
|
- var receiveData gateway.DeviceDataReceived
|
|
|
- errJson := json.Unmarshal(msg.Data, &receiveData)
|
|
|
- if errJson != nil {
|
|
|
- fmt.Println("receive data parse error:" + errJson.Error())
|
|
|
- //panic(err)
|
|
|
- return
|
|
|
- }
|
|
|
- messageMap := make(map[string]string)
|
|
|
- messageMap["msgType"] = constant.MessageTypeData
|
|
|
- messageMap["content"] = string(msg.Data)
|
|
|
- messageMap["Sender"] = "server"
|
|
|
- messageMap["Recipient"] = "client"
|
|
|
- bytes, err := json.Marshal(messageMap)
|
|
|
- err = ws.WriteMessage(websocket.TextMessage, bytes)
|
|
|
- if err != nil {
|
|
|
- fmt.Println(errors.SendMessageError + err.Error())
|
|
|
- return
|
|
|
+ //脑电数据
|
|
|
+ var receiveData gateway.DeviceDataReceived
|
|
|
+ errJson := json.Unmarshal(msg.Data, &receiveData)
|
|
|
+ if errJson != nil {
|
|
|
+ fmt.Println("receive data parse error:" + errJson.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ messageMap := make(map[string]string)
|
|
|
+ if strings.Contains(receiveData.Value, "AAAA") {
|
|
|
+ messageMap["msgType"] = constant.MessageTypeEEGData
|
|
|
+ marshal, _ := json.Marshal(receiveData)
|
|
|
+ messageMap["content"] = string(marshal)
|
|
|
+ } else { //心电数据
|
|
|
+ flag := strings.HasPrefix(receiveData.Value, "E840") || strings.HasPrefix(receiveData.Value, "E841") || strings.HasPrefix(receiveData.Value, "E823")
|
|
|
+ if !flag {
|
|
|
+ //var ecgData []int
|
|
|
+ ecgData := [12]int{}
|
|
|
+ fmt.Println("收到的心电数据:" + string(msg.Data))
|
|
|
+ data := []byte(receiveData.Value[4:])
|
|
|
+ fmt.Println(len(data))
|
|
|
+ if len(data) == 36 {
|
|
|
+ for i := 0; i < 36; i += 3 {
|
|
|
+ ecgData[i/3] = int(16<<data[i]&0xFF | 8<<data[i+1]&0xFF | data[i+2]&0xFF)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for i := len(data); i < 36; i++ {
|
|
|
+ data = append(data, data[i])
|
|
|
+ }
|
|
|
+ for i := 0; i < 36; i += 3 {
|
|
|
+ ecgData[i/3] = int(16<<data[i]&0xFF | 8<<data[i+1]&0xFF | data[i+2]&0xFF)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ messageMap["msgType"] = constant.MessageTypeECGData
|
|
|
+ var dataStr []string
|
|
|
+ for _, i := range ecgData {
|
|
|
+ dataStr = append(dataStr, strconv.Itoa(i))
|
|
|
+ }
|
|
|
+ receiveData.Value = strings.Join(dataStr, ",")
|
|
|
+ marshal, _ := json.Marshal(receiveData)
|
|
|
+ messageMap["content"] = string(marshal)
|
|
|
}
|
|
|
- //心电数据
|
|
|
- } else {
|
|
|
- if len(bytes) == 18 {
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ messageMap["Sender"] = "server"
|
|
|
+ messageMap["Recipient"] = "client"
|
|
|
+ bytes, err := json.Marshal(messageMap)
|
|
|
+ err = ws.WriteMessage(websocket.TextMessage, bytes)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(errors.SendMessageError + err.Error())
|
|
|
+ return
|
|
|
}
|
|
|
- //time.Sleep(100)
|
|
|
- //time.Sleep(100 * time.Millisecond)
|
|
|
- //time.Sleep(10 * time.Millisecond)
|
|
|
})
|
|
|
if err != nil {
|
|
|
return
|