blood_pressure.dart 15 KB

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