Browse Source

add:新网关,双人对抗

zzf 11 months ago
parent
commit
4cce660d0c
7 changed files with 86 additions and 22 deletions
  1. 2 1
      api/gateway/gateway.go
  2. 2 1
      config/application.yaml
  3. 24 1
      docs/docs.go
  4. 24 1
      docs/swagger.json
  5. 16 1
      docs/swagger.yaml
  6. 17 16
      initialize/emq.go
  7. 1 1
      initialize/router.go

+ 2 - 1
api/gateway/gateway.go

@@ -460,7 +460,8 @@ func DisConnectAll(c *gin.Context) {
 	topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
 	topic := "/" + global.Config.EmqConfig.GatewayMac + constant.TopicConnectSub
 	//断开所有的连接
 	//断开所有的连接
 	for s := range global.DeviceMap {
 	for s := range global.DeviceMap {
-		if len(s) == 18 {
+		if len(s) == 17 {
+			s = strings.ReplaceAll(s, ":", "")
 			DisConnectDeviceEmq(global.EmqClient, topic, s)
 			DisConnectDeviceEmq(global.EmqClient, topic, s)
 		}
 		}
 	}
 	}

+ 2 - 1
config/application.yaml

@@ -59,7 +59,8 @@ emq:
   filter:
   filter:
   - MIND
   - MIND
   - BW-ECG
   - BW-ECG
-  gatewayMac: EE3870DA24C4
+#  gatewayMac: EE3870DA24C4
+  gatewayMac: FC33BC55470E
 
 
 #  [{"cmd":"AT+FLT=","total":"2"},{"t":"0","d":"MIND"},{"t":"0","d":"BW-ECG"}]
 #  [{"cmd":"AT+FLT=","total":"2"},{"t":"0","d":"MIND"},{"t":"0","d":"BW-ECG"}]
 
 

+ 24 - 1
docs/docs.go

@@ -661,7 +661,7 @@ const docTemplate = `{
                 "tags": [
                 "tags": [
                     "设备管理"
                     "设备管理"
                 ],
                 ],
-                "summary": "已连接列表",
+                "summary": "已连接列表 已连接列表中不再做任何处理,在连接或断开连接 成功或失败时系统自动调用",
                 "responses": {
                 "responses": {
                     "200": {
                     "200": {
                         "description": "ok",
                         "description": "ok",
@@ -706,6 +706,29 @@ const docTemplate = `{
                 }
                 }
             }
             }
         },
         },
+        "/v2/device/dis/connAll": {
+            "get": {
+                "description": "断开所有已连接设备",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "设备管理"
+                ],
+                "summary": "断开所有已连接设备",
+                "responses": {
+                    "200": {
+                        "description": "ok",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/v2/device/scan": {
         "/v2/device/scan": {
             "get": {
             "get": {
                 "description": "扫描设备",
                 "description": "扫描设备",

+ 24 - 1
docs/swagger.json

@@ -652,7 +652,7 @@
                 "tags": [
                 "tags": [
                     "设备管理"
                     "设备管理"
                 ],
                 ],
-                "summary": "已连接列表",
+                "summary": "已连接列表 已连接列表中不再做任何处理,在连接或断开连接 成功或失败时系统自动调用",
                 "responses": {
                 "responses": {
                     "200": {
                     "200": {
                         "description": "ok",
                         "description": "ok",
@@ -697,6 +697,29 @@
                 }
                 }
             }
             }
         },
         },
+        "/v2/device/dis/connAll": {
+            "get": {
+                "description": "断开所有已连接设备",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "设备管理"
+                ],
+                "summary": "断开所有已连接设备",
+                "responses": {
+                    "200": {
+                        "description": "ok",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/v2/device/scan": {
         "/v2/device/scan": {
             "get": {
             "get": {
                 "description": "扫描设备",
                 "description": "扫描设备",

+ 16 - 1
docs/swagger.yaml

@@ -426,7 +426,7 @@ paths:
           description: ok
           description: ok
           schema:
           schema:
             type: string
             type: string
-      summary: 已连接列表
+      summary: 已连接列表 已连接列表中不再做任何处理,在连接或断开连接 成功或失败时系统自动调用
       tags:
       tags:
       - 设备管理
       - 设备管理
   /v2/device/dis/conn:
   /v2/device/dis/conn:
@@ -451,6 +451,21 @@ paths:
       summary: 断开连接设备
       summary: 断开连接设备
       tags:
       tags:
       - 设备管理
       - 设备管理
+  /v2/device/dis/connAll:
+    get:
+      consumes:
+      - application/json
+      description: 断开所有已连接设备
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: ok
+          schema:
+            type: string
+      summary: 断开所有已连接设备
+      tags:
+      - 设备管理
   /v2/device/scan:
   /v2/device/scan:
     get:
     get:
       consumes:
       consumes:

+ 17 - 16
initialize/emq.go

@@ -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)

+ 1 - 1
initialize/router.go

@@ -78,7 +78,7 @@ func Router() {
 	device2.GET("/stop/scan", gateway.StopScanDeviceEmq)
 	device2.GET("/stop/scan", gateway.StopScanDeviceEmq)
 	device2.POST("/conn", gateway.ConnectDevice2)
 	device2.POST("/conn", gateway.ConnectDevice2)
 	device2.POST("/dis/conn", gateway.DisConnect)
 	device2.POST("/dis/conn", gateway.DisConnect)
-	device2.POST("/dis/connAll", gateway.DisConnectAll)
+	device2.GET("/dis/connAll", gateway.DisConnectAll)
 	device2.GET("/connected/list", gateway.ConnectList)
 	device2.GET("/connected/list", gateway.ConnectList)
 	device2.POST("/write/data", gateway.WriteDataEmq)
 	device2.POST("/write/data", gateway.WriteDataEmq)
 	//初始化SSE
 	//初始化SSE