body_weight_height.dart 8.9 KB

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