|
@@ -35,7 +35,8 @@ func CreateEmqClient() {
|
|
emqConfig := global.EmqConfig
|
|
emqConfig := global.EmqConfig
|
|
connectAddress := fmt.Sprintf("%s://%s:%d", emqConfig.Protocol, emqConfig.Broker, emqConfig.Port)
|
|
connectAddress := fmt.Sprintf("%s://%s:%d", emqConfig.Protocol, emqConfig.Broker, emqConfig.Port)
|
|
fmt.Println("connect address:", connectAddress)
|
|
fmt.Println("connect address:", connectAddress)
|
|
- global.Log4J.Info("connect address:", connectAddress)
|
|
|
|
|
|
+ _ = global.Log4J.Info("connect address:", connectAddress)
|
|
|
|
+
|
|
opts := mqtt.NewClientOptions()
|
|
opts := mqtt.NewClientOptions()
|
|
opts.AddBroker(connectAddress)
|
|
opts.AddBroker(connectAddress)
|
|
opts.SetUsername(emqConfig.UserName)
|
|
opts.SetUsername(emqConfig.UserName)
|
|
@@ -46,7 +47,7 @@ func CreateEmqClient() {
|
|
client := global.EmqClient
|
|
client := global.EmqClient
|
|
token := client.Connect()
|
|
token := client.Connect()
|
|
if token.WaitTimeout(time.Second*3) && token.Error() != nil {
|
|
if token.WaitTimeout(time.Second*3) && token.Error() != nil {
|
|
- global.Log4J.Error(token.Error())
|
|
|
|
|
|
+ _ = global.Log4J.Error(token.Error())
|
|
}
|
|
}
|
|
SubScribe(client)
|
|
SubScribe(client)
|
|
topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
|
|
topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
|
|
@@ -54,14 +55,14 @@ func CreateEmqClient() {
|
|
|
|
|
|
//添加过滤
|
|
//添加过滤
|
|
fmt.Println("添加设备过滤:")
|
|
fmt.Println("添加设备过滤:")
|
|
- global.Log4J.Info("添加设备过滤:")
|
|
|
|
|
|
+ _ = global.Log4J.Info("添加设备过滤:")
|
|
filterCmd := "[{\"cmd\":\"AT+FLT=\",\"total\":\"2\"}"
|
|
filterCmd := "[{\"cmd\":\"AT+FLT=\",\"total\":\"2\"}"
|
|
for _, filter := range global.EmqConfig.Filter {
|
|
for _, filter := range global.EmqConfig.Filter {
|
|
filterCmd = filterCmd + ",{\"t\":\"0\",\"d\":\"" + filter + "\"}"
|
|
filterCmd = filterCmd + ",{\"t\":\"0\",\"d\":\"" + filter + "\"}"
|
|
}
|
|
}
|
|
filterCmd = filterCmd + "]"
|
|
filterCmd = filterCmd + "]"
|
|
fmt.Println(filterCmd)
|
|
fmt.Println(filterCmd)
|
|
- global.Log4J.Info(filterCmd)
|
|
|
|
|
|
+ _ = global.Log4J.Info(filterCmd)
|
|
//filterCmd = "[{\"cmd\":\"AT+FLT=\",\"total\":\"2\"},{\"t\":\"0\",\"d\":\"MIND\"},{\"t\":\"0\",\"d\":\"BW-ECG\"}]"
|
|
//filterCmd = "[{\"cmd\":\"AT+FLT=\",\"total\":\"2\"},{\"t\":\"0\",\"d\":\"MIND\"},{\"t\":\"0\",\"d\":\"BW-ECG\"}]"
|
|
Publish(client, topic, filterCmd)
|
|
Publish(client, topic, filterCmd)
|
|
//SendUUID(client, "/EE3870DA24C4/connect_packet/connect1_subscribe")
|
|
//SendUUID(client, "/EE3870DA24C4/connect_packet/connect1_subscribe")
|
|
@@ -80,10 +81,10 @@ func Publish(client mqtt.Client, topic string, message string) {
|
|
|
|
|
|
if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
|
|
if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
|
|
fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
|
|
fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
|
|
- global.Log4J.Info("publish failed, topic: ", topic, "payload: ", message)
|
|
|
|
|
|
+ _ = global.Log4J.Info("publish failed, topic: ", topic, "payload: ", message)
|
|
} else {
|
|
} else {
|
|
fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
|
|
fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
|
|
- global.Log4J.Info("publish failed, topic: ", topic, "payload: ", message)
|
|
|
|
|
|
+ _ = global.Log4J.Info("publish failed, topic: ", topic, "payload: ", message)
|
|
}
|
|
}
|
|
//time.Sleep(time.Second * 1)
|
|
//time.Sleep(time.Second * 1)
|
|
time.Sleep(time.Millisecond * 100)
|
|
time.Sleep(time.Millisecond * 100)
|
|
@@ -92,11 +93,11 @@ func SendUUID(client mqtt.Client, topic string) {
|
|
|
|
|
|
if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, constant.CmdSetUUID); token.Wait() && token.Error() != nil {
|
|
if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, constant.CmdSetUUID); token.Wait() && token.Error() != nil {
|
|
fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
|
|
fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
|
|
- global.Log4J.Info("publish failed, topic: ", topic, "payload: ", constant.CmdSetUUID)
|
|
|
|
|
|
+ _ = global.Log4J.Info("publish failed, topic: ", topic, "payload: ", constant.CmdSetUUID)
|
|
|
|
|
|
} else {
|
|
} else {
|
|
fmt.Printf("publish success, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
|
|
fmt.Printf("publish success, topic: %s, payload: %s\n", topic, constant.CmdSetUUID)
|
|
- global.Log4J.Info("publish success, topic: ", topic, "payload: ", constant.CmdSetUUID)
|
|
|
|
|
|
+ _ = global.Log4J.Info("publish success, topic: ", topic, "payload: ", constant.CmdSetUUID)
|
|
}
|
|
}
|
|
//time.Sleep(time.Second * 1)
|
|
//time.Sleep(time.Second * 1)
|
|
time.Sleep(time.Millisecond * 100)
|
|
time.Sleep(time.Millisecond * 100)
|
|
@@ -105,11 +106,11 @@ func ConnectDevice(client mqtt.Client, topic string, mac string, index string, a
|
|
message := "[{\"cmd\":\"AT+CNN=\",\"m\":\"" + mac + "\",\"i\":\"" + index + "\",\"ai\":\"" + ai + "\",\"at\":\"" + at + "\",\"l\":\"1\",\"x\":\"251\",\"relink\":\"0\",\"timeout\":\"12000\"}]"
|
|
message := "[{\"cmd\":\"AT+CNN=\",\"m\":\"" + mac + "\",\"i\":\"" + index + "\",\"ai\":\"" + ai + "\",\"at\":\"" + at + "\",\"l\":\"1\",\"x\":\"251\",\"relink\":\"0\",\"timeout\":\"12000\"}]"
|
|
if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
|
|
if token := client.Publish(topic, byte(global.EmqConfig.Qos), false, message); token.Wait() && token.Error() != nil {
|
|
fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
|
|
fmt.Printf("publish failed, topic: %s, payload: %s\n", topic, message)
|
|
- global.Log4J.Info("publish failed, topic: ", topic, "payload: ", message)
|
|
|
|
|
|
+ _ = global.Log4J.Info("publish failed, topic: ", topic, "payload: ", message)
|
|
|
|
|
|
} else {
|
|
} else {
|
|
fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
|
|
fmt.Printf("publish success, topic: %s, payload: %s\n", topic, message)
|
|
- global.Log4J.Info("publish success, topic: ", topic, "payload: ", message)
|
|
|
|
|
|
+ _ = global.Log4J.Info("publish success, topic: ", topic, "payload: ", message)
|
|
}
|
|
}
|
|
//time.Sleep(time.Second * 1)
|
|
//time.Sleep(time.Second * 1)
|
|
time.Sleep(time.Millisecond * 100)
|
|
time.Sleep(time.Millisecond * 100)
|
|
@@ -119,7 +120,7 @@ func SubScribe(client mqtt.Client) {
|
|
ws, _, err := websocket.DefaultDialer.Dial(global.Config.Websocket.WSUrl, nil)
|
|
ws, _, err := websocket.DefaultDialer.Dial(global.Config.Websocket.WSUrl, nil)
|
|
if err != nil {
|
|
if err != nil {
|
|
fmt.Println("socket通道初始化失败")
|
|
fmt.Println("socket通道初始化失败")
|
|
- global.Log4J.Error("socket通道初始化失败")
|
|
|
|
|
|
+ _ = global.Log4J.Error("socket通道初始化失败")
|
|
panic(err)
|
|
panic(err)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -134,7 +135,7 @@ func SubScribe(client mqtt.Client) {
|
|
for _, topic := range emqConfig.Topic {
|
|
for _, topic := range emqConfig.Topic {
|
|
topic = "/" + global.EmqConfig.GatewayMac + topic
|
|
topic = "/" + global.EmqConfig.GatewayMac + topic
|
|
client.Subscribe(topic, byte(emqConfig.Qos), func(client mqtt.Client, message mqtt.Message) {
|
|
client.Subscribe(topic, byte(emqConfig.Qos), func(client mqtt.Client, message mqtt.Message) {
|
|
- global.Log4J.Info("`%s` Received `%s` from `%s` topic\n", common.NowTime("2006-01-02 15:04:05"), message.Payload(), message.Topic())
|
|
|
|
|
|
+ _ = global.Log4J.Info("`%s` Received `%s` from `%s` topic\n", common.NowTime("2006-01-02 15:04:05"), message.Payload(), message.Topic())
|
|
fmt.Printf("`%s` Received `%s` from `%s` topic\n", common.NowTime("2006-01-02 15:04:05"), message.Payload(), message.Topic())
|
|
fmt.Printf("`%s` Received `%s` from `%s` topic\n", common.NowTime("2006-01-02 15:04:05"), message.Payload(), message.Topic())
|
|
messageMap := make(map[string]string)
|
|
messageMap := make(map[string]string)
|
|
var payloads []emq.Payload
|
|
var payloads []emq.Payload
|
|
@@ -318,16 +319,16 @@ func SubScribe(client mqtt.Client) {
|
|
if messageMap["msgType"] == constant.MessageTypeECGData {
|
|
if messageMap["msgType"] == constant.MessageTypeECGData {
|
|
|
|
|
|
fmt.Printf("发送心电数据===" + common.NowTime("2006-01-02 15:04:05") + "=====" + string(bytes))
|
|
fmt.Printf("发送心电数据===" + common.NowTime("2006-01-02 15:04:05") + "=====" + string(bytes))
|
|
- global.Log4J.Info("发送心电数据===" + common.NowTime("2006-01-02 15:04:05") + "=====" + string(bytes))
|
|
|
|
|
|
+ _ = global.Log4J.Info("发送心电数据===" + common.NowTime("2006-01-02 15:04:05") + "=====" + string(bytes))
|
|
}
|
|
}
|
|
- for k, _ := range messageMap {
|
|
|
|
|
|
+ for k := range messageMap {
|
|
delete(messageMap, k)
|
|
delete(messageMap, k)
|
|
}
|
|
}
|
|
- global.Log4J.Info("发送消息===" + common.NowTime("2006-01-02 15:04:05") + "=====" + string(bytes))
|
|
|
|
|
|
+ _ = global.Log4J.Info("发送消息===" + common.NowTime("2006-01-02 15:04:05") + "=====" + string(bytes))
|
|
err = ws.WriteMessage(websocket.TextMessage, bytes)
|
|
err = ws.WriteMessage(websocket.TextMessage, bytes)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Infoln("消息发送异常:" + err.Error())
|
|
log.Infoln("消息发送异常:" + err.Error())
|
|
- global.Log4J.Error("消息发送异常:" + err.Error())
|
|
|
|
|
|
+ _ = global.Log4J.Error("消息发送异常:" + err.Error())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
time.Sleep(time.Millisecond * 5)
|
|
time.Sleep(time.Millisecond * 5)
|