controller.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import 'package:fis_jsonrpc/rpc.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vitalapp/architecture/defines.dart';
  4. import 'package:vitalapp/managers/device_controller_manager.dart';
  5. import 'package:vitalapp/managers/interfaces/device.dart';
  6. import 'package:vitalapp/managers/interfaces/models/device.dart';
  7. import 'package:vitalapp/pages/controllers/blue_location_mixin.dart';
  8. import 'package:vitalapp/pages/medical/models/worker.dart';
  9. import 'package:vitalapp/pages/patient/card_reader/controller.dart';
  10. import 'package:vnote_device_plugin/consts/types.dart';
  11. import 'package:vnote_device_plugin/devices/ic_reader.dart';
  12. import 'package:vnote_device_plugin/models/exams/ic_reader.dart';
  13. import 'package:fis_common/logger/logger.dart';
  14. class BluetoothCardReaderController extends FControllerBase
  15. with BluetoothAndLocationMixin {
  16. BluetoothCardReaderController();
  17. ICReaderDeviceWorker? worker;
  18. DeviceControllerManager? icReader;
  19. WorkerStatus connectStatus = WorkerStatus.connecting;
  20. /// 是否连接中
  21. bool get isCardReaderConnecting => connectStatus == WorkerStatus.connecting;
  22. /// 是否已连接
  23. bool get isCardReaderConnected => connectStatus == WorkerStatus.connected;
  24. final _deviceManager = Get.find<IDeviceManager>();
  25. // _initData() async {
  26. // bool result = await Idread.init();
  27. // if (result) {
  28. // bool start = await Idread.startRead();
  29. // if (start) {
  30. // Idread.dataStreamListen((data) {
  31. // if (data is IdCardInfoModel) {
  32. // onReadInfo(data);
  33. // }
  34. // });
  35. // }
  36. // }
  37. // isCardReaderConnected = result;
  38. // update(["bluetooth_card_reader"]);
  39. // }
  40. Future<DeviceModel?> getDevice(String type) async {
  41. List<DeviceModel> devices = await _deviceManager.getDeviceList();
  42. return devices.firstWhereOrNull((element) => element.type == type);
  43. }
  44. connectDevice() async {
  45. logger.i('BluetoothCardReaderController connectDevice');
  46. DeviceModel? device = await getDevice(DeviceTypes.IC_READER);
  47. if (device == null) {
  48. logger.i('BluetoothCardReaderController device is null');
  49. connectStatus = WorkerStatus.unboundDevice;
  50. worker = null;
  51. return;
  52. }
  53. logger.i('BluetoothCardReaderController device is not null');
  54. if (icReader != null) {
  55. icReader!.dispose();
  56. releaseListeners();
  57. await Future.delayed(const Duration(milliseconds: 500)); // 等待上一个reader释放
  58. }
  59. icReader = DeviceControllerManager(
  60. DeviceTypes.IC_READER, device.model, device.mac);
  61. logger.i(
  62. 'BluetoothCardReaderController icReader: ${device.model}, ${device.mac} .');
  63. worker = icReader?.worker as ICReaderDeviceWorker;
  64. logger.i('BluetoothCardReaderController get worker');
  65. loadListeners();
  66. connectStatus = icReader!.connectStatus;
  67. update(["bluetooth_card_reader"]);
  68. }
  69. Future<void> icReaderDevice() async {
  70. logger.i('BluetoothCardReaderController icReaderDevice');
  71. await connectDevice();
  72. }
  73. void loadListeners() {
  74. logger.i('BluetoothCardReaderController loadListeners');
  75. worker?.successEvent.addListener(_onSuccess);
  76. worker!.connectErrorEvent.addListener(_onConnectFail);
  77. worker!.connectedEvent.addListener(_onConnectSuccess);
  78. worker!.disconnectedEvent.addListener(_onDisconnected);
  79. worker!.connect();
  80. }
  81. void releaseListeners() {
  82. worker!.successEvent.removeListener(_onSuccess);
  83. worker!.connectErrorEvent.removeListener(_onConnectFail);
  84. worker!.connectedEvent.removeListener(_onConnectSuccess);
  85. worker!.disconnectedEvent.removeListener(_onDisconnected);
  86. }
  87. /// 读取到数据的回调
  88. void onReadInfo(ICReaderExamData data) {
  89. final result = CardReaderResult(
  90. success: true,
  91. cardNo: data.cardNo,
  92. name: data.name,
  93. nation: data.nation,
  94. gender: data.gender == '男' ? GenderEnum.Male : GenderEnum.Female,
  95. birthday: DateTime.parse(data.birthday),
  96. address: data.address,
  97. );
  98. Get.back(
  99. result: result,
  100. );
  101. }
  102. void _onSuccess(_, ICReaderExamData e) {
  103. logger.i('读卡成功 ${e.name}');
  104. onReadInfo(e);
  105. connectStatus = WorkerStatus.connected;
  106. update(["bluetooth_card_reader"]);
  107. }
  108. void _onConnectFail(sender, e) {
  109. print('连接设备失败');
  110. logger.i('BluetoothCardReaderController 连接设备失败');
  111. connectStatus = WorkerStatus.connectionFailed;
  112. update(["bluetooth_card_reader"]);
  113. }
  114. void _onConnectSuccess(sender, e) {
  115. logger.i('BluetoothCardReaderController connectStatus:$connectStatus 连接成功');
  116. logger.i('BluetoothCardReaderController _onConnectSuccess');
  117. connectStatus = WorkerStatus.connected;
  118. update(["bluetooth_card_reader"]);
  119. }
  120. void _onDisconnected(sender, e) {
  121. print('设备连接中断');
  122. logger.i('BluetoothCardReaderController 设备连接中断');
  123. connectStatus = WorkerStatus.disconnected;
  124. update(["bluetooth_card_reader"]);
  125. }
  126. @override
  127. void onReady() {
  128. super.onReady();
  129. icReaderDevice();
  130. }
  131. @override
  132. void onClose() {
  133. if (worker != null) {
  134. releaseListeners();
  135. worker!.disconnect();
  136. worker!.dispose();
  137. worker = null;
  138. }
  139. super.onClose();
  140. }
  141. }