From 35cf04b580e37215480c32dfb3c9f184f75acbd9 Mon Sep 17 00:00:00 2001 From: wyf <494641114@qq.com> Date: Tue, 15 Jul 2025 10:59:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=A4=9A=E4=B8=AA=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=BF=9E=E6=8E=A5=E5=90=8C=E4=B8=80=E4=B8=AA=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=9A=84=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/main.dart | 10 ++- lib/model/WebSocketMessage.dart | 3 +- lib/model/WebSocketMessage.g.dart | 2 + .../component/main_page_b_bottom_change.dart | 81 +++++++++++++++++-- lib/pages/mh_page/room_picker.dart | 3 +- lib/pages/mh_page/test/WebviewTestModel.dart | 9 ++- 6 files changed, 93 insertions(+), 15 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index f3a848c..a96221c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -177,6 +177,10 @@ void initEasyDartModule() { WebSocketConfig(ServiceConstant.webSocketService, (data) { // 接收到服务消息 var json = jsonDecode(data); + ef.log("[websokcet]:${json}"); + if (json['wsId'] != null) { + ef.kvRoot.websocketId = json['wsId']; + } if (json['code'] != null && json['code'] != 200) { EasyDartModule.logger.error("websocket连接失败--》" + json); } @@ -184,7 +188,11 @@ void initEasyDartModule() { var call = CommonVariables.callMap[json["path"]]; if (call != null) { try { - call(json["data"]); + if (json['path'] != "/smartbed/connect") { + call(json["data"]); + } else { + call(json); + } } catch (e) { print(e); } diff --git a/lib/model/WebSocketMessage.dart b/lib/model/WebSocketMessage.dart index 67ed567..274e04d 100644 --- a/lib/model/WebSocketMessage.dart +++ b/lib/model/WebSocketMessage.dart @@ -7,8 +7,9 @@ class WebSocketMessage { String path; int? type; dynamic data; + String? to; - WebSocketMessage({required this.path, this.type, this.data}); + WebSocketMessage({required this.path, this.type, this.data, this.to}); static WebSocketMessage fromJson(Map json) => _$WebSocketMessageFromJson(json); diff --git a/lib/model/WebSocketMessage.g.dart b/lib/model/WebSocketMessage.g.dart index ca9147d..256ed78 100644 --- a/lib/model/WebSocketMessage.g.dart +++ b/lib/model/WebSocketMessage.g.dart @@ -11,6 +11,7 @@ WebSocketMessage _$WebSocketMessageFromJson(Map json) => path: json['path'] as String, type: (json['type'] as num?)?.toInt(), data: json['data'], + to: json['to'] as String?, ); Map _$WebSocketMessageToJson(WebSocketMessage instance) => @@ -18,4 +19,5 @@ Map _$WebSocketMessageToJson(WebSocketMessage instance) => 'path': instance.path, 'type': instance.type, 'data': instance.data, + 'to': instance.to, }; diff --git a/lib/pages/main_bottom/component/main_page_b_bottom_change.dart b/lib/pages/main_bottom/component/main_page_b_bottom_change.dart index 31a9169..b238d14 100644 --- a/lib/pages/main_bottom/component/main_page_b_bottom_change.dart +++ b/lib/pages/main_bottom/component/main_page_b_bottom_change.dart @@ -4,6 +4,7 @@ import 'package:ef/ef.dart'; import 'package:flutter/material.dart'; import 'package:vbvs_app/common/util/CommonVariables.dart'; import 'package:vbvs_app/component/NullDataComponentWidget.dart'; +import 'package:vbvs_app/controller/user_info_controller.dart'; import 'package:vbvs_app/model/WebSocketMessage.dart'; import 'package:vbvs_app/pages/common/bezier_bottom_navigation_bar.dart'; import 'package:vbvs_app/pages/mh_page/MattressControl.dart'; @@ -139,26 +140,90 @@ class _HomePageState extends State setState(() { selectedIndex = index; + dealWebSource(index); }); } Future dealWebSource(int index) async { try { + UserInfoController userInfoController = Get.find(); WebviewTestController webviewTestController = Get.find(); if (index == 2) { await webviewTestController.web.jsbridge?.dart.pageActive(true); //激活websocket - CommonVariables.callMap["/smartbed/connect"] = (data) { - //订阅连接消息 - ef.log("[设备申请连接]:$data"); - }; edm.EasyDartModule.websocket.sendData(jsonEncode(WebSocketMessage( - path: "/smartbed/connect", - type: 5, - data: {"mac": webviewTestController.selectDevice['mac']}))); + path: "/smartbed/connect", + type: 1, + ))); + await Future.delayed(Duration(seconds: 1)); + edm.EasyDartModule.websocket.sendData(jsonEncode( + WebSocketMessage(path: "/smartbed/connect", type: 5, data: { + 'mac': webviewTestController.selectDevice['mac'], + }))); } else { - await webviewTestController.web.jsbridge?.dart.pageInActive(); + await webviewTestController.web.jsbridge?.dart.pageInActive(); } + CommonVariables.callMap["/smartbed/connect"] = (data) { + //订阅连接消息 + ef.log("[websocket]:$data"); + ef.log("[设备申请连接]:$data"); + if (data['type'] == 4) { + if (ef.kvRoot.websocketId == data['from']) { + return; + } + //接收到有人正在控制,停止连接 + if (index == 2) { + //通知显示弹窗已被用户绑定 + try { + webviewTestController.web.jsbridge?.dart.haveConnected(data); + } catch (e) { + ef.log("[websocket]调用网页异常:$e"); + } + } + } + if (data['type'] == 5) { + //接收到别人正在申请控制,判断自己是否处于控制页并且连接了该设备 + if (ef.kvRoot.websocketId == null) { + return; + } + if (ef.kvRoot.websocketId == data['from']) { + return; + } + if (webviewTestController.selectDevice['mac'] == null) { + return; + } + if (index != 2) { + //当前不处于控制页且没有连接该设备 + if (webviewTestController.selectDevice == null || + webviewTestController.selectDevice['mac'] != + data['data']?['mac']) { + //如果当前没有连接该设备,则返回 + return; + } + //当前不处于控制页且连接了该设备-->进行断开 + //web todo 断开设备连接 + webviewTestController.web.jsbridge?.dart.disconnectByOtherUser(); + } else { + ef.log("[websocket]:当前处于控制页,给指定用户发送xxx用户已连接"); + //当前处于控制页,给指定用户发送xxx用户已连接 + if (webviewTestController.selectDevice == null || + webviewTestController.selectDevice['mac'] != + data['data']['mac']) { + //如果当前没有连接该设备,则返回 + return; + } + edm.EasyDartModule.websocket.sendData(jsonEncode(WebSocketMessage( + to: data['from'], + // to: ef.kvRoot.websocketId, + path: "/smartbed/connect", + type: 4, + data: { + "mac": webviewTestController.selectDevice['mac'], + "user": userInfoController.model + }))); + } + } + }; } catch (e) { ef.log("[激活控制页面报错]:$e"); } diff --git a/lib/pages/mh_page/room_picker.dart b/lib/pages/mh_page/room_picker.dart index 4853cd7..534b2c7 100644 --- a/lib/pages/mh_page/room_picker.dart +++ b/lib/pages/mh_page/room_picker.dart @@ -188,8 +188,7 @@ class _RoomPickerPageState extends State { "id": editedData["_id"], "roomId": rooms[selectedIndex]['_id'], }, - onSuccess: (res) { - + onSuccess: (res) { TopSlideNotification.show(context, text: "更新成功".tr, textColor: Color(0XFF00C1AA)); diff --git a/lib/pages/mh_page/test/WebviewTestModel.dart b/lib/pages/mh_page/test/WebviewTestModel.dart index 1de75b0..eae4723 100644 --- a/lib/pages/mh_page/test/WebviewTestModel.dart +++ b/lib/pages/mh_page/test/WebviewTestModel.dart @@ -13,6 +13,7 @@ import 'package:vbvs_app/common/util/FitTool.dart'; import 'package:vbvs_app/common/util/MyUtils.dart'; import 'package:vbvs_app/common/util/requestWithLog.dart'; import 'package:vbvs_app/controller/user_info_controller.dart'; +import 'package:vbvs_app/model/WebSocketMessage.dart'; import 'package:vbvs_app/pages/mh_page/homepage/controller/mht_home_controller.dart'; class WebviewTestModel { @@ -69,13 +70,15 @@ class WebviewTestController extends GetControllerEx { (selectDevice['mac'] != args[0]['mac'])) { lastSelectDevice = selectDevice; } + edm.EasyDartModule.websocket.sendData(jsonEncode( + WebSocketMessage(path: "/smartbed/connect", type: 5, data: { + 'mac': selectDevice['mac'], + }))); dealInstantData(selectDevice); } catch (e) { - ef.log("[aaaa]$e"); + ef.log("[切换设备失败]$e"); } - //查询人员信息 - return true; }); bridge.sdk.updateBlueToothStatus((args) async {