|
@@ -7,15 +7,15 @@ import (
|
|
"confrontation-training/models/gateway"
|
|
"confrontation-training/models/gateway"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/r3labs/sse/v2"
|
|
"github.com/r3labs/sse/v2"
|
|
- "net/http"
|
|
|
|
"strings"
|
|
"strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
// SseScanDevice 扫描设备
|
|
// SseScanDevice 扫描设备
|
|
-func SseScanDevice(paramMap map[string]string, filterType string, r *http.Request) {
|
|
|
|
|
|
+func SseScanDevice(paramMap map[string]string, filterType string, c *gin.Context) {
|
|
var scanUrl = global.Config.Gateway.BaseUrl + global.Config.Gateway.ScanUrl
|
|
var scanUrl = global.Config.Gateway.BaseUrl + global.Config.Gateway.ScanUrl
|
|
|
|
|
|
deviceMap := make(map[string]gateway.DeviceScanned)
|
|
deviceMap := make(map[string]gateway.DeviceScanned)
|
|
@@ -24,49 +24,48 @@ func SseScanDevice(paramMap map[string]string, filterType string, r *http.Reques
|
|
scanUrl += "&" + key + "=" + value
|
|
scanUrl += "&" + key + "=" + value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ fmt.Println("创建新的sse")
|
|
//events := make(chan *sse.Event)
|
|
//events := make(chan *sse.Event)
|
|
- client := sse.NewClient(scanUrl)
|
|
|
|
- go func() {
|
|
|
|
- fmt.Println("开始SSE")
|
|
|
|
- //err := client.SubscribeChan("messages", events)
|
|
|
|
- //if err != nil {
|
|
|
|
- // return
|
|
|
|
- //}
|
|
|
|
- err := client.SubscribeRaw(func(msg *sse.Event) {
|
|
|
|
- //fmt.Println(string(msg.Data))
|
|
|
|
- var data gateway.DeviceScannedFromGateway
|
|
|
|
- err := json.Unmarshal(msg.Data, &data)
|
|
|
|
- if err != nil {
|
|
|
|
- fmt.Printf("SSE数据转换异常:%s", err.Error())
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- deviceScanned := gateway.DeviceScanned{}
|
|
|
|
- deviceScanned.MAC = data.Bdaddrs[0].Bdaddr
|
|
|
|
- if filterType == "1" {
|
|
|
|
- deviceService := GetDeviceService()
|
|
|
|
- _, i := deviceService.FindDeviceByMac(deviceScanned.MAC)
|
|
|
|
- if i > 0 {
|
|
|
|
- deviceScanned.Name = data.Name
|
|
|
|
- deviceScanned.Rssi = data.Rssi
|
|
|
|
- deviceScanned.BdadrType = data.Bdaddrs[0].BdaddrType
|
|
|
|
- deviceScanned.Chip = data.ChipId
|
|
|
|
- deviceMap[deviceScanned.MAC] = deviceScanned
|
|
|
|
- fmt.Println(deviceScanned)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
|
|
+ client := global.SseClientDevice
|
|
|
|
+ client.URL = scanUrl
|
|
|
|
+ //ws, _, err := websocket.DefaultDialer.Dial(global.Config.Websocket.WSUrl, nil)
|
|
|
|
+ //if err != nil {
|
|
|
|
+ // response.Failed(errors.SocketInitError, c)
|
|
|
|
+ //}
|
|
|
|
+ ws, _, err := websocket.DefaultDialer.Dial(global.Config.Websocket.WSUrl, nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ panic(err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ fmt.Println("开始SSE")
|
|
|
|
+ err = client.SubscribeRaw(func(msg *sse.Event) {
|
|
|
|
+ var data gateway.DeviceScannedFromGateway
|
|
|
|
+ err := json.Unmarshal(msg.Data, &data)
|
|
if err != nil {
|
|
if err != nil {
|
|
- fmt.Println(err.Error())
|
|
|
|
|
|
+ fmt.Printf("SSE数据转换异常:%s", err.Error())
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- <-r.Context().Done()
|
|
|
|
- return
|
|
|
|
- }()
|
|
|
|
- ticket := time.NewTicker(time.Duration(global.Config.Gateway.ScanSecond) * time.Millisecond)
|
|
|
|
- for range ticket.C {
|
|
|
|
- fmt.Println("当前时间:" + time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
- ws, _, err := websocket.DefaultDialer.Dial(global.Config.Websocket.WSUrl, nil)
|
|
|
|
|
|
+ deviceScanned := gateway.DeviceScanned{}
|
|
|
|
+ deviceScanned.MAC = data.Bdaddrs[0].Bdaddr
|
|
|
|
+ //库中存在的设备
|
|
|
|
+ if filterType == "1" {
|
|
|
|
+ deviceService := GetDeviceService()
|
|
|
|
+ _, i := deviceService.FindDeviceByMac(deviceScanned.MAC)
|
|
|
|
+ if i > 0 {
|
|
|
|
+ deviceScanned.Name = data.Name
|
|
|
|
+ deviceScanned.Rssi = data.Rssi
|
|
|
|
+ deviceScanned.BdadrType = data.Bdaddrs[0].BdaddrType
|
|
|
|
+ deviceScanned.Chip = data.ChipId
|
|
|
|
+ deviceMap[deviceScanned.MAC] = deviceScanned
|
|
|
|
+ fmt.Println(deviceScanned)
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ deviceScanned.Name = data.Name
|
|
|
|
+ deviceScanned.Rssi = data.Rssi
|
|
|
|
+ deviceScanned.BdadrType = data.Bdaddrs[0].BdaddrType
|
|
|
|
+ deviceScanned.Chip = data.ChipId
|
|
|
|
+ deviceMap[deviceScanned.MAC] = deviceScanned
|
|
|
|
+ }
|
|
if len(deviceMap) > 0 {
|
|
if len(deviceMap) > 0 {
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -80,7 +79,9 @@ func SseScanDevice(paramMap map[string]string, filterType string, r *http.Reques
|
|
fmt.Println("data parse Error :" + err.Error())
|
|
fmt.Println("data parse Error :" + err.Error())
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- messageMap["data"] = string(marshal)
|
|
|
|
|
|
+ messageMap["content"] = string(marshal)
|
|
|
|
+ messageMap["Sender"] = "server"
|
|
|
|
+ messageMap["Recipient"] = "client"
|
|
bytes, err := json.Marshal(messageMap)
|
|
bytes, err := json.Marshal(messageMap)
|
|
if err != nil {
|
|
if err != nil {
|
|
fmt.Printf("json异常:%s", err.Error())
|
|
fmt.Printf("json异常:%s", err.Error())
|
|
@@ -98,9 +99,8 @@ func SseScanDevice(paramMap map[string]string, filterType string, r *http.Reques
|
|
return
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- client = nil
|
|
|
|
- ws = nil
|
|
|
|
- }
|
|
|
|
|
|
+ time.Sleep(200 * time.Millisecond)
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
// SseOpenNotify 开启通知
|
|
// SseOpenNotify 开启通知
|
|
@@ -113,12 +113,16 @@ func SseOpenNotify() {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- client := sse.NewClient(notifyUrl)
|
|
|
|
|
|
+ //client := global.SseClientData(notifyUrl)
|
|
|
|
+ client := global.SseClientData
|
|
|
|
+ client.URL = notifyUrl
|
|
err = client.SubscribeRaw(func(msg *sse.Event) {
|
|
err = client.SubscribeRaw(func(msg *sse.Event) {
|
|
- s := string(msg.Data)
|
|
|
|
|
|
+ bytes := msg.Data
|
|
|
|
+ s := string(bytes)
|
|
//fmt.Println("notify receive data :" + s)
|
|
//fmt.Println("notify receive data :" + s)
|
|
|
|
|
|
flag := strings.HasPrefix(s, "E840") || strings.HasPrefix(s, "E841") || strings.HasPrefix(s, "E823")
|
|
flag := strings.HasPrefix(s, "E840") || strings.HasPrefix(s, "E841") || strings.HasPrefix(s, "E823")
|
|
|
|
+ //非心电数据
|
|
if !flag {
|
|
if !flag {
|
|
//websocket 通知数据
|
|
//websocket 通知数据
|
|
//msgMap := make(map[string]string)
|
|
//msgMap := make(map[string]string)
|
|
@@ -132,13 +136,24 @@ func SseOpenNotify() {
|
|
}
|
|
}
|
|
messageMap := make(map[string]string)
|
|
messageMap := make(map[string]string)
|
|
messageMap["msgType"] = constant.MessageTypeData
|
|
messageMap["msgType"] = constant.MessageTypeData
|
|
- messageMap["data"] = string(msg.Data)
|
|
|
|
- err := ws.WriteMessage(websocket.TextMessage, msg.Data)
|
|
|
|
|
|
+ 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 {
|
|
if err != nil {
|
|
fmt.Println(errors.SendMessageError + err.Error())
|
|
fmt.Println(errors.SendMessageError + err.Error())
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+ //心电数据
|
|
|
|
+ } else {
|
|
|
|
+ if len(bytes) == 18 {
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ //time.Sleep(100)
|
|
|
|
+ //time.Sleep(100 * time.Millisecond)
|
|
|
|
+ //time.Sleep(10 * time.Millisecond)
|
|
})
|
|
})
|
|
if err != nil {
|
|
if err != nil {
|
|
return
|
|
return
|