base.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'package:flutter/foundation.dart';
  2. import 'package:vnote_device_plugin/consts/status.dart';
  3. import 'package:vnote_device_plugin/event_entrepot.dart';
  4. import 'package:vnote_device_plugin/event_type.dart';
  5. import 'package:vnote_device_plugin/vnote_device_plugin_platform_interface.dart';
  6. abstract class DeviceWorkerBase {
  7. bool _busy = false;
  8. final String type;
  9. final String model;
  10. final String mac;
  11. final int maxStatus;
  12. final int minStatus;
  13. @protected
  14. late final EventListenUnit eventUnit;
  15. final connectFailEvent = FEventHandler<void>();
  16. final connectSuccessEvent = FEventHandler<void>();
  17. bool get busy => _busy;
  18. DeviceWorkerBase(
  19. this.type, {
  20. required this.model,
  21. required this.mac,
  22. required this.maxStatus,
  23. required this.minStatus,
  24. }) {
  25. final statusArr = <int>[];
  26. for (var i = 95; i <= 109; i++) {
  27. statusArr.add(i);
  28. }
  29. for (var i = minStatus; i <= maxStatus; i++) {
  30. statusArr.add(i);
  31. }
  32. eventUnit = EventListenUnit(
  33. type,
  34. model: model,
  35. // maxStatus: maxStatus,
  36. // minStatus: minStatus,
  37. acceptStatus: statusArr,
  38. callback: _innerOnEventCallback,
  39. );
  40. }
  41. /// 连接设备
  42. Future<String?> connect() async {
  43. if (_busy) return null;
  44. _busy = true;
  45. EventEntrepot.instance.register(eventUnit);
  46. await VnoteDevicePluginPlatform.instance.callAction(
  47. "BLE_CONNECT",
  48. {
  49. "MODEL": model,
  50. "MAC": mac,
  51. },
  52. );
  53. return null;
  54. }
  55. /// 端开连接
  56. Future<void> disconnect() async {
  57. if (!_busy) return;
  58. await VnoteDevicePluginPlatform.instance.callAction(
  59. "BLE_DISCONNECT",
  60. {"MODEL": model},
  61. );
  62. EventEntrepot.instance.unregister(eventUnit);
  63. _busy = false;
  64. }
  65. @protected
  66. void onEventCallback(int status, Map<String, dynamic> data);
  67. void _innerOnEventCallback(int status, Map<String, dynamic> e) {
  68. if (minStatus <= status && status <= maxStatus) {
  69. final data = _convertDataMap(e["DATAS"]);
  70. onEventCallback(status, data);
  71. } else {
  72. if (VDeviceSdkStatus.DEVICE_MAC_INVALID == status) {
  73. //
  74. } else if (VDeviceSdkStatus.BLE_CONNECTING == status) {
  75. //
  76. } else if (VDeviceSdkStatus.BLE_CONNECT_SUCCESS == status) {
  77. connectSuccessEvent.emit(this, null);
  78. } else if (VDeviceSdkStatus.BLE_CONNECT_FAIL == status) {
  79. connectFailEvent.emit(this, null);
  80. } else {
  81. //
  82. }
  83. }
  84. }
  85. Map<String, dynamic> _convertDataMap(List<dynamic> datas) {
  86. final Map<String, dynamic> map = {};
  87. for (final item in datas) {
  88. map[item['key']] = item['value'];
  89. }
  90. return map;
  91. }
  92. }