123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- 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: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 BloodPressure extends StatefulWidget {
- const BloodPressure({
- super.key,
- });
- @override
- State<BloodPressure> createState() => _BloodPressureState();
- }
- class _BloodPressureState extends State<BloodPressure> {
- 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;
- bool isShowError = false;
- String errorMessage = '';
- WorkerStatus _connectStatus = WorkerStatus.connecting;
- @override
- void initState() {
- WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
- currentDevice();
- });
- super.initState();
- }
- @override
- void didUpdateWidget(BloodPressure 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();
- }
- 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) {
- if (controller.diagnosisDataValue['NIBP'] != null) {
- controller.diagnosisDataValue['NIBP'] = null;
- }
- setState(() {
- liveValue = e;
- isShowError = false;
- });
- }
- /// 检测报错
- 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;
- 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("Sbp");
- if (paramDbp != null) {
- dbp = nibp!.handleExamValueSpecially(dbp, paramDbp);
- logger.i('NIBP Value handle specially - Dbp:$dbp.');
- }
- setState(() {
- pressureDeviceStatus = PressureDeviceStatus.end;
- /// 这是第三方需要的数据
- 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();
- }
- //收缩压(高压)
- var hightValue =
- controller.diagnosisDataValue['NIBP']?['Sbp']?.toString() ?? '';
- //舒张压(低压)
- var lowValue =
- controller.diagnosisDataValue['NIBP']?['Dbp']?.toString() ?? '';
- if (hightValue.isNotEmpty || lowValue.isNotEmpty) {
- return _buildResultWidget();
- }
- return _buildLiveWidget();
- }
- @override
- Widget build(BuildContext context) {
- return Stack(
- children: [
- ExamCard(
- title: '',
- clickCard: _onClickBloodPressure,
- content: Column(
- children: [
- Row(
- children: [
- const SizedBox(
- width: 25,
- ),
- const Text(
- '血压',
- style: TextStyle(fontSize: 25),
- ),
- const Expanded(child: SizedBox()),
- InkWell(
- child: _SideBar(
- value: _buildValue(),
- unit: 'mmHg',
- ),
- ),
- ],
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const SizedBox(
- width: 25,
- ),
- const Text(
- '脉率',
- style: TextStyle(fontSize: 25),
- ),
- const Expanded(child: SizedBox()),
- Text(
- controller.diagnosisDataValue['NIBP']?['Pulse_Beat']
- .toString() ??
- '',
- 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: () {
- controller.diagnosisDataValue['NIBP'] = null;
- liveValue = 0;
- tryReconnect();
- setState(() {
- _connectStatus = WorkerStatus.connecting;
- isConnectFail = false;
- });
- },
- icon: const Icon(Icons.refresh),
- iconSize: 32,
- ),
- ],
- ),
- );
- }
- Widget _buildLiveWidget() {
- return SizedBox(
- height: 130,
- width: 180,
- 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() {
- 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(
- controller.diagnosisDataValue['NIBP']['Sbp'],
- style: textStyle,
- ),
- ),
- uuitText,
- const SizedBox(
- width: 30,
- ),
- ],
- ),
- Row(
- children: [
- Text(
- '舒张压',
- style: normalStyle,
- ),
- Container(
- alignment: Alignment.centerRight,
- width: 200,
- child: Text(
- controller.diagnosisDataValue['NIBP']['Dbp'],
- style: textStyle,
- ),
- ),
- uuitText,
- const SizedBox(
- width: 30,
- ),
- ],
- ),
- ],
- ),
- ],
- );
- }
- Future<void> _onClickBloodPressure() async {
- String sbp = "";
- String dbp = "";
- String heart = "";
- if (controller.diagnosisDataValue.containsKey("NIBP")) {
- var nibp = controller.diagnosisDataValue["NIBP"];
- if (nibp is Map) {
- if (nibp.containsKey("Sbp")) {
- sbp = nibp["Sbp"];
- }
- if (nibp.containsKey("Dbp")) {
- dbp = nibp["Dbp"];
- }
- if (nibp.containsKey("Pulse_Beat")) {
- heart = nibp["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) {
- //收缩压(高压)
- controller.diagnosisDataValue['NIBP'] = {
- 'Sbp': sbp,
- 'Dbp': dbp,
- 'Pulse_Beat': heart,
- };
- setState(() {});
- }
- } catch (e) {
- logger.e("BloodPressure _onClickBloodPressure ex:", e);
- }
- }
- }
- }
- class _SideBar extends StatelessWidget {
- final Widget value;
- final String unit;
- const _SideBar({
- required this.value,
- required this.unit,
- });
- @override
- Widget build(BuildContext context) {
- return Row(
- mainAxisAlignment: MainAxisAlignment.end,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Container(
- alignment: Alignment.bottomRight,
- child: FittedBox(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.end,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- value,
- ],
- ),
- ),
- ),
- ],
- );
- }
- }
|