exam_blood_pressure.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:vitalapp/architecture/app_parameters.dart';
  5. import 'package:vitalapp/managers/device_controller_manager.dart';
  6. import 'package:vitalapp/managers/interfaces/organization.dart';
  7. import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_blood_pressure.dart';
  8. import 'package:vitalapp/pages/medical/widgets/device_status_position.dart';
  9. import 'package:vitalapp/pages/medical/widgets/exam_card.dart';
  10. import 'package:vitalapp/pages/medical/widgets/switch_button.dart';
  11. import 'package:vnote_device_plugin/consts/types.dart';
  12. import 'package:vnote_device_plugin/devices/nibp.dart';
  13. import 'package:vnote_device_plugin/models/exams/nibp.dart';
  14. import 'package:fis_common/logger/logger.dart';
  15. import 'package:vitalapp/managers/interfaces/models/device.dart';
  16. import 'package:vitalapp/pages/medical/controller.dart';
  17. import 'package:vitalapp/pages/medical/models/item.dart';
  18. import 'package:vitalapp/pages/medical/models/worker.dart';
  19. import 'package:vitalapp/pages/medical/widgets/device_status.dart';
  20. // ignore: must_be_immutable
  21. class ExamBloodPressure extends StatefulWidget {
  22. const ExamBloodPressure({
  23. super.key,
  24. });
  25. @override
  26. State<ExamBloodPressure> createState() => _ExamBloodPressureState();
  27. }
  28. class _ExamBloodPressureState extends State<ExamBloodPressure> {
  29. var controller = Get.find<MedicalController>();
  30. bool get isPureSoftwareMode => AppParameters.data.isPureSoftwareMode;
  31. PressureDeviceStatus pressureDeviceStatus = PressureDeviceStatus.start;
  32. bool isConnectFail = false;
  33. DeviceControllerManager? nibp;
  34. NibpDeviceWorker? worker;
  35. int liveValue = 0;
  36. int errorCount = 0;
  37. PressureStatus pressureStatus = PressureStatus.left;
  38. bool isShowError = false;
  39. String errorMessage = '';
  40. WorkerStatus _connectStatus = WorkerStatus.connecting;
  41. // 左侧收缩压(高压)
  42. var leftHightValue = '';
  43. // 左侧舒张压(低压)
  44. var leftLowValue = '';
  45. // 右侧收缩压(高压)
  46. var rightHightValue = '';
  47. // 右侧舒张压(低压)
  48. var rightLowValue = '';
  49. @override
  50. void initState() {
  51. WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
  52. currentDevice();
  53. await initData();
  54. });
  55. super.initState();
  56. }
  57. @override
  58. void didUpdateWidget(ExamBloodPressure oldWidget) {
  59. super.didUpdateWidget(oldWidget);
  60. }
  61. Future<void> currentDevice() async {
  62. DeviceModel? device = await controller.getDevice(DeviceTypes.NIBP);
  63. if (device == null) {
  64. _connectStatus = WorkerStatus.unboundDevice;
  65. worker = null;
  66. setState(() {});
  67. return;
  68. }
  69. nibp = DeviceControllerManager(DeviceTypes.NIBP, device.model, device.mac);
  70. worker = nibp!.worker as NibpDeviceWorker;
  71. _connectStatus = nibp!.connectStatus;
  72. loadListeners();
  73. connect();
  74. }
  75. Future<void> initData() async {
  76. /// 体检系统 基础检查的特殊处理
  77. if (controller.diagnosisDataValue['Sbp_Left'] != null ||
  78. controller.diagnosisDataValue['Dbp_Left'] != null ||
  79. controller.diagnosisDataValue['Sbp_Right'] != null ||
  80. controller.diagnosisDataValue['Dbp_Right'] != null) {
  81. leftHightValue = controller.diagnosisDataValue['Sbp_Left'] ?? '';
  82. leftLowValue = controller.diagnosisDataValue['Dbp_Left'] ?? '';
  83. rightHightValue = controller.diagnosisDataValue['Sbp_Right'] ?? '';
  84. rightLowValue = controller.diagnosisDataValue['Dbp_Right'] ?? '';
  85. logger.i(
  86. '_ExamBloodSugarState initData leftHightValue:$leftHightValue leftLowValue:$leftLowValue rightHightValue:$rightHightValue rightLowValue:$rightLowValue');
  87. setState(() {});
  88. return;
  89. }
  90. }
  91. void loadListeners() {
  92. worker!.liveUpdateEvent.addListener(_onLiveUpdate);
  93. worker!.errorEvent.addListener(_onError);
  94. worker!.resultUpdateEvent.addListener(_onSuccess);
  95. worker!.connectErrorEvent.addListener(_onConnectFail);
  96. worker!.connectedEvent.addListener(_onConnectSuccess);
  97. worker!.disconnectedEvent.addListener(_onDisconnected);
  98. }
  99. void releaseListeners() {
  100. if (worker != null) {
  101. worker!.connectErrorEvent.removeListener(_onConnectFail);
  102. worker!.connectedEvent.removeListener(_onConnectSuccess);
  103. worker!.liveUpdateEvent.removeListener(_onLiveUpdate);
  104. worker!.errorEvent.removeListener(_onError);
  105. worker!.resultUpdateEvent.removeListener(_onSuccess);
  106. worker!.disconnectedEvent.removeListener(_onDisconnected);
  107. }
  108. }
  109. Future<void> connect() async {
  110. try {
  111. if (worker != null) {
  112. await worker!.connect();
  113. }
  114. } catch (err) {
  115. print(err);
  116. }
  117. }
  118. Future<void> disconnect() async {
  119. try {
  120. if (worker != null) {
  121. await worker!.disconnect();
  122. }
  123. } catch (err) {
  124. print(err);
  125. }
  126. }
  127. /// 尝试重连
  128. Future<void> tryReconnect() async {
  129. if (worker != null) {
  130. await disconnect();
  131. await connect();
  132. }
  133. }
  134. @override
  135. void dispose() {
  136. nibp?.dispose();
  137. nibp = null;
  138. releaseListeners();
  139. disconnect();
  140. worker?.dispose();
  141. super.dispose();
  142. }
  143. void _onLiveUpdate(_, int e) {
  144. setState(() {
  145. liveValue = e;
  146. });
  147. }
  148. /// 检测报错
  149. void _onError(_, String errorMessage) async {
  150. logger.i('errorMessage:$errorMessage ${DateTime.now().toString()}');
  151. final message = errorMessage.replaceAll("1506|Error|", "");
  152. setState(() {
  153. isShowError = true;
  154. this.errorMessage = message;
  155. liveValue = 0;
  156. pressureDeviceStatus = PressureDeviceStatus.end;
  157. });
  158. }
  159. /// TODO 需求不清,检测的数据需要传给体检,但是检测又不区分左右侧血压
  160. void _onSuccess(_, NibpExamValue e) async {
  161. logger.i(
  162. '检测完成,高压:${e.systolicPressure},低压:${e.diastolicPressure},脉率:${e.pulse}');
  163. int sbp = e.systolicPressure;
  164. int dbp = e.diastolicPressure;
  165. liveValue = 0;
  166. final orgManager = Get.find<IOrganizationManager>();
  167. final paramSbp = await orgManager.getDynamicParamByKey("Sbp");
  168. if (paramSbp != null) {
  169. sbp = nibp!.handleExamValueSpecially(sbp, paramSbp);
  170. logger.i('NIBP Value handle specially - Sbp: $sbp.');
  171. }
  172. final paramDbp = await orgManager.getDynamicParamByKey("Dbp");
  173. if (paramDbp != null) {
  174. dbp = nibp!.handleExamValueSpecially(dbp, paramDbp);
  175. logger.i('NIBP Value handle specially - Dbp:$dbp.');
  176. }
  177. setState(() {
  178. pressureDeviceStatus = PressureDeviceStatus.end;
  179. if (controller.diagnosisDataValue['NIBP'] == null) {
  180. controller.diagnosisDataValue['NIBP'] = {};
  181. }
  182. // controller.diagnosisDataValue['NIBP'] = {};
  183. if (pressureStatus == PressureStatus.left) {
  184. controller.diagnosisDataValue['NIBP'].addAll({
  185. 'Sbp_Left': sbp.toString(),
  186. 'Dbp_Left': dbp.toString(),
  187. 'Pulse_Beat': e.pulse.toString(),
  188. });
  189. logger.i('NIBP Value is left: $sbp, $dbp, $e');
  190. } else if (pressureStatus == PressureStatus.right) {
  191. controller.diagnosisDataValue['NIBP'].addAll({
  192. 'Sbp_Right': sbp.toString(),
  193. 'Dbp_Right': dbp.toString(),
  194. 'Pulse_Beat': e.pulse.toString(),
  195. });
  196. logger.i('NIBP Value is right: $sbp, $dbp, $e');
  197. }
  198. // controller.diagnosisDataValue['NIBP'] = {
  199. // 'Sbp': sbp.toString(),
  200. // 'Dbp': dbp.toString(),
  201. // 'Pulse_Beat': e.pulse.toString(),
  202. // };
  203. controller.saveCachedRecord();
  204. });
  205. }
  206. void _onConnectFail(sender, e) {
  207. logger.i("连接设备失败:${worker!.mac}");
  208. print('连接设备失败:${worker!.mac},errorCount:$errorCount,${DateTime.now()}');
  209. if (errorCount < 3) {
  210. print('连接设备失败:${worker!.mac},$errorCount,${DateTime.now()}');
  211. logger.i('连接设备失败:${worker!.mac},$errorCount,${DateTime.now()}');
  212. errorCount++;
  213. tryReconnect();
  214. } else {
  215. print('连接设备失败:${worker!.mac},$errorCount,${DateTime.now()}');
  216. logger.i('连接设备失败:${worker!.mac},$errorCount,${DateTime.now()}');
  217. isConnectFail = true;
  218. }
  219. _connectStatus = WorkerStatus.connectionFailed;
  220. setState(() {});
  221. }
  222. void _onDisconnected(sender, e) {
  223. logger.i("设备连接中断:${worker!.mac}");
  224. print('设备连接中断:${worker!.mac}');
  225. tryReconnect();
  226. errorCount = 0;
  227. _connectStatus = WorkerStatus.disconnected;
  228. setState(() {});
  229. }
  230. void _onConnectSuccess(sender, e) {
  231. logger.i("设备连接成功:$e");
  232. print("设备连接成功:$e");
  233. setState(() {
  234. _connectStatus = WorkerStatus.connected;
  235. errorCount = 0;
  236. isConnectFail = false;
  237. isShowError = false;
  238. });
  239. }
  240. Widget _buildValue() {
  241. if (controller.diagnosisDataValue['NIBP'] == null) {
  242. return _buildLiveWidget();
  243. }
  244. return _buildLiveWidget();
  245. }
  246. Widget _buildButtonGroup() {
  247. return Container(
  248. margin: EdgeInsets.only(right: 16),
  249. child: AnimatedToggle(
  250. values: const ['左侧', '右侧'],
  251. onToggleCallback: (value) {
  252. setState(() {
  253. pressureStatus = value;
  254. });
  255. },
  256. statusValue: pressureStatus,
  257. buttonColor: Theme.of(context).primaryColor,
  258. backgroundColor: const Color(0xFFB5C1CC),
  259. textColor: const Color(0xFFFFFFFF),
  260. ),
  261. );
  262. }
  263. @override
  264. Widget build(BuildContext context) {
  265. final nibp = controller.diagnosisDataValue['NIBP'];
  266. if (nibp != null) {
  267. // 左侧收缩压(高压)
  268. leftHightValue = nibp['Sbp_Left']?.toString() ?? '';
  269. // 左侧舒张压(低压)
  270. leftLowValue = nibp['Dbp_Left']?.toString() ?? '';
  271. // 右侧收缩压(高压)
  272. rightHightValue = nibp['Sbp_Right']?.toString() ?? '';
  273. // 右侧舒张压(低压)
  274. rightLowValue = nibp['Dbp_Right']?.toString() ?? '';
  275. setState(() {});
  276. }
  277. return Stack(
  278. children: [
  279. ExamCard(
  280. title: '',
  281. clickCard: _onClickBloodPressure,
  282. content: Column(
  283. crossAxisAlignment: CrossAxisAlignment.end,
  284. children: [
  285. SizedBox(
  286. height: 16,
  287. ),
  288. _buildButtonGroup(),
  289. Row(
  290. children: [
  291. const SizedBox(
  292. width: 25,
  293. ),
  294. const Text(
  295. '血压',
  296. style: TextStyle(fontSize: 25),
  297. ),
  298. const Expanded(child: SizedBox()),
  299. _buildValue(),
  300. ],
  301. ),
  302. Row(
  303. children: [
  304. const SizedBox(
  305. width: 25,
  306. ),
  307. const Text(
  308. '左侧血压',
  309. style: TextStyle(fontSize: 25),
  310. ),
  311. const Expanded(child: SizedBox()),
  312. if (leftHightValue.isNotEmpty || leftLowValue.isNotEmpty)
  313. _buildResultWidget(leftHightValue, leftLowValue),
  314. ],
  315. ),
  316. Row(
  317. children: [
  318. const SizedBox(
  319. width: 25,
  320. ),
  321. const Text(
  322. '右侧血压',
  323. style: TextStyle(fontSize: 25),
  324. ),
  325. const Expanded(child: SizedBox()),
  326. if (rightHightValue.isNotEmpty || rightLowValue.isNotEmpty)
  327. _buildResultWidget(rightHightValue, rightLowValue),
  328. ],
  329. ),
  330. Row(
  331. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  332. children: [
  333. const SizedBox(
  334. width: 25,
  335. ),
  336. const Text(
  337. '脉率',
  338. style: TextStyle(fontSize: 25),
  339. ),
  340. const Expanded(child: SizedBox()),
  341. Text(
  342. controller.diagnosisDataValue['NIBP']?['Pulse_Beat']
  343. .toString() ??
  344. '',
  345. style: const TextStyle(
  346. fontSize: 45,
  347. color: Colors.black,
  348. ),
  349. ),
  350. const Text(
  351. ' bpm',
  352. style: TextStyle(fontSize: 25),
  353. ),
  354. const SizedBox(
  355. width: 30,
  356. ),
  357. ],
  358. ),
  359. Container(
  360. height: 30,
  361. margin: const EdgeInsets.only(bottom: 10),
  362. child: isShowError
  363. ? Row(
  364. children: [
  365. const SizedBox(
  366. width: 25,
  367. ),
  368. Text(
  369. '检测失败:',
  370. style: TextStyle(
  371. fontSize: 20,
  372. ),
  373. ),
  374. Text(
  375. '${errorMessage}',
  376. style: TextStyle(fontSize: 20, color: Colors.grey),
  377. ),
  378. const Expanded(child: SizedBox()),
  379. ],
  380. )
  381. : SizedBox(),
  382. ),
  383. ],
  384. ),
  385. ),
  386. if (!isPureSoftwareMode) ...[
  387. if (isConnectFail) ...[
  388. _buildErrorButton(),
  389. ] else ...[
  390. Positioned(
  391. right: 10,
  392. top: 10,
  393. child: DeviceStatus(connectStatus: _connectStatus),
  394. ),
  395. ],
  396. ],
  397. ],
  398. );
  399. }
  400. /// 需要封装一下
  401. Widget _buildErrorButton() {
  402. return DeviceStatusPosition(
  403. deviceStatus: Row(
  404. children: [
  405. const Text(
  406. '请确认设备是否启动',
  407. style: TextStyle(fontSize: 24, color: Colors.red),
  408. ),
  409. IconButton(
  410. onPressed: () {
  411. liveValue = 0;
  412. tryReconnect();
  413. setState(() {
  414. _connectStatus = WorkerStatus.connecting;
  415. isConnectFail = false;
  416. });
  417. },
  418. icon: const Icon(Icons.refresh),
  419. iconSize: 32,
  420. ),
  421. ],
  422. ),
  423. );
  424. }
  425. Widget _buildLiveWidget() {
  426. return SizedBox(
  427. height: 70,
  428. child: Row(
  429. crossAxisAlignment: CrossAxisAlignment.center,
  430. children: [
  431. Container(
  432. alignment: Alignment.center,
  433. child: Text(
  434. liveValue.toString() == '0' ? '--' : liveValue.toString(),
  435. style: const TextStyle(
  436. fontSize: 60,
  437. color: Colors.black,
  438. ),
  439. ),
  440. ),
  441. const Text(
  442. ' mmHg',
  443. style: TextStyle(fontSize: 25),
  444. ),
  445. const SizedBox(
  446. width: 30,
  447. )
  448. ],
  449. ),
  450. );
  451. }
  452. Widget _buildResultWidget(String? sbp, String? dbp) {
  453. const textStyle = TextStyle(
  454. fontSize: 45,
  455. color: Colors.black,
  456. );
  457. var normalStyle = const TextStyle(
  458. fontSize: 25,
  459. color: Colors.black,
  460. );
  461. var uuitText = const Text(
  462. ' mmHg',
  463. style: TextStyle(
  464. fontSize: 25,
  465. color: Colors.black,
  466. ),
  467. );
  468. return Stack(
  469. children: [
  470. Column(
  471. mainAxisAlignment: MainAxisAlignment.center,
  472. mainAxisSize: MainAxisSize.min,
  473. crossAxisAlignment: CrossAxisAlignment.end,
  474. children: [
  475. Row(
  476. children: [
  477. Text(
  478. '收缩压',
  479. style: normalStyle,
  480. ),
  481. Container(
  482. alignment: Alignment.centerRight,
  483. width: 200,
  484. child: Text(
  485. sbp ?? '',
  486. style: textStyle,
  487. ),
  488. ),
  489. uuitText,
  490. const SizedBox(
  491. width: 30,
  492. ),
  493. ],
  494. ),
  495. Row(
  496. children: [
  497. Text(
  498. '舒张压',
  499. style: normalStyle,
  500. ),
  501. Container(
  502. alignment: Alignment.centerRight,
  503. width: 200,
  504. child: Text(
  505. dbp ?? '',
  506. style: textStyle,
  507. ),
  508. ),
  509. uuitText,
  510. const SizedBox(
  511. width: 30,
  512. ),
  513. ],
  514. ),
  515. ],
  516. ),
  517. ],
  518. );
  519. }
  520. Future<void> _onClickBloodPressure() async {
  521. String sbp = "";
  522. String dbp = "";
  523. String heart = "";
  524. if (controller.diagnosisDataValue.containsKey("NIBP")) {
  525. var nibp = controller.diagnosisDataValue["NIBP"];
  526. if (nibp is Map) {
  527. if (pressureStatus == PressureStatus.left) {
  528. if (nibp.containsKey("Sbp_Left")) {
  529. sbp = nibp["Sbp_Left"];
  530. }
  531. if (nibp.containsKey("Dbp_Left")) {
  532. dbp = nibp["Dbp_Left"];
  533. }
  534. if (nibp.containsKey("Pulse_Beat")) {
  535. heart = nibp["Pulse_Beat"];
  536. }
  537. } else {
  538. if (nibp.containsKey("Sbp_Right")) {
  539. sbp = nibp["Sbp_Left"];
  540. }
  541. if (nibp.containsKey("Dbp_Right")) {
  542. dbp = nibp["Dbp_Left"];
  543. }
  544. if (nibp.containsKey("Pulse_Beat")) {
  545. heart = nibp["Pulse_Beat"];
  546. }
  547. }
  548. }
  549. }
  550. String? result = await VDialogBloodPressure(
  551. title: '血压',
  552. initialValue: [
  553. sbp,
  554. dbp,
  555. ],
  556. isDisplayHeartRate: true,
  557. ).show();
  558. if (result != null) {
  559. try {
  560. String sbp = jsonDecode(result).first;
  561. String dbp = jsonDecode(result)[1];
  562. String heart = jsonDecode(result)[2];
  563. if (sbp.isNotEmpty && dbp.isNotEmpty) {
  564. if (pressureStatus == PressureStatus.left) {
  565. var nibp = controller.diagnosisDataValue["NIBP"];
  566. String? sdpRight = nibp?['Sbp_Right'];
  567. String? dbpRight = nibp?['Dbp_Right'];
  568. controller.diagnosisDataValue['NIBP'] = {
  569. 'Sbp_Left': sbp,
  570. 'Dbp_Left': dbp,
  571. 'Pulse_Beat': heart,
  572. };
  573. if (sdpRight != null && dbpRight != null) {
  574. controller.diagnosisDataValue['NIBP'].addAll({
  575. 'Sbp_Right': sdpRight,
  576. 'Dbp_Right': dbpRight,
  577. });
  578. }
  579. } else {
  580. var nibp = controller.diagnosisDataValue["NIBP"];
  581. String? sdpLeft = nibp?['Sbp_Left'];
  582. String? dbpLeft = nibp?['Dbp_Left'];
  583. controller.diagnosisDataValue['NIBP'] = {
  584. 'Sbp_Right': sbp,
  585. 'Dbp_Right': dbp,
  586. 'Pulse_Beat': heart,
  587. };
  588. if (sdpLeft != null && dbpLeft != null) {
  589. controller.diagnosisDataValue['NIBP'].addAll({
  590. 'Sbp_Left': sdpLeft,
  591. 'Dbp_Left': dbpLeft,
  592. });
  593. }
  594. }
  595. setState(() {});
  596. }
  597. } catch (e) {
  598. logger.e("BloodPressure _onClickBloodPressure ex:", e);
  599. }
  600. }
  601. }
  602. }