body_weight_height.dart 8.1 KB

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