body_bmi.dart 7.8 KB

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