123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/architecture/app_parameters.dart';
- import 'package:vitalapp/managers/device_controller_manager.dart';
- import 'package:vitalapp/managers/interfaces/organization.dart';
- import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_blood_pressure.dart';
- import 'package:vitalapp/pages/medical/widgets/device_status_position.dart';
- import 'package:vitalapp/pages/medical/widgets/exam_card.dart';
- import 'package:vitalapp/pages/medical/widgets/switch_button.dart';
- import 'package:vnote_device_plugin/consts/types.dart';
- import 'package:vnote_device_plugin/devices/nibp.dart';
- import 'package:vnote_device_plugin/models/exams/nibp.dart';
- import 'package:fis_common/logger/logger.dart';
- import 'package:vitalapp/managers/interfaces/models/device.dart';
- import 'package:vitalapp/pages/medical/controller.dart';
- import 'package:vitalapp/pages/medical/models/item.dart';
- import 'package:vitalapp/pages/medical/models/worker.dart';
- import 'package:vitalapp/pages/medical/widgets/device_status.dart';
- // ignore: must_be_immutable
- class ExamBloodPressure extends StatefulWidget {
- const ExamBloodPressure({
- super.key,
- });
- @override
- State<ExamBloodPressure> createState() => _ExamBloodPressureState();
- }
- class _ExamBloodPressureState extends State<ExamBloodPressure> {
- var controller = Get.find<MedicalController>();
- bool get isPureSoftwareMode => AppParameters.data.isPureSoftwareMode;
- PressureDeviceStatus pressureDeviceStatus = PressureDeviceStatus.start;
- bool isConnectFail = false;
- DeviceControllerManager? nibp;
- NibpDeviceWorker? worker;
- int liveValue = 0;
- int errorCount = 0;
- PressureStatus pressureStatus = PressureStatus.left;
- bool isShowError = false;
- String errorMessage = '';
- WorkerStatus _connectStatus = WorkerStatus.connecting;
- // 左侧收缩压(高压)
- var leftHightValue = '';
- // 左侧舒张压(低压)
- var leftLowValue = '';
- // 右侧收缩压(高压)
- var rightHightValue = '';
- // 右侧舒张压(低压)
- var rightLowValue = '';
- @override
- void initState() {
- WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
- currentDevice();
- await initData();
- });
- super.initState();
- }
- @override
- void didUpdateWidget(ExamBloodPressure oldWidget) {
- super.didUpdateWidget(oldWidget);
- }
- Future<void> currentDevice() async {
- DeviceModel? device = await controller.getDevice(DeviceTypes.NIBP);
- if (device == null) {
- _connectStatus = WorkerStatus.unboundDevice;
- worker = null;
- setState(() {});
- return;
- }
- nibp = DeviceControllerManager(DeviceTypes.NIBP, device.model, device.mac);
- worker = nibp!.worker as NibpDeviceWorker;
- _connectStatus = nibp!.connectStatus;
- loadListeners();
- connect();
- }
- Future<void> initData() async {
- /// 体检系统 基础检查的特殊处理
- if (controller.diagnosisDataValue['Sbp_Left'] != null ||
- controller.diagnosisDataValue['Dbp_Left'] != null ||
- controller.diagnosisDataValue['Sbp_Right'] != null ||
- controller.diagnosisDataValue['Dbp_Right'] != null) {
- leftHightValue = controller.diagnosisDataValue['Sbp_Left'] ?? '';
- leftLowValue = controller.diagnosisDataValue['Dbp_Left'] ?? '';
- rightHightValue = controller.diagnosisDataValue['Sbp_Right'] ?? '';
- rightLowValue = controller.diagnosisDataValue['Dbp_Right'] ?? '';
- logger.i(
- '_ExamBloodSugarState initData leftHightValue:$leftHightValue leftLowValue:$leftLowValue rightHightValue:$rightHightValue rightLowValue:$rightLowValue');
- setState(() {});
- return;
- }
- }
- void loadListeners() {
- worker!.liveUpdateEvent.addListener(_onLiveUpdate);
- worker!.errorEvent.addListener(_onError);
- worker!.resultUpdateEvent.addListener(_onSuccess);
- worker!.connectErrorEvent.addListener(_onConnectFail);
- worker!.connectedEvent.addListener(_onConnectSuccess);
- worker!.disconnectedEvent.addListener(_onDisconnected);
- }
- void releaseListeners() {
- if (worker != null) {
- worker!.connectErrorEvent.removeListener(_onConnectFail);
- worker!.connectedEvent.removeListener(_onConnectSuccess);
- worker!.liveUpdateEvent.removeListener(_onLiveUpdate);
- worker!.errorEvent.removeListener(_onError);
- worker!.resultUpdateEvent.removeListener(_onSuccess);
- worker!.disconnectedEvent.removeListener(_onDisconnected);
- }
- }
- Future<void> connect() async {
- try {
- if (worker != null) {
- await worker!.connect();
- }
- } catch (err) {
- print(err);
- }
- }
- Future<void> disconnect() async {
- try {
- if (worker != null) {
- await worker!.disconnect();
- }
- } catch (err) {
- print(err);
- }
- }
- /// 尝试重连
- Future<void> tryReconnect() async {
- if (worker != null) {
- await disconnect();
- await connect();
- }
- }
- @override
- void dispose() {
- nibp?.dispose();
- nibp = null;
- releaseListeners();
- disconnect();
- worker?.dispose();
- super.dispose();
- }
- void _onLiveUpdate(_, int e) {
- setState(() {
- liveValue = e;
- });
- }
- /// 检测报错
- void _onError(_, String errorMessage) async {
- logger.i('errorMessage:$errorMessage ${DateTime.now().toString()}');
- final message = errorMessage.replaceAll("1506|Error|", "");
- setState(() {
- isShowError = true;
- this.errorMessage = message;
- liveValue = 0;
- pressureDeviceStatus = PressureDeviceStatus.end;
- });
- }
- /// TODO 需求不清,检测的数据需要传给体检,但是检测又不区分左右侧血压
- void _onSuccess(_, NibpExamValue e) async {
- logger.i(
- '检测完成,高压:${e.systolicPressure},低压:${e.diastolicPressure},脉率:${e.pulse}');
- int sbp = e.systolicPressure;
- int dbp = e.diastolicPressure;
- liveValue = 0;
- final orgManager = Get.find<IOrganizationManager>();
- final paramSbp = await orgManager.getDynamicParamByKey("Sbp");
- if (paramSbp != null) {
- sbp = nibp!.handleExamValueSpecially(sbp, paramSbp);
- logger.i('NIBP Value handle specially - Sbp: $sbp.');
- }
- final paramDbp = await orgManager.getDynamicParamByKey("Dbp");
- if (paramDbp != null) {
- dbp = nibp!.handleExamValueSpecially(dbp, paramDbp);
- logger.i('NIBP Value handle specially - Dbp:$dbp.');
- }
- setState(() {
- pressureDeviceStatus = PressureDeviceStatus.end;
- if (controller.diagnosisDataValue['NIBP'] == null) {
- controller.diagnosisDataValue['NIBP'] = {};
- }
- // controller.diagnosisDataValue['NIBP'] = {};
- if (pressureStatus == PressureStatus.left) {
- controller.diagnosisDataValue['NIBP'].addAll({
- 'Sbp_Left': sbp.toString(),
- 'Dbp_Left': dbp.toString(),
- 'Pulse_Beat': e.pulse.toString(),
- });
- logger.i('NIBP Value is left: $sbp, $dbp, $e');
- } else if (pressureStatus == PressureStatus.right) {
- controller.diagnosisDataValue['NIBP'].addAll({
- 'Sbp_Right': sbp.toString(),
- 'Dbp_Right': dbp.toString(),
- 'Pulse_Beat': e.pulse.toString(),
- });
- logger.i('NIBP Value is right: $sbp, $dbp, $e');
- }
- // controller.diagnosisDataValue['NIBP'] = {
- // 'Sbp': sbp.toString(),
- // 'Dbp': dbp.toString(),
- // 'Pulse_Beat': e.pulse.toString(),
- // };
- controller.saveCachedRecord();
- });
- }
- void _onConnectFail(sender, e) {
- logger.i("连接设备失败:${worker!.mac}");
- print('连接设备失败:${worker!.mac},errorCount:$errorCount,${DateTime.now()}');
- if (errorCount < 3) {
- print('连接设备失败:${worker!.mac},$errorCount,${DateTime.now()}');
- logger.i('连接设备失败:${worker!.mac},$errorCount,${DateTime.now()}');
- errorCount++;
- tryReconnect();
- } else {
- print('连接设备失败:${worker!.mac},$errorCount,${DateTime.now()}');
- logger.i('连接设备失败:${worker!.mac},$errorCount,${DateTime.now()}');
- isConnectFail = true;
- }
- _connectStatus = WorkerStatus.connectionFailed;
- setState(() {});
- }
- void _onDisconnected(sender, e) {
- logger.i("设备连接中断:${worker!.mac}");
- print('设备连接中断:${worker!.mac}');
- tryReconnect();
- errorCount = 0;
- _connectStatus = WorkerStatus.disconnected;
- setState(() {});
- }
- void _onConnectSuccess(sender, e) {
- logger.i("设备连接成功:$e");
- print("设备连接成功:$e");
- setState(() {
- _connectStatus = WorkerStatus.connected;
- errorCount = 0;
- isConnectFail = false;
- isShowError = false;
- });
- }
- Widget _buildValue() {
- if (controller.diagnosisDataValue['NIBP'] == null) {
- return _buildLiveWidget();
- }
- return _buildLiveWidget();
- }
- Widget _buildButtonGroup() {
- return Container(
- margin: EdgeInsets.only(right: 16),
- child: AnimatedToggle(
- values: const ['左侧', '右侧'],
- onToggleCallback: (value) {
- setState(() {
- pressureStatus = value;
- });
- },
- statusValue: pressureStatus,
- buttonColor: Theme.of(context).primaryColor,
- backgroundColor: const Color(0xFFB5C1CC),
- textColor: const Color(0xFFFFFFFF),
- ),
- );
- }
- @override
- Widget build(BuildContext context) {
- final nibp = controller.diagnosisDataValue['NIBP'];
- String pulsebeat = "";
- if (nibp != null && nibp is Map) {
- // 左侧收缩压(高压)
- leftHightValue = nibp['Sbp_Left']?.toString() ?? '';
- // 左侧舒张压(低压)
- leftLowValue = nibp['Dbp_Left']?.toString() ?? '';
- // 右侧收缩压(高压)
- rightHightValue = nibp['Sbp_Right']?.toString() ?? '';
- // 右侧舒张压(低压)
- rightLowValue = nibp['Dbp_Right']?.toString() ?? '';
- if (nibp.containsKey("Pulse_Beat")) {
- pulsebeat = nibp['Pulse_Beat'];
- }
- setState(() {});
- }
- if (controller.diagnosisDataValue.containsKey("Pulse_Beat")) {
- pulsebeat = controller.diagnosisDataValue['Pulse_Beat'];
- }
- return Stack(
- children: [
- ExamCard(
- title: '',
- clickCard: _onClickBloodPressure,
- content: Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- SizedBox(
- height: 16,
- ),
- _buildButtonGroup(),
- Row(
- children: [
- const SizedBox(
- width: 25,
- ),
- const Text(
- '血压',
- style: TextStyle(fontSize: 25),
- ),
- const Expanded(child: SizedBox()),
- _buildValue(),
- ],
- ),
- Row(
- children: [
- const SizedBox(
- width: 25,
- ),
- const Text(
- '左侧血压',
- style: TextStyle(fontSize: 25),
- ),
- const Expanded(child: SizedBox()),
- if (leftHightValue.isNotEmpty || leftLowValue.isNotEmpty)
- _buildResultWidget(leftHightValue, leftLowValue),
- ],
- ),
- Row(
- children: [
- const SizedBox(
- width: 25,
- ),
- const Text(
- '右侧血压',
- style: TextStyle(fontSize: 25),
- ),
- const Expanded(child: SizedBox()),
- if (rightHightValue.isNotEmpty || rightLowValue.isNotEmpty)
- _buildResultWidget(rightHightValue, rightLowValue),
- ],
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const SizedBox(
- width: 25,
- ),
- const Text(
- '脉率',
- style: TextStyle(fontSize: 25),
- ),
- const Expanded(child: SizedBox()),
- Text(
- pulsebeat,
- style: const TextStyle(
- fontSize: 45,
- color: Colors.black,
- ),
- ),
- const Text(
- ' bpm',
- style: TextStyle(fontSize: 25),
- ),
- const SizedBox(
- width: 30,
- ),
- ],
- ),
- Container(
- height: 30,
- margin: const EdgeInsets.only(bottom: 10),
- child: isShowError
- ? Row(
- children: [
- const SizedBox(
- width: 25,
- ),
- Text(
- '检测失败:',
- style: TextStyle(
- fontSize: 20,
- ),
- ),
- Text(
- '${errorMessage}',
- style: TextStyle(fontSize: 20, color: Colors.grey),
- ),
- const Expanded(child: SizedBox()),
- ],
- )
- : SizedBox(),
- ),
- ],
- ),
- ),
- if (!isPureSoftwareMode) ...[
- if (isConnectFail) ...[
- _buildErrorButton(),
- ] else ...[
- Positioned(
- right: 10,
- top: 10,
- child: DeviceStatus(connectStatus: _connectStatus),
- ),
- ],
- ],
- ],
- );
- }
- /// 需要封装一下
- Widget _buildErrorButton() {
- return DeviceStatusPosition(
- deviceStatus: Row(
- children: [
- const Text(
- '请确认设备是否启动',
- style: TextStyle(fontSize: 24, color: Colors.red),
- ),
- IconButton(
- onPressed: () {
- liveValue = 0;
- tryReconnect();
- setState(() {
- _connectStatus = WorkerStatus.connecting;
- isConnectFail = false;
- });
- },
- icon: const Icon(Icons.refresh),
- iconSize: 32,
- ),
- ],
- ),
- );
- }
- Widget _buildLiveWidget() {
- return SizedBox(
- height: 70,
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Container(
- alignment: Alignment.center,
- child: Text(
- liveValue.toString() == '0' ? '--' : liveValue.toString(),
- style: const TextStyle(
- fontSize: 60,
- color: Colors.black,
- ),
- ),
- ),
- const Text(
- ' mmHg',
- style: TextStyle(fontSize: 25),
- ),
- const SizedBox(
- width: 30,
- )
- ],
- ),
- );
- }
- Widget _buildResultWidget(String? sbp, String? dbp) {
- const textStyle = TextStyle(
- fontSize: 45,
- color: Colors.black,
- );
- var normalStyle = const TextStyle(
- fontSize: 25,
- color: Colors.black,
- );
- var uuitText = const Text(
- ' mmHg',
- style: TextStyle(
- fontSize: 25,
- color: Colors.black,
- ),
- );
- return Stack(
- children: [
- Column(
- mainAxisAlignment: MainAxisAlignment.center,
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Row(
- children: [
- Text(
- '收缩压',
- style: normalStyle,
- ),
- Container(
- alignment: Alignment.centerRight,
- width: 200,
- child: Text(
- sbp ?? '',
- style: textStyle,
- ),
- ),
- uuitText,
- const SizedBox(
- width: 30,
- ),
- ],
- ),
- Row(
- children: [
- Text(
- '舒张压',
- style: normalStyle,
- ),
- Container(
- alignment: Alignment.centerRight,
- width: 200,
- child: Text(
- dbp ?? '',
- style: textStyle,
- ),
- ),
- uuitText,
- const SizedBox(
- width: 30,
- ),
- ],
- ),
- ],
- ),
- ],
- );
- }
- Future<void> _onClickBloodPressure() async {
- String sbp = "";
- String dbp = "";
- String heart = "";
- Map<String, dynamic> diagnosisDataValue = controller.diagnosisDataValue;
- ///结构化的数据填充
- if (controller.diagnosisDataValue.containsKey("NIBP")) {
- var nibp = controller.diagnosisDataValue["NIBP"];
- if (nibp is Map) {
- if (pressureStatus == PressureStatus.left) {
- if (nibp.containsKey("Sbp_Left")) {
- sbp = nibp["Sbp_Left"];
- }
- if (nibp.containsKey("Dbp_Left")) {
- dbp = nibp["Dbp_Left"];
- }
- if (nibp.containsKey("Pulse_Beat")) {
- heart = nibp["Pulse_Beat"];
- }
- } else {
- if (nibp.containsKey("Sbp_Right")) {
- sbp = nibp["Sbp_Right"];
- }
- if (nibp.containsKey("Dbp_Right")) {
- dbp = nibp["Dbp_Right"];
- }
- if (nibp.containsKey("Pulse_Beat")) {
- heart = nibp["Pulse_Beat"];
- }
- }
- }
- }
- ///平铺方式的数据填充
- if (pressureStatus == PressureStatus.left) {
- if (diagnosisDataValue.containsKey("Sbp_Left")) {
- sbp = diagnosisDataValue["Sbp_Left"];
- }
- if (diagnosisDataValue.containsKey("Dbp_Left")) {
- dbp = diagnosisDataValue["Dbp_Left"];
- }
- } else {
- if (diagnosisDataValue.containsKey("Sbp_Right")) {
- sbp = diagnosisDataValue["Sbp_Left"];
- }
- if (diagnosisDataValue.containsKey("Dbp_Right")) {
- dbp = diagnosisDataValue["Dbp_Right"];
- }
- }
- if (diagnosisDataValue.containsKey("Pulse_Beat")) {
- heart = diagnosisDataValue["Pulse_Beat"];
- }
- String? result = await VDialogBloodPressure(
- title: '血压',
- initialValue: [
- sbp,
- dbp,
- ],
- heartRate: heart,
- isDisplayHeartRate: true,
- ).show();
- if (result != null) {
- try {
- String sbp = jsonDecode(result).first;
- String dbp = jsonDecode(result)[1];
- String heart = jsonDecode(result)[2];
- if (sbp.isNotEmpty && dbp.isNotEmpty) {
- if (pressureStatus == PressureStatus.left) {
- var nibp = controller.diagnosisDataValue["NIBP"];
- String? sdpRight = nibp?['Sbp_Right'];
- String? dbpRight = nibp?['Dbp_Right'];
- controller.diagnosisDataValue['NIBP'] = {
- 'Sbp_Left': sbp,
- 'Dbp_Left': dbp,
- 'Pulse_Beat': heart,
- };
- if (sdpRight != null && dbpRight != null) {
- controller.diagnosisDataValue['NIBP'].addAll({
- 'Sbp_Right': sdpRight,
- 'Dbp_Right': dbpRight,
- });
- }
- } else {
- var nibp = controller.diagnosisDataValue["NIBP"];
- String? sdpLeft = nibp?['Sbp_Left'];
- String? dbpLeft = nibp?['Dbp_Left'];
- controller.diagnosisDataValue['NIBP'] = {
- 'Sbp_Right': sbp,
- 'Dbp_Right': dbp,
- 'Pulse_Beat': heart,
- };
- if (sdpLeft != null && dbpLeft != null) {
- controller.diagnosisDataValue['NIBP'].addAll({
- 'Sbp_Left': sdpLeft,
- 'Dbp_Left': dbpLeft,
- });
- }
- }
- setState(() {});
- }
- } catch (e) {
- logger.e("BloodPressure _onClickBloodPressure ex:", e);
- }
- }
- }
- }
|