body_bmi.dart 7.7 KB

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