body_bmi.dart 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import 'dart:math' as math;
  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/pages/medical/widgets/device_status_position.dart';
  7. import 'package:vnote_device_plugin/consts/types.dart';
  8. import 'package:vnote_device_plugin/devices/weight.dart';
  9. import 'package:vitalapp/managers/interfaces/models/device.dart';
  10. import 'package:vitalapp/pages/medical/widgets/exam_card.dart';
  11. import 'package:vitalapp/components/dialog_number.dart';
  12. import 'package:vitalapp/pages/medical/controller.dart';
  13. import 'package:vitalapp/pages/medical/models/worker.dart';
  14. import 'package:vitalapp/pages/medical/widgets/device_status.dart';
  15. import 'package:vitalapp/pages/medical/widgets/side_bar.dart';
  16. import 'package:fis_common/logger/logger.dart';
  17. class BodyWeight extends StatefulWidget {
  18. const BodyWeight({
  19. super.key,
  20. });
  21. @override
  22. State<BodyWeight> createState() => _ExamBodyWeightState();
  23. }
  24. class _ExamBodyWeightState extends State<BodyWeight> {
  25. var controller = Get.find<MedicalController>();
  26. bool get isPureSoftwareMode => AppParameters.data.isPureSoftwareMode;
  27. DeviceControllerManager? bmi;
  28. WeightDeviceWorker? worker;
  29. bool isConnectFail = false;
  30. int errorCount = 0;
  31. WorkerStatus _connectStatus = WorkerStatus.connecting;
  32. late String _weight = controller.diagnosisDataValue['BMI']?['Weight'] ?? '';
  33. late String _height = controller.diagnosisDataValue['BMI']?['Height'] ?? '';
  34. late String _bmi = controller.diagnosisDataValue['BMI']?['Bmi'] ?? '';
  35. @override
  36. void initState() {
  37. WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  38. initBmi();
  39. });
  40. super.initState();
  41. }
  42. Future<void> connect() async {
  43. await worker!.connect();
  44. }
  45. /// 尝试重连
  46. Future<void> tryReconnect() async {
  47. if (worker != null) {
  48. await disconnect();
  49. await connect();
  50. }
  51. }
  52. Future<void> disconnect() async {
  53. if (worker != null) {
  54. await worker!.disconnect();
  55. }
  56. }
  57. void releaseListeners() {
  58. if (worker != null) {
  59. worker!.connectErrorEvent.removeListener(_onConnectFail);
  60. worker!.connectedEvent.removeListener(_onConnectSuccess);
  61. worker!.successEvent.removeListener(_onSuccess);
  62. worker!.disconnectedEvent.removeListener(_onDisconnected);
  63. }
  64. }
  65. @override
  66. void dispose() {
  67. bmi?.dispose();
  68. bmi = null;
  69. releaseListeners();
  70. disconnect();
  71. worker?.dispose();
  72. super.dispose();
  73. }
  74. void loadListeners() {
  75. worker!.successEvent.addListener(_onSuccess);
  76. worker!.connectErrorEvent.addListener(_onConnectFail);
  77. worker!.connectedEvent.addListener(_onConnectSuccess);
  78. worker!.disconnectedEvent.addListener(_onDisconnected);
  79. }
  80. Future<void> currentDevice() async {
  81. DeviceModel? device = await controller.getDevice(DeviceTypes.WEIGHT);
  82. if (device == null) {
  83. _connectStatus = WorkerStatus.unboundDevice;
  84. worker = null;
  85. setState(() {});
  86. return;
  87. }
  88. bmi = DeviceControllerManager(DeviceTypes.WEIGHT, device.model, device.mac);
  89. worker = bmi!.worker as WeightDeviceWorker;
  90. _connectStatus = bmi!.connectStatus;
  91. loadListeners();
  92. connect();
  93. }
  94. Future<void> initBmi() async {
  95. currentDevice();
  96. await initData();
  97. }
  98. Future<void> initData() async {
  99. _weight = controller.diagnosisDataValue['BMI']?['Weight'] ?? '';
  100. _height = controller.diagnosisDataValue['BMI']?['Height'] ?? '';
  101. _bmi = controller.diagnosisDataValue['BMI']?['Bmi'] ?? '';
  102. logger.i(
  103. '_ExamBodyWeightState initData _weight:$_weight _height:$_height _bmi:$_bmi');
  104. /// 体检系统 基础检查的特殊处理
  105. if (controller.diagnosisDataValue['Weight'] != null ||
  106. controller.diagnosisDataValue['Height'] != null ||
  107. controller.diagnosisDataValue['Bmi'] != null) {
  108. _weight = controller.diagnosisDataValue['Weight'] ?? '';
  109. _height = controller.diagnosisDataValue['Height'] ?? '';
  110. _bmi = controller.diagnosisDataValue['Bmi'] ?? '';
  111. logger.i(
  112. '_ExamBodyWeightState initData _weight:$_weight _height:$_height _bmi:$_bmi');
  113. setState(() {});
  114. return;
  115. }
  116. setState(() {});
  117. }
  118. void _onSuccess(_, double e) {
  119. _weight = e.toString();
  120. if (_height.isNotEmpty && _weight.isNotEmpty) {
  121. getBmi();
  122. controller.diagnosisDataValue['BMI'] = {
  123. 'Bmi': _bmi,
  124. 'Weight': _weight,
  125. 'Height': _height,
  126. };
  127. } else {
  128. controller.diagnosisDataValue['BMI'] = {
  129. 'Weight': _weight,
  130. };
  131. }
  132. controller.saveCachedRecord();
  133. _connectStatus = WorkerStatus.connected;
  134. setState(() {});
  135. }
  136. void _onConnectFail(sender, e) {
  137. print('连接设备失败');
  138. logger.i("连接设备失败:${worker!.mac}");
  139. if (errorCount < 3) {
  140. errorCount++;
  141. tryReconnect();
  142. } else {
  143. isConnectFail = true;
  144. }
  145. _connectStatus = WorkerStatus.connectionFailed;
  146. setState(() {});
  147. }
  148. void _onDisconnected(sender, e) {
  149. print('设备连接中断');
  150. logger.i("设备连接中断:${worker!.mac}");
  151. tryReconnect();
  152. errorCount = 0;
  153. _connectStatus = WorkerStatus.disconnected;
  154. setState(() {});
  155. }
  156. void _onConnectSuccess(sender, e) {
  157. logger.i("设备连接成功:${worker!.mac}");
  158. isConnectFail = false;
  159. errorCount = 0;
  160. _connectStatus = WorkerStatus.connected;
  161. setState(() {});
  162. }
  163. @override
  164. Widget build(BuildContext context) {
  165. return ExamCard(
  166. titleText: const SizedBox(),
  167. // clickCard: () {},
  168. content: Column(
  169. mainAxisAlignment: MainAxisAlignment.start,
  170. children: [
  171. SideBar(
  172. title: '身高',
  173. value: _height.isEmpty ? '--' : _height,
  174. unit: 'cm',
  175. onTap: _inputHeight,
  176. ),
  177. const Divider(indent: 30),
  178. Stack(
  179. children: [
  180. SideBar(
  181. title: '体重',
  182. value: _weight.isEmpty ? '--' : _weight,
  183. hasDevice: true,
  184. unit: 'kg',
  185. onTap: _inputWeight,
  186. ),
  187. if (!isPureSoftwareMode) ...[
  188. if (!isConnectFail)
  189. DeviceStatusPosition(
  190. deviceStatus: DeviceStatus(connectStatus: _connectStatus),
  191. )
  192. else
  193. _buildErrorButton(),
  194. ],
  195. ],
  196. ),
  197. const Divider(indent: 30),
  198. SideBar(
  199. title: 'BMI',
  200. value: _bmi.isEmpty ? '--' : _bmi,
  201. unit: 'kg/m²',
  202. ),
  203. ],
  204. ));
  205. }
  206. /// 需要封装一下
  207. Widget _buildErrorButton() {
  208. return DeviceStatusPosition(
  209. deviceStatus: Row(
  210. children: [
  211. const Text(
  212. '请确认设备是否启动',
  213. style: TextStyle(fontSize: 24, color: Colors.red),
  214. ),
  215. IconButton(
  216. onPressed: () {
  217. tryReconnect();
  218. setState(() {
  219. _connectStatus = WorkerStatus.connecting;
  220. isConnectFail = false;
  221. });
  222. },
  223. icon: const Icon(Icons.refresh),
  224. iconSize: 32,
  225. ),
  226. ],
  227. ),
  228. );
  229. }
  230. Future<void> _inputWeight() async {
  231. String? result = await VDialogNumber(
  232. title: '体重',
  233. initialValue: _weight,
  234. ).show();
  235. if (result?.isNotEmpty ?? false) {
  236. _weight = result ?? '';
  237. if (_height.isNotEmpty) {
  238. getBmi();
  239. }
  240. controller.diagnosisDataValue['BMI'] = {
  241. 'Height': _height,
  242. 'Bmi': _bmi,
  243. 'Weight': _weight,
  244. };
  245. controller.saveCachedRecord();
  246. }
  247. setState(() {});
  248. }
  249. void getBmi() {
  250. if (_weight.isNotEmpty && _height.isNotEmpty) {
  251. final w = double.parse(_weight);
  252. final h = double.parse(_height) / 100.0;
  253. if (h == 0) {
  254. // 0不能被除
  255. _bmi = "0";
  256. } else {
  257. _bmi = (w / math.pow(h, 2)).toStringAsFixed(2);
  258. }
  259. controller.saveCachedRecord();
  260. }
  261. }
  262. Future<void> _inputHeight() async {
  263. String? result = await VDialogNumber(
  264. title: '身高',
  265. initialValue: _height,
  266. ).show();
  267. if (result?.isNotEmpty ?? false) {
  268. _height = result ?? '';
  269. if (_weight.isNotEmpty) {
  270. getBmi();
  271. }
  272. controller.diagnosisDataValue['BMI'] = {
  273. 'Height': _height,
  274. 'Bmi': _bmi,
  275. 'Weight': _weight,
  276. };
  277. controller.saveCachedRecord();
  278. }
  279. setState(() {});
  280. }
  281. }