123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- import 'dart:math' as math;
- 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/pages/medical/widgets/device_status_position.dart';
- import 'package:vnote_device_plugin/consts/types.dart';
- import 'package:vnote_device_plugin/devices/weight.dart';
- import 'package:vitalapp/managers/interfaces/models/device.dart';
- import 'package:vitalapp/pages/medical/widgets/exam_card.dart';
- import 'package:vitalapp/components/dialog_number.dart';
- import 'package:vitalapp/pages/medical/controller.dart';
- import 'package:vitalapp/pages/medical/models/worker.dart';
- import 'package:vitalapp/pages/medical/widgets/device_status.dart';
- import 'package:vitalapp/pages/medical/widgets/side_bar.dart';
- import 'package:fis_common/logger/logger.dart';
- class BodyWeight extends StatefulWidget {
- const BodyWeight({
- super.key,
- });
- @override
- State<BodyWeight> createState() => _ExamBodyWeightState();
- }
- class _ExamBodyWeightState extends State<BodyWeight> {
- var controller = Get.find<MedicalController>();
- bool get isPureSoftwareMode => AppParameters.data.isPureSoftwareMode;
- DeviceControllerManager? bmi;
- WeightDeviceWorker? worker;
- bool isConnectFail = false;
- int errorCount = 0;
- WorkerStatus _connectStatus = WorkerStatus.connecting;
- late String _weight = controller.diagnosisDataValue['BMI']?['Weight'] ?? '';
- late String _height = controller.diagnosisDataValue['BMI']?['Height'] ?? '';
- late String _bmi = controller.diagnosisDataValue['BMI']?['Bmi'] ?? '';
- @override
- void initState() {
- WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
- initBmi();
- });
- super.initState();
- }
- Future<void> connect() async {
- await worker!.connect();
- }
- /// 尝试重连
- Future<void> tryReconnect() async {
- if (worker != null) {
- await disconnect();
- await connect();
- }
- }
- Future<void> disconnect() async {
- if (worker != null) {
- await worker!.disconnect();
- }
- }
- void releaseListeners() {
- if (worker != null) {
- worker!.connectErrorEvent.removeListener(_onConnectFail);
- worker!.connectedEvent.removeListener(_onConnectSuccess);
- worker!.successEvent.removeListener(_onSuccess);
- worker!.disconnectedEvent.removeListener(_onDisconnected);
- }
- }
- @override
- void dispose() {
- bmi?.dispose();
- bmi = null;
- releaseListeners();
- disconnect();
- worker?.dispose();
- super.dispose();
- }
- void loadListeners() {
- worker!.successEvent.addListener(_onSuccess);
- worker!.connectErrorEvent.addListener(_onConnectFail);
- worker!.connectedEvent.addListener(_onConnectSuccess);
- worker!.disconnectedEvent.addListener(_onDisconnected);
- }
- Future<void> currentDevice() async {
- DeviceModel? device = await controller.getDevice(DeviceTypes.WEIGHT);
- if (device == null) {
- _connectStatus = WorkerStatus.unboundDevice;
- worker = null;
- setState(() {});
- return;
- }
- bmi = DeviceControllerManager(DeviceTypes.WEIGHT, device.model, device.mac);
- worker = bmi!.worker as WeightDeviceWorker;
- _connectStatus = bmi!.connectStatus;
- loadListeners();
- connect();
- }
- Future<void> initBmi() async {
- currentDevice();
- await initData();
- }
- Future<void> initData() async {
- _weight = controller.diagnosisDataValue['BMI']?['Weight'] ?? '';
- _height = controller.diagnosisDataValue['BMI']?['Height'] ?? '';
- _bmi = controller.diagnosisDataValue['BMI']?['Bmi'] ?? '';
- logger.i(
- '_ExamBodyWeightState initData _weight:$_weight _height:$_height _bmi:$_bmi');
- /// 体检系统 基础检查的特殊处理
- if (controller.diagnosisDataValue['Weight'] != null ||
- controller.diagnosisDataValue['Height'] != null ||
- controller.diagnosisDataValue['Bmi'] != null) {
- _weight = controller.diagnosisDataValue['Weight'] ?? '';
- _height = controller.diagnosisDataValue['Height'] ?? '';
- _bmi = controller.diagnosisDataValue['Bmi'] ?? '';
- logger.i(
- '_ExamBodyWeightState initData _weight:$_weight _height:$_height _bmi:$_bmi');
- setState(() {});
- return;
- }
- setState(() {});
- }
- void _onSuccess(_, double e) {
- _weight = e.toString();
- if (_height.isNotEmpty && _weight.isNotEmpty) {
- getBmi();
- controller.diagnosisDataValue['BMI'] = {
- 'Bmi': _bmi,
- 'Weight': _weight,
- 'Height': _height,
- };
- } else {
- controller.diagnosisDataValue['BMI'] = {
- 'Weight': _weight,
- };
- }
- controller.saveCachedRecord();
- _connectStatus = WorkerStatus.connected;
- setState(() {});
- }
- void _onConnectFail(sender, e) {
- print('连接设备失败');
- logger.i("连接设备失败:${worker!.mac}");
- if (errorCount < 3) {
- errorCount++;
- tryReconnect();
- } else {
- isConnectFail = true;
- }
- _connectStatus = WorkerStatus.connectionFailed;
- setState(() {});
- }
- void _onDisconnected(sender, e) {
- print('设备连接中断');
- logger.i("设备连接中断:${worker!.mac}");
- tryReconnect();
- errorCount = 0;
- _connectStatus = WorkerStatus.disconnected;
- setState(() {});
- }
- void _onConnectSuccess(sender, e) {
- logger.i("设备连接成功:${worker!.mac}");
- isConnectFail = false;
- errorCount = 0;
- _connectStatus = WorkerStatus.connected;
- setState(() {});
- }
- @override
- Widget build(BuildContext context) {
- return ExamCard(
- titleText: const SizedBox(),
- // clickCard: () {},
- content: Column(
- mainAxisAlignment: MainAxisAlignment.start,
- children: [
- SideBar(
- title: '身高',
- value: _height.isEmpty ? '--' : _height,
- unit: 'cm',
- onTap: _inputHeight,
- ),
- const Divider(indent: 30),
- Stack(
- children: [
- SideBar(
- title: '体重',
- value: _weight.isEmpty ? '--' : _weight,
- hasDevice: true,
- unit: 'kg',
- onTap: _inputWeight,
- ),
- if (!isPureSoftwareMode) ...[
- if (!isConnectFail)
- DeviceStatusPosition(
- deviceStatus: DeviceStatus(connectStatus: _connectStatus),
- )
- else
- _buildErrorButton(),
- ],
- ],
- ),
- const Divider(indent: 30),
- SideBar(
- title: 'BMI',
- value: _bmi.isEmpty ? '--' : _bmi,
- unit: 'kg/m²',
- ),
- ],
- ));
- }
- /// 需要封装一下
- Widget _buildErrorButton() {
- return DeviceStatusPosition(
- deviceStatus: Row(
- children: [
- const Text(
- '请确认设备是否启动',
- style: TextStyle(fontSize: 24, color: Colors.red),
- ),
- IconButton(
- onPressed: () {
- tryReconnect();
- setState(() {
- _connectStatus = WorkerStatus.connecting;
- isConnectFail = false;
- });
- },
- icon: const Icon(Icons.refresh),
- iconSize: 32,
- ),
- ],
- ),
- );
- }
- Future<void> _inputWeight() async {
- String? result = await VDialogNumber(
- title: '体重',
- initialValue: _weight,
- ).show();
- if (result?.isNotEmpty ?? false) {
- _weight = result ?? '';
- if (_height.isNotEmpty) {
- getBmi();
- }
- controller.diagnosisDataValue['BMI'] = {
- 'Height': _height,
- 'Bmi': _bmi,
- 'Weight': _weight,
- };
- controller.saveCachedRecord();
- }
- setState(() {});
- }
- void getBmi() {
- if (_weight.isNotEmpty && _height.isNotEmpty) {
- final w = double.parse(_weight);
- final h = double.parse(_height) / 100.0;
- if (h == 0) {
- // 0不能被除
- _bmi = "0";
- } else {
- _bmi = (w / math.pow(h, 2)).toStringAsFixed(2);
- }
- controller.saveCachedRecord();
- }
- }
- Future<void> _inputHeight() async {
- String? result = await VDialogNumber(
- title: '身高',
- initialValue: _height,
- ).show();
- if (result?.isNotEmpty ?? false) {
- _height = result ?? '';
- if (_weight.isNotEmpty) {
- getBmi();
- }
- controller.diagnosisDataValue['BMI'] = {
- 'Height': _height,
- 'Bmi': _bmi,
- 'Weight': _weight,
- };
- controller.saveCachedRecord();
- }
- setState(() {});
- }
- }
|