123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import 'package:flutter/foundation.dart';
- import 'package:vnote_device_plugin/consts/status.dart';
- import 'package:vnote_device_plugin/event_entrepot.dart';
- import 'package:vnote_device_plugin/event_type.dart';
- import 'package:vnote_device_plugin/vnote_device_plugin_platform_interface.dart';
- abstract class DeviceWorkerBase {
- bool _busy = false;
- final String type;
- final String model;
- final String mac;
- final int maxStatus;
- final int minStatus;
- @protected
- late final EventListenUnit eventUnit;
- final connectFailEvent = FEventHandler<void>();
- final connectSuccessEvent = FEventHandler<void>();
- bool get busy => _busy;
- DeviceWorkerBase(
- this.type, {
- required this.model,
- required this.mac,
- required this.maxStatus,
- required this.minStatus,
- }) {
- final statusArr = <int>[];
- for (var i = 95; i <= 109; i++) {
- statusArr.add(i);
- }
- for (var i = minStatus; i <= maxStatus; i++) {
- statusArr.add(i);
- }
- eventUnit = EventListenUnit(
- type,
- model: model,
- // maxStatus: maxStatus,
- // minStatus: minStatus,
- acceptStatus: statusArr,
- callback: _innerOnEventCallback,
- );
- }
- /// 连接设备
- Future<String?> connect() async {
- if (_busy) return null;
- _busy = true;
- EventEntrepot.instance.register(eventUnit);
- await VnoteDevicePluginPlatform.instance.callAction(
- "BLE_CONNECT",
- {
- "MODEL": model,
- "MAC": mac,
- },
- );
- return null;
- }
- /// 端开连接
- Future<void> disconnect() async {
- if (!_busy) return;
- await VnoteDevicePluginPlatform.instance.callAction(
- "BLE_DISCONNECT",
- {"MODEL": model},
- );
- EventEntrepot.instance.unregister(eventUnit);
- _busy = false;
- }
- @protected
- void onEventCallback(int status, Map<String, dynamic> data);
- void _innerOnEventCallback(int status, Map<String, dynamic> e) {
- if (minStatus <= status && status <= maxStatus) {
- final data = _convertDataMap(e["DATAS"]);
- onEventCallback(status, data);
- } else {
- if (VDeviceSdkStatus.DEVICE_MAC_INVALID == status) {
- //
- } else if (VDeviceSdkStatus.BLE_CONNECTING == status) {
- //
- } else if (VDeviceSdkStatus.BLE_CONNECT_SUCCESS == status) {
- connectSuccessEvent.emit(this, null);
- } else if (VDeviceSdkStatus.BLE_CONNECT_FAIL == status) {
- connectFailEvent.emit(this, null);
- } else {
- //
- }
- }
- }
- Map<String, dynamic> _convertDataMap(List<dynamic> datas) {
- final Map<String, dynamic> map = {};
- for (final item in datas) {
- map[item['key']] = item['value'];
- }
- return map;
- }
- }
|